package shipment

import (
	"context"
	"git.kumo.work/shama/service/client"
	bean2 "git.kumo.work/shama/service/erp/bean"
	"git.kumo.work/shama/service/lib/bean"
	"time"
)

type serial struct {
}

type ArgsSerialList struct {
	Page   bean.Page
	Search SerialSearch
}
type SerialSearch struct {
	SerialNo      string  // 商检单号
	InvoiceSerial string  // 出运发票号
	CustomIds     []int64 // 客户筛选
	StaffIds      []int64 // 业务员筛选
	BanFlag       int64   // 是否有效
	SerialIds     []int64 // 商检id
}
type ReplySerialList struct {
	List  []SerialItem `json:"list"`
	Total int64        `json:"total"`
}

type SerialItem struct {
	Id               int64      `json:"id"`
	ShipmentId       int64      `json:"shipmentId"`
	InvoiceSerial    string     `json:"invoiceSerial"`
	SerialNo         string     `json:"serialNo"`
	ContractDate     *time.Time `json:"contractDate"`
	InvoiceDate      time.Time  `json:"invoiceDate"`
	CustomName       string     `json:"customName"`
	CustomShortName  string     `json:"customShortName"`
	EstSailingDate   *time.Time `json:"estSailingDate"`
	ShipPort         string     `json:"shipPort"`
	DischargePort    string     `json:"dischargePort"`
	DischargePortEng string     `json:"dischargePortEng"`
	BanFlag          int64      `json:"banFlag"`
	CreatedStaffId   int64      `json:"createdStaffId"`
	CreatedAt        *time.Time `json:"createdAt"`
	UpdatedAt        *time.Time `json:"updatedAt"`
}

// List @TITLE 列表
func (s *serial) List(ctx context.Context, args ArgsSerialList) (reply ReplySerialList, err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "List", args, &reply)
	return
}

type ReplySerialInfo struct {
	ReplyShipmentInfo
	SerialNo   string `json:"serialNo"`
	ShipmentId int64  `json:"shipmentId"`
	BanFlag    int64  `json:"banFlag"`
}

// Info @TITLE 详情
func (s *serial) Info(ctx context.Context, serialId int64) (reply ReplySerialInfo, err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Info", serialId, &reply)
	return
}

// Products @TITLE 获取产品
func (s *serial) Products(ctx context.Context, serialId int64) (reply []SaleProductItem, err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Products", serialId, &reply)
	return
}

// Gifts @TITLE 获取赠品
func (s *serial) Gifts(ctx context.Context, serialId int64) (reply []GiftItem, err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Gifts", serialId, &reply)
	return
}

type ArgsSerialGen struct {
	ShipmentId             int64   // 订舱id
	SerialNo               string  // 商检单号
	ShipmentSaleProductIds []int64 // 订舱产品id
	ShipmentGiftIds        []int64 // 订舱赠品id
	StaffId                int64   // 操作员工
}

// Gen @TITLE 生成
func (s *serial) Gen(ctx context.Context, args ArgsSerialGen) (err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Gen", args, &reply)
	return
}

// Cancel @TITLE 作废
func (s *serial) Cancel(ctx context.Context, serialId int64) (err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Cancel", serialId, &reply)
	return
}

// Customs @TITLE 报关信息
func (s *serial) Customs(ctx context.Context, customsId int64) (reply bean2.ReplyCustoms, err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Customs", customsId, &reply)
	return
}