package channel

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

const (
	AfterServiceStatusApply   = 1 // 售后申请中
	AfterServiceStatusDeliver = 2 // 客户发货
	AfterServiceStatusHandle  = 3 // 收货处理中
	AfterServiceStatusFinal   = 4 // 售后完成
	AfterServiceStatusClose   = 5 // 售后关闭

	AfterServicePackageSendNone    = 1 // 无
	AfterServicePackageSendWait    = 2 // 待客户发货
	AfterServicePackageSendAlready = 3 // 客户已发货
)

type afterService struct {
}
type ArgsAfterServiceCan struct {
	OrderSn string // 订单编号
	SkuId   uint   // skuId
}
type ReplyAfterServiceCan struct {
	CanApply       uint               `json:"canApply"`       // 是否可申请售后 1=可申请 2=不可申请
	SkuId          uint               `json:"skuId"`          // skuId
	AppliedNum     uint               `json:"appliedNum"`     // 已申请售后商品数量
	CannotApplyTip string             `json:"cannotApplyTip"` // 不可申请原因
	Types          []AfterServiceType `json:"types"`
}
type AfterServiceType struct {
	TypeId   string `json:"typeId"`   // 类型
	TypeName string `json:"typeName"` // 类型名称
}

// Can @Title 获取是否可发起售后
func (a *afterService) Can(ctx context.Context, channelId string, args ArgsAfterServiceCan) (reply ReplyAfterServiceCan, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"channelId": channelId}), "Can", args, &reply)
	return
}

type ArgsAfterServiceReason struct {
	OrderSn string // 订单编号
	SkuId   uint   // skuId
	Type    string // 售后类型
}
type ReplyAfterServiceReason struct {
	ReasonCode  string `json:"reasonCode"`  // 售后原因编码
	ReasonName  string `json:"reasonName"`  // 售后原因描述
	NeedPicture uint   `json:"needPicture"` // 是否需要上传图片 1=需要上传 2=不需要上传
}

// Reason @Title 获取售后原因
func (a *afterService) Reason(ctx context.Context, channelId string, args ArgsAfterServiceReason) (reply []ReplyAfterServiceReason, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"channelId": channelId}), "Reason", args, &reply)
	return
}

type ArgsAfterServiceApply struct {
	TypeId                string   // 售后类型
	TypeName              string   // 售后名称
	ReasonCode            string   // 售后原因编码
	ReasonName            string   // 售后原因描述
	ChannelAfterServiceSn string   // 渠道售后单号
	OrderSn               string   // 订单编号
	SkuId                 uint     // skuId
	Pictures              []string // 图片地址数组
	Quantity              uint     // 售后申请数量
}
type ReplyAfterServiceApply struct {
	AfterServiceSn        string `json:"afterServiceSn"`        // 渠道售后单号
	ChannelAfterServiceSn string `json:"channelAfterServiceSn"` // 渠道售后单号
}

// Apply @Title 发起售后
func (a *afterService) Apply(ctx context.Context, channelId string, args ArgsAfterServiceApply) (reply ReplyAfterServiceApply, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"channelId": channelId}), "Apply", args, &reply)
	return
}

type ReplyAfterServiceLogisticsAddress struct {
	Name    string `json:"name"`    // 姓名
	Mobile  string `json:"mobile"`  // 手机号
	ZipCode string `json:"zipCode"` // 邮编
	Address string `json:"address"` // 地址
}

// LogisticsAddress @Title 寄回地址
func (a *afterService) LogisticsAddress(ctx context.Context, channelId string, afterServiceSn string) (reply ReplyAfterServiceLogisticsAddress, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"channelId": channelId}), "LogisticsAddress", afterServiceSn, &reply)
	return
}

type ArgsAfterServiceBackLogisticsBill struct {
	AfterServiceSn   string // 售后单号
	LogisticsCompany string // 物流公司
	WaybillCode      string // 运单号
	SendGoodsDate    int64  // 运单发货日期
}

// BackLogisticsBill @Title 回传物流信息
func (a *afterService) BackLogisticsBill(ctx context.Context, channelId string, args ArgsAfterServiceBackLogisticsBill) (err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"channelId": channelId}), "BackLogisticsBill", args, &reply)
	return
}

// Cancel @Title 取消售后
func (a *afterService) Cancel(ctx context.Context, channelId string, afterServiceSn string) (err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"channelId": channelId}), "Cancel", afterServiceSn, &reply)
	return
}

type AfterServiceSearch struct {
	Name             string // 商品名称
	OrderSn          string // 订单单号
	AfsSn            string // 售后单号
	CreatedDateStart string // 售后日期开始
	CreatedDateEnd   string // 售后日期结束
}
type ArgsAfterServiceLists struct {
	Search AfterServiceSearch
	Page   bean.Page
}
type ReplyAfterServiceLists struct {
	Lists []AfterServiceItem `json:"lists"`
	Total int64              `json:"total"`
}

type AfterServiceItem struct {
	Id             uint   `json:"id"`
	AfsSn          string `json:"afsSn"`
	OrderSubSn     string `json:"orderSubSn"`
	Name           string `json:"name"`
	ImgUrl         string `json:"imgUrl"`
	Quantity       uint   `json:"quantity"`
	TypeReasonName string `json:"typeReasonName"`
	OrderSubId     uint   `json:"orderSubId"`
	Result         string `json:"result"`
	Status         uint   `json:"status"`
	CreatedAt      int64  `json:"createdAt"`
	UpdatedAt      int64  `json:"updatedAt"`
}

// Lists @Title 售后列表
func (a *afterService) Lists(ctx context.Context, channelId string, args ArgsAfterServiceLists) (reply ReplyAfterServiceLists, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"channelId": channelId}), "Lists", args, &reply)
	return
}

type ReplyAfterServiceDetail struct {
	Id               uint            `json:"id"`
	AfsSn            string          `json:"afsSn"`
	Status           uint            `json:"status"`
	ApproveNotes     string          `json:"approveNotes"`
	Result           string          `json:"result"`
	CreatedAt        int64           `json:"createdAt"`
	UpdatedAt        int64           `json:"updatedAt"`
	SkuName          string          `json:"skuName"`
	ImgUrl           string          `json:"imgUrl"`
	SkuId            uint            `json:"skuId"`
	Price            decimal.Decimal `json:"price"`
	Quantity         uint            `json:"quantity"`
	HopeTypeName     string          `json:"hopeTypeName"`
	TypeReasonName   string          `json:"typeReasonName"`
	Imgs             []string        `json:"imgs"`
	LogisticsCompany string          `json:"logisticsCompany"`
	WaybillCode      string          `json:"waybillCode"`
	SendAt           int64           `json:"sendAt"`
	PackageSend      uint            `json:"packageSend"`
	AfsPackageSend   AfsPackageSend  `json:"afsPackageSend"`
}
type AfsPackageSend struct {
	Name             string `json:"name"`
	Mobile           string `json:"mobile"`
	ZipCode          string `json:"zipCode"`
	Address          string `json:"address"`
	LogisticsCompany string `json:"logisticsCompany"`
	WaybillCode      string `json:"waybillCode"`
	SendGoodsDate    int64  `json:"sendGoodsDate"`
}

// Detail @Title 售后详情
func (a *afterService) Detail(ctx context.Context, channelId string, afsSn string) (reply ReplyAfterServiceDetail, err error) {
	xClient, err := client.GetClient(a)
	if err != nil {
		return
	}
	err = xClient.Call(context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"channelId": channelId}), "Detail", afsSn, &reply)
	return
}