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

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