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 PlayletAdvOrder() { defer func() { if err := recover(); err != nil { _ = logx.Error(err) } }() uid := "0" // 获取上次获取订单时候的时间戳 pvdTimeKey := "playlet_adv_order_time" timeRange := utils.GetTimeRange("today") 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 = 86400 past = past + leave beginTime = past - eveTime endTime = past if endTime > now { endTime = now } if endTime > timeRange["start"] { beginTime = timeRange["start"] } for { // 分配堆内存 time.Sleep(time.Microsecond * 500) // 等待500毫秒 //获取订单 arg := map[string]interface{}{ "start": time.Unix(beginTime, 0).Format("2006-01-02"), "end": time.Unix(endTime, 0).Format("2006-01-02"), "page_size": pageSize, "page_index": pageNo, } count := GetAdvOrder(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 } if endTime > timeRange["start"] { beginTime = timeRange["start"] } continue } break } // 更新最后供应商执行订单时间 SetRunTime(uid, pvdTimeKey, utils.TimeToStr(now)) } func PlayletAdvOrderYesterday(hours int, runtimeStr string) { defer func() { if err := recover(); err != nil { _ = logx.Error(err) } }() uid := "0" timeRange := utils.GetTimeRange("today") hour := time.Now().Hour() if hour < hours { return } syscfgDb := db.MasterListCfgDb{} syscfgDb.Set() playletVideoOrderYesterdayRuntime := syscfgDb.MasterListCfgGetOneData(uid, runtimeStr) t := time.Now() if hours == 10 { hours += 4 } stime := time.Date(t.Year(), t.Month(), t.Day(), hours, 0, 0, 0, t.Location()) if utils.TimeStdParseUnix(playletVideoOrderYesterdayRuntime) > stime.Unix() { 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"), "end": time.Unix(endTime, 0).Format("2006-01-02"), "page_size": pageSize, "page_index": pageNo, } count := GetAdvOrder(uid, arg) if count == 0 { break } if count == pageSize { pageNo++ continue } } syscfgDb.MasterListCfgUpdate(uid, runtimeStr, time.Now().Format("2006-01-02 15:04:05")) return } func PlayletAdvOrderMonth() { 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_adv_order_month_starttime" starttime := syscfgDb.MasterListCfgGetOneData(uid, keyStart) //运行到哪一天 key := "playlet_adv_order_month_runtime" runtime := syscfgDb.MasterListCfgGetOneData(uid, key) keyIsEnd := "playlet_adv_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"), "end": time.Unix(endTime, 0).Format("2006-01-02"), "page_size": pageSize, "page_index": pageNo, } count := GetAdvOrder(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 } /*** 注意 广告点击pv,uv每小时统计一次,结算金额将于次日,投流平台更新数据后更新 建议 所有统计数据:每小时在整点拉取当日统计,在次日凌晨1点后拉取一次昨日所有统计数据。金额数据, 建议在次日12:00后拉取后更新,可以将channel,source_id,created_time联合作为唯一索引, 如果遇到数据更新不及时,请联系客服。 */ func GetAdvOrder(uids string, arg map[string]interface{}) int { list := make([]map[string]string, 0) token := GetTpdarenToken(uids) paging, err := tpdaren.AdStatisticFindPaging(token, arg) fmt.Println("playletAdvOrder", paging) fmt.Println("playletAdvOrder", err) if paging == "" { return len(list) } data := gjson.Get(paging, "data.data").String() if data == "" { return len(list) } dataList := make([]md.PlayletAdvOrder, 0) json.Unmarshal([]byte(data), &dataList) sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() playletKuaishouBili := sysCfgDb.SysCfgGetOneData("adv_kuaishou_bili") playletDouyinBili := sysCfgDb.SysCfgGetOneData("adv_douyin_bili") playletChannelBili := sysCfgDb.SysCfgGetOneData("adv_channel_bili") var biliMap = map[string]string{ "kuaishou": playletKuaishouBili, "douyin": playletDouyinBili, "channel": playletChannelBili, } masterListCfgDb := db.MasterListCfgDb{} masterListCfgDb.Set() zyPlayletKuaishouBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_kuaishou_bili") zyPlayletDouyinBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_douyin_bili") zyPlayletChannelBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_channel_bili") var zyBiliMap = map[string]string{ "kuaishou": zyPlayletKuaishouBili, "douyin": zyPlayletDouyinBili, "channel": zyPlayletChannelBili, } 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" } oid := v.Channel + v.SourceId + v.CreatedTime oid = strings.ReplaceAll(oid, "_", "") oid = strings.ReplaceAll(oid, "-", "") oid = strings.ReplaceAll(oid, " ", "") oid = strings.ReplaceAll(oid, ":", "") 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: oid, Amount: money, Commission: commission, CreateTime: utils.TimeParseStd(v.CreatedTime + " 00:00:00"), UpdateTime: time.Now(), Title: v.Title, VideoType: v.Channel, PlatformType: "tpdaren", GoodsType: "playlet", OrdType: "adv", ExtendUid: uids, Fee: zyFee, PlatformFee: platformFee, } playletSaleOrderDb := db.PlayletSaleOrderDb{} playletSaleOrderDb.Set() ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(tmp.Oid, tmp.OrdType) tmp.Status = "订单付款" if v.SettleType == "1" { tmp.Status = "订单结算" if ord != nil && ord.PlatformSettleTime.IsZero() { tmp.PlatformSettleTime = time.Now() } } if ord != nil { playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp) } else { tmp.CustomOid = utils.OrderUUID(tmp.SubUid) playletSaleOrderDb.PlayletVideoOrderInsert(&tmp) } } return len(list) }