package ebai

import (
	"encoding/json"
	"git.rosy.net.cn/baseapi/platformapi/ebaiapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/globals/api"
	"strings"
)

func (c *PurchaseHandler) makeAfsOrderInfoPartRefund(msg *ebaiapi.CallbackMsg, orderStatus *model.OrderStatus) *model.AfsOrder {
	var afsOrder *model.AfsOrder
	partRefundData := msg.Data.(*ebaiapi.CBPartRefundInfo)
	afsOrder = &model.AfsOrder{
		VendorID:         model.VendorIDEBAI,
		AfsOrderID:       orderStatus.VendorOrderID,
		VendorOrderID:    orderStatus.RefVendorOrderID,
		VendorStoreID:    "",
		StoreID:          0,
		AfsCreatedAt:     utils.Timestamp2Time(msg.Timestamp),
		VendorAppealType: "",
		AppealType:       model.AfsAppealTypeRefund,
		VendorReasonType: partRefundData.ReasonType,
		ReasonType:       c.convertAfsReasonType(partRefundData.ReasonType),
		ReasonDesc:       utils.LimitUTF8StringLen(buildFullReason(partRefundData.Reason, partRefundData.AdditionReason), 1024),
		ReasonImgList:    utils.LimitUTF8StringLen(strings.Join(partRefundData.Photos, ","), 1024),
		RefundType:       model.AfsTypePartRefund,
		VendorOrgCode:    msg.Source,
	}
	for _, sku := range partRefundData.RefundProducts {
		orderSku := &model.OrderSkuFinancial{
			Count:             sku.Number,
			VendorSkuID:       sku.SkuID,
			SkuID:             int(utils.Str2Int64WithDefault(sku.CustomSkuID, 0)),
			Name:              sku.Name,
			UserMoney:         sku.TotalRefund,
			PmSkuSubsidyMoney: sku.ShopEleRefund,
		}
		afsOrder.SkuUserMoney += orderSku.UserMoney
		afsOrder.PmSubsidyMoney += orderSku.PmSubsidyMoney
		afsOrder.Skus = append(afsOrder.Skus, orderSku)
	}
	return afsOrder
}

func (c *PurchaseHandler) makeAfsOrderInfoReverseRefund(msg *ebaiapi.CallbackMsg, orderStatus *model.OrderStatus) (*model.AfsOrder, error) {
	var afsOrder *model.AfsOrder
	partRefundData := msg.Data.(*ebaiapi.CBOrderReversePush)
	afsOrder = &model.AfsOrder{
		VendorID:         model.VendorIDEBAI,
		AfsOrderID:       orderStatus.VendorOrderID,
		VendorOrderID:    orderStatus.RefVendorOrderID,
		VendorStoreID:    utils.Int2Str(partRefundData.PlatformShopId),
		StoreID:          0,
		AfsCreatedAt:     utils.Timestamp2Time(msg.Timestamp),
		VendorAppealType: "",
		AppealType:       model.AfsAppealTypeRefund,
		VendorReasonType: utils.Int2Str(partRefundData.CurReverseEvent.ReasonCode),
		ReasonType:       c.convertAfsReasonType(utils.Int2Str(partRefundData.CurReverseEvent.ReasonCode)),
		ReasonDesc:       utils.LimitUTF8StringLen(partRefundData.CurReverseEvent.RefundReasonDesc, 1024),
		ReasonImgList:    utils.LimitUTF8StringLen(strings.Join(partRefundData.CurReverseEvent.ImageList, ","), 1024),
		RefundType:       model.AfsTypePartRefund,
		VendorOrgCode:    msg.Source,
	}
	// 查询售后列表
	refundSku, _, err := api.EbaiAPI.GetReverseOrder(orderStatus.RefVendorOrderID)
	if err == nil {
		refundSkuList := make([]*ebaiapi.ReverseSkuList, 0, len(refundSku))
		refundByte, _ := json.Marshal(refundSku)
		if err = json.Unmarshal(refundByte, &refundSkuList); err != nil {
			return nil, err
		}
		for _, sku := range refundSkuList {
			if sku.SkuName == "配送费" || sku.SkuName == "包装费" {
				continue
			}
			orderSku := &model.OrderSkuFinancial{
				Count:             sku.RefundQuantity,
				VendorSkuID:       utils.Int64ToStr(sku.PlatformSkuId),
				SkuID:             utils.Str2Int(sku.CustomSkuId),
				Name:              sku.SkuName,
				UserMoney:         int64(sku.RefundUserAmount),
				PmSkuSubsidyMoney: int64(sku.DiscountDetail.PlatformDiscountAmount),
			}
			afsOrder.SkuUserMoney += orderSku.UserMoney
			afsOrder.PmSubsidyMoney += orderSku.PmSubsidyMoney
			afsOrder.Skus = append(afsOrder.Skus, orderSku)
		}
	}
	return afsOrder, nil
}

func (c *PurchaseHandler) makeAfsOrderInfoMerchantAgreed(msg *ebaiapi.CallbackMsg, orderStatus *model.OrderStatus) (*model.AfsOrder, error) {
	var afsOrder *model.AfsOrder
	partRefundData := msg.Data.(*ebaiapi.CBUserCancelInfo)
	afsOrder = &model.AfsOrder{
		VendorID:         model.VendorIDEBAI,
		AfsOrderID:       orderStatus.VendorOrderID,
		VendorOrderID:    orderStatus.RefVendorOrderID,
		VendorStoreID:    utils.Int2Str(partRefundData.PlatformShopId),
		StoreID:          0,
		AfsCreatedAt:     utils.Timestamp2Time(msg.Timestamp),
		VendorAppealType: "",
		AppealType:       model.AfsAppealTypeRefund,
		VendorReasonType: utils.Int2Str(partRefundData.ReasonCode),
		ReasonType:       c.convertAfsReasonType(utils.Int2Str(partRefundData.ReasonCode)),
		ReasonDesc:       utils.LimitUTF8StringLen(partRefundData.CancelReason, 1024),
		ReasonImgList:    utils.LimitUTF8StringLen(strings.Join(partRefundData.Pictures, ","), 1024),
		RefundType:       model.AfsTypePartRefund,
		VendorOrgCode:    msg.Source,
	}
	// 查询售后列表
	refundSku, _, err := api.EbaiAPI.GetReverseOrder(orderStatus.RefVendorOrderID)
	if err == nil {
		refundSkuList := make([]*ebaiapi.ReverseSkuList, 0, len(refundSku))
		refundByte, _ := json.Marshal(refundSku)
		if err = json.Unmarshal(refundByte, &refundSkuList); err != nil {
			return nil, err
		}
		for _, sku := range refundSkuList {
			if sku.SkuName == "配送费" || sku.SkuName == "包装费" {
				continue
			}
			orderSku := &model.OrderSkuFinancial{
				Count:             sku.RefundQuantity,
				VendorSkuID:       utils.Int64ToStr(sku.PlatformSkuId),
				SkuID:             utils.Str2Int(sku.CustomSkuId),
				Name:              sku.SkuName,
				UserMoney:         int64(sku.RefundUserAmount),
				PmSkuSubsidyMoney: int64(sku.DiscountDetail.PlatformDiscountAmount),
			}
			afsOrder.SkuUserMoney += orderSku.UserMoney
			afsOrder.PmSubsidyMoney += orderSku.PmSubsidyMoney
			afsOrder.Skus = append(afsOrder.Skus, orderSku)
		}
	}
	return afsOrder, nil
}
