智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.1 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. md2 "applet/app/es/md"
  6. "applet/app/task/md"
  7. "applet/app/utils"
  8. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  9. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/tik_tok"
  10. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  11. "context"
  12. "encoding/json"
  13. "fmt"
  14. "github.com/olivere/elastic/v7"
  15. "github.com/syyongx/php2go"
  16. "github.com/tidwall/gjson"
  17. "strings"
  18. "time"
  19. )
  20. func TikTokTaskMoney(types string) {
  21. endTime := time.Now().Unix()
  22. startTime := endTime - 89*86400
  23. now := zhios_third_party_utils.GetTimeRange("today")
  24. if types == "yestday" {
  25. now["start"] = now["start"] - 10
  26. }
  27. if types == "now" {
  28. now["start"] = now["start"] - 10 - 86400
  29. }
  30. date := time.Unix(now["start"], 0).Format("2006-01-02")
  31. p := 1
  32. for {
  33. param := map[string]interface{}{
  34. "page_no": p,
  35. "page_size": 100,
  36. "video_publish_start_time": startTime,
  37. "billing_date": date,
  38. "video_publish_end_time": endTime,
  39. }
  40. api, _ := tik_tok.TaskApi("query_agency_video_daily_data/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param))
  41. fmt.Println(api)
  42. orderStr := gjson.Get(api, "data.results").String()
  43. if orderStr == "" {
  44. break
  45. }
  46. var order = make([]md.TikTokTaskOrder, 0)
  47. json.Unmarshal([]byte(orderStr), &order)
  48. for _, v := range order {
  49. if v.BillingGMV1D <= 0 {
  50. continue
  51. }
  52. var TalentProfit1D float64 = 0
  53. var result *elastic.SearchResult
  54. boolQuery := elastic.NewBoolQuery() // 创建bool查询
  55. rangeQuery := elastic.NewMatchQuery("task_id", v.TaskId)
  56. boolQuery.Must(rangeQuery)
  57. rangeQuery1 := elastic.NewRangeQuery("task_end_time").Gt(time.Now().Unix())
  58. boolQuery.Must(rangeQuery1)
  59. key := md2.ZhiosTikTokTaskDetailEsIndex
  60. esWhere := es.EsClient.Search().
  61. Index(key).
  62. TrackTotalHits(true).
  63. Query(boolQuery)
  64. var err error
  65. result, err = esWhere. // 设置查询条件
  66. Size(1). // 设置分页参数 - 每页大小,设置为0,代表不返回搜索结果,仅返回聚合分析结果
  67. From(0).
  68. Pretty(true). // 返回可读的json格式
  69. Do(context.Background())
  70. fmt.Println(result)
  71. fmt.Println(err)
  72. appId := ""
  73. if result != nil && len(result.Hits.Hits) > 0 {
  74. for _, hit := range result.Hits.Hits {
  75. if hit == nil {
  76. continue
  77. }
  78. jsonByte, marErr := hit.Source.MarshalJSON()
  79. var tmps md.TikTokTaskDetailSecond
  80. if marErr == nil {
  81. err := json.Unmarshal(jsonByte, &tmps)
  82. if err == nil {
  83. appId = tmps.Appid
  84. bili := float64(tmps.PaymentAllocateRatio) / 10000 * (10.0 / 11.0) * 1000
  85. TalentProfit1D = float64(int(bili)) / 10
  86. }
  87. }
  88. }
  89. }
  90. if appId == "" {
  91. continue
  92. }
  93. discount := float64(v.BillingGMV1D)/100 - float64(v.BillingRefundGMV1D)/100
  94. if discount < 0 {
  95. discount = 0
  96. }
  97. talentBili := float64(int(discount*(TalentProfit1D/100)*100)) / 100
  98. //每个平台不一样的比例
  99. sourceBili, ok := md.SourceBili[appId]
  100. if ok == false {
  101. sourceBili = "20"
  102. }
  103. platformBili := php2go.Round(discount*(utils.StrToFloat64(sourceBili)/100), 2)
  104. commission := discount - talentBili - platformBili
  105. fee := discount - commission
  106. oid := "douyin" + zhios_third_party_utils.Int64ToStr(v.VideoId) + v.AgentID + v.DouyinID + date
  107. oid = strings.ReplaceAll(oid, "_", "")
  108. oid = strings.ReplaceAll(oid, "-", "")
  109. oid = strings.ReplaceAll(oid, " ", "")
  110. oid = strings.ReplaceAll(oid, ":", "")
  111. var tikTokData model.TikTokPlanTeam
  112. db.ZhimengDb.Where("team_id=? and tik_tok_id=?", v.AgentID, v.DouyinID).Get(&tikTokData)
  113. if tikTokData.Id == 0 || tikTokData.Ext == "" {
  114. continue
  115. }
  116. if commission < 0 {
  117. commission = 0
  118. }
  119. split := strings.Split(tikTokData.Ext, "_")
  120. var tmp = model.PlayletSaleOrder{
  121. Uid: split[0],
  122. SubUid: utils.StrToInt(split[1]),
  123. Data: utils.SerializeStr(v),
  124. Oid: php2go.Md5(oid),
  125. Amount: utils.Float64ToStr(discount),
  126. Commission: utils.Float64ToStr(commission),
  127. CreateTime: utils.TimeParseStd(date + " 00:00:00"),
  128. UpdateTime: time.Now(),
  129. Title: v.TaskName,
  130. VideoType: "douyin",
  131. PlatformType: md.SourceCate[appId],
  132. GoodsType: "playlet",
  133. OrdType: "video",
  134. ExtendUid: v.DouyinID + "_" + v.AgentID,
  135. PlatformFee: utils.Float64ToStr(fee),
  136. TikFee: utils.Float64ToStr(talentBili),
  137. }
  138. if utils.InArr(appId, []string{"ttd02aa6974f166cca01", "tt8a93fc2e15affaaa01", "tt87bdf1001882dd4b01"}) {
  139. tmp.OrdType = "story"
  140. }
  141. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  142. playletSaleOrderDb.Set()
  143. ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(tmp.Oid, tmp.OrdType)
  144. tmp.Status = "订单付款"
  145. if ord != nil {
  146. tmp.CustomOid = ord.CustomOid
  147. if ord.CustomOid == "" || ord.CustomOid == "0" {
  148. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  149. }
  150. playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp)
  151. } else {
  152. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  153. playletSaleOrderDb.PlayletVideoOrderInsert(&tmp)
  154. }
  155. }
  156. p++
  157. }
  158. return
  159. }