智盟项目
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.

306 lines
8.5 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. "status": 2,
  113. }
  114. count := getVideoOrder(uid, arg)
  115. if count == 0 {
  116. break
  117. }
  118. if count == pageSize {
  119. pageNo++
  120. continue
  121. }
  122. }
  123. syscfgDb.MasterListCfgUpdate(uid, "playlet_video_order_yesterday_runtime", time.Now().Format("2006-01-02 15:04:05"))
  124. return
  125. }
  126. func PlayletVideoOrderMonth() {
  127. defer func() {
  128. if err := recover(); err != nil {
  129. _ = logx.Error(err)
  130. }
  131. }()
  132. uid := "0"
  133. timeRange := utils.GetTimeRange("last_month")
  134. t := time.Now()
  135. stime := time.Date(t.Year(), t.Month(), 5, 0, 0, 0, 0, t.Location()).Unix()
  136. day := time.Now().Day()
  137. if day < 5 {
  138. return
  139. }
  140. syscfgDb := db.MasterListCfgDb{}
  141. syscfgDb.Set()
  142. //上次开始的时间
  143. keyStart := "playlet_video_order_month_starttime"
  144. starttime := syscfgDb.MasterListCfgGetOneData(uid, keyStart)
  145. //运行到哪一天
  146. key := "playlet_video_order_month_runtime"
  147. runtime := syscfgDb.MasterListCfgGetOneData(uid, key)
  148. keyIsEnd := "playlet_video_order_month_is_end"
  149. if utils.TimeStdParseUnix(starttime) < stime {
  150. syscfgDb.MasterListCfgUpdate(uid, key, time.Now().Format("2006-01-02 15:04:05"))
  151. syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "")
  152. runtime = time.Unix(timeRange["start"], 0).Format("2006-01-02 15:04:05")
  153. }
  154. syscfgDb.MasterListCfgUpdate(uid, keyStart, time.Now().Format("2006-01-02 15:04:05"))
  155. //当前是否结束了
  156. isEnd := syscfgDb.MasterListCfgGetOneData(uid, keyIsEnd)
  157. if isEnd == "1" {
  158. return
  159. }
  160. var (
  161. beginTime int64 = utils.TimeStdParseUnix(runtime)
  162. endTime int64 = utils.TimeStdParseUnix(runtime) + 86400
  163. pageNo int = 1
  164. pageSize int = 200
  165. )
  166. for {
  167. // 分配堆内存
  168. time.Sleep(time.Microsecond * 500) // 等待500毫秒
  169. //获取订单
  170. arg := map[string]interface{}{
  171. "start": time.Unix(beginTime, 0).Format("2006-01-02 15:04:05"),
  172. "end": time.Unix(endTime, 0).Format("2006-01-02 15:04:05"),
  173. "page_size": pageSize,
  174. "page_index": pageNo,
  175. }
  176. count := getVideoOrder(uid, arg)
  177. if count == 0 {
  178. break
  179. }
  180. if count == pageSize {
  181. pageNo++
  182. continue
  183. }
  184. }
  185. if endTime > time.Now().Unix() {
  186. syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "1")
  187. }
  188. syscfgDb.MasterListCfgUpdate(uid, key, time.Unix(endTime, 0).Format("2006-01-02 15:04:05"))
  189. return
  190. }
  191. /***
  192. 注意 在选择status为2退款时,start,end的筛选字段将是退款时间,时间范围最大为30天
  193. 建议 所有订单:实时订单,延迟最多1分钟内。退款订单:建议每日凌晨1点后拉取一次昨日退款订单。
  194. */
  195. func getVideoOrder(uids string, arg map[string]interface{}) int {
  196. list := make([]map[string]string, 0)
  197. token := GetTpdarenToken(uids)
  198. paging, err := tpdaren.OrderFindPaging(token, arg)
  199. fmt.Println("playletVideoOrder", paging)
  200. fmt.Println("playletVideoOrder", err)
  201. if paging == "" {
  202. return len(list)
  203. }
  204. data := gjson.Get(paging, "data.data").String()
  205. if data == "" {
  206. return len(list)
  207. }
  208. dataList := make([]md.PlayletVideoOrder, 0)
  209. json.Unmarshal([]byte(data), &dataList)
  210. sysCfgDb := db.SysCfgDb{}
  211. sysCfgDb.Set()
  212. playletKuaishouBili := sysCfgDb.SysCfgGetOneData("playlet_kuaishou_bili")
  213. playletDouyinBili := sysCfgDb.SysCfgGetOneData("playlet_douyin_bili")
  214. playletChannelBili := sysCfgDb.SysCfgGetOneData("playlet_channel_bili")
  215. var biliMap = map[string]string{
  216. "kuaishou": playletKuaishouBili,
  217. "douyin": playletDouyinBili,
  218. "channel": playletChannelBili,
  219. }
  220. masterListCfgDb := db.MasterListCfgDb{}
  221. masterListCfgDb.Set()
  222. zyPlayletKuaishouBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_kuaishou_bili")
  223. zyPlayletDouyinBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_douyin_bili")
  224. zyPlayletChannelBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_playlet_channel_bili")
  225. var zyBiliMap = map[string]string{
  226. "kuaishou": zyPlayletKuaishouBili,
  227. "douyin": zyPlayletDouyinBili,
  228. "channel": zyPlayletChannelBili,
  229. }
  230. var statusMap = map[int]string{
  231. 2: "订单退款", 4: "订单付款",
  232. }
  233. for _, v := range dataList {
  234. sourceIdArr := strings.Split(v.SourceId, "_")
  235. mid := ""
  236. uid := ""
  237. if len(sourceIdArr) == 2 {
  238. mid = sourceIdArr[0]
  239. uid = sourceIdArr[1]
  240. }
  241. if mid == "" || uid == "" {
  242. continue
  243. }
  244. money := utils.Float64ToStr(float64(v.Price) / 100)
  245. if v.Channel == "wechat" {
  246. v.Channel = "channel"
  247. }
  248. bili := biliMap[v.Channel]
  249. zyBili := zyBiliMap[v.Channel]
  250. platformFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(bili) / 100)
  251. zyFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(zyBili) / 100)
  252. commission := "0"
  253. if utils.StrToFloat64(money) > 0 {
  254. commission = utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee))
  255. }
  256. if utils.StrToFloat64(commission) < 0 {
  257. commission = "0"
  258. }
  259. var tmp = model.PlayletSaleOrder{
  260. Uid: mid,
  261. SubUid: utils.StrToInt(uid),
  262. Data: utils.SerializeStr(v),
  263. Oid: v.OrderId,
  264. Amount: money,
  265. Commission: commission,
  266. CreateTime: utils.TimeParseStd(v.PayDate),
  267. UpdateTime: time.Now(),
  268. Title: v.Title,
  269. VideoType: v.Channel,
  270. PlatformType: "tpdaren",
  271. GoodsType: "playlet",
  272. OrdType: "video",
  273. ExtendUid: uids,
  274. Fee: zyFee,
  275. PlatformFee: platformFee,
  276. }
  277. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  278. playletSaleOrderDb.Set()
  279. ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(v.OrderId, tmp.OrdType)
  280. tmp.Status = statusMap[v.Status]
  281. if v.Status != 2 && v.SettleType == "1" {
  282. tmp.Status = "订单结算"
  283. if ord != nil && ord.PlatformSettleTime.IsZero() {
  284. tmp.PlatformSettleTime = time.Now()
  285. }
  286. }
  287. if v.RefundDate != "" {
  288. tmp.RefundTime = utils.TimeParseStd(v.RefundDate)
  289. }
  290. if ord != nil {
  291. playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp)
  292. } else {
  293. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  294. playletSaleOrderDb.PlayletVideoOrderInsert(&tmp)
  295. }
  296. }
  297. return len(list)
  298. }