package tbunionapi

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

const (
	JxAdzoneID   = 111339100149
	JxAdzoneName = "京西推广位"

	TbElmActTypeBDH = 1
)

//创建推广位
func (a *API) AdzoneCreate(sid int64, adzoneName string) (err error) {
	_, err = a.AccessAPI("taobao.tbk.adzone.create", false, map[string]interface{}{
		"site_id":     sid,
		"adzone_name": adzoneName,
	})
	return err
}

type ActivityInfoGetResult struct {
	Data struct {
		ClickURL          string `json:"click_url"`
		ShortClickURL     string `json:"short_click_url"`
		WxMiniprogramPath string `json:"wx_miniprogram_path"`
		WxQrcodeURL       string `json:"wx_qrcode_url"`
	} `json:"data"`
	RequestID string `json:"request_id"`
}

//官方活动转链
func (a *API) ActivityInfoGet(uid, actMaterialID string, adzoneID int64) (activityInfoGetResult *ActivityInfoGetResult, err error) {
	result, err := a.AccessAPI("taobao.tbk.activity.info.get", false, map[string]interface{}{
		"union_id":             uid,
		"activity_material_id": actMaterialID,
		"adzone_id":            adzoneID,
	})
	if err == nil {
		utils.Map2StructByJson(result, &activityInfoGetResult, false)
	}
	return activityInfoGetResult, err
}

//订单查询
func (a *API) AllOrders(beginTime, endTime string) (activityInfoGetResult *ActivityInfoGetResult, err error) {
	result, err := a.AccessAPI("taobao.tbk.order.details.get", false, map[string]interface{}{
		"start_time": beginTime,
		"end_time":   endTime,
	})
	if err == nil {
		utils.Map2StructByJson(result, &activityInfoGetResult, false)
	}
	return activityInfoGetResult, err
}

//新用户订单查询
func (a *API) NewUserAllOrders(actID string) (activityInfoGetResult *ActivityInfoGetResult, err error) {
	result, err := a.AccessAPI("taobao.tbk.dg.newuser.order.get", false, map[string]interface{}{
		"activity_id": actID,
		"start_time":  "2021-04-20 00:00:00",
		"end_time":    "2021-04-25 00:00:00",
	})
	if err == nil {
		utils.Map2StructByJson(result, &activityInfoGetResult, false)
	}
	return activityInfoGetResult, err
}

//返利订单查询
func (a *API) RebateOrderGet(beginTime string) (activityInfoGetResult *ActivityInfoGetResult, err error) {
	result, err := a.AccessAPI("taobao.tbk.rebate.order.get", false, map[string]interface{}{
		"fields":     "tb_trade_parent_id,tb_trade_id,num_iid,item_title,item_num,price,pay_price,seller_nick,seller_shop_title,commission,commission_rate,unid,create_time,earning_time",
		"start_time": beginTime,
		"span":       600,
	})
	if err == nil {
		utils.Map2StructByJson(result, &activityInfoGetResult, false)
	}
	return activityInfoGetResult, err
}

type GatewayUnionpubResult struct {
	Eventbusourceplatform string `json:"eventBuSourcePlatform"`
	Pagesourcebu          string `json:"pageSourceBu"`
	Eventenname           string `json:"eventEnName"`
	Pagename              string `json:"pageName"`
	Pageurl               string `json:"pageUrl"`
	Pagestarttime         string `json:"pageStartTime"`
	Pageid                string `json:"pageId"`
	Pageendtime           string `json:"pageEndTime"`
	Pagepicturl           string `json:"pagePictUrl"`
	UdfTempStore          struct {
	} `json:"udf_temp_store"`
	Terminaltype      string `json:"terminalType"`
	Pageurlmd5        string `json:"pageUrlMd5"`
	Busourceplatform  string `json:"buSourcePlatform"`
	Eventperiodtype   int    `json:"eventPeriodType"`
	Eventid           string `json:"eventId"`
	Lensid            string `json:"lensId"`
	Tobizshowforcetop int    `json:"toBizShowForceTop"`
	Pagetracetype     string `json:"pageTraceType"`
	Eventname         string `json:"eventName"`
}

//查询饿了么活动（扒的）
func (a *API) GatewayUnionpub() (gatewayUnionpubResult []*GatewayUnionpubResult, err error) {
	result, err := a.AccessStorePage("https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json", map[string]interface{}{
		"floorId":     38766,
		"t":           time.Now().UnixNano(),
		"_tb_token_":  "7e385ebeee37e",
		"pageSize":    20,
		"refpid":      "mm_1172080007_0_0",
		"pageNum":     0,
		"variableMap": "{\"traceType\":\"4\",\"terminalType\":\"\",\"filterQuery\":\"\"}",
	}, false)
	if err == nil {
		utils.Map2StructByJson(result["data"].(map[string]interface{})["resultList"], &gatewayUnionpubResult, false)
	}
	return gatewayUnionpubResult, err
}

//长链转短链
func (a *API) SpreadGet(url string) (result string, err error) {
	data, err := a.AccessAPI("taobao.tbk.spread.get", false, map[string]interface{}{
		"requests": "[{\"url\":\"" + url + "\"}]",
	})
	if err == nil {
		return data["results"].([]interface{})[0].(map[string]interface{})["content"].(string), err
	}
	return result, err
}

type TpwdCreateResult struct {
	Model          string `json:"model"`
	PasswordSimple string `json:"password_simple"`
}

//淘口令生成
func (a *API) TpwdCreate(url string) (tpwdCreateResult *TpwdCreateResult, err error) {
	result, err := a.AccessAPI("taobao.tbk.tpwd.create", false, map[string]interface{}{
		"url": url,
	})
	if err == nil {
		utils.Map2StructByJson(result["data"], &tpwdCreateResult, false)
	}
	return tpwdCreateResult, err
}
