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

232 lines
6.7 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-180*86400 || 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. vipMsg := utils.Base62Decode(v.VipMsg)
  110. if vipMsg == 0 {
  111. continue
  112. }
  113. vipMsgStr := utils.IntToStr(vipMsg)
  114. orderTypeStr := vipMsgStr[0:1]
  115. orderType := 0
  116. if orderTypeStr == "2" {
  117. orderType = 1
  118. }
  119. mid := uuidMap[utils.StrToInt(vipMsgStr[1:5])]
  120. if mid == "" {
  121. continue
  122. }
  123. uid := vipMsgStr[5:]
  124. var ord = &model.GuideOrder{
  125. Oid: utils.StrToInt64(utils.OrderUUID(utils.StrToInt(uid))),
  126. Uid: utils.StrToInt(mid),
  127. StationUid: utils.StrToInt(uid),
  128. PvdOid: utils.AnyToString(v.OrderId),
  129. Status: stationState[v.FlowPoint],
  130. UpdateTime: int(time.Now().Unix()),
  131. Commission: utils.Float64ToStr(float64(v.EstimatedCommission) / 100),
  132. Title: utils.AnyToString(v.ProductName),
  133. Payment: utils.Float64ToStr(float64(v.TotalPayAmount) / 100),
  134. Pvd: "csjp",
  135. Img: v.ProductImg,
  136. IsShare: orderType,
  137. Gid: v.ProductId,
  138. ThreeType: "chan_xuan",
  139. }
  140. if v.PaySuccessTime != "" {
  141. ord.CreateTime = int(utils.TimeStdParseUnix(v.PaySuccessTime))
  142. }
  143. if v.SettleTime != "" && v.SettleTime != nil {
  144. ord.PlatformSettleTime = int(utils.TimeStdParseUnix(utils.AnyToString(v.SettleTime)))
  145. }
  146. if v.RealCommission > 0 {
  147. ord.RealCommission = utils.Float64ToStr(float64(v.RealCommission) / 100)
  148. }
  149. if utils.AnyToInt64(v.ActivistType) == 1 {
  150. ord.Status = "订单退款"
  151. }
  152. one := db.GetGuideOrderByOne(ord.PvdOid, utils.IntToStr(ord.Uid), ord.Pvd)
  153. if one == nil {
  154. insertOne, err := db.ZhimengDb.InsertOne(ord)
  155. fmt.Println(insertOne)
  156. fmt.Println(err)
  157. } else {
  158. ord.SettleTime = one.SettleTime
  159. if one.PlatformSettleTime > 0 {
  160. ord.PlatformSettleTime = one.PlatformSettleTime
  161. }
  162. if one.Status == "违规订单" {
  163. ord.Status = "违规订单"
  164. }
  165. if ord.SettleTime > 0 && ord.Status == "订单退款" {
  166. ord.SettleTime = 0
  167. masterDb := db.MasterDb{}
  168. masterDb.Set()
  169. master := masterDb.GetMaster(utils.IntToStr(ord.Uid))
  170. if master != nil {
  171. masterAmountDb := db.MasterAmountDb{}
  172. masterAmountDb.Set()
  173. amount := masterAmountDb.GetMasterAmountByExtendUid(utils.IntToStr(master.Id), utils.IntToStr(ord.ExtendUid), "playlet")
  174. if amount != nil {
  175. oldAmount := amount.Amount
  176. amount.Amount = utils.Float64ToStr(utils.StrToFloat64(amount.Amount) - utils.StrToFloat64(ord.RealCommission))
  177. update := masterAmountDb.MasterAmountUpdate(amount.Id, amount)
  178. if update {
  179. var tmp = model.MasterAmountFlow{
  180. Uid: utils.IntToStr(ord.Uid),
  181. Time: time.Now(),
  182. BeforeAmount: oldAmount,
  183. Amount: ord.RealCommission,
  184. AfterAmount: amount.Amount,
  185. Platform: "playlet",
  186. Oid: utils.Int64ToStr(ord.Oid),
  187. Title: "订单退款扣除",
  188. FlowType: "order_refund_deduct",
  189. IncomeType: 0,
  190. ExtendUid: utils.IntToStr(ord.ExtendUid),
  191. }
  192. masterAmountFlowDb := db.MasterAmountFlowDb{}
  193. masterAmountFlowDb.Set()
  194. masterAmountFlowDb.MasterAmountFlowInsert(&tmp)
  195. }
  196. }
  197. }
  198. }
  199. db.ZhimengDb.Where("id=?", one.Id).AllCols().Update(ord)
  200. }
  201. }
  202. return len(order)
  203. }
  204. func GetChanxuanToken() string {
  205. tiktokChanxuanId := offical.SysCfgByKeyStr("tikTok_chanxuan_id")
  206. tiktokChanxuanSecret := offical.SysCfgByKeyStr("tikTok_chanxuan_secret")
  207. key := "app:chanxuan.token:" + tiktokChanxuanId
  208. getString, err := cache.GetString(key)
  209. if err != nil || getString == "" {
  210. data := chanxuanTikTok.GetToken(tiktokChanxuanId, tiktokChanxuanSecret)
  211. ex := gjson.Get(data, "expires_in_v2").Int() - time.Now().Unix() - 60
  212. token := gjson.Get(data, "access_token").String()
  213. if token != "" {
  214. cache.SetEx(key, token, int(ex))
  215. getString = token
  216. }
  217. }
  218. return getString
  219. }