package jdapi

import (
	"git.rosy.net.cn/baseapi/utils"
)

// 新版订单级促销

const (
	OrderDiscountActivityTypeManJian     = 1    // 满减
	OrderDiscountActivityTypeHuanGou     = 1202 // 换购
	OrderDiscountActivityTypeManZeng     = 1203 // 满赠
	OrderDiscountActivityTypeManJianJian = 1206 // 满件减
	OrderDiscountActivityTypeManJianZhe  = 1207 // 满件折
	OrderDiscountActivityTypeKunBang     = 1208 // 捆绑
	OrderDiscountActivityTypeMeiManJian  = 2210 // 每满减

	ParticipationModeAllSku  = 1 // 全部商品维度
	ParticipationModePartSku = 2 // 部分商品维度
)

type OrderDiscountGift struct {
	SkuID      string `json:"skuId"`
	StockCount int    `json:"stockCount"`
}

type OrderDiscountRuleRequest struct {
	LowerLimitAmount int                  `json:"lowerLimitAmount,omitempty"`
	DiscountAmount   int                  `json:"discountAmount,omitempty"`
	UpperLimitCount  int                  `json:"upperLimitCount,omitempty"`
	LowerLimitCount  int                  `json:"lowerLimitCount,omitempty"`
	DiscountRate     float32              `json:"discountRate,omitempty"`
	AddPrice         int                  `json:"addPrice,omitempty"` // 分
	GiftList         []*OrderDiscountGift `json:"giftList,omitempty"`
	LadderLimit      int                  `json:"ladderLimit"`
}

type OrderDiscountActivity struct {
	OutActivityID         string                      `json:"outActivityId"`     // 必须
	PromName              string                      `json:"promName"`          // 必须
	PromType              int                         `json:"promType"`          // 必须
	BeginTime             string                      `json:"beginTime"`         // 必须
	EndTime               string                      `json:"endTime"`           // 必须
	ParticipationMode     int                         `json:"participationMode"` // 必须
	OutStationNos         []string                    `json:"outStationNos,omitempty"`
	StationNos            []string                    `json:"stationNos,omitempty"`
	OutSkuIDs             []string                    `json:"outSkuIds,omitempty"`
	SkuIDs                []string                    `json:"skuIds,omitempty"`
	LimitOrderTotalNumber int                         `json:"limitOrderTotalNumber"`          // 必须, 活动限购单量(0为不限，不超过10万)
	LimitUserTotalNumber  int                         `json:"limitUserTotalNumber,omitempty"` // 用户限购单量(0为不限，不超过100，仅满减和每满减有效)
	Display               string                      `json:"display"`                        // 必须
	RuleRequestList       []*OrderDiscountRuleRequest `json:"ruleRequestList,omitempty"`
}

type ActivityOpResultInfo struct {
	ActivityID    int64  `json:"activityID"`
	PromName      string `json:"promName"`
	StationNo     int64  `json:"stationNo"`
	SkuID         int64  `json:"skuID"`
	OutActivityID string `json:"outActivityId"`
	OutSkuID      string `json:"outSkuId"`
	OutStationNo  string `json:"outStationNo"`
}

type ActivityOpQueryResultResponse struct {
	SubCode    string                  `json:"subCode"`
	SubMsg     string                  `json:"subMsg"`
	ResultList []*ActivityOpResultInfo `json:"resultList"`
}

type ActivityOpQueryInfoResponse struct {
	ActivityID    int64           `json:"activityID"`
	OutActivityID string          `json:"outActivityId"`
	PromotionName string          `json:"promotionName"`
	BeginDate     *utils.JavaDate `json:"beginDate"`
	EndDate       *utils.JavaDate `json:"endDate"`
	Awords        string          `json:"awords"`
	State         int             `json:"state"`
	OrderLadder   string          `json:"orderLadder"`
	StationList   []struct {
		OrgName      string `json:"orgName"`
		StationNo    int64  `json:"stationNo"`
		OutStationNo string `json:"outStationNo"`
		StationName  string `json:"stationName"`
	} `json:"stationList"`
	SkuList []struct {
		OrgName  string `json:"orgName"`
		SkuID    int64  `json:"skuId"`
		OutSkuID string `json:"outSkuId"`
		SkuName  string `json:"skuName"`
	} `json:"skuList"`
	LadderList []struct {
		BenefitMaxCount int     `json:"benefitMaxCount"`
		DiscountAmount  int     `json:"discountAmount"`
		DiscountRate    float32 `json:"discountRate"`
		LowMoney        int     `json:"lowMoney"`
		LowerLimitCount int     `json:"lowerLimitCount"`
		OpGiftSkuList   []struct {
			OrgName    string `json:"orgName"`
			SkuID      int64  `json:"skuId"`
			OutSkuID   string `json:"outSkuId"`
			SkuName    string `json:"skuName"`
			StockCount int    `json:"stockCount"`
		} `json:"opGiftSkuList"`
		OrderLadder string `json:"orderLadder"`
	} `json:"ladderList"`
}

// 订单级促销活动提交接口
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=211&apiid=cf98af94d4124ca287af6cfe48f0f3aa
func (a *API) OrderDiscountSubmitActivity(actInfo *OrderDiscountActivity) (activityID int64, err error) {
	result, err := a.AccessAPINoPage("orderdiscount/submitActivity", utils.Struct2FlatMap(actInfo), nil, nil, nil)
	if err == nil {
		return utils.MustInterface2Int64((result.(map[string]interface{}))["activityId"]), nil
	}
	return 0, err
}

// 订单级促销活动查询活动提交处理结果接口
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=211&apiid=b04e5426948349a78db1c5c9585d8df7
func (a *API) OrderDiscountQuerySubmitActivityResult(activityID int64) (response *ActivityOpQueryResultResponse, err error) {
	result, err := a.AccessAPINoPage("orderdiscount/querySubmitActivityResult", map[string]interface{}{
		"activityId": activityID,
		"operator":   utils.APIPin,
		"traceId":    utils.GetUUID(),
	}, nil, nil, genNoPageResultParser("code", "detail", "result", "0"))
	if err == nil {
		err = JdMap2StructByJson(result.([]interface{})[0], &response, true) // todo，只取第一个？
	}
	return response, err
}

// 订单级促销活动查询促销详情接口
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=211&apiid=116d7dfe0b4e4122be300ce26b4e5381
func (a *API) OrderDiscountQueryActivityInfo(activityID int64) (response *ActivityOpQueryInfoResponse, err error) {
	result, err := a.AccessAPINoPage("orderdiscount/queryActivityInfo", map[string]interface{}{
		"activityId": activityID,
		"sourceFrom": 3,
		"operator":   utils.APIPin,
		"traceId":    "",
		"version":    "",
	}, nil, nil, genNoPageResultParser("code", "detail", "result", "0"))
	if err == nil {
		err = JdMap2StructByJson(result, &response, true)
	}
	return response, err
}

// 订单级促销活动取消接口
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=211&apiid=42509fdfeec14105a516b07b774a3055
func (a *API) OrderDiscountCancelActivity(activityID int64, operator, traceID string) (err error) {
	if traceID == "" {
		traceID = utils.GetUUID()
	}
	_, err = a.AccessAPINoPage("orderdiscount/cancelActivity", map[string]interface{}{
		"activityId": activityID,
		"operator":   utils.GetAPIOperator(operator),
		"traceId":    traceID,
	}, nil, nil, nil)
	return err
}
