package pddapi

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

const (
	GoodsActTagMS   = 4     //-秒杀，
	GoodsActTagBYBT = 7     //-百亿补贴，
	GoodsActTagQWBT = 10851 //-千万补贴，
	GoodsActTagPPHB = 31    //-品牌黑标，
	GoodsActTagJXBK = 10564 //-精选爆品-官方直推爆款，
	GoodsActTagTZTJ = 10584 //-精选爆品-团长推荐，
	GoodsActTagPPGY = 24    //-品牌高佣 =
)

type GoodsCatsGetResult struct {
	CatName     string `json:"cat_name"`
	Level       int    `json:"level"`
	CatID       int    `json:"cat_id"`
	ParentCatID int    `json:"parent_cat_id"`
}

//多多进宝商品类目查询
func (a *API) GoodsCatsGet(catID int) (goodsCatsGetResult []*GoodsCatsGetResult, err error) {
	result, err := a.AccessAPI("pdd.goods.cats.get", false, map[string]interface{}{
		"parent_cat_id": catID,
	})
	if err == nil {
		utils.Map2StructByJson(result["goods_cats_get_response"].(map[string]interface{})["goods_cats_list"], &goodsCatsGetResult, false)
	}
	return goodsCatsGetResult, err
}

type GoodsSearchParam struct {
	CatID         int      `json:"cat_id,omitempty"`
	GoodsSignList []string `json:"goods_sign_list,omitempty"`
	ActivityTags  []int    `json:"activity_tags,omitempty"`
	Keyword       string   `json:"keyword,omitempty"`
	ListID        string   `json:"list_id,omitempty"`
	Page          int      `json:"page,omitempty"`
	PageSize      int      `json:"page_size,omitempty"`
	SortType      int      `json:"sort_type,omitempty"`
}
type GoodsSearchResult struct {
	GoodsList []struct {
		CategoryName                string   `json:"category_name"`
		CouponRemainQuantity        int      `json:"coupon_remain_quantity"`
		PromotionRate               int      `json:"promotion_rate"`
		ServiceTags                 []int    `json:"service_tags"`
		MallID                      int      `json:"mall_id"`
		MallName                    string   `json:"mall_name"`
		MallCouponEndTime           int      `json:"mall_coupon_end_time"`
		LgstTxt                     string   `json:"lgst_txt"`
		GoodsName                   string   `json:"goods_name"`
		GoodsID                     int      `json:"goods_id"`
		PredictPromotionRate        int      `json:"predict_promotion_rate"`
		GoodsDesc                   string   `json:"goods_desc"`
		OptName                     string   `json:"opt_name"`
		ShareRate                   int      `json:"share_rate"`
		OptIds                      []int    `json:"opt_ids"`
		GoodsImageURL               string   `json:"goods_image_url"`
		HasMallCoupon               bool     `json:"has_mall_coupon"`
		UnifiedTags                 []string `json:"unified_tags"`
		CouponStartTime             int      `json:"coupon_start_time"`
		MinGroupPrice               int      `json:"min_group_price"`
		CouponDiscount              int      `json:"coupon_discount"`
		CouponEndTime               int      `json:"coupon_end_time"`
		ZsDuoID                     int      `json:"zs_duo_id"`
		MallCouponRemainQuantity    int      `json:"mall_coupon_remain_quantity"`
		PlanType                    int      `json:"plan_type"`
		ExtraCouponAmount           int      `json:"extra_coupon_amount,omitempty"`
		CatIds                      []int    `json:"cat_ids"`
		CouponMinOrderAmount        int      `json:"coupon_min_order_amount"`
		CategoryID                  int      `json:"category_id"`
		MallCouponDiscountPct       int      `json:"mall_coupon_discount_pct"`
		CouponTotalQuantity         int      `json:"coupon_total_quantity"`
		MallCouponMinOrderAmount    int      `json:"mall_coupon_min_order_amount"`
		MerchantType                int      `json:"merchant_type"`
		SalesTip                    string   `json:"sales_tip"`
		OnlySceneAuth               bool     `json:"only_scene_auth"`
		DescTxt                     string   `json:"desc_txt"`
		MallCouponID                int      `json:"mall_coupon_id"`
		GoodsThumbnailURL           string   `json:"goods_thumbnail_url"`
		OptID                       int      `json:"opt_id"`
		SearchID                    string   `json:"search_id"`
		ActivityTags                []int    `json:"activity_tags"`
		HasCoupon                   bool     `json:"has_coupon"`
		MinNormalPrice              int      `json:"min_normal_price"`
		MallCouponStartTime         int      `json:"mall_coupon_start_time"`
		ServTxt                     string   `json:"serv_txt"`
		MallCouponTotalQuantity     int      `json:"mall_coupon_total_quantity"`
		MallCouponMaxDiscountAmount int      `json:"mall_coupon_max_discount_amount"`
		MallCps                     int      `json:"mall_cps"`
		GoodsSign                   string   `json:"goods_sign"`
		CltCpnEndTime               int      `json:"clt_cpn_end_time,omitempty"`
		CltCpnMinAmt                int      `json:"clt_cpn_min_amt,omitempty"`
		CltCpnRemainQuantity        int      `json:"clt_cpn_remain_quantity,omitempty"`
		CltCpnBatchSn               string   `json:"clt_cpn_batch_sn,omitempty"`
		CltCpnDiscount              int      `json:"clt_cpn_discount,omitempty"`
		CltCpnQuantity              int      `json:"clt_cpn_quantity,omitempty"`
		CltCpnStartTime             int      `json:"clt_cpn_start_time,omitempty"`
		ActivityType                int      `json:"activity_type,omitempty"`
	} `json:"goods_list"`
	ListID     string `json:"list_id"`
	TotalCount int    `json:"total_count"`
	RequestID  string `json:"request_id"`
	SearchID   string `json:"search_id"`
}

