智盟项目
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

222 righe
6.1 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. offical "applet/app/db/official"
  6. md2 "applet/app/md"
  7. "applet/app/utils"
  8. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/meituan_cps"
  9. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/tidwall/gjson"
  13. "strings"
  14. "time"
  15. )
  16. func MeituanOrder() {
  17. pvdTimeKey := "meituan_time"
  18. // 获得最后时间
  19. latest := offical.SysCfgByKey(pvdTimeKey)
  20. if latest == nil {
  21. offical.DbsSysCfgInserts(pvdTimeKey, time.Now().String())
  22. latest = offical.SysCfgByKey(pvdTimeKey)
  23. }
  24. // 所有时间都是在操作秒数
  25. now := time.Now().Unix()
  26. strs := strings.Split(latest.V, ":")
  27. timeStr := latest.V
  28. if len(strs) == 3 {
  29. timeStr = strs[0] + ":" + strs[1] + ":00"
  30. }
  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. var (
  38. beginTime int64 = 0
  39. endTime int64 = 0
  40. pageNo int = 1
  41. pageSize int = 50
  42. nextPositionIndex string = ""
  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. for {
  56. count := 0
  57. var positionIndex = ""
  58. if pageNo == 1 {
  59. nextPositionIndex = ""
  60. }
  61. count, positionIndex = OrdersMeituanGet(nextPositionIndex, pageSize, beginTime, endTime, "update", 1)
  62. if count == 0 {
  63. nextPositionIndex = ""
  64. goto ChkArg
  65. }
  66. // 判断是否分页已经全部取完了
  67. if count <= pageSize {
  68. nextPositionIndex = positionIndex
  69. pageNo++
  70. fmt.Println("========下一页========" + utils.IntToStr(pageNo))
  71. count = 0
  72. continue
  73. }
  74. ChkArg:
  75. nextPositionIndex = ""
  76. // 查询完后重置时间, 最后查询时间
  77. if endTime < now {
  78. pageNo = 1
  79. offical.DbsSysCfgUpdate(pvdTimeKey, utils.TimeToStr(endTime))
  80. beginTime = endTime
  81. endTime = endTime + 300
  82. if endTime > now {
  83. endTime = now
  84. }
  85. count = 0
  86. continue
  87. }
  88. count = 0
  89. break
  90. }
  91. }
  92. func OrdersMeituanGet(nextPositionIndex string, pageSize int, sTime, eTime int64, timeType string, pvd int) (int, string) {
  93. key := offical.SysCfgByKeyStr("meituan_cps_key")
  94. secret := offical.SysCfgByKeyStr("meituan_cps_secret")
  95. arg := map[string]interface{}{
  96. "queryTimeType": 2, // 1 按订单支付时间查询, 2 按照更新时间查询, 默认为1
  97. "page": 1, //页码,默认1,从1开始,若searchType选择2,本字段必须传1,若不传参数默认1
  98. "searchType": 2, //订单分页查询方案选择,不填则默认为1。1 分页查询(最多能查询到1万条订单),当选择本查询方案,page参数不能为空。此查询方式后续不再维护,建议使用2逐页查询。2 逐页查询(不限制查询订单数,只能逐页查询,不能指定页数),当选择本查询方案,需配合scrollId参数使用
  99. "startTime": sTime, //
  100. "endTime": eTime, //
  101. "limit": pageSize,
  102. "platform": pvd,
  103. "scrollId": nextPositionIndex,
  104. }
  105. method1 := "query_order"
  106. send, err := meituan_cps.Send(method1, key, secret, arg)
  107. goods := gjson.Get(send, "data.dataList").String()
  108. newPcursor := gjson.Get(send, "data.scrollId").String()
  109. if goods == "" || err != nil {
  110. return 0, ""
  111. }
  112. var list = make([]md2.MeituanOrder, 0)
  113. err = json.Unmarshal([]byte(goods), &list)
  114. if err != nil {
  115. return 0, ""
  116. }
  117. var meituanState = map[string]string{
  118. "2": "订单付款",
  119. "3": "订单完成",
  120. "4": "订单失效",
  121. "5": "风控",
  122. "6": "订单结算",
  123. }
  124. var meituanState1 = map[string]string{
  125. "1": "订单付款",
  126. "2": "订单完成",
  127. "4": "订单失效",
  128. "5": "风控",
  129. "3": "订单结算",
  130. }
  131. for _, v := range list {
  132. var res = model.LifeOrder{
  133. PvdParentOid: v.OrderId,
  134. Pvd: "meituan",
  135. Status: meituanState[v.Status],
  136. CreateTime: v.PayTime,
  137. }
  138. if v.PayTime == 0 {
  139. res.CreateTime = int(time.Now().Unix())
  140. }
  141. if v.UpdateTime > 0 && v.Status == "6" {
  142. res.PlatformSettleTime = v.UpdateTime
  143. }
  144. ex := strings.Split(v.Sid, "_")
  145. if len(ex) < 3 {
  146. continue
  147. }
  148. orderType := 0
  149. if ex[0] == "share" {
  150. orderType = 1
  151. }
  152. res.Uid = utils.StrToInt(ex[1])
  153. res.StationUid = utils.StrToInt(ex[2])
  154. orderDetail, ok := v.OrderDetail.([]interface{})
  155. Profit := v.Profit
  156. state := "1"
  157. if ok {
  158. if len(orderDetail) > 0 {
  159. allEnd := "1"
  160. Profit = "0"
  161. for k1, v1 := range orderDetail {
  162. v2, ok1 := v1.(map[string]interface{})
  163. if ok1 {
  164. if k1 == 0 {
  165. state = utils.AnyToString(v2["couponStatus"])
  166. }
  167. if utils.StrToInt(utils.AnyToString(v2["couponStatus"])) < utils.StrToInt(state) {
  168. state = utils.AnyToString(v2["couponStatus"])
  169. }
  170. if utils.InArr(utils.AnyToString(v2["couponStatus"]), []string{"1", "2", "3"}) {
  171. allEnd = "0"
  172. }
  173. Profit = zhios_third_party_utils.Float64ToStr(utils.StrToFloat64(Profit) + utils.AnyToFloat64(v2["couponFee"]))
  174. }
  175. }
  176. if allEnd == "0" {
  177. res.Status = meituanState1[state]
  178. res.PlatformSettleTime = 0
  179. }
  180. }
  181. }
  182. res.Oid = utils.StrToInt64(utils.OrderUUID(utils.StrToInt(ex[1])))
  183. res.PvdOid = v.OrderId
  184. res.Uid = utils.StrToInt(ex[1])
  185. res.StationUid = utils.StrToInt(ex[2])
  186. res.UpdateTime = int(time.Now().Unix())
  187. res.Commission = Profit
  188. res.RealCommission = Profit
  189. res.Title = v.ProductName
  190. if v.ProductId == "null" {
  191. v.ProductId = v.OrderId
  192. }
  193. res.Gid = v.ProductId
  194. res.IsShare = orderType
  195. res.Payment = v.PayPrice
  196. one := db.GetLifeOrderByOne(res.PvdOid, utils.IntToStr(res.Uid), res.Pvd)
  197. if one == nil {
  198. insertOne, err := db.ZhimengDb.InsertOne(&res)
  199. fmt.Println(insertOne)
  200. fmt.Println(err)
  201. } else {
  202. res.SettleTime = one.SettleTime
  203. res.CreateTime = one.CreateTime
  204. if one.PlatformSettleTime > 0 {
  205. res.PlatformSettleTime = one.PlatformSettleTime
  206. }
  207. db.ZhimengDb.Where("id=?", one.Id).AllCols().Update(&res)
  208. }
  209. }
  210. return len(list), newPcursor
  211. }