package svc import ( "applet/app/db" "applet/app/db/model" offical "applet/app/db/official" model2 "applet/app/db/official/model" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/chanxuanTikTok" "fmt" "github.com/tidwall/gjson" "time" ) func TikTokChanxuan(ordType int) { pvdTimeKey := "chanxuan_time_" + utils.IntToStr(ordType) // 获得最后时间 latest := offical.SysCfgByKey(pvdTimeKey) if latest == nil { offical.DbsSysCfgInserts(pvdTimeKey, time.Now().String()) latest = offical.SysCfgByKey(pvdTimeKey) } // 所有时间都是在操作秒数 now := time.Now().Unix() timeStr := latest.V fmt.Println(timeStr) past := utils.TimeParseStd(timeStr).Unix() // 如果上次记录超过30天或者 过去时间大于当前时间戳, 把时间设置为此前20分钟 if past < now-180*86400 || past > now { past = now } userId := offical.SysCfgByKeyStr("tikTok_chanxuan_user_id") var ( beginTime int64 = 0 endTime int64 = 0 pageNo int = 1 pageSize int = 50 ) //怕时间不是走最新的 leave := now - past if leave > 500 { leave = 0 } past = past + leave beginTime = past - 20*60 endTime = past if endTime > now { endTime = now } token := GetChanxuanToken() for { var err error count := OrdersTikTokChanxuanGet(pageNo, pageSize, beginTime, endTime, userId, ordType, token) if err != nil || count == 0 { goto ChkArg } if count > 0 { count = pageSize } // 判断是否分页已经全部取完了 if count == pageSize { pageNo++ fmt.Println("========蝉选下一页========" + utils.IntToStr(pageNo)) continue } ChkArg: // 查询完后重置时间, 最后查询时间 if endTime < now { pageNo = 1 offical.DbsSysCfgUpdate(pvdTimeKey, utils.TimeToStr(endTime)) beginTime = endTime endTime = endTime + 20*60 if endTime > now { endTime = now } continue } break } offical.DbsSysCfgUpdate(pvdTimeKey, time.Unix(now, 0).Format("2006-01-02 15:04:05")) } var stationState = map[string]string{ "PAY_SUCC": "订单付款", "REFUND": "订单退款", "SETTLE": "订单结算", "CONFIRM": "订单成功", } func OrdersTikTokChanxuanGet(page, pageSize int, sTime, eTime int64, userId string, pvd int, token string) int { distributionType := pvd tikTokArgs := map[string]string{ "page_size": utils.IntToStr(pageSize), "page": utils.IntToStr(page), "start_time": utils.Int64ToStr(time.Unix(sTime, 0).Unix()), "end_time": utils.Int64ToStr(time.Unix(eTime, 0).Unix()), "query_type": utils.IntToStr(distributionType), "user_id": userId, } order := chanxuanTikTok.GetOrder(token, userId, tikTokArgs) fmt.Println("==================蝉选", utils.SerializeStr(tikTokArgs)+" "+utils.IntToStr(len(order))) uuidList := make([]model2.UuidList, 0) uuidMap := make(map[int]string) db.Db.Find(&uuidList) for _, v := range uuidList { uuidMap[v.Id] = v.Uuid } for _, v := range order { if v.VipMsg == "" { continue } vipMsg := utils.Base62Decode(v.VipMsg) if vipMsg == 0 { continue } vipMsgStr := utils.IntToStr(vipMsg) orderTypeStr := vipMsgStr[0:1] orderType := 0 if orderTypeStr == "2" { orderType = 1 } mid := uuidMap[utils.StrToInt(vipMsgStr[1:5])] if mid == "" { continue } uid := vipMsgStr[5:] var ord = &model.GuideOrder{ Oid: utils.StrToInt64(utils.OrderUUID(utils.StrToInt(uid))), Uid: utils.StrToInt(mid), StationUid: utils.StrToInt(uid), PvdOid: utils.AnyToString(v.OrderId), Status: stationState[v.FlowPoint], UpdateTime: int(time.Now().Unix()), Commission: utils.Float64ToStr(float64(v.EstimatedCommission) / 100), Title: utils.AnyToString(v.ProductName), Payment: utils.Float64ToStr(float64(v.TotalPayAmount) / 100), Pvd: "csjp", Img: v.ProductImg, IsShare: orderType, Gid: v.ProductId, ThreeType: "chan_xuan", } if v.PaySuccessTime != "" { ord.CreateTime = int(utils.TimeStdParseUnix(v.PaySuccessTime)) } if v.SettleTime != "" && v.SettleTime != nil { ord.PlatformSettleTime = int(utils.TimeStdParseUnix(utils.AnyToString(v.SettleTime))) } if v.RealCommission > 0 { ord.RealCommission = utils.Float64ToStr(float64(v.RealCommission) / 100) } if utils.AnyToInt64(v.ActivistType) == 1 { ord.Status = "订单退款" } one := db.GetGuideOrderByOne(ord.PvdOid, utils.IntToStr(ord.Uid), ord.Pvd) if one == nil { insertOne, err := db.ZhimengDb.InsertOne(ord) fmt.Println(insertOne) fmt.Println(err) } else { ord.SettleTime = one.SettleTime if one.PlatformSettleTime > 0 { ord.PlatformSettleTime = one.PlatformSettleTime } if one.Status == "违规订单" { ord.Status = "违规订单" } if ord.SettleTime > 0 && ord.Status == "订单退款" { ord.SettleTime = 0 masterDb := db.MasterDb{} masterDb.Set() master := masterDb.GetMaster(utils.IntToStr(ord.Uid)) if master != nil { masterAmountDb := db.MasterAmountDb{} masterAmountDb.Set() amount := masterAmountDb.GetMasterAmountByExtendUid(utils.IntToStr(master.Id), utils.IntToStr(ord.ExtendUid), "playlet") if amount != nil { oldAmount := amount.Amount amount.Amount = utils.Float64ToStr(utils.StrToFloat64(amount.Amount) - utils.StrToFloat64(ord.RealCommission)) update := masterAmountDb.MasterAmountUpdate(amount.Id, amount) if update { var tmp = model.MasterAmountFlow{ Uid: utils.IntToStr(ord.Uid), Time: time.Now(), BeforeAmount: oldAmount, Amount: ord.RealCommission, AfterAmount: amount.Amount, Platform: "playlet", Oid: utils.Int64ToStr(ord.Oid), Title: "订单退款扣除", FlowType: "order_refund_deduct", IncomeType: 0, ExtendUid: utils.IntToStr(ord.ExtendUid), } masterAmountFlowDb := db.MasterAmountFlowDb{} masterAmountFlowDb.Set() masterAmountFlowDb.MasterAmountFlowInsert(&tmp) } } } } db.ZhimengDb.Where("id=?", one.Id).AllCols().Update(ord) } } return len(order) } func GetChanxuanToken() string { tiktokChanxuanId := offical.SysCfgByKeyStr("tikTok_chanxuan_id") tiktokChanxuanSecret := offical.SysCfgByKeyStr("tikTok_chanxuan_secret") key := "app:chanxuan.token:" + tiktokChanxuanId getString, err := cache.GetString(key) if err != nil || getString == "" { data := chanxuanTikTok.GetToken(tiktokChanxuanId, tiktokChanxuanSecret) ex := gjson.Get(data, "expires_in_v2").Int() - time.Now().Unix() - 60 token := gjson.Get(data, "access_token").String() if token != "" { cache.SetEx(key, token, int(ex)) getString = token } } return getString }