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

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