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

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