智盟项目
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

312 rindas
8.6 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 15:04:05"))
  157. syscfgDb.MasterListCfgUpdate(uid, keyIsEnd, "")
  158. runtime = time.Unix(timeRange["start"], 0).Format("2006-01-02 15:04:05")
  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. /***
  197. 注意 广告点击pv,uv每小时统计一次,结算金额将于次日,投流平台更新数据后更新
  198. 建议 所有统计数据:每小时在整点拉取当日统计,在次日凌晨1点后拉取一次昨日所有统计数据。金额数据,
  199. 建议在次日12:00后拉取后更新,可以将channel,source_id,created_time联合作为唯一索引,
  200. 如果遇到数据更新不及时,请联系客服。
  201. */
  202. func getAdvOrder(uids string, arg map[string]interface{}) int {
  203. list := make([]map[string]string, 0)
  204. token := GetTpdarenToken(uids)
  205. paging, err := tpdaren.AdStatisticFindPaging(token, arg)
  206. fmt.Println("playletAdvOrder", paging)
  207. fmt.Println("playletAdvOrder", err)
  208. if paging == "" {
  209. return len(list)
  210. }
  211. data := gjson.Get(paging, "data.data").String()
  212. if data == "" {
  213. return len(list)
  214. }
  215. dataList := make([]md.PlayletAdvOrder, 0)
  216. json.Unmarshal([]byte(data), &dataList)
  217. sysCfgDb := db.SysCfgDb{}
  218. sysCfgDb.Set()
  219. playletKuaishouBili := sysCfgDb.SysCfgGetOneData("adv_kuaishou_bili")
  220. playletDouyinBili := sysCfgDb.SysCfgGetOneData("adv_douyin_bili")
  221. playletChannelBili := sysCfgDb.SysCfgGetOneData("adv_channel_bili")
  222. var biliMap = map[string]string{
  223. "kuaishou": playletKuaishouBili,
  224. "douyin": playletDouyinBili,
  225. "channel": playletChannelBili,
  226. }
  227. masterListCfgDb := db.MasterListCfgDb{}
  228. masterListCfgDb.Set()
  229. zyPlayletKuaishouBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_kuaishou_bili")
  230. zyPlayletDouyinBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_douyin_bili")
  231. zyPlayletChannelBili := masterListCfgDb.MasterListCfgGetOneData(uids, "zy_adv_channel_bili")
  232. var zyBiliMap = map[string]string{
  233. "kuaishou": zyPlayletKuaishouBili,
  234. "douyin": zyPlayletDouyinBili,
  235. "channel": zyPlayletChannelBili,
  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. oid := v.Channel + v.SourceId + v.CreatedTime
  253. oid = strings.ReplaceAll(oid, "_", "")
  254. oid = strings.ReplaceAll(oid, "-", "")
  255. oid = strings.ReplaceAll(oid, " ", "")
  256. oid = strings.ReplaceAll(oid, ":", "")
  257. bili := biliMap[v.Channel]
  258. zyBili := zyBiliMap[v.Channel]
  259. platformFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(bili) / 100)
  260. zyFee := utils.Float64ToStr(utils.StrToFloat64(money) * utils.StrToFloat64(zyBili) / 100)
  261. commission := "0"
  262. if utils.StrToFloat64(money) > 0 {
  263. commission = utils.Float64ToStr(utils.StrToFloat64(money) - utils.StrToFloat64(platformFee) - utils.StrToFloat64(zyFee))
  264. }
  265. if utils.StrToFloat64(commission) < 0 {
  266. commission = "0"
  267. }
  268. var tmp = model.PlayletSaleOrder{
  269. Uid: mid,
  270. SubUid: utils.StrToInt(uid),
  271. Data: utils.SerializeStr(v),
  272. Oid: oid,
  273. Amount: money,
  274. Commission: commission,
  275. CreateTime: utils.TimeParseStd(v.CreatedTime + " 00:00:00"),
  276. UpdateTime: time.Now(),
  277. Title: v.Title,
  278. VideoType: v.Channel,
  279. PlatformType: "tpdaren",
  280. GoodsType: "playlet",
  281. OrdType: "adv",
  282. ExtendUid: uids,
  283. Fee: zyFee,
  284. PlatformFee: platformFee,
  285. }
  286. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  287. playletSaleOrderDb.Set()
  288. ord := playletSaleOrderDb.GetPlayletVideoOrderByOid(tmp.Oid, tmp.OrdType)
  289. tmp.Status = "订单付款"
  290. if v.SettleType == "1" {
  291. tmp.Status = "订单结算"
  292. if ord != nil && ord.PlatformSettleTime.IsZero() {
  293. tmp.PlatformSettleTime = time.Now()
  294. }
  295. }
  296. if ord != nil {
  297. playletSaleOrderDb.PlayletVideoOrderUpdate(ord.Id, &tmp)
  298. } else {
  299. tmp.CustomOid = utils.OrderUUID(tmp.SubUid)
  300. playletSaleOrderDb.PlayletVideoOrderInsert(&tmp)
  301. }
  302. }
  303. return len(list)
  304. }