智盟项目
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 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. 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. if result != nil && len(result.Hits.Hits) > 0 {
  73. for _, hit := range result.Hits.Hits {
  74. if hit == nil {
  75. continue
  76. }
  77. jsonByte, marErr := hit.Source.MarshalJSON()
  78. var tmps md.TikTokTaskDetailSecond
  79. if marErr == nil {
  80. err := json.Unmarshal(jsonByte, &tmps)
  81. if err == nil {
  82. bili := float64(tmps.PaymentAllocateRatio) / 10000 * (10.0 / 11.0) * 1000
  83. TalentProfit1D = float64(int(bili)) / 10
  84. }
  85. }
  86. }
  87. }
  88. discount := float64(v.BillingGMV1D)/100 - float64(v.BillingRefundGMV1D)/100
  89. if discount < 0 {
  90. discount = 0
  91. }
  92. talentBili := float64(int(discount*(TalentProfit1D/100)*100)) / 100
  93. platformBili := php2go.Round(discount*(15.0/100), 2)
  94. commission := discount - talentBili - platformBili
  95. fee := discount - commission
  96. oid := "douyin" + zhios_third_party_utils.Int64ToStr(v.VideoId) + v.AgentID + v.DouyinID + date
  97. oid = strings.ReplaceAll(oid, "_", "")
  98. oid = strings.ReplaceAll(oid, "-", "")
  99. oid = strings.ReplaceAll(oid, " ", "")
  100. oid = strings.ReplaceAll(oid, ":", "")
  101. var tikTokData model.TikTokPlanTeam
  102. db.ZhimengDb.Where("team_id=? and tik_tok_id=?", v.AgentID, v.DouyinID).Get(&tikTokData)
  103. if tikTokData.Id == 0 || tikTokData.Ext == "" {
  104. continue
  105. }
  106. split := strings.Split(tikTokData.Ext, "_")
  107. var tmp = model.PlayletSaleOrder{
  108. Uid: split[0],
  109. SubUid: utils.StrToInt(split[1]),
  110. Data: utils.SerializeStr(v),
  111. Oid: php2go.Md5(oid),
  112. Amount: utils.Float64ToStr(discount),
  113. Commission: utils.Float64ToStr(commission),
  114. CreateTime: utils.TimeParseStd(date + " 00:00:00"),
  115. UpdateTime: time.Now(),
  116. Title: v.VideoTitle,
  117. VideoType: "douyin",
  118. PlatformType: "dianzhong",
  119. GoodsType: "playlet",
  120. OrdType: "video",
  121. ExtendUid: v.DouyinID + "_" + v.AgentID,
  122. PlatformFee: utils.Float64ToStr(fee),
  123. TikFee: utils.Float64ToStr(talentBili),
  124. }
  125. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  126. playletSaleOrderDb.Set()
  127. ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(tmp.Oid, tmp.OrdType)
  128. tmp.Status = "订单付款"
  129. if ord != nil {
  130. playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp)
  131. } else {
  132. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  133. playletSaleOrderDb.PlayletVideoOrderInsert(&tmp)
  134. }
  135. }
  136. p++
  137. }
  138. return
  139. }