智盟项目
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.

143 lines
4.6 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. date = "2023-10-25"
  32. p := 1
  33. for {
  34. param := map[string]interface{}{
  35. "page_no": p,
  36. "page_size": 100,
  37. "video_publish_start_time": startTime,
  38. "billing_date": date,
  39. "video_publish_end_time": endTime,
  40. }
  41. api, _ := tik_tok.TaskApi("query_agency_video_daily_data/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param))
  42. fmt.Println(api)
  43. orderStr := gjson.Get(api, "data.results").String()
  44. if orderStr == "" {
  45. break
  46. }
  47. var order = make([]md.TikTokTaskOrder, 0)
  48. json.Unmarshal([]byte(orderStr), &order)
  49. for _, v := range order {
  50. if v.BillingGMV1D <= 0 {
  51. continue
  52. }
  53. var TalentProfit1D float64 = 0
  54. var result *elastic.SearchResult
  55. boolQuery := elastic.NewBoolQuery() // 创建bool查询
  56. rangeQuery := elastic.NewMatchQuery("task_id", v.TaskId)
  57. boolQuery.Must(rangeQuery)
  58. rangeQuery1 := elastic.NewRangeQuery("task_end_time").Gt(time.Now().Unix())
  59. boolQuery.Must(rangeQuery1)
  60. key := md2.ZhiosTikTokTaskDetailEsIndex
  61. esWhere := es.EsClient.Search().
  62. Index(key).
  63. TrackTotalHits(true).
  64. Query(boolQuery)
  65. var err error
  66. result, err = esWhere. // 设置查询条件
  67. Size(1). // 设置分页参数 - 每页大小,设置为0,代表不返回搜索结果,仅返回聚合分析结果
  68. From(0).
  69. Pretty(true). // 返回可读的json格式
  70. Do(context.Background())
  71. fmt.Println(result)
  72. fmt.Println(err)
  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. bili := float64(tmps.PaymentAllocateRatio) / 10000 * (10.0 / 11.0) * 1000
  84. TalentProfit1D = float64(int(bili)) / 10
  85. }
  86. }
  87. }
  88. }
  89. discount := float64(v.BillingGMV1D)/100 - float64(v.BillingRefundGMV1D)/100
  90. if discount < 0 {
  91. discount = 0
  92. }
  93. talentBili := float64(int(discount*(TalentProfit1D/100)*100)) / 100
  94. platformBili := php2go.Round(discount*(15.0/100), 2)
  95. commission := discount - talentBili - platformBili
  96. fee := discount - commission
  97. oid := "douyin" + zhios_third_party_utils.Int64ToStr(v.VideoId) + v.AgentID + v.DouyinID + date
  98. oid = strings.ReplaceAll(oid, "_", "")
  99. oid = strings.ReplaceAll(oid, "-", "")
  100. oid = strings.ReplaceAll(oid, " ", "")
  101. oid = strings.ReplaceAll(oid, ":", "")
  102. var tikTokData model.TikTokPlanTeam
  103. db.ZhimengDb.Where("team_id=? and tik_tok_id=?", v.AgentID, v.DouyinID).Get(&tikTokData)
  104. if tikTokData.Id == 0 || tikTokData.Ext == "" {
  105. continue
  106. }
  107. split := strings.Split(tikTokData.Ext, "_")
  108. var tmp = model.PlayletSaleOrder{
  109. Uid: split[0],
  110. SubUid: utils.StrToInt(split[1]),
  111. Data: utils.SerializeStr(v),
  112. Oid: php2go.Md5(oid),
  113. Amount: utils.Float64ToStr(discount),
  114. Commission: utils.Float64ToStr(commission),
  115. CreateTime: utils.TimeParseStd(date + " 00:00:00"),
  116. UpdateTime: time.Now(),
  117. Title: v.VideoTitle,
  118. VideoType: "douyin",
  119. PlatformType: "dianzhong",
  120. GoodsType: "playlet",
  121. OrdType: "video",
  122. ExtendUid: v.DouyinID + "_" + v.AgentID,
  123. PlatformFee: utils.Float64ToStr(fee),
  124. TikFee: utils.Float64ToStr(talentBili),
  125. }
  126. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  127. playletSaleOrderDb.Set()
  128. ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(tmp.Oid, tmp.OrdType)
  129. tmp.Status = "订单付款"
  130. if ord != nil {
  131. playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp)
  132. } else {
  133. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  134. playletSaleOrderDb.PlayletVideoOrderInsert(&tmp)
  135. }
  136. }
  137. p++
  138. }
  139. return
  140. }