package erp

import (
	"context"
	"git.kumo.work/shama/service/client"
	bean2 "git.kumo.work/shama/service/erp/bean"
	product2 "git.kumo.work/shama/service/erp/product"
	"git.kumo.work/shama/service/lib/bean"
	"github.com/shopspring/decimal"
	"time"
)

type product struct {
	product2.Product
}
type ArgsProductList struct {
	Page   bean.Page
	Search ProductSearch
}
type ProductSearch struct {
	CategoryId           int64      // 分类id
	Serial               string     // 产品货号
	CustomSerial         string     // 客户货号
	Type                 string     // 产品类型
	Mold                 int64      // 组成方式 1=单件 2=套件
	CreatedStaffIds      []int64    // 录入人
	CreatedDepartmentIds []int64    // 归属部门
	BanFlag              int64      // 禁用标记 1=禁用 2=可用
	Name                 string     // 中文品名
	CreatedAtStart       *time.Time // 录入开始时间
	CreatedAtEnd         *time.Time // 录入结束时间
	ParentIds            []int64    // 上级id
}

type ProductItem struct {
	Id             int64            `json:"id"`
	Mold           int64            `json:"mold"`
	Serial         string           `json:"serial"`
	CustomSerial   string           `json:"customSerial"`
	Name           string           `json:"name"`
	EngName        string           `json:"engName"`
	ImgFilePaths   []string         `json:"imgFilePaths"`
	CategoryId     *int64           `json:"categoryId"`
	CategoryName   string           `json:"categoryName"`
	Weight         *decimal.Decimal `json:"weight"`
	Texture        string           `json:"texture"`
	SalePrice      *decimal.Decimal `json:"salePrice"`
	BanFlag        int64            `json:"banFlag"`
	Type           string           `json:"type"`
	Barcode        string           `json:"barcode"`
	SellCountry    string           `json:"sellCountry"`
	Info           string           `json:"info"`
	QuotePrice     *decimal.Decimal `json:"quotePrice"`
	CreatedStaffId int64            `json:"createdStaffId"`
	DepartmentId   int64            `json:"departmentId"`
	CreatedAt      *time.Time       `json:"createdAt"`
	UpdatedAt      *time.Time       `json:"updatedAt"`
}
type ReplyProductList struct {
	List  []ProductItem `json:"list"`
	Total int64         `json:"total"`
}

// List @TITLE 产品列表
func (p *product) List(ctx context.Context, args ArgsProductList) (reply ReplyProductList, err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "List", args, &reply)
	return
}

type ArgsProductAdd struct {
	StaffId int64 // 员工id
	ProductAdd
}
type ProductAdd struct {
	Serial                string                  // 货号
	CustomSerial          string                  // 客户货号
	Name                  string                  // 中文品名
	EngName               string                  // 英文品名
	CategoryId            *int64                  // 类目ID
	Weight                *decimal.Decimal        // 重量
	Texture               string                  // 材质
	SalePrice             *decimal.Decimal        // 销售单价
	BanFlag               int64                   // 禁用标记 1=禁用,2=可用
	Mold                  int64                   // 组成类型 1=单件 2=套件
	MoldCount             int64                   // 子组件数量
	ParentId              int64                   // 子套件父级ID
	Type                  string                  // 产品类型
	Barcode               string                  // 条形码
	SellCountry           string                  // 销售国家
	Info                  string                  // 备注
	Description           string                  // 中文描述
	EngDescription        string                  // 英文描述
	PackageDescription    string                  // 包装中文描述
	PackageEngDescription string                  // 包装英文描述
	PackageInfoFile       bean2.FileData          // 包装描述文件
	Packages              []ProductPackage        // 包装
	Customs               ProductCustoms          // 海关
	Quotes                []product2.ArgsQuoteAdd // 工厂报价
	ImgFilePaths          []string                // 图片地址集合
}

type ProductCustoms struct {
	Serial         string           // 海关编码
	Name           string           // 中文报关名称
	DomesticSupply string           // 境内货源地
	AddTaxRate     *decimal.Decimal // 增值税率
	MinusTaxRate   *decimal.Decimal // 退税率
	MeasureUnit    string           // 报关单位
	InvoiceUnit    string           // 开票单位
	Info           string           // 备注
	HsSerial       int64            // 是否商检 1=商检 2=未商检
	Brand          string           // 品牌
	Detail         string           // 报关要素
}
type ProductPackage struct {
	Type        int64            // 包装类型 1=内盒 2=外箱
	Material    string           // 材质
	Num         *int64           // 入数
	Length      *decimal.Decimal // 长
	Width       *decimal.Decimal // 宽
	Height      *decimal.Decimal // 高
	GrossWeight *decimal.Decimal // 毛重
	NetWeight   *decimal.Decimal // 净重
	Info        string           // 包装备注
}

// Add @TITLE 添加商品
func (p *product) Add(ctx context.Context, args ArgsProductAdd) (productId int64, err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Add", args, &productId)
	return
}

