智盟项目
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

239 linhas
6.8 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. offical "applet/app/db/official"
  6. model2 "applet/app/db/official/model"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/chanxuanTikTok"
  10. "fmt"
  11. "github.com/tidwall/gjson"
  12. "time"
  13. )
  14. func TikTokChanxuan(ordType int) {
  15. pvdTimeKey := "chanxuan_time_" + utils.IntToStr(ordType)
  16. // 获得最后时间
  17. latest := offical.SysCfgByKey(pvdTimeKey)
  18. if latest == nil {
  19. offical.DbsSysCfgInserts(pvdTimeKey, time.Now().String())
  20. latest = offical.SysCfgByKey(pvdTimeKey)
  21. }
  22. // 所有时间都是在操作秒数
  23. now := time.Now().Unix()
  24. timeStr := latest.V
  25. fmt.Println(timeStr)
  26. past := utils.TimeParseStd(timeStr).Unix()
  27. // 如果上次记录超过30天或者 过去时间大于当前时间戳, 把时间设置为此前20分钟
  28. if past > now {
  29. past = now
  30. }
  31. userId := offical.SysCfgByKeyStr("tikTok_chanxuan_user_id")
  32. var (
  33. beginTime int64 = 0
  34. endTime int64 = 0
  35. pageNo int = 1
  36. pageSize int = 50
  37. )
  38. //怕时间不是走最新的
  39. leave := now - past
  40. if leave > 500 {
  41. leave = 0
  42. }
  43. past = past + leave
  44. beginTime = past - 20*60
  45. endTime = past
  46. if endTime > now {
  47. endTime = now
  48. }
  49. token := GetChanxuanToken()
  50. for {
  51. var err error
  52. count := OrdersTikTokChanxuanGet(pageNo, pageSize, beginTime, endTime, userId, ordType, token)
  53. if err != nil || count == 0 {
  54. goto ChkArg
  55. }
  56. if count > 0 {
  57. count = pageSize
  58. }
  59. // 判断是否分页已经全部取完了
  60. if count == pageSize {
  61. pageNo++
  62. fmt.Println("========蝉选下一页========" + utils.IntToStr(pageNo))
  63. continue
  64. }
  65. ChkArg:
  66. // 查询完后重置时间, 最后查询时间
  67. if endTime < now {
  68. pageNo = 1
  69. offical.DbsSysCfgUpdate(pvdTimeKey, utils.TimeToStr(endTime))
  70. beginTime = endTime
  71. endTime = endTime + 20*60
  72. if endTime > now {
  73. endTime = now
  74. }
  75. continue
  76. }
  77. break
  78. }
  79. offical.DbsSysCfgUpdate(pvdTimeKey, time.Unix(now, 0).Format("2006-01-02 15:04:05"))
  80. }
  81. var stationState = map[string]string{
  82. "PAY_SUCC": "订单付款",
  83. "REFUND": "订单退款",
  84. "SETTLE": "订单结算",
  85. "CONFIRM": "订单成功",
  86. }
  87. func OrdersTikTokChanxuanGet(page, pageSize int, sTime, eTime int64, userId string, pvd int, token string) int {
  88. distributionType := pvd
  89. tikTokArgs := map[string]string{
  90. "page_size": utils.IntToStr(pageSize),
  91. "page": utils.IntToStr(page),
  92. "start_time": utils.Int64ToStr(time.Unix(sTime, 0).Unix()),
  93. "end_time": utils.Int64ToStr(time.Unix(eTime, 0).Unix()),
  94. "query_type": utils.IntToStr(distributionType),
  95. "user_id": userId,
  96. }
  97. order := chanxuanTikTok.GetOrder(token, userId, tikTokArgs)
  98. fmt.Println("==================蝉选", utils.SerializeStr(tikTokArgs)+" "+utils.IntToStr(len(order)))
  99. uuidList := make([]model2.UuidList, 0)
  100. uuidMap := make(map[int]string)
  101. db.Db.Find(&uuidList)
  102. for _, v := range uuidList {
  103. uuidMap[v.Id] = v.Uuid
  104. }
  105. for _, v := range order {
  106. if v.VipMsg == "" {
  107. continue
  108. }
  109. orderType := 0
  110. mid := ""
  111. uid := ""
  112. one := db.GetGuideOrderByOid(utils.AnyToString(v.OrderId), "csjp")
  113. if one != nil {
  114. orderType = one.IsShare
  115. mid = utils.IntToStr(one.Uid)
  116. uid = utils.IntToStr(one.StationUid)
  117. } else {
  118. vipMsg := utils.Base62Decode(v.VipMsg)
  119. if vipMsg == 0 {
  120. continue
  121. }
  122. vipMsgStr := utils.IntToStr(vipMsg)
  123. orderTypeStr := vipMsgStr[0:1]
  124. if orderTypeStr == "2" {
  125. orderType = 1
  126. }
  127. mid = uuidMap[utils.StrToInt(vipMsgStr[1:5])]
  128. uid = vipMsgStr[5:]
  129. }
  130. if mid == "" || uid == "" {
  131. continue
  132. }
  133. var ord = &model.GuideOrder{
  134. Oid: utils.StrToInt64(utils.OrderUUID(utils.StrToInt(uid))),
  135. Uid: utils.StrToInt(mid),
  136. StationUid: utils.StrToInt(uid),
  137. PvdOid: utils.AnyToString(v.OrderId),
  138. Status: stationState[v.FlowPoint],
  139. UpdateTime: int(time.Now().Unix()),
  140. Commission: utils.Float64ToStr(float64(v.EstimatedCommission) / 100),
  141. Title: utils.AnyToString(v.ProductName),
  142. Payment: utils.Float64ToStr(float64(v.TotalPayAmount) / 100),
  143. Pvd: "csjp",
  144. Img: v.ProductImg,
  145. IsShare: orderType,
  146. Gid: v.ProductId,
  147. ThreeType: "chan_xuan",
  148. }
  149. if v.PaySuccessTime != "" {
  150. ord.CreateTime = int(utils.TimeStdParseUnix(v.PaySuccessTime))
  151. }
  152. if v.SettleTime != "" && v.SettleTime != nil {
  153. ord.PlatformSettleTime = int(utils.TimeStdParseUnix(utils.AnyToString(v.SettleTime)))
  154. }
  155. if v.RealCommission > 0 {
  156. ord.RealCommission = utils.Float64ToStr(float64(v.RealCommission) / 100)
  157. }
  158. if utils.AnyToInt64(v.ActivistType) == 1 {
  159. ord.Status = "订单退款"
  160. }
  161. if one == nil {
  162. insertOne, err := db.ZhimengDb.InsertOne(ord)
  163. fmt.Println(insertOne)
  164. fmt.Println(err)
  165. } else {
  166. ord.SettleTime = one.SettleTime
  167. if one.PlatformSettleTime > 0 {
  168. ord.PlatformSettleTime = one.PlatformSettleTime
  169. }
  170. if one.Status == "违规订单" {
  171. ord.Status = "违规订单"
  172. }
  173. if ord.SettleTime > 0 && ord.Status == "订单退款" {
  174. ord.SettleTime = 0
  175. masterDb := db.MasterDb{}
  176. masterDb.Set()
  177. master := masterDb.GetMaster(utils.IntToStr(ord.Uid))
  178. if master != nil {
  179. masterAmountDb := db.MasterAmountDb{}
  180. masterAmountDb.Set()
  181. amount := masterAmountDb.GetMasterAmountByExtendUid(utils.IntToStr(master.Id), utils.IntToStr(ord.ExtendUid), "playlet")
  182. if amount != nil {
  183. oldAmount := amount.Amount
  184. amount.Amount = utils.Float64ToStr(utils.StrToFloat64(amount.Amount) - utils.StrToFloat64(ord.RealCommission))
  185. update := masterAmountDb.MasterAmountUpdate(amount.Id, amount)
  186. if update {
  187. var tmp = model.MasterAmountFlow{
  188. Uid: utils.IntToStr(ord.Uid),
  189. Time: time.Now(),
  190. BeforeAmount: oldAmount,
  191. Amount: ord.RealCommission,
  192. AfterAmount: amount.Amount,
  193. Platform: "playlet",
  194. Oid: utils.Int64ToStr(ord.Oid),
  195. Title: "订单退款扣除",
  196. FlowType: "order_refund_deduct",
  197. IncomeType: 0,
  198. ExtendUid: utils.IntToStr(ord.ExtendUid),
  199. }
  200. masterAmountFlowDb := db.MasterAmountFlowDb{}
  201. masterAmountFlowDb.Set()
  202. masterAmountFlowDb.MasterAmountFlowInsert(&tmp)
  203. }
  204. }
  205. }
  206. }
  207. db.ZhimengDb.Where("id=?", one.Id).AllCols().Update(ord)
  208. }
  209. }
  210. return len(order)
  211. }
  212. func GetChanxuanToken() string {
  213. tiktokChanxuanId := offical.SysCfgByKeyStr("tikTok_chanxuan_id")
  214. tiktokChanxuanSecret := offical.SysCfgByKeyStr("tikTok_chanxuan_secret")
  215. key := "app:chanxuan.token:" + tiktokChanxuanId
  216. getString, err := cache.GetString(key)
  217. if err != nil || getString == "" {
  218. data := chanxuanTikTok.GetToken(tiktokChanxuanId, tiktokChanxuanSecret)
  219. ex := gjson.Get(data, "expires_in_v2").Int() - time.Now().Unix() - 60
  220. token := gjson.Get(data, "access_token").String()
  221. if token != "" {
  222. cache.SetEx(key, token, int(ex))
  223. getString = token
  224. }
  225. }
  226. return getString
  227. }