//多多进宝商品查询
func (a *API) GoodsSearch(goodsSearchParam *GoodsSearchParam) (goodsSearchResult *GoodsSearchResult, err error) {
	result, err := a.AccessAPI("pdd.ddk.goods.search", false, utils.Struct2FlatMap(goodsSearchParam))
	if err == nil {
		utils.Map2StructByJson(result["goods_search_response"], &goodsSearchResult, false)
	}
	return goodsSearchResult, err
}

type GoodsDetailResult struct {
	CategoryName                string        `json:"category_name"`
	CouponRemainQuantity        int           `json:"coupon_remain_quantity"`
	PromotionRate               int           `json:"promotion_rate"`
	ServiceTags                 []int         `json:"service_tags"`
	MallID                      int           `json:"mall_id"`
	MallName                    string        `json:"mall_name"`
	MallCouponEndTime           int           `json:"mall_coupon_end_time"`
	LgstTxt                     string        `json:"lgst_txt"`
	GoodsName                   string        `json:"goods_name"`
	GoodsGalleryUrls            []string      `json:"goods_gallery_urls"`
	GoodsID                     int64         `json:"goods_id"`
	PredictPromotionRate        int           `json:"predict_promotion_rate"`
	GoodsDesc                   string        `json:"goods_desc"`
	OptName                     string        `json:"opt_name"`
	ShareRate                   int           `json:"share_rate"`
	OptIds                      []int         `json:"opt_ids"`
	GoodsImageURL               string        `json:"goods_image_url"`
	MallImgURL                  string        `json:"mall_img_url"`
	HasMallCoupon               bool          `json:"has_mall_coupon"`
	UnifiedTags                 []interface{} `json:"unified_tags"`
	VideoUrls                   []string      `json:"video_urls"`
	CouponStartTime             int           `json:"coupon_start_time"`
	MinGroupPrice               int           `json:"min_group_price"`
	CouponDiscount              int           `json:"coupon_discount"`
	CouponEndTime               int           `json:"coupon_end_time"`
	ZsDuoID                     int           `json:"zs_duo_id"`
	MallCouponRemainQuantity    int           `json:"mall_coupon_remain_quantity"`
	PlanType                    int           `json:"plan_type"`
	ExtraCouponAmount           int           `json:"extra_coupon_amount"`
	CatIds                      []int         `json:"cat_ids"`
	CouponMinOrderAmount        int           `json:"coupon_min_order_amount"`
	CategoryID                  int           `json:"category_id"`
	MallCouponDiscountPct       int           `json:"mall_coupon_discount_pct"`
	CouponTotalQuantity         int           `json:"coupon_total_quantity"`
	MallCouponMinOrderAmount    int           `json:"mall_coupon_min_order_amount"`
	MerchantType                int           `json:"merchant_type"`
	SalesTip                    string        `json:"sales_tip"`
	OnlySceneAuth               bool          `json:"only_scene_auth"`
	DescTxt                     string        `json:"desc_txt"`
	GoodsThumbnailURL           string        `json:"goods_thumbnail_url"`
	OptID                       int           `json:"opt_id"`
	HasCoupon                   bool          `json:"has_coupon"`
	MinNormalPrice              int           `json:"min_normal_price"`
	MallCouponStartTime         int           `json:"mall_coupon_start_time"`
	ServTxt                     string        `json:"serv_txt"`
	MallCouponTotalQuantity     int           `json:"mall_coupon_total_quantity"`
	MallCouponMaxDiscountAmount int           `json:"mall_coupon_max_discount_amount"`
	MallCps                     int           `json:"mall_cps"`
	GoodsSign                   string        `json:"goods_sign"`
}

//多多进宝商品详情
func (a *API) GoodsDetail(goodsID string) (goodsDetailResult *GoodsDetailResult, err error) {
	var goodsDetailResults []*GoodsDetailResult
	result, err := a.AccessAPI("pdd.ddk.goods.detail", false, map[string]interface{}{
		"goods_sign": goodsID,
	})
	if err == nil {
		utils.Map2StructByJson(result["goods_detail_response"].(map[string]interface{})["goods_details"], &goodsDetailResults, false)
		if len(goodsDetailResults) > 0 {
			goodsDetailResult = goodsDetailResults[0]
		}
	}
	return goodsDetailResult, err
}

type GoodsRecommendGetResult struct {
	Total  int    `json:"total"`
	ListID string `json:"list_id"`
	List   []struct {
		CategoryName         string        `json:"category_name"`
		CouponRemainQuantity int           `json:"coupon_remain_quantity"`
		PromotionRate        int           `json:"promotion_rate"`
		CatIds               []int         `json:"cat_ids"`
		CouponMinOrderAmount int           `json:"coupon_min_order_amount"`
		CategoryID           string        `json:"category_id"`
		MallID               int           `json:"mall_id"`
		MallName             string        `json:"mall_name"`
		CouponTotalQuantity  int           `json:"coupon_total_quantity"`
		MerchantType         string        `json:"merchant_type"`
		LgstTxt              string        `json:"lgst_txt"`
		GoodsName            string        `json:"goods_name"`
		SalesTip             string        `json:"sales_tip"`
		GoodsID              int64         `json:"goods_id"`
		DescTxt              string        `json:"desc_txt"`
		PredictPromotionRate int           `json:"predict_promotion_rate"`
		GoodsDesc            string        `json:"goods_desc"`
		OptName              string        `json:"opt_name"`
		GoodsThumbnailURL    string        `json:"goods_thumbnail_url"`
		ShareRate            int           `json:"share_rate"`
		OptID                string        `json:"opt_id"`
		OptIds               []int         `json:"opt_ids"`
		SearchID             string        `json:"search_id"`
		GoodsImageURL        string        `json:"goods_image_url"`
		ActivityTags         []int         `json:"activity_tags"`
		HasCoupon            bool          `json:"has_coupon"`
		MinNormalPrice       int           `json:"min_normal_price"`
		ServTxt              string        `json:"serv_txt"`
		UnifiedTags          []interface{} `json:"unified_tags"`
		CouponStartTime      int           `json:"coupon_start_time"`
		MinGroupPrice        int           `json:"min_group_price"`
		CouponDiscount       int           `json:"coupon_discount"`
		GoodsSign            string        `json:"goods_sign"`
		CouponEndTime        int           `json:"coupon_end_time"`
		ExtraCouponAmount    int           `json:"extra_coupon_amount,omitempty"`
	} `json:"list"`
	RequestID string `json:"request_id"`
	SearchID  string `json:"search_id"`
}

//多多进宝商品推荐查询
func (a *API) GoodsRecommendGet(goodsID string, rcmmdType, offset, pageSize int) (goodsSearchResult *GoodsRecommendGetResult, err error) {
	result, err := a.AccessAPI("pdd.ddk.goods.recommend.get", false, map[string]interface{}{
		"goods_sign_list": []string{"\"" + goodsID + "\""},
		"channel_type":    rcmmdType,
		"limit":           pageSize,
		"offset":          offset,
	})
	if err == nil {
		utils.Map2StructByJson(result["goods_basic_detail_response"], &goodsSearchResult, false)
	}
	return goodsSearchResult, err
}
