|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 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)
- }
|