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

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