|
- 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)
- 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"),
- "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
- }
- 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"))
- syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "")
- runtime = time.Unix(timeRange["start"], 0).Format("2006-01-02")
- }
- //当前是否结束了
- isEnd := syscfgDb.MasterListCfgGetOneData(uid, keyIsEnd)
- if isEnd == "1" {
- return
- }
- var (
- beginTime int64 = utils.TimeStdParseUnix(runtime) - 86400
- endTime int64 = utils.TimeStdParseUnix(runtime)
- 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
- }
-
- 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.MasterListCfgDb{}
- sysCfgDb.Set()
- playletKuaishouBili := sysCfgDb.MasterListCfgGetOneData(uids, "adv_kuaishou_bili")
- playletDouyinBili := sysCfgDb.MasterListCfgGetOneData(uids, "adv_douyin_bili")
- playletChannelBili := sysCfgDb.MasterListCfgGetOneData(uids, "adv_channel_bili")
- var biliMap = map[string]string{
- "kuaishou": playletKuaishouBili,
- "douyin": playletDouyinBili,
- "channel": playletChannelBili,
- }
- zyPlayletKuaishouBili := sysCfgDb.MasterListCfgGetOneData(uids, "zy_adv_kuaishou_bili")
- zyPlayletDouyinBili := sysCfgDb.MasterListCfgGetOneData(uids, "zy_adv_douyin_bili")
- zyPlayletChannelBili := sysCfgDb.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)
- }
|