type ProductCustomsInfo struct {
	Id             int64            `json:"id"`
	Serial         string           `json:"serial"`
	Name           string           `json:"name"`
	DomesticSupply string           `json:"domesticSupply"`
	AddTaxRate     *decimal.Decimal `json:"addTaxRate"`
	MinusTaxRate   *decimal.Decimal `json:"minusTaxRate"`
	MeasureUnit    string           `json:"measureUnit"`
	InvoiceUnit    string           `json:"invoiceUnit"`
	Info           string           `json:"info"`
	HsSerial       int64            `json:"hsSerial"`
	Brand          string           `json:"brand"`
	Detail         string           `json:"detail"`
}

type ReplyProductInfo struct {
	Id                    int64                `json:"id"`
	Mold                  int64                `json:"mold"`
	MoldCount             int64                `json:"moldCount"`
	Serial                string               `json:"serial"`
	CustomSerial          string               `json:"customSerial"`
	Name                  string               `json:"name"`
	EngName               string               `json:"engName"`
	ImgFilePaths          []string             `json:"imgFilePaths"`
	CategoryId            *int64               `json:"categoryId"`
	CategoryName          string               `json:"categoryName"`
	Weight                *decimal.Decimal     `json:"weight"`
	Texture               string               `json:"texture"`
	SalePrice             *decimal.Decimal     `json:"salePrice"`
	BanFlag               int64                `json:"banFlag"`
	Type                  string               `json:"type"`
	Barcode               string               `json:"barcode"`
	SellCountry           string               `json:"sellCountry"`
	Info                  string               `json:"info"`
	Description           string               `json:"description"`
	EngDescription        string               `json:"engDescription"`
	PackageDescription    string               `json:"packageDescription"`
	PackageEngDescription string               `json:"packageEngDescription"`
	PackageInfoFile       bean2.FileData       `json:"packageInfoFile"`
	CreatedStaffId        int64                `json:"createdStaffId"`
	DepartmentId          int64                `json:"departmentId"`
	CreatedAt             *time.Time           `json:"createdAt"`
	UpdatedAt             *time.Time           `json:"updatedAt"`
	Packages              []ProductPackageInfo `json:"packages"`
	Customs               ProductCustomsInfo   `json:"customs"`
	Quotes                []ProductQuoteInfo   `json:"quotes"`
	Children              []ReplyProductInfo   `json:"children"` // 子组件
}

type ProductPackageInfo struct {
	Type        int64            `json:"type"`
	Material    string           `json:"material"`
	Num         *int64           `json:"num"`
	Length      *decimal.Decimal `json:"length"`
	Width       *decimal.Decimal `json:"width"`
	Height      *decimal.Decimal `json:"height"`
	Volume      *decimal.Decimal `json:"volume"`
	GrossWeight *decimal.Decimal `json:"grossWeight"`
	NetWeight   *decimal.Decimal `json:"netWeight"`
	Info        string           `json:"info"`
}

type ProductQuoteInfo struct {
	Id             int64            `json:"id"`
	FactorySerial  string           `json:"factorySerial"`
	FactoryId      int64            `json:"factoryId"`
	FactoryName    string           `json:"factoryName"`
	Price          *decimal.Decimal `json:"price"`
	StartNum       *int64           `json:"startNum"`
	MeasureUnit    string           `json:"measureUnit"`
	Currency       string           `json:"currency"`
	CurrencyName   string           `json:"currencyName"`
	CurrencySymbol string           `json:"currencySymbol"`
	CurrencyRate   *decimal.Decimal `json:"currencyRate"`
	MainFlag       int64            `json:"mainFlag"`
	CreatedAt      *time.Time       `json:"createdAt"`
	UpdatedAt      *time.Time       `json:"updatedAt"`
}

// Info @TITLE 产品详情
func (p *product) Info(ctx context.Context, productId int64) (reply ReplyProductInfo, err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Info", productId, &reply)
	return
}

// Infos @TITLE 产品详情
func (p *product) Infos(ctx context.Context, productIds []int64) (reply []ReplyProductInfo, err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Infos", productIds, &reply)
	return
}

type ArgsProductEdit struct {
	ProductId int64 // 产品ID
	ProductAdd
}

// Edit @TITLE 编辑产品
func (p *product) Edit(ctx context.Context, args ArgsProductEdit) (err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	reply := 0
	return xClient.Call(ctx, "Edit", args, &reply)
}

// Delete @TITLE 删除产品
func (p *product) Delete(ctx context.Context, productIds []int64) (err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	reply := 0
	return xClient.Call(ctx, "Delete", productIds, &reply)
}

type ArgsProductClone struct {
	StaffId      int64 // 员工id
	DepartmentId int64 // 部门id
	ProductId    int64 // 产品ID
}

// Clone @TITLE 复制产品
func (p *product) Clone(ctx context.Context, args ArgsProductClone) (newProductId int64, err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Clone", args, &newProductId)
	return
}