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

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