package svc import ( "applet/app/db" "applet/app/db/model" offical "applet/app/db/official" "applet/app/task/md" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/playlet_bihai" "encoding/json" "github.com/tidwall/gjson" "strings" "time" ) func PlayletBihaiOrder() { bihaiAppId := offical.SysCfgByKeyStr("bihai_app_id") key := "bihai_token_" + bihaiAppId bihaiAppSecret := offical.SysCfgByKeyStr("bihai_app_secret") getString, err := cache.GetString(key) if getString == "" || err != nil { args := map[string]string{ "app_id": bihaiAppId, "app_secret": bihaiAppSecret, } login, _ := playlet_bihai.Login(args) token := gjson.Get(login, "data.access_token").String() expiresIn := gjson.Get(login, "data.expires_in").Int() if token != "" { cache.SetEx(key, token, int(expiresIn)) } getString = token } if getString == "" { return } uid := "0" // 获取上次获取订单时候的时间戳 pvdTimeKey := "bihai_order_time" now := time.Now().Unix() past := GetRunTime(uid, pvdTimeKey, "短剧获取订单时间") var ( beginTime int64 = 0 endTime int64 = 0 pageNo int = 1 pageSize int = 100 ) //怕时间不是走最新的 leave := now - past if leave > 500 { leave = 0 } var eveTime int64 = 3600 past = past + leave beginTime = past - eveTime endTime = past if endTime > now { endTime = now } nextId := "0" for { // 分配堆内存 time.Sleep(time.Microsecond * 500) // 等待500毫秒 //获取订单 arg := map[string]string{ "start_at": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"), "end_at": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"), "page_size": utils.IntToStr(pageSize), "next_id": nextId, } link, _ := playlet_bihai.OrderList(getString, arg) var order md.BihaiOrder err := json.Unmarshal([]byte(gjson.Get(link, "data").String()), &order) if err != nil { break } statusMap := map[string]string{ "1": "订单付款", "2": "订单失败", } for _, v := range order.List { if utils.StrToInt(v.Status) < 1 { continue } sourceIdArr := strings.Split(v.Ext.QwExt, "_") mid := "" uid1 := "" if len(sourceIdArr) == 2 { mid = sourceIdArr[0] uid1 = sourceIdArr[1] } if mid == "" || uid1 == "" { continue } Channel := "douyin" if v.MarketId == "2" { Channel = "kuaishou" } money := utils.Float64ToStr(utils.StrToFloat64(v.Price) / 100) commission := utils.StrToFloat64(money) * (80.0 / 100) var tmp = model.PlayletSaleOrder{ Uid: mid, SubUid: utils.StrToInt(uid1), Data: utils.SerializeStr(v), Oid: v.OrderId, Amount: money, Commission: utils.Float64ToStr(commission), CreateTime: utils.TimeParseStd(v.CreatedAt), UpdateTime: time.Now(), Title: "碧海剧场", VideoType: Channel, PlatformType: "bihai", GoodsType: "playlet", OrdType: "video", ExtendUid: uid, } playletSaleOrderDb := db.PlayletSaleOrderDb{} playletSaleOrderDb.Set() ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(v.OrderId, tmp.OrdType) tmp.Status = statusMap[v.Status] if ord != nil { playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp) } else { tmp.CustomOid = utils.OrderUUID(tmp.SubUid) playletSaleOrderDb.PlayletVideoOrderInsert(&tmp) } } if order.HasMore == false { nextId = "0" goto ChkArg } if len(order.List) == pageSize { nextId = utils.IntToStr(order.NextId) pageNo++ continue } ChkArg: // 查询完后重置时间, 最后查询时间 if endTime < now { nextId = "0" pageNo = 1 SetRunTime(uid, pvdTimeKey, utils.TimeToStr(endTime)) beginTime = endTime endTime = endTime + eveTime if endTime > now { endTime = now } continue } break } // 更新最后供应商执行订单时间 SetRunTime(uid, pvdTimeKey, utils.TimeToStr(now)) }