package tbunionapi

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

const (
	SSRXCatID = 28026
)

type OptimusMaterialResult struct {
	CategoryID           int      `json:"category_id"`
	ClickURL             string   `json:"click_url"`
	CommissionRate       string   `json:"commission_rate"`
	CouponAmount         int      `json:"coupon_amount"`
	CouponClickURL       string   `json:"coupon_click_url,omitempty"`
	CouponEndTime        string   `json:"coupon_end_time"`
	CouponRemainCount    int      `json:"coupon_remain_count"`
	CouponShareURL       string   `json:"coupon_share_url,omitempty"`
	CouponStartFee       string   `json:"coupon_start_fee"`
	CouponStartTime      string   `json:"coupon_start_time"`
	CouponTotalCount     int      `json:"coupon_total_count"`
	ItemDescription      string   `json:"item_description"`
	ItemID               int64    `json:"item_id"`
	JhsPriceUspList      string   `json:"jhs_price_usp_list"`
	LevelOneCategoryID   int      `json:"level_one_category_id"`
	LevelOneCategoryName string   `json:"level_one_category_name"`
	Nick                 string   `json:"nick"`
	PictURL              string   `json:"pict_url"`
	ReservePrice         string   `json:"reserve_price"`
	SellerID             int64    `json:"seller_id"`
	ShopTitle            string   `json:"shop_title"`
	ShortTitle           string   `json:"short_title"`
	SmallImages          []string `json:"small_images,omitempty"`
	SubTitle             string   `json:"sub_title"`
	Title                string   `json:"title"`
	UserType             int      `json:"user_type"`
	Volume               int      `json:"volume"`
	WhiteImage           string   `json:"white_image"`
	ZkFinalPrice         string   `json:"zk_final_price"`
}

//物料精选
func (a *API) OptimusMaterial(itemID, materialID, adzoneID, pageNo, pageSize int) (activityInfoGetResult []*OptimusMaterialResult, err error) {
	params := map[string]interface{}{
		"material_id": materialID,
		"adzone_id":   adzoneID,
		"page_no":     pageNo,
		"page_size":   pageSize,
	}
	if itemID != 0 {
		params["item_id"] = itemID
	}
	result, err := a.AccessAPI("taobao.tbk.dg.optimus.material", false, params)
	if err == nil {
		utils.Map2StructByJson(result["result_list"], &activityInfoGetResult, false)
	}
	return activityInfoGetResult, err
}

type MaterialOptionalParam struct {
	AdzoneID int    `json:"adzone_id,omitempty"`
	Q        string `json:"q,omitempty"`
	PageNo   int    `json:"page_no,omitempty"`
	PageSize int    `json:"page_size,omitempty"`
	Sort     string `json:"sort,omitempty"`
}

type MaterialOptionalResult struct {
	ResultList []struct {
		CategoryID           int      `json:"category_id"`
		CategoryName         string   `json:"category_name"`
		CommissionRate       string   `json:"commission_rate"`
		CommissionType       string   `json:"commission_type"`
		CouponAmount         string   `json:"coupon_amount,omitempty"`
		CouponEndTime        string   `json:"coupon_end_time,omitempty"`
		CouponID             string   `json:"coupon_id"`
		CouponInfo           string   `json:"coupon_info"`
		CouponRemainCount    int      `json:"coupon_remain_count"`
		CouponShareURL       string   `json:"coupon_share_url,omitempty"`
		CouponStartFee       string   `json:"coupon_start_fee,omitempty"`
		CouponStartTime      string   `json:"coupon_start_time,omitempty"`
		CouponTotalCount     int      `json:"coupon_total_count"`
		IncludeDxjh          string   `json:"include_dxjh"`
		IncludeMkt           string   `json:"include_mkt"`
		InfoDxjh             string   `json:"info_dxjh"`
		ItemDescription      string   `json:"item_description"`
		ItemID               int64    `json:"item_id"`
		ItemURL              string   `json:"item_url"`
		LevelOneCategoryID   int      `json:"level_one_category_id"`
		LevelOneCategoryName string   `json:"level_one_category_name"`
		Nick                 string   `json:"nick"`
		NumIid               int64    `json:"num_iid"`
		PictURL              string   `json:"pict_url"`
		PresaleDeposit       string   `json:"presale_deposit"`
		PresaleEndTime       int      `json:"presale_end_time"`
		PresaleStartTime     int      `json:"presale_start_time"`
		PresaleTailEndTime   int      `json:"presale_tail_end_time"`
		PresaleTailStartTime int      `json:"presale_tail_start_time"`
		Provcity             string   `json:"provcity"`
		RealPostFee          string   `json:"real_post_fee"`
		ReservePrice         string   `json:"reserve_price"`
		SellerID             int64    `json:"seller_id"`
		ShopDsr              int      `json:"shop_dsr"`
		ShopTitle            string   `json:"shop_title"`
		ShortTitle           string   `json:"short_title"`
		SmallImages          []string `json:"small_images"`
		SuperiorBrand        string   `json:"superior_brand"`
		Title                string   `json:"title"`
		TkTotalCommi         string   `json:"tk_total_commi"`
		TkTotalSales         string   `json:"tk_total_sales"`
		URL                  string   `json:"url"`
		UserType             int      `json:"user_type"`
		Volume               int      `json:"volume"`
		WhiteImage           string   `json:"white_image"`
		XID                  string   `json:"x_id"`
		ZkFinalPrice         string   `json:"zk_final_price"`
	} `json:"result_list"`
	TotalResults int    `json:"total_results"`
	RequestID    string `json:"request_id"`
}

//物料搜索
func (a *API) MaterialOptional(param *MaterialOptionalParam) (activityInfoGetResult *MaterialOptionalResult, err error) {
	result, err := a.AccessAPI("taobao.tbk.dg.material.optional", false, utils.Struct2FlatMap(param))
	if err == nil {
		utils.Map2StructByJson(result, &activityInfoGetResult, false)
	}
	return activityInfoGetResult, err
}

//商品链接转换
func (a *API) ItemConvert(itemID int) (activityInfoGetResult []*OptimusMaterialResult, err error) {
	result, err := a.AccessAPI("taobao.tbk.item.convert", false, map[string]interface{}{
		"fields":    "num_iid,click_url",
		"num_iids":  utils.Int2Str(itemID),
		"adzone_id": JxAdzoneID,
	})
	if err == nil {
		utils.Map2StructByJson(result["result_list"], &activityInfoGetResult, false)
	}
	return activityInfoGetResult, err
}

//商品类目查询
func (a *API) ItemcatsGet(itemID int) (activityInfoGetResult []*OptimusMaterialResult, err error) {
	result, err := a.AccessAPI("taobao.itemcats.get", false, map[string]interface{}{
		"parent_cid": 0,
	})
	if err == nil {
		utils.Map2StructByJson(result["result_list"], &activityInfoGetResult, false)
	}
	return activityInfoGetResult, err
}
