智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_playlet_video_order.go 8.1 KiB

1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/task/md"
  6. "applet/app/utils"
  7. "applet/app/utils/logx"
  8. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/tpdaren"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/tidwall/gjson"
  12. "strings"
  13. "time"
  14. )
  15. func PlayletVideoOrder() {
  16. defer func() {
  17. if err := recover(); err != nil {
  18. _ = logx.Error(err)
  19. }
  20. }()
  21. uid := "0"
  22. // 获取上次获取订单时候的时间戳
  23. pvdTimeKey := "playlet_video_order_time"
  24. now := time.Now().Unix()
  25. past := GetRunTime(uid, pvdTimeKey, "短剧获取订单时间")
  26. var (
  27. beginTime int64 = 0
  28. endTime int64 = 0
  29. pageNo int = 1
  30. pageSize int = 200
  31. )
  32. //怕时间不是走最新的
  33. leave := now - past
  34. if leave > 500 {
  35. leave = 0
  36. }
  37. var eveTime int64 = 3600
  38. past = past + leave
  39. beginTime = past - eveTime
  40. endTime = past
  41. if endTime > now {
  42. endTime = now
  43. }
  44. for {
  45. // 分配堆内存
  46. time.Sleep(time.Microsecond * 500) // 等待500毫秒
  47. //获取订单
  48. arg := map[string]interface{}{
  49. "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"),
  50. "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"),
  51. "page_size": pageSize,
  52. "page_index": pageNo,
  53. }
  54. count := getVideoOrder(uid, arg)
  55. if count == 0 {
  56. goto ChkArg
  57. }
  58. if count == pageSize {
  59. pageNo++
  60. continue
  61. }
  62. ChkArg:
  63. // 查询完后重置时间, 最后查询时间
  64. if endTime < now {
  65. pageNo = 1
  66. SetRunTime(uid, pvdTimeKey, utils.TimeToStr(endTime))
  67. beginTime = endTime
  68. endTime = endTime + eveTime
  69. if endTime > now {
  70. endTime = now
  71. }
  72. continue
  73. }
  74. break
  75. }
  76. // 更新最后供应商执行订单时间
  77. SetRunTime(uid, pvdTimeKey, utils.TimeToStr(now))
  78. }
  79. func PlayletVideoOrderYesterDay() {
  80. defer func() {
  81. if err := recover(); err != nil {
  82. _ = logx.Error(err)
  83. }
  84. }()
  85. uid := "0"
  86. timeRange := utils.GetTimeRange("today")
  87. hour := time.Now().Hour()
  88. if hour < 1 {
  89. return
  90. }
  91. syscfgDb := db.MasterListCfgDb{}
  92. syscfgDb.Set()
  93. playletVideoOrderYesterdayRuntime := syscfgDb.MasterListCfgGetOneData(uid, "playlet_video_order_yesterday_runtime")
  94. if utils.TimeStdParseUnix(playletVideoOrderYesterdayRuntime) > timeRange["start"] {
  95. return
  96. }
  97. var (
  98. beginTime int64 = timeRange["start"] - 86400
  99. endTime int64 = timeRange["start"]
  100. pageNo int = 1
  101. pageSize int = 200
  102. )
  103. for {
  104. // 分配堆内存
  105. time.Sleep(time.Microsecond * 500) // 等待500毫秒
  106. //获取订单
  107. arg := map[string]interface{}{
  108. "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"),
  109. "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"),
  110. "page_size": pageSize,
  111. "page_index": pageNo,
  112. }
  113. count := getVideoOrder(uid, arg)
  114. if count == 0 {
  115. break
  116. }
  117. if count == pageSize {
  118. pageNo++
  119. continue
  120. }
  121. }
  122. syscfgDb.MasterListCfgUpdate(uid, "playlet_video_order_yesterday_runtime", time.Now().Format("2006-01-02 15:04:05"))
  123. return
  124. }
  125. func PlayletVideoOrderMonth() {
  126. defer func() {
  127. if err := recover(); err != nil {
  128. _ = logx.Error(err)
  129. }
  130. }()
  131. uid := "0"
  132. timeRange := utils.GetTimeRange("last_month")
  133. t := time.Now()
  134. stime := time.Date(t.Year(), t.Month(), 5, 0, 0, 0, 0, t.Location()).Unix()
  135. day := time.Now().Day()
  136. if day < 5 {
  137. return
  138. }
  139. syscfgDb := db.MasterListCfgDb{}
  140. syscfgDb.Set()
  141. //上次开始的时间
  142. keyStart := "playlet_video_order_month_starttime"
  143. starttime := syscfgDb.MasterListCfgGetOneData(uid, keyStart)
  144. //运行到哪一天
  145. key := "playlet_video_order_month_runtime"
  146. runtime := syscfgDb.MasterListCfgGetOneData(uid, key)
  147. keyIsEnd := "playlet_video_order_month_is_end"
  148. if utils.TimeStdParseUnix(starttime) < stime {
  149. syscfgDb.MasterListCfgUpdate(uid, key, time.Now().Format("2006-01-02 15:04:05"))
  150. syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "")
  151. runtime = time.Unix(timeRange["start"], 0).Format("2006-01-02 15:04:05")
  152. }
  153. //当前是否结束了
  154. isEnd := syscfgDb.MasterListCfgGetOneData(uid, keyIsEnd)
  155. if isEnd == "1" {
  156. return
  157. }
  158. var (
  159. beginTime int64 = utils.TimeStdParseUnix(runtime) - 86400
  160. endTime int64 = utils.TimeStdParseUnix(runtime)
  161. pageNo int = 1
  162. pageSize int = 200
  163. )
  164. for {
  165. // 分配堆内存
  166. time.Sleep(time.Microsecond * 500) // 等待500毫秒
  167. //获取订单
  168. arg := map[string]interface{}{
  169. "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"),
  170. "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"),
  171. "page_size": pageSize,
  172. "page_index": pageNo,
  173. }
  174. count := getVideoOrder(uid, arg)
  175. if count == 0 {
  176. break
  177. }
  178. if count == pageSize {
  179. pageNo++
  180. continue
  181. }
  182. }
  183. if endTime > time.Now().Unix() {
  184. syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "1")
  185. }
  186. syscfgDb.MasterListCfgUpdate(uid, key, time.Unix(endTime, 0).Format("2006-01-02 15:04:05"))
  187. return
  188. }
  189. func getVideoOrder(uids string, arg map[string]interface{}) int {
  190. list := make([]map[string]string, 0)
  191. token := GetTpdarenToken(uids)
  192. paging, err := tpdaren.OrderFindPaging(token, arg)
  193. fmt.Println("playletVideoOrder", paging)
  194. fmt.Println("playletVideoOrder", err)
  195. if paging == "" {
  196. return len(list)
  197. }
  198. data := gjson.Get(paging, "data.data").String()
  199. if data == "" {
  200. return len(list)
  201. }
  202. dataList := make([]md.PlayletVideoOrder, 0)
  203. json.Unmarshal([]byte(data), &dataList)
  204. sysCfgDb := db.MasterListCfgDb{}
  205. sysCfgDb.Set()
  206. playletKuaishouBili := sysCfgDb.MasterListCfgGetOneData(uids, "playlet_kuaishou_bili")
  207. playletDouyinBili := sysCfgDb.MasterListCfgGetOneData(uids, "playlet_douyin_bili")
  208. playletChannelBili := sysCfgDb.MasterListCfgGetOneData(uids, "playlet_channel_bili")
  209. var biliMap = map[string]string{
  210. "kuaishou": playletKuaishouBili,
  211. "douyin": playletDouyinBili,
  212. "channel": playletChannelBili,
  213. }
  214. zyPlayletKuaishouBili := sysCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_kuaishou_bili")
  215. zyPlayletDouyinBili := sysCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_douyin_bili")
  216. zyPlayletChannelBili := sysCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_channel_bili")
  217. var zyBiliMap = map[string]string{
  218. "kuaishou": zyPlayletKuaishouBili,
  219. "douyin": zyPlayletDouyinBili,
  220. "channel": zyPlayletChannelBili,
  221. }
  222. var statusMap = map[int]string{
  223. 2: "订单退款", 4: "订单付款",
  224. }
  225. for _, v := range dataList {
  226. sourceIdArr := strings.Split(v.SourceId, "_")
  227. mid := ""
  228. uid := ""
  229. if len(sourceIdArr) == 2 {
  230. mid = sourceIdArr[0]
  231. uid = sourceIdArr[1]
  232. }
  233. if mid == "" || uid == "" {
  234. continue
  235. }
  236. money := utils.Float64ToStr(float64(v.Price) / 100)
  237. if v.Channel == "wechat" {
  238. v.Channel = "channel"
  239. }
  240. bili := biliMap[v.Channel]
  241. zyBili := zyBiliMap[v.Channel]
  242. platformFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(bili) / 100)
  243. zyFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(zyBili) / 100)
  244. commission := "0"
  245. if utils.StrToFloat64(money) > 0 {
  246. commission = utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee))
  247. }
  248. if utils.StrToFloat64(commission) < 0 {
  249. commission = "0"
  250. }
  251. var tmp = model.PlayletSaleOrder{
  252. Uid: mid,
  253. SubUid: utils.StrToInt(uid),
  254. Data: utils.SerializeStr(v),
  255. Oid: v.OrderId,
  256. Amount: money,
  257. Commission: commission,
  258. CreateTime: utils.TimeParseStd(v.PayDate),
  259. UpdateTime: time.Now(),
  260. Title: v.Title,
  261. VideoType: v.Channel,
  262. PlatformType: "tpdaren",
  263. GoodsType: "playlet",
  264. OrdType: "video",
  265. ExtendUid: uids,
  266. Fee: zyFee,
  267. PlatformFee: platformFee,
  268. }
  269. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  270. playletSaleOrderDb.Set()
  271. ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(v.OrderId, tmp.OrdType)
  272. tmp.Status = statusMap[v.Status]
  273. if v.Status != 2 && v.SettleType == "1" {
  274. tmp.Status = "订单结算"
  275. if ord != nil && ord.PlatformSettleTime.IsZero() {
  276. tmp.PlatformSettleTime = time.Now()
  277. }
  278. }
  279. if v.RefundDate != "" {
  280. tmp.RefundTime = utils.TimeParseStd(v.RefundDate)
  281. }
  282. if ord != nil {
  283. playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp)
  284. } else {
  285. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  286. playletSaleOrderDb.PlayletVideoOrderInsert(&tmp)
  287. }
  288. }
  289. return len(list)
  290. }