智盟项目
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

323 Zeilen
8.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 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. t := time.Now()
  102. if hours == 10 {
  103. hours += 4
  104. }
  105. stime := time.Date(t.Year(), t.Month(), t.Day(), hours, 0, 0, 0, t.Location())
  106. if utils.TimeStdParseUnix(playletVideoOrderYesterdayRuntime) > stime.Unix() {
  107. return
  108. }
  109. var (
  110. beginTime int64 = timeRange["start"] - 86400
  111. endTime int64 = timeRange["start"]
  112. pageNo int = 1
  113. pageSize int = 200
  114. )
  115. for {
  116. // 分配堆内存
  117. time.Sleep(time.Microsecond * 500) // 等待500毫秒
  118. //获取订单
  119. arg := map[string]interface{}{
  120. "start": time.Unix(beginTime, 0).Format("2006-01-02"),
  121. "end": time.Unix(endTime, 0).Format("2006-01-02"),
  122. "page_size": pageSize,
  123. "page_index": pageNo,
  124. }
  125. count := GetAdvOrder(uid, arg)
  126. if count == 0 {
  127. break
  128. }
  129. if count == pageSize {
  130. pageNo++
  131. continue
  132. }
  133. }
  134. syscfgDb.MasterListCfgUpdate(uid, runtimeStr, time.Now().Format("2006-01-02 15:04:05"))
  135. return
  136. }
  137. func PlayletAdvOrderMonth() {
  138. defer func() {
  139. if err := recover(); err != nil {
  140. _ = logx.Error(err)
  141. }
  142. }()
  143. uid := "0"
  144. timeRange := utils.GetTimeRange("last_month")
  145. t := time.Now()
  146. stime := time.Date(t.Year(), t.Month(), 5, 0, 0, 0, 0, t.Location()).Unix()
  147. day := time.Now().Day()
  148. if day < 5 {
  149. return
  150. }
  151. hour := time.Now().Hour()
  152. if hour < 3 {
  153. return
  154. }
  155. syscfgDb := db.MasterListCfgDb{}
  156. syscfgDb.Set()
  157. //上次开始的时间
  158. keyStart := "playlet_adv_order_month_starttime"
  159. starttime := syscfgDb.MasterListCfgGetOneData(uid, keyStart)
  160. //运行到哪一天
  161. key := "playlet_adv_order_month_runtime"
  162. runtime := syscfgDb.MasterListCfgGetOneData(uid, key)
  163. keyIsEnd := "playlet_adv_order_month_is_end"
  164. if utils.TimeStdParseUnix(starttime) < stime {
  165. syscfgDb.MasterListCfgUpdate(uid, key, time.Now().Format("2006-01-02 15:04:05"))
  166. syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "")
  167. runtime = time.Unix(timeRange["start"], 0).Format("2006-01-02 15:04:05")
  168. }
  169. syscfgDb.MasterListCfgUpdate(uid, keyStart, time.Now().Format("2006-01-02 15:04:05"))
  170. //当前是否结束了
  171. isEnd := syscfgDb.MasterListCfgGetOneData(uid, keyIsEnd)
  172. if isEnd == "1" {
  173. return
  174. }
  175. var (
  176. beginTime int64 = utils.TimeStdParseUnix(runtime)
  177. endTime int64 = utils.TimeStdParseUnix(runtime) + 86400
  178. pageNo int = 1
  179. pageSize int = 200
  180. )
  181. for {
  182. // 分配堆内存
  183. time.Sleep(time.Microsecond * 500) // 等待500毫秒
  184. //获取订单
  185. arg := map[string]interface{}{
  186. "start": time.Unix(beginTime, 0).Format("2006-01-02"),
  187. "end": time.Unix(endTime, 0).Format("2006-01-02"),
  188. "page_size": pageSize,
  189. "page_index": pageNo,
  190. }
  191. count := GetAdvOrder(uid, arg)
  192. if count == 0 {
  193. break
  194. }
  195. if count == pageSize {
  196. pageNo++
  197. continue
  198. }
  199. }
  200. if endTime > time.Now().Unix() {
  201. syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "1")
  202. }
  203. syscfgDb.MasterListCfgUpdate(uid, key, time.Unix(endTime, 0).Format("2006-01-02 15:04:05"))
  204. return
  205. }
  206. /***
  207. 注意 广告点击pv,uv每小时统计一次,结算金额将于次日,投流平台更新数据后更新
  208. 建议 所有统计数据:每小时在整点拉取当日统计,在次日凌晨1点后拉取一次昨日所有统计数据。金额数据,
  209. 建议在次日12:00后拉取后更新,可以将channel,source_id,created_time联合作为唯一索引,
  210. 如果遇到数据更新不及时,请联系客服。
  211. */
  212. func GetAdvOrder(uids string, arg map[string]interface{}) int {
  213. list := make([]map[string]string, 0)
  214. token := GetTpdarenToken(uids)
  215. paging, err := tpdaren.AdStatisticFindPaging(token, arg)
  216. fmt.Println("playletAdvOrder", paging)
  217. fmt.Println("playletAdvOrder", err)
  218. if paging == "" {
  219. return len(list)
  220. }
  221. data := gjson.Get(paging, "data.data").String()
  222. if data == "" {
  223. return len(list)
  224. }
  225. dataList := make([]md.PlayletAdvOrder, 0)
  226. json.Unmarshal([]byte(data), &dataList)
  227. sysCfgDb := db.SysCfgDb{}
  228. sysCfgDb.Set()
  229. playletKuaishouBili := sysCfgDb.SysCfgGetOneData("adv_kuaishou_bili")
  230. playletDouyinBili := sysCfgDb.SysCfgGetOneData("adv_douyin_bili")
  231. playletChannelBili := sysCfgDb.SysCfgGetOneData("adv_channel_bili")
  232. var biliMap = map[string]string{
  233. "kuaishou": playletKuaishouBili,
  234. "douyin": playletDouyinBili,
  235. "channel": playletChannelBili,
  236. }
  237. masterListCfgDb := db.MasterListCfgDb{}
  238. masterListCfgDb.Set()
  239. zyPlayletKuaishouBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_kuaishou_bili")
  240. zyPlayletDouyinBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_douyin_bili")
  241. zyPlayletChannelBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_channel_bili")
  242. var zyBiliMap = map[string]string{
  243. "kuaishou": zyPlayletKuaishouBili,
  244. "douyin": zyPlayletDouyinBili,
  245. "channel": zyPlayletChannelBili,
  246. }
  247. for _, v := range dataList {
  248. sourceIdArr := strings.Split(v.SourceId, "_")
  249. mid := ""
  250. uid := ""
  251. if len(sourceIdArr) == 2 {
  252. mid = sourceIdArr[0]
  253. uid = sourceIdArr[1]
  254. }
  255. if mid == "" || uid == "" {
  256. continue
  257. }
  258. money := utils.Float64ToStr(float64(v.Price) / 100)
  259. if v.Channel == "wechat" {
  260. v.Channel = "channel"
  261. }
  262. oid := v.Channel + v.SourceId + v.CreatedTime
  263. oid = strings.ReplaceAll(oid, "_", "")
  264. oid = strings.ReplaceAll(oid, "-", "")
  265. oid = strings.ReplaceAll(oid, " ", "")
  266. oid = strings.ReplaceAll(oid, ":", "")
  267. bili := biliMap[v.Channel]
  268. zyBili := zyBiliMap[v.Channel]
  269. platformFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(bili) / 100)
  270. zyFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(zyBili) / 100)
  271. commission := "0"
  272. if utils.StrToFloat64(money) > 0 {
  273. commission = utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee))
  274. }
  275. if utils.StrToFloat64(commission) < 0 {
  276. commission = "0"
  277. }
  278. var tmp = model.PlayletSaleOrder{
  279. Uid: mid,
  280. SubUid: utils.StrToInt(uid),
  281. Data: utils.SerializeStr(v),
  282. Oid: oid,
  283. Amount: money,
  284. Commission: commission,
  285. CreateTime: utils.TimeParseStd(v.CreatedTime + " 00:00:00"),
  286. UpdateTime: time.Now(),
  287. Title: v.Title,
  288. VideoType: v.Channel,
  289. PlatformType: "tpdaren",
  290. GoodsType: "playlet",
  291. OrdType: "adv",
  292. ExtendUid: uids,
  293. Fee: zyFee,
  294. PlatformFee: platformFee,
  295. }
  296. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  297. playletSaleOrderDb.Set()
  298. ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(tmp.Oid, tmp.OrdType)
  299. tmp.Status = "订单付款"
  300. if v.SettleType == "1" {
  301. tmp.Status = "订单结算"
  302. if ord != nil && ord.PlatformSettleTime.IsZero() {
  303. tmp.PlatformSettleTime = time.Now()
  304. }
  305. }
  306. if ord != nil {
  307. playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp)
  308. } else {
  309. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  310. playletSaleOrderDb.PlayletVideoOrderInsert(&tmp)
  311. }
  312. }
  313. return len(list)
  314. }