package svc import ( "applet/app/db" "applet/app/db/model" md2 "applet/app/es/md" "applet/app/task/md" "applet/app/utils" "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/tik_tok" zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils" "context" "encoding/json" "fmt" "github.com/olivere/elastic/v7" "github.com/syyongx/php2go" "github.com/tidwall/gjson" "strings" "time" ) func TikTokTaskMoney(types string) { endTime := time.Now().Unix() startTime := endTime - 89*86400 now := zhios_third_party_utils.GetTimeRange("today") if types == "yestday" { now["start"] = now["start"] - 10 } if types == "now" { now["start"] = now["start"] - 10 - 86400 } date := time.Unix(now["start"], 0).Format("2006-01-02") p := 1 for { param := map[string]interface{}{ "page_no": p, "page_size": 100, "video_publish_start_time": startTime, "billing_date": date, "video_publish_end_time": endTime, } api, _ := tik_tok.TaskApi("query_agency_video_daily_data/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param)) fmt.Println(api) orderStr := gjson.Get(api, "data.results").String() if orderStr == "" { break } var order = make([]md.TikTokTaskOrder, 0) json.Unmarshal([]byte(orderStr), &order) for _, v := range order { if v.BillingGMV1D <= 0 { continue } var TalentProfit1D float64 = 0 var result *elastic.SearchResult boolQuery := elastic.NewBoolQuery() // 创建bool查询 rangeQuery := elastic.NewMatchQuery("task_id", v.TaskId) boolQuery.Must(rangeQuery) rangeQuery1 := elastic.NewRangeQuery("task_end_time").Gt(time.Now().Unix()) boolQuery.Must(rangeQuery1) key := md2.ZhiosTikTokTaskDetailEsIndex esWhere := es.EsClient.Search(). Index(key). TrackTotalHits(true). Query(boolQuery) var err error result, err = esWhere. // 设置查询条件 Size(1). // 设置分页参数 - 每页大小,设置为0,代表不返回搜索结果,仅返回聚合分析结果 From(0). Pretty(true). // 返回可读的json格式 Do(context.Background()) fmt.Println(result) fmt.Println(err) appId := "" if result != nil && len(result.Hits.Hits) > 0 { for _, hit := range result.Hits.Hits { if hit == nil { continue } jsonByte, marErr := hit.Source.MarshalJSON() var tmps md.TikTokTaskDetailSecond if marErr == nil { err := json.Unmarshal(jsonByte, &tmps) if err == nil { appId = tmps.Appid bili := float64(tmps.PaymentAllocateRatio) / 10000 * (10.0 / 11.0) * 1000 TalentProfit1D = float64(int(bili)) / 10 } } } } if appId == "" { continue } discount := float64(v.BillingGMV1D)/100 - float64(v.BillingRefundGMV1D)/100 if discount < 0 { discount = 0 } talentBili := float64(int(discount*(TalentProfit1D/100)*100)) / 100 //每个平台不一样的比例 sourceBili, ok := md.SourceBili[appId] if ok == false { sourceBili = "30" } platformBili := php2go.Round(discount*(utils.StrToFloat64(sourceBili)/100), 2) commission := discount - talentBili - platformBili fee := discount - commission oid := "douyin" + zhios_third_party_utils.Int64ToStr(v.VideoId) + v.AgentID + v.DouyinID + date oid = strings.ReplaceAll(oid, "_", "") oid = strings.ReplaceAll(oid, "-", "") oid = strings.ReplaceAll(oid, " ", "") oid = strings.ReplaceAll(oid, ":", "") var tikTokData model.TikTokPlanTeam db.ZhimengDb.Where("team_id=? and tik_tok_id=?", v.AgentID, v.DouyinID).Get(&tikTokData) if tikTokData.Id == 0 || tikTokData.Ext == "" { continue } if commission < 0 { commission = 0 } split := strings.Split(tikTokData.Ext, "_") var tmp = model.PlayletSaleOrder{ Uid: split[0], SubUid: utils.StrToInt(split[1]), Data: utils.SerializeStr(v), Oid: php2go.Md5(oid), Amount: utils.Float64ToStr(discount), Commission: utils.Float64ToStr(commission), CreateTime: utils.TimeParseStd(date + " 00:00:00"), UpdateTime: time.Now(), Title: v.TaskName, VideoType: "douyin", PlatformType: md.SourceCate[appId], GoodsType: "playlet", OrdType: "video", ExtendUid: v.DouyinID + "_" + v.AgentID, PlatformFee: utils.Float64ToStr(fee), TikFee: utils.Float64ToStr(talentBili), } if utils.InArr(appId, []string{"ttd02aa6974f166cca01", "tt8a93fc2e15affaaa01", "tt87bdf1001882dd4b01"}) { tmp.OrdType = "story" } playletSaleOrderDb := db.PlayletSaleOrderDb{} playletSaleOrderDb.Set() ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(tmp.Oid, tmp.OrdType) tmp.Status = "订单付款" if ord != nil { tmp.CustomOid = ord.CustomOid if ord.CustomOid == "" || ord.CustomOid == "0" { tmp.CustomOid = utils.OrderUUID(tmp.SubUid) } playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp) } else { tmp.CustomOid = utils.OrderUUID(tmp.SubUid) playletSaleOrderDb.PlayletVideoOrderInsert(&tmp) } } p++ } return }