package erp

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

type purchase struct {
	purchase2.Purchase
}
type ArgsPurchaseList struct {
	Page   bean.Page
	Search PurchaseSearch
}
type PurchaseSearch struct {
	PiSerial        string     // 采购合同编号
	PoSerial        string     // po
	CustomId        int64      // 客户id
	CustomShortName string     // 客户简称
	CreatedStaffIds []int64    // 创建人筛选
	StaffIds        []int64    // 业务员
	FactoryName     string     // 采购工厂
	PurchaseIds     []int64    // 采购id
	OrderDateStart  *time.Time // 下单日期
	OrderDateEnd    *time.Time // 下单日期
	WorkflowStatus  []int64    // 状态
}
type ReplyPurchaseList struct {
	List  []PurchaseItem `json:"list"`
	Total int64          `json:"total"`
}
type PurchaseItem struct {
	Id              int64           `json:"id"`
	SaleId          int64           `json:"saleId"`
	PiSerial        string          `json:"piSerial"`
	PoSerial        string          `json:"poSerial"`
	CustomName      string          `json:"customName"`
	CustomShortName string          `json:"customShortName"`
	DeliveryDate    *time.Time      `json:"deliveryDate"`
	DeliveryDateEnd *time.Time      `json:"deliveryDateEnd"`
	OrderDate       time.Time       `json:"orderDate"`
	CreatedStaffId  int64           `json:"createdStaffId"`
	WorkflowId      int64           `json:"workflowId"`
	WorkflowStatus  int64           `json:"workflowStatus"`
	FactoryName     string          `json:"factoryName"`
	FactoryId       int64           `json:"factoryId"`
	ProductAmount   decimal.Decimal `json:"productAmount"`
	CostAmount      decimal.Decimal `json:"costAmount"`
	CreatedAt       *time.Time      `json:"createdAt"`
	UpdatedAt       *time.Time      `json:"updatedAt"`
}

// List @TITLE 采购合同列表
func (p *purchase) List(ctx context.Context, args ArgsPurchaseList) (reply ReplyPurchaseList, err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "List", args, &reply)
	return
}

type ArgsPurchaseAdd struct {
	SaleId   int64 // 销售合同
	Products []PurchaseProduct
}
type PurchaseProduct struct {
	SaleProductId  int64           // 销售合同产品id
	FactoryId      int64           // 采购工厂id
	Num            int64           // 采购数量
	Price          decimal.Decimal // 采购单价
	Currency       string          // 币种
	CurrencyName   string          // 币种名称
	CurrencySymbol string          // 币种符号
	CurrencyRate   decimal.Decimal // 币种汇率
	MeasureUnit    string          // 数量单位
}

// Add @TITLE 添加采购合同
func (p *purchase) Add(ctx context.Context, args ArgsPurchaseAdd) (err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Add", args, &reply)
	return
}

type ReplyPurchaseInfo struct {
	PurchaseId      int64            `json:"purchaseId"`
	FactoryId       int64            `json:"factoryId"`
	FactoryName     string           `json:"factoryName"`
	FactoryAddress  string           `json:"factoryAddress"`
	FactoryContact  string           `json:"factoryContact"`
	FactoryPhone    string           `json:"factoryPhone"`
	FactoryFax      string           `json:"factoryFax"`
	DeliveryDate    *time.Time       `json:"deliveryDate"`
	DeliveryDateEnd *time.Time       `json:"deliveryDateEnd"`
	DeliveryPlace   string           `json:"deliveryPlace"`
	AdvancePayment  *decimal.Decimal `json:"advancePayment"`
	PoSerial        string           `json:"poSerial"`
	PiSerial        string           `json:"piSerial"`
	WorkflowId      int64            `json:"workflowId"`
	WorkflowStatus  int64            `json:"workflowStatus"`
	WorkflowReason  string           `json:"workflowReason"`
	OrderDate       time.Time        `json:"orderDate"`
	CustomShortName string           `json:"customShortName"`
	OurCompany      string           `json:"ourCompany"`
	CreatedStaffId  int64            `json:"createdStaffId"`
	FrontMark       string           `json:"frontMark"`
	SideMark        string           `json:"sideMark"`
	InnerBoxText    string           `json:"innerBoxText"`
	Remarks         string           `json:"remarks"`
	Currency        string           `json:"currency"`
	CurrencyName    string           `json:"currencyName"`
	CurrencySymbol  string           `json:"currencySymbol"`
	CurrencyRate    decimal.Decimal  `json:"currencyRate"`
	CreatedAt       *time.Time       `json:"createdAt"`
	UpdatedAt       *time.Time       `json:"updatedAt"`
}

// Info @TITLE 采购合同详情
func (p *purchase) Info(ctx context.Context, purchaseId int64) (reply ReplyPurchaseInfo, err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Info", purchaseId, &reply)
	return
}

type ArgsPurchaseEdit struct {
	PurchaseId      int64            // 采购合同id
	PoSerial        string           // PoSerial
	OrderDate       *time.Time       // 下单日期
	FactoryAddress  string           // 工厂地址
	FactoryContact  string           // 工厂联系人
	FactoryPhone    string           // 工厂电话
	FactoryFax      string           // 工厂传真
	DeliveryDate    *time.Time       // 采购交期
	DeliveryDateEnd *time.Time       // 采购交期结束
	DeliveryPlace   string           // 交货地点
	AdvancePayment  *decimal.Decimal // 预付款货
	FrontMark       string           // 正面唛头
	SideMark        string           // 侧面唛头
	InnerBoxText    string           // 内盒文字
	Remarks         string           // 合同备注
	Currency        string           // 币种
	CurrencyName    string           // 币种名称
	CurrencySymbol  string           // 币种符号
	CurrencyRate    decimal.Decimal  // 币种汇率
}

// Edit @TITLE 编辑采购合同
func (p *purchase) Edit(ctx context.Context, args ArgsPurchaseEdit) (err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Edit", args, &reply)
	return
}

// Cancel @TITLE 作废采购合同
func (p *purchase) Cancel(ctx context.Context, purchaseId int64) (err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Cancel", purchaseId, &reply)
	return
}

type ArgsPurchaseHistory struct {
	Page   bean.Page
	Search PurchaseHistorySearch
}
type PurchaseHistorySearch struct {
	ProductId int64 // 产品id
}
type ReplyPurchaseHistory struct {
	List  []PurchaseHistoryItem `json:"list"`
	Total int64                 `json:"total"`
}
type PurchaseHistoryItem struct {
	PurchaseId             int64           `json:"purchaseId"`
	PiSerial               string          `json:"piSerial"`
	OrderDate              time.Time       `json:"orderDate"`
	FactorySerial          string          `json:"factorySerial"`
	FactoryName            string          `json:"factoryName"`
	PurchasePrice          decimal.Decimal `json:"purchasePrice"`
	PurchaseCount          int64           `json:"purchaseCount"`
	PurchaseAmount         decimal.Decimal `json:"purchaseAmount"`
	PurchaseCurrency       string          `json:"purchaseCurrency"`
	PurchaseCurrencyName   string          `json:"purchaseCurrencyName"`
	PurchaseCurrencySymbol string          `json:"purchaseCurrencySymbol"`
	PurchaseCurrencyRate   decimal.Decimal `json:"purchaseCurrencyRate"`
	CreatedStaffId         int64           `json:"createdStaffId"`
}

// History @TITLE 历史记录
func (p *purchase) History(ctx context.Context, args ArgsPurchaseHistory) (reply ReplyPurchaseHistory, err error) {
	xClient, err := client.GetClient(p)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "History", args, &reply)
	return
}