package jdapi

import (
	"time"

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

const (
	AuditPromotionStateNew      = 101 // 待开始
	AuditPromotionStateOnGoing  = 102 // 进行中
	AuditPromotionStateEnded    = 103 // 结束
	AuditPromotionStateCanceled = 104 // 取消
	AuditPromotionStateAll      = 105 // 全部
)

type OpenPlatActivityQResponse struct {
	AddLadderList []struct {
		BenefitMaxCount int     `json:"benefitMaxCount"`
		DiscountAmount  int     `json:"discountAmount"`
		DiscountRate    float32 `json:"discountRate"`
		LowMoney        int     `json:"lowMoney"`
		LowerLimitCount int     `json:"lowerLimitCount"`
		OrderLadder     string  `json:"orderLadder"`
	} `json:"addLadderList"`
	Awords        string          `json:"awords"`
	BeginDate     *utils.JavaDate `json:"beginDate"`
	CostRadios    int             `json:"costRadios"`
	EndDate       *utils.JavaDate `json:"endDate"`
	ID            int             `json:"id"`
	IsMerchant    int             `json:"isMerchant"`
	OrderLadder   string          `json:"orderLadder"`
	OrgCode       string          `json:"orgCode"`
	OutActivityID string          `json:"outActivityId"`
	PromotionName string          `json:"promotionName"`
	SkuBeanList   []struct {
		SkuID   int64 `json:"skuId"`
		SkuName int64 `json:"skuName"`
	} `json:"skuBeanList"`
	State           int `json:"state"`
	StationBeanList []struct {
		OrgCode      interface{} `json:"orgCode"`
		OrgName      string      `json:"orgName"`
		OutStationNo string      `json:"outStationNo"`
		StationName  string      `json:"stationName"`
		StationNo    int         `json:"stationNo"`
	} `json:"stationBeanList"`
}

type PromotionLspQuerySkuResult struct {
	BeginTime      *utils.JavaDate `json:"beginTime"`
	EndTime        *utils.JavaDate `json:"endTime"`
	LimitDaily     int             `json:"limitDaily"`
	LimitDevice    int             `json:"limitDevice"`
	LimitPin       int             `json:"limitPin"`
	PlatformRatio  int             `json:"platformRatio"`
	PromotionPrice int             `json:"promotionPrice"`
	PromotionState int             `json:"promotionState"`
	PromotionType  int             `json:"promotionType"`
	SkuID          int64           `json:"skuId"`
	Source         string          `json:"source"`
	StationNo      int64           `json:"stationNo"`
	StoreRatio     int             `json:"storeRatio"`
}

type PromotionLspQueryInfoResult struct {
	BeginTime       *utils.JavaDate               `json:"beginTime"`
	EndTime         *utils.JavaDate               `json:"endTime"`
	PromotionInfoID int64                         `json:"promotionInfoId"`
	PromotionState  int                           `json:"promotionState"`
	PromotionType   int                           `json:"promotionType"`
	SkuResultList   []*PromotionLspQuerySkuResult `json:"skuResultList"`
	Source          string                        `json:"source"`
}

// 此接口逐渐会被[新版订单级促销]中相应接口替换
// 根据到家活动ID查询订单级活动明细接口
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=196&apiid=ff1ade31ac1b4a50be760854c777b567
func (a *API) OrderDiscountQueryActivityInfoById(activityID int64, promotionType, state int, operator string) (response *OpenPlatActivityQResponse, err error) {
	params := map[string]interface{}{
		"activityId":  activityID,
		"type":        promotionType,
		"traceId":     utils.GetUUID(),
		"operator":    operator,
		"requestTime": time.Now().UnixNano() / 1000000,
	}
	if state > 0 {
		params["state"] = state
	}
	result, err := a.AccessAPINoPage("orderdiscount/queryActivityInfoById", params, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "data", "10000"))
	if err == nil {
		err = JdMap2StructByJson(result, &response, false)
	}
	return response, err
}

// 根据到家活动ID查询单品级促销活动接口
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=196&apiid=a47520a9757f4b0dbac1e6d36fd1103d
func (a *API) QueryPromotionInfo(promotionInfoId int64) (promotionInfo *PromotionLspQueryInfoResult, err error) {
	jdParams := map[string]interface{}{
		"promotionInfoId": promotionInfoId,
	}
	result, err := a.AccessAPINoPage("singlePromote/queryPromotionInfo", jdParams, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "data", "0"))
	if err == nil {
		err = JdMap2StructByJson(result, &promotionInfo, false)
	}
	return promotionInfo, err
}

// 根据到家商品ID查询单品级优惠活动列表接口
// https://openo2o.jddj.com/staticnew/widgets/resources.html?groupid=196&apiid=d73baba02c484109a3c3c1b1236ca13d
func (a *API) QueryPromotionSku(promotionType int, skuID int64, promotionState int /*, pageNo int64, pageSize int*/) (skuResultList []*PromotionLspQuerySkuResult, err error) {
	jdParams := map[string]interface{}{
		"promotionType":  promotionType,
		"skuId":          skuID,
		"promotionState": promotionState,
	}
	result, _, err := a.AccessAPIHavePage("singlePromote/queryPromotionSku", jdParams, nil, nil, genNormalHavePageResultParser("data"))
	if err == nil {
		err = JdMap2StructByJson(result, &skuResultList, false)
	}
	return skuResultList, err
}
