|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package svc
-
- import (
- "applet/app/db"
- "applet/app/db/model"
- offical "applet/app/db/official"
- md2 "applet/app/md"
- "applet/app/utils"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/meituan_cps"
- zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
- "encoding/json"
- "fmt"
- "github.com/tidwall/gjson"
- "strings"
- "time"
- )
-
- func MeituanOrder() {
- pvdTimeKey := "meituan_time"
-
- // 获得最后时间
- latest := offical.SysCfgByKey(pvdTimeKey)
- if latest == nil {
- offical.DbsSysCfgInserts(pvdTimeKey, time.Now().String())
- latest = offical.SysCfgByKey(pvdTimeKey)
- }
- // 所有时间都是在操作秒数
- now := time.Now().Unix()
- strs := strings.Split(latest.V, ":")
- timeStr := latest.V
- if len(strs) == 3 {
- timeStr = strs[0] + ":" + strs[1] + ":00"
- }
- fmt.Println(timeStr)
- past := utils.TimeParseStd(timeStr).Unix()
- // 如果上次记录超过30天或者 过去时间大于当前时间戳, 把时间设置为此前20分钟
- if past < now-180*86400 || past > now {
- past = now
- }
- var (
- beginTime int64 = 0
- endTime int64 = 0
- pageNo int = 1
- pageSize int = 50
- nextPositionIndex string = ""
- )
-
- //怕时间不是走最新的
- leave := now - past
- if leave > 500 {
- leave = 0
- }
- past = past + leave
- beginTime = past - 300
- endTime = past
-
- if endTime > now {
- endTime = now
- }
-
- for {
- count := 0
- var positionIndex = ""
- if pageNo == 1 {
- nextPositionIndex = ""
- }
- count, positionIndex = OrdersMeituanGet(nextPositionIndex, pageSize, beginTime, endTime, "update", 1)
- if count == 0 {
- nextPositionIndex = ""
- goto ChkArg
- }
- // 判断是否分页已经全部取完了
- if count <= pageSize {
- nextPositionIndex = positionIndex
- pageNo++
- fmt.Println("========下一页========" + utils.IntToStr(pageNo))
- count = 0
- continue
- }
- ChkArg:
- nextPositionIndex = ""
-
- // 查询完后重置时间, 最后查询时间
- if endTime < now {
- pageNo = 1
- offical.DbsSysCfgUpdate(pvdTimeKey, utils.TimeToStr(endTime))
- beginTime = endTime
- endTime = endTime + 300
- if endTime > now {
- endTime = now
- }
- count = 0
- continue
- }
- count = 0
- break
- }
- }
-
- func OrdersMeituanGet(nextPositionIndex string, pageSize int, sTime, eTime int64, timeType string, pvd int) (int, string) {
- key := offical.SysCfgByKeyStr("meituan_cps_key")
- secret := offical.SysCfgByKeyStr("meituan_cps_secret")
- arg := map[string]interface{}{
- "queryTimeType": 2, // 1 按订单支付时间查询, 2 按照更新时间查询, 默认为1
- "page": 1, //页码,默认1,从1开始,若searchType选择2,本字段必须传1,若不传参数默认1
- "searchType": 2, //订单分页查询方案选择,不填则默认为1。1 分页查询(最多能查询到1万条订单),当选择本查询方案,page参数不能为空。此查询方式后续不再维护,建议使用2逐页查询。2 逐页查询(不限制查询订单数,只能逐页查询,不能指定页数),当选择本查询方案,需配合scrollId参数使用
- "startTime": sTime, //
- "endTime": eTime, //
- "limit": pageSize,
- "platform": pvd,
- "scrollId": nextPositionIndex,
- }
- method1 := "query_order"
- send, err := meituan_cps.Send(method1, key, secret, arg)
- goods := gjson.Get(send, "data.dataList").String()
- newPcursor := gjson.Get(send, "data.scrollId").String()
- if goods == "" || err != nil {
- return 0, ""
- }
- var list = make([]md2.MeituanOrder, 0)
- err = json.Unmarshal([]byte(goods), &list)
- if err != nil {
- return 0, ""
- }
- var meituanState = map[string]string{
- "2": "订单付款",
- "3": "订单完成",
- "4": "订单失效",
- "5": "风控",
- "6": "订单结算",
- }
- var meituanState1 = map[string]string{
- "1": "订单付款",
- "2": "订单完成",
- "4": "订单失效",
- "5": "风控",
- "3": "订单结算",
- }
- for _, v := range list {
- var res = model.LifeOrder{
- PvdParentOid: v.OrderId,
- Pvd: "meituan",
- Status: meituanState[v.Status],
- CreateTime: v.PayTime,
- }
- if v.PayTime == 0 {
- res.CreateTime = int(time.Now().Unix())
- }
- if v.UpdateTime > 0 && v.Status == "6" {
- res.PlatformSettleTime = v.UpdateTime
- }
- ex := strings.Split(v.Sid, "_")
- if len(ex) < 3 {
- continue
- }
- orderType := 0
- if ex[0] == "share" {
- orderType = 1
- }
- res.Uid = utils.StrToInt(ex[1])
- res.StationUid = utils.StrToInt(ex[2])
- orderDetail, ok := v.OrderDetail.([]interface{})
- Profit := v.Profit
- state := "1"
- if ok {
- if len(orderDetail) > 0 {
- allEnd := "1"
- Profit = "0"
- for k1, v1 := range orderDetail {
- v2, ok1 := v1.(map[string]interface{})
- if ok1 {
- if k1 == 0 {
- state = utils.AnyToString(v2["couponStatus"])
- }
- if utils.StrToInt(utils.AnyToString(v2["couponStatus"])) < utils.StrToInt(state) {
- state = utils.AnyToString(v2["couponStatus"])
- }
- if utils.InArr(utils.AnyToString(v2["couponStatus"]), []string{"1", "2", "3"}) {
- allEnd = "0"
- }
- Profit = zhios_third_party_utils.Float64ToStr(utils.StrToFloat64(Profit) + utils.AnyToFloat64(v2["couponFee"]))
- }
- }
- if allEnd == "0" {
- res.Status = meituanState1[state]
-
- res.PlatformSettleTime = 0
- }
- }
- }
- res.Oid = utils.StrToInt64(utils.OrderUUID(utils.StrToInt(ex[1])))
- res.PvdOid = v.OrderId
- res.Uid = utils.StrToInt(ex[1])
- res.StationUid = utils.StrToInt(ex[2])
- res.UpdateTime = int(time.Now().Unix())
- res.Commission = Profit
- res.RealCommission = Profit
- res.Title = v.ProductName
- if v.ProductId == "null" {
- v.ProductId = v.OrderId
- }
- res.Gid = v.ProductId
- res.IsShare = orderType
- res.Payment = v.PayPrice
- one := db.GetLifeOrderByOne(res.PvdOid, utils.IntToStr(res.Uid), res.Pvd)
- if one == nil {
- insertOne, err := db.ZhimengDb.InsertOne(&res)
- fmt.Println(insertOne)
- fmt.Println(err)
- } else {
- res.SettleTime = one.SettleTime
- res.CreateTime = one.CreateTime
- if one.PlatformSettleTime > 0 {
- res.PlatformSettleTime = one.PlatformSettleTime
- }
- db.ZhimengDb.Where("id=?", one.Id).AllCols().Update(&res)
- }
-
- }
- return len(list), newPcursor
- }
|