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

svc_kuaishou_order.go 6.3 KiB

7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. offical "applet/app/db/official"
  6. "applet/app/utils"
  7. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/kuaishou"
  8. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/tidwall/gjson"
  12. "strings"
  13. "time"
  14. )
  15. func KuaishouOrder() {
  16. pvdTimeKey := "kuaishou_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 = "0"
  59. }
  60. if nextPositionIndex != "" {
  61. count, positionIndex = OrdersKuaishouGet(nextPositionIndex, pageSize, beginTime, endTime, "update", 0)
  62. }
  63. if count == 0 {
  64. nextPositionIndex = ""
  65. goto ChkArg
  66. }
  67. // 判断是否分页已经全部取完了
  68. if count <= pageSize {
  69. nextPositionIndex = positionIndex
  70. pageNo++
  71. fmt.Println("========下一页========" + utils.IntToStr(pageNo))
  72. count = 0
  73. continue
  74. }
  75. ChkArg:
  76. nextPositionIndex = ""
  77. // 查询完后重置时间, 最后查询时间
  78. if endTime < now {
  79. pageNo = 1
  80. offical.DbsSysCfgUpdate(pvdTimeKey, utils.TimeToStr(endTime))
  81. beginTime = endTime
  82. endTime = endTime + 300
  83. if endTime > now {
  84. endTime = now
  85. }
  86. count = 0
  87. continue
  88. }
  89. count = 0
  90. break
  91. }
  92. }
  93. func OrdersKuaishouGet(nextPositionIndex string, pageSize int, sTime, eTime int64, timeType string, pvd int) (int, string) {
  94. arg := map[string]interface{}{
  95. "cpsOrderStatus": 0,
  96. "pageSize": pageSize,
  97. "sortType": 2, //排序类型 [1:按指定查询类型降序] [2:按指定查询类型升序]
  98. "queryType": 2, //查询类型 [1:按分销订单创建时间查询] [2:按分销订单更新时间查询]
  99. "beginTime": time.Unix(sTime, 0).Unix() * 1000,
  100. "endTime": time.Unix(eTime, 0).Unix() * 1000,
  101. "pcursor": nextPositionIndex,
  102. }
  103. param, kuaishouData := GetKuaishouKey()
  104. param["param"] = utils.SerializeStr(arg)
  105. data, err := kuaishou.GetKuishouCpsOrder(kuaishouData["sign_secret"], param)
  106. goods := gjson.Get(data, "data.orderList").String()
  107. newPcursor := gjson.Get(data, "data.pcursor").String()
  108. if goods == "" || err != nil {
  109. return 0, ""
  110. }
  111. var list = make([]md.KuaishouCpsOrder, 0)
  112. err = json.Unmarshal([]byte(goods), &list)
  113. if err != nil {
  114. return 0, ""
  115. }
  116. var kuaishouState = map[int]string{
  117. 30: "订单付款",
  118. 50: "订单完成",
  119. 80: "订单失效",
  120. 60: "订单结算",
  121. }
  122. for _, v := range list {
  123. var res = model.GuideOrder{
  124. PvdParentOid: utils.IntToStr(v.Oid),
  125. Pvd: "kuaishou",
  126. Status: kuaishouState[v.CpsOrderStatus],
  127. CreateTime: v.OrderCreateTime / 1000,
  128. }
  129. if v.SettlementSuccessTime > 0 {
  130. res.PlatformSettleTime = int(v.SettlementSuccessTime / 1000)
  131. }
  132. for _, v1 := range v.CpsKwaimoneyOrderProductView {
  133. resNew := res
  134. if v1.CpsType == 3 {
  135. resNew.Pvd = "kuaishou_live"
  136. }
  137. if strings.Contains(v1.Comments, "official_") == false {
  138. continue
  139. }
  140. v1.Comments = strings.ReplaceAll(v1.Comments, "official_", "")
  141. ex := strings.Split(v1.Comments, "_")
  142. if len(ex) < 3 {
  143. continue
  144. }
  145. orderType := 0
  146. if ex[0] == "s" {
  147. orderType = 1
  148. }
  149. // 判断免单
  150. if ex[0] == "f" {
  151. orderType = 2
  152. }
  153. if ex[0] == "cloud" {
  154. orderType = 10
  155. }
  156. if ex[0] == "sef" {
  157. orderType = 11
  158. }
  159. if ex[0] == "shareSeFree" {
  160. orderType = 12
  161. }
  162. if ex[0] == "moref" {
  163. orderType = 13
  164. }
  165. resNew.Oid = utils.StrToInt64(utils.OrderUUID(utils.StrToInt(ex[1])))
  166. resNew.PvdOid = utils.IntToStr(v1.Oid)
  167. resNew.Uid = utils.StrToInt(ex[2])
  168. resNew.StationUid = utils.StrToInt(ex[1])
  169. resNew.UpdateTime = int(time.Now().Unix())
  170. resNew.Commission = utils.Float64ToStr(float64(v1.EstimatedIncome) / 100)
  171. resNew.RealCommission = utils.Float64ToStr(float64(v1.EstimatedIncome) / 100)
  172. resNew.Title = v1.ItemTitle
  173. resNew.Img = v1.ItemPicUrl
  174. resNew.Gid = utils.IntToStr(v1.ItemId)
  175. resNew.IsShare = orderType
  176. resNew.Payment = utils.Float64ToStr(float64(v1.PaymentFee) / 100)
  177. one := db.GetGuideOrderByOne(resNew.PvdOid, utils.IntToStr(resNew.Uid), resNew.Pvd)
  178. if one == nil {
  179. insertOne, err := db.ZhimengDb.InsertOne(&resNew)
  180. fmt.Println(insertOne)
  181. fmt.Println(err)
  182. } else {
  183. resNew.SettleTime = one.SettleTime
  184. if one.PlatformSettleTime > 0 {
  185. resNew.PlatformSettleTime = one.PlatformSettleTime
  186. }
  187. db.ZhimengDb.Where("id=?", one.Id).AllCols().Update(&resNew)
  188. }
  189. }
  190. }
  191. return len(list), newPcursor
  192. }
  193. func GetKuaishouKey() (map[string]string, map[string]string) {
  194. kuaishouData := CommKuaishou()
  195. param := map[string]string{
  196. "appkey": kuaishouData["appkey"],
  197. "access_token": kuaishouData["token"],
  198. }
  199. return param, kuaishouData
  200. }
  201. func CommKuaishou() map[string]string {
  202. res := OfficialCommKuaishou()
  203. return res
  204. }
  205. func OfficialCommKuaishou() map[string]string {
  206. kuaishouAppkey := offical.MasterListCfgGetOneData("0", "kuaishou_kfx_appkey")
  207. kuaishouAppSecret := offical.MasterListCfgGetOneData("0", "kuaishou_kfx_app_secret")
  208. kuaishouSignSecret := offical.MasterListCfgGetOneData("0", "kuaishou_kfx_sign_secret")
  209. authType := "0"
  210. kuaishouToken := db.MasterListCfgGetOneData("0", "kuaishou_official_access_token")
  211. kuaishouPid := offical.MasterListCfgGetOneData("0", "kuaishou_official_pid")
  212. res := map[string]string{
  213. "pid": kuaishouPid,
  214. "authType": authType,
  215. "appkey": kuaishouAppkey,
  216. "token": kuaishouToken,
  217. "app_secret": kuaishouAppSecret,
  218. "sign_secret": kuaishouSignSecret,
  219. }
  220. return res
  221. }