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

238 lines
6.5 KiB

  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. taskType := ""
  166. if utils.InArr(ex[0], []string{"happy_orchard"}) {
  167. orderType = 1
  168. taskType = ex[0]
  169. }
  170. resNew.TaskType = taskType
  171. resNew.Oid = utils.StrToInt64(utils.OrderUUID(utils.StrToInt(ex[1])))
  172. resNew.PvdOid = utils.IntToStr(v1.Oid)
  173. resNew.Uid = utils.StrToInt(ex[2])
  174. resNew.StationUid = utils.StrToInt(ex[1])
  175. resNew.UpdateTime = int(time.Now().Unix())
  176. resNew.Commission = utils.Float64ToStr(float64(v1.EstimatedIncome) / 100)
  177. resNew.RealCommission = utils.Float64ToStr(float64(v1.EstimatedIncome) / 100)
  178. resNew.Title = v1.ItemTitle
  179. resNew.Img = v1.ItemPicUrl
  180. resNew.Gid = utils.IntToStr(v1.ItemId)
  181. resNew.IsShare = orderType
  182. resNew.Payment = utils.Float64ToStr(float64(v1.PaymentFee) / 100)
  183. one := db.GetGuideOrderByOne(resNew.PvdOid, utils.IntToStr(resNew.Uid), resNew.Pvd)
  184. if one == nil {
  185. insertOne, err := db.ZhimengDb.InsertOne(&resNew)
  186. fmt.Println(insertOne)
  187. fmt.Println(err)
  188. } else {
  189. resNew.SettleTime = one.SettleTime
  190. if one.PlatformSettleTime > 0 {
  191. resNew.PlatformSettleTime = one.PlatformSettleTime
  192. }
  193. db.ZhimengDb.Where("id=?", one.Id).AllCols().Update(&resNew)
  194. }
  195. }
  196. }
  197. return len(list), newPcursor
  198. }
  199. func GetKuaishouKey() (map[string]string, map[string]string) {
  200. kuaishouData := CommKuaishou()
  201. param := map[string]string{
  202. "appkey": kuaishouData["appkey"],
  203. "access_token": kuaishouData["token"],
  204. }
  205. return param, kuaishouData
  206. }
  207. func CommKuaishou() map[string]string {
  208. res := OfficialCommKuaishou()
  209. return res
  210. }
  211. func OfficialCommKuaishou() map[string]string {
  212. kuaishouAppkey := offical.MasterListCfgGetOneData("0", "kuaishou_kfx_appkey")
  213. kuaishouAppSecret := offical.MasterListCfgGetOneData("0", "kuaishou_kfx_app_secret")
  214. kuaishouSignSecret := offical.MasterListCfgGetOneData("0", "kuaishou_kfx_sign_secret")
  215. authType := "0"
  216. kuaishouToken := db.MasterListCfgGetOneData("0", "kuaishou_official_access_token")
  217. kuaishouPid := offical.MasterListCfgGetOneData("0", "kuaishou_official_pid")
  218. res := map[string]string{
  219. "pid": kuaishouPid,
  220. "authType": authType,
  221. "appkey": kuaishouAppkey,
  222. "token": kuaishouToken,
  223. "app_secret": kuaishouAppSecret,
  224. "sign_secret": kuaishouSignSecret,
  225. }
  226. return res
  227. }