package sale

import (
	"context"
	"git.kumo.work/shama/service/client"
	"github.com/shopspring/decimal"
	"time"
)

type benefit struct {
}

type BenefitProductItem struct {
	Id                 int64            `json:"id"`
	ProductId          int64            `json:"productId"`
	Serial             string           `json:"serial"`
	CustomSerial       string           `json:"customSerial"`
	Name               string           `json:"name"`
	EngName            string           `json:"engName"`
	Mold               int64            `json:"mold"`
	FactoryId          int64            `json:"factoryId"`
	FactoryName        string           `json:"factoryName"`
	FactorySerial      string           `json:"factorySerial"`
	SaleCount          int64            `json:"saleCount"`
	Price              decimal.Decimal  `json:"price"`
	Amount             decimal.Decimal  `json:"amount"`
	AmountMinusTax     decimal.Decimal  `json:"amountMinusTax"`
	Currency           string           `json:"currency"`
	CurrencySymbol     string           `json:"currencySymbol"`
	CurrencyName       string           `json:"currencyName"`
	CurrencyRate       decimal.Decimal  `json:"currencyRate"`
	MinusTaxRate       *decimal.Decimal `json:"minusTaxRate"`
	SalePrice          decimal.Decimal  `json:"salePrice"`
	SaleAmount         decimal.Decimal  `json:"saleAmount"`
	SaleCurrency       string           `json:"saleCurrency"`
	SaleCurrencySymbol string           `json:"saleCurrencySymbol"`
	SaleCurrencyName   string           `json:"saleCurrencyName"`
	SaleCurrencyRate   decimal.Decimal  `json:"saleCurrencyRate"`
	SaleAmountCny      decimal.Decimal  `json:"saleAmountCny"`
	ProfitAndLossCny   decimal.Decimal  `json:"profitAndLossCny"`
}

// Product @TITLE 获取产品
func (b *benefit) Product(ctx context.Context, saleId int64) (reply []BenefitProductItem, err error) {
	xClient, err := client.GetClient(b)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Product", saleId, &reply)
	return
}

type ReplyBenefitInfo struct {
	Id                    int64           `json:"id"`
	TotalBoxCount         int64           `json:"totalBoxCount"`
	TotalNetWeight        decimal.Decimal `json:"totalNetWeight"`
	TotalGrossWeight      decimal.Decimal `json:"totalGrossWeight"`
	TotalVolume           decimal.Decimal `json:"totalVolume"`
	TotalCount            int64           `json:"totalCount"`
	TotalProductAmount    decimal.Decimal `json:"totalProductAmount"`
	TotalProductCost      decimal.Decimal `json:"totalProductCost"`
	DomesticShippingCost  decimal.Decimal `json:"domesticShippingCost"`
	DocumentCost          decimal.Decimal `json:"documentCost"`
	OtherCost             decimal.Decimal `json:"otherCost"`
	TotalSaleAmount       decimal.Decimal `json:"totalSaleAmount"`
	ForeignShippingCost   decimal.Decimal `json:"foreignShippingCost"`
	BankFees              decimal.Decimal `json:"bankFees"`
	ForeignSafeCost       decimal.Decimal `json:"foreignSafeCost"`
	ForeignCommission     decimal.Decimal `json:"foreignCommission"`
	Expense               decimal.Decimal `json:"expense"`
	Income                decimal.Decimal `json:"income"`
	IncomePurchase        decimal.Decimal `json:"incomePurchase"`
	ProfitAndLoss         decimal.Decimal `json:"profitAndLoss"`
	ProfitAndLossPurchase decimal.Decimal `json:"profitAndLossPurchase"`
	SettlementRate        decimal.Decimal `json:"settlementRate"`
	CreatedAt             *time.Time      `json:"createdAt"`
	UpdatedAt             *time.Time      `json:"updatedAt"`
}

// Info @TITLE 效益测算详情
func (b *benefit) Info(ctx context.Context, saleId int64) (reply ReplyBenefitInfo, err error) {
	xClient, err := client.GetClient(b)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Info", saleId, &reply)
	return
}

type ArgsBenefitEdit struct {
	SaleId               int64           // 销售合同
	DomesticShippingCost decimal.Decimal // 国内运杂费
	DocumentCost         decimal.Decimal // 单证费
	OtherCost            decimal.Decimal // 其他成本
	TotalSaleAmount      decimal.Decimal // 出口总价
	ForeignShippingCost  decimal.Decimal // 国外运杂费
	BankFees             decimal.Decimal // 银行手续费
	ForeignSafeCost      decimal.Decimal // 国外保险费
}

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