package svc import ( "applet/app/db" "applet/app/db/model" "applet/app/task/md" "applet/app/utils" "applet/app/utils/logx" "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/tpdaren" "encoding/json" "fmt" "github.com/tidwall/gjson" "strings" "time" ) func PlayletVideoOrder() { defer func() { if err := recover(); err != nil { _ = logx.Error(err) } }() uid := "0" // 获取上次获取订单时候的时间戳 pvdTimeKey := "playlet_video_order_time" now := time.Now().Unix() past := GetRunTime(uid, pvdTimeKey, "短剧获取订单时间") var ( beginTime int64 = 0 endTime int64 = 0 pageNo int = 1 pageSize int = 200 ) //怕时间不是走最新的 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 } for { // 分配堆内存 time.Sleep(time.Microsecond * 500) // 等待500毫秒 //获取订单 arg := map[string]interface{}{ "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"), "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"), "page_size": pageSize, "page_index": pageNo, } count := GetVideoOrder(uid, arg) if count == 0 { goto ChkArg } if count == pageSize { pageNo++ continue } ChkArg: // 查询完后重置时间, 最后查询时间 if endTime < now { 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)) } func PlayletVideoOrderYesterDay() { defer func() { if err := recover(); err != nil { _ = logx.Error(err) } }() uid := "0" timeRange := utils.GetTimeRange("today") hour := time.Now().Hour() if hour < 1 { return } syscfgDb := db.MasterListCfgDb{} syscfgDb.Set() playletVideoOrderYesterdayRuntime := syscfgDb.MasterListCfgGetOneData(uid, "playlet_video_order_yesterday_runtime") if utils.TimeStdParseUnix(playletVideoOrderYesterdayRuntime) > timeRange["start"] { return } var ( beginTime int64 = timeRange["start"] - 86400 endTime int64 = timeRange["start"] pageNo int = 1 pageSize int = 200 ) for { // 分配堆内存 time.Sleep(time.Microsecond * 500) // 等待500毫秒 //获取订单 arg := map[string]interface{}{ "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"), "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"), "page_size": pageSize, "page_index": pageNo, "status": 2, } count := GetVideoOrder(uid, arg) if count == 0 { break } if count == pageSize { pageNo++ continue } } syscfgDb.MasterListCfgUpdate(uid, "playlet_video_order_yesterday_runtime", time.Now().Format("2006-01-02 15:04:05")) return } func PlayletVideoOrderMonth() { defer func() { if err := recover(); err != nil { _ = logx.Error(err) } }() uid := "0" timeRange := utils.GetTimeRange("last_month") t := time.Now() stime := time.Date(t.Year(), t.Month(), 5, 0, 0, 0, 0, t.Location()).Unix() day := time.Now().Day() if day < 5 { return } hour := time.Now().Hour() if hour < 3 { return } syscfgDb := db.MasterListCfgDb{} syscfgDb.Set() //上次开始的时间 keyStart := "playlet_video_order_month_starttime" starttime := syscfgDb.MasterListCfgGetOneData(uid, keyStart) //运行到哪一天 key := "playlet_video_order_month_runtime" runtime := syscfgDb.MasterListCfgGetOneData(uid, key) keyIsEnd := "playlet_video_order_month_is_end" if utils.TimeStdParseUnix(starttime) < stime { syscfgDb.MasterListCfgUpdate(uid, key, time.Now().Format("2006-01-02 15:04:05")) syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "") runtime = time.Unix(timeRange["start"], 0).Format("2006-01-02 15:04:05") } syscfgDb.MasterListCfgUpdate(uid, keyStart, time.Now().Format("2006-01-02 15:04:05")) //当前是否结束了 isEnd := syscfgDb.MasterListCfgGetOneData(uid, keyIsEnd) if isEnd == "1" { return } var ( beginTime int64 = utils.TimeStdParseUnix(runtime) endTime int64 = utils.TimeStdParseUnix(runtime) + 86400 pageNo int = 1 pageSize int = 200 ) for { // 分配堆内存 time.Sleep(time.Microsecond * 500) // 等待500毫秒 //获取订单 arg := map[string]interface{}{ "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"), "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"), "page_size": pageSize, "page_index": pageNo, } count := GetVideoOrder(uid, arg) if count == 0 { break } if count == pageSize { pageNo++ continue } } if endTime > time.Now().Unix() { syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "1") } syscfgDb.MasterListCfgUpdate(uid, key, time.Unix(endTime, 0).Format("2006-01-02 15:04:05")) return } /*** 注意 在选择status为2退款时,start,end的筛选字段将是退款时间,时间范围最大为30天 建议 所有订单:实时订单,延迟最多1分钟内。退款订单:建议每日凌晨1点后拉取一次昨日退款订单。 */ func GetVideoOrder(uids string, arg map[string]interface{}) int { list := make([]map[string]string, 0) token := GetTpdarenToken(uids) paging, err := tpdaren.OrderFindPaging(token, arg) fmt.Println("playletVideoOrder", paging) fmt.Println("playletVideoOrder", err) if paging == "" { return len(list) } data := gjson.Get(paging, "data.data").String() if data == "" { return len(list) } dataList := make([]md.PlayletVideoOrder, 0) json.Unmarshal([]byte(data), &dataList) sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() playletKuaishouBili := sysCfgDb.SysCfgGetOneData("playlet_kuaishou_bili") playletDouyinBili := sysCfgDb.SysCfgGetOneData("playlet_douyin_bili") playletChannelBili := sysCfgDb.SysCfgGetOneData("playlet_channel_bili") var biliMap = map[string]string{ "kuaishou": playletKuaishouBili, "douyin": playletDouyinBili, "channel": playletChannelBili, } masterListCfgDb := db.MasterListCfgDb{} masterListCfgDb.Set() zyPlayletKuaishouBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_kuaishou_bili") zyPlayletDouyinBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_douyin_bili") zyPlayletChannelBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_channel_bili") var zyBiliMap = map[string]string{ "kuaishou": zyPlayletKuaishouBili, "douyin": zyPlayletDouyinBili, "channel": zyPlayletChannelBili, } var statusMap = map[int]string{ 2: "订单退款", 4: "订单付款", } for _, v := range dataList { sourceIdArr := strings.Split(v.SourceId, "_") mid := "" uid := "" if len(sourceIdArr) == 2 { mid = sourceIdArr[0] uid = sourceIdArr[1] } if mid == "" || uid == "" { continue } money := utils.Float64ToStr(float64(v.Price) / 100) if v.Channel == "wechat" { v.Channel = "channel" } bili := biliMap[v.Channel] zyBili := zyBiliMap[v.Channel] platformFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(bili) / 100) zyFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(zyBili) / 100) commission := "0" if utils.StrToFloat64(money) > 0 { commission = utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee)) } if utils.StrToFloat64(commission) < 0 { commission = "0" } var tmp = model.PlayletSaleOrder{ Uid: mid, SubUid: utils.StrToInt(uid), Data: utils.SerializeStr(v), Oid: v.OrderId, Amount: money, Commission: commission, CreateTime: utils.TimeParseStd(v.PayDate), UpdateTime: time.Now(), Title: v.Title, VideoType: v.Channel, PlatformType: "tpdaren", GoodsType: "playlet", OrdType: "video", ExtendUid: uids, Fee: zyFee, PlatformFee: platformFee, } playletSaleOrderDb := db.PlayletSaleOrderDb{} playletSaleOrderDb.Set() ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(v.OrderId, tmp.OrdType) tmp.Status = statusMap[v.Status] if v.Status != 2 && v.SettleType == "1" { tmp.Status = "订单结算" if ord != nil && ord.PlatformSettleTime.IsZero() { tmp.PlatformSettleTime = time.Now() } } if v.RefundDate != "" { tmp.RefundTime = utils.TimeParseStd(v.RefundDate) } if ord != nil { playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp) } else { tmp.CustomOid = utils.OrderUUID(tmp.SubUid) playletSaleOrderDb.PlayletVideoOrderInsert(&tmp) } } return len(list) }