智盟项目
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

290 lignes
7.9 KiB

  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 := utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee))
  245. var tmp = model.PlayletSaleOrder{
  246. Uid: mid,
  247. SubUid: utils.StrToInt(uid),
  248. Data: utils.SerializeStr(v),
  249. Oid: v.OrderId,
  250. Amount: money,
  251. Commission: commission,
  252. CreateTime: utils.TimeParseStd(v.PayDate),
  253. UpdateTime: time.Now(),
  254. Title: v.Title,
  255. VideoType: v.Channel,
  256. PlatformType: "tpdaren",
  257. GoodsType: "playlet",
  258. OrdType: "video",
  259. ExtendUid: uids,
  260. Fee: zyFee,
  261. PlatformFee: platformFee,
  262. }
  263. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  264. playletSaleOrderDb.Set()
  265. ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(v.OrderId, tmp.OrdType)
  266. tmp.Status = statusMap[v.Status]
  267. if v.Status != 2 && v.SettleType == "1" {
  268. tmp.Status = "订单结算"
  269. if ord != nil && ord.PlatformSettleTime.IsZero() {
  270. tmp.PlatformSettleTime = time.Now()
  271. }
  272. }
  273. if v.RefundDate != "" {
  274. tmp.RefundTime = utils.TimeParseStd(v.RefundDate)
  275. }
  276. if ord != nil {
  277. playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp)
  278. } else {
  279. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  280. playletSaleOrderDb.PlayletVideoOrderInsert(&tmp)
  281. }
  282. }
  283. return len(list)
  284. }