package supply

import (
	"context"
	"git.oa00.com/supply-chain/service/client"
	"git.oa00.com/supply-chain/service/lib/bean"
	"github.com/shopspring/decimal"
)

type sku struct {
}

type source uint

const (
	SkuSourceJd source = 1 // 京东开普勒渠道
)

const (
	SkuAdjustTypeRate   = 1 // 加价比例
	SkuAdjustTypeAmount = 2 // 加价金额

	SkuPlatformStatusUp   = 1 // 平台上架
	SkuPlatformStatusDown = 2 // 平台下架

	SkuSourceStatusUp   = 1 // 供应商上架
	SkuSourceStatusDown = 2 // 供应商下架

	SkuHandleNone  = 1 // 未处理
	SkuHandleFinal = 2 // 已处理

	SkuAuditStatusNone   = 0 // 无
	SkuAuditStatusWait   = 1 // 待审核
	SkuAuditStatusAdopt  = 2 // 审核通过
	SkuAuditStatusReject = 3 // 审核驳回

	ProfitGtZero = 1 // 利润比大于0
	ProfitEqZero = 2 // 利润比等于0
	ProfitLtZero = 3 // 利润比小于0
)

type ArgsSkuAdd struct {
	SourceSkuId       string             // 源skuId
	SourceGroupSkuIds []string           // 源skuId关系数组
	SourceStatus      uint               // 源状态
	Name              string             // 商品名称
	BrandId           uint               // 品牌id
	ThirdCategoryId   uint               // 三级分类id
	SupplyPrice       decimal.Decimal    // 采购价
	GuidePrice        decimal.Decimal    // 指导价-建议售价
	Size              string             // 尺码
	Color             string             // 颜色
	Tax               string             // 税率
	Unit              string             // 销售单位
	UpcCode           string             // 商品条码
	Source            source             // 商品来源
	Content           string             // 商品详情
	Imgs              []SkuImg           // 商品图片 第一张主图
	Specifications    []SkuSpecification // 商品参数信息
}

type SkuImg struct {
	Path string `json:"path"`
}

type SkuSpecification struct {
	Name       string         `json:"name"`
	Attributes []SkuAttribute `json:"attributes"`
}

type SkuAttribute struct {
	Name  string   `json:"name"`
	Value []string `json:"value"`
}

// Add @Title 添加商品
func (s *sku) Add(ctx context.Context, args ArgsSkuAdd) error {
	reply := 0
	return client.GetClient(s).Call(ctx, "Add", args, &reply)
}

type ArgsSkuAdjust struct {
	ApplyUserId uint            // 修改人
	SkuIds      []uint          // 商品id
	AdjustType  uint            // 加价类型
	AdjustPrice decimal.Decimal // 加价金额
}

// Adjust @Title 加价
func (s *sku) Adjust(ctx context.Context, args ArgsSkuAdjust) error {
	reply := 0
	return client.GetClient(s).Call(ctx, "Adjust", args, &reply)
}

type ArgsSkuLists struct {
	Search SkuListsSearch
	Page   bean.Page
}

type SkuListsSearch struct {
	Name               string          // 商品名称
	SourceId           uint            // 所属供应商 1=京东
	Id                 uint            // 瑞库客id
	SourceSkuId        uint            // 供应商skuId
	BrandId            uint            // 品牌Id
	ThridCategoryId    uint            // 三级分类Id
	AdjustType         uint            // 加价规则
	MaxSupplyPrice     decimal.Decimal // 最大采购价格
	MinSupplyPrice     decimal.Decimal // 最小采购价格
	CustomerProfitRate uint            // 客户利润比 1=大于0 2=等于0 3=小于0
	Handle             uint            // 处理状态
	AuditStatus        uint            // 审核状态
	SourceStatus       uint            // 供应商下架状态
	PlatformStatus     uint            // 平台下架状态
}

type ReplySkuList struct {
	Lists []SkuItem
	Total int64
}

type SkuItem struct {
	Id                 uint
	Name               string          // 名称
	BrandName          string          // 品牌名称
	FirstCategoryId    uint            // 一级分类id
	SecondCategoryId   uint            // 二级分类id
	ThirdCategoryId    uint            // 三级分类id
	SupplyPrice        decimal.Decimal // 采购价
	GuidePrice         decimal.Decimal // 指导价
	ImgUrl             string          // 商品主图
	PlatformStatus     uint            // 平台状态 1=上架 2=下架
	SourceName         string          // 供应商名称
	SourceSkuId        string          // 源skuId
	SourceStatus       uint            // 供应商状态 1=上架 2=下架
	CustomerPrice      decimal.Decimal // 供货最高价
	CustomerProfitRate decimal.Decimal // 供货利润率 供货利润/供货最高价%
	AdjustType         uint            // 加价类型
	AdjustPrice        decimal.Decimal // 加价金额
	Handle             uint            // 1=未处理 2=已处理
	Imgs               []SkuImg
}

// Lists @Title 商品列表
func (s *sku) Lists(ctx context.Context, args ArgsSkuLists) (reply ReplySkuList, err error) {
	err = client.GetClient(s).Call(ctx, "Lists", args, &reply)
	return
}

type ArgsSkuOnShelves struct {
	SkuIds []uint
}

type ArgsSkuDownShelves struct {
	SkuIds []uint
}

// OnShelves @Title 上架
func (s *sku) OnShelves(ctx context.Context, args ArgsSkuOnShelves) error {
	reply := 0
	return client.GetClient(s).Call(ctx, "OnShelves", args, &reply)
}

// DownShelves @Title 下架
func (s *sku) DownShelves(ctx context.Context, args ArgsSkuDownShelves) error {
	reply := 0
	return client.GetClient(s).Call(ctx, "DownShelves", args, &reply)
}

type ArgsSkuAdjustType struct {
	SkuId       uint
	AdjustType  uint
	AdjustPrice decimal.Decimal
	ApplyUserId uint
}

// EditAdjust @Title 修改改价类型
func (s *sku) EditAdjust(ctx context.Context, args ArgsSkuAdjustType) error {
	reply := 0
	return client.GetClient(s).Call(ctx, "EditAdjust", args, &reply)
}