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