智盟项目
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

svc_playlet_adv_order.go 8.8 KiB

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