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

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