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

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