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

94 lines
2.4 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/utils"
  6. "applet/app/utils/cache"
  7. "fmt"
  8. "time"
  9. )
  10. func PlayletOrderSettle() {
  11. day := time.Now().Day()
  12. if day < 5 {
  13. return
  14. }
  15. month := utils.GetTimeRange("current_month")
  16. fmt.Println(month)
  17. arg := map[string]string{
  18. "status": "订单结算",
  19. "sort": "id desc",
  20. "is_to_settle": "1",
  21. "to_settle_time": time.Unix(month["start"], 0).Format("2006-01-02 15:04:05"),
  22. "p": "1",
  23. "size": "100",
  24. }
  25. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  26. playletSaleOrderDb.Set()
  27. order := playletSaleOrderDb.GetPlayletVideoOrderList(arg)
  28. if len(order) == 0 {
  29. return
  30. }
  31. for _, v := range order {
  32. // 加锁 防止并发提取
  33. mutexKey := fmt.Sprintf("playlet_order_settle:%s", v.CustomOid)
  34. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 1800, "NX")
  35. if err != nil {
  36. continue
  37. }
  38. if withdrawAvailable != "OK" {
  39. continue
  40. }
  41. if v.SettleTime > 0 {
  42. continue
  43. }
  44. //加到上月结算
  45. if v.Uid == "" || v.Uid == "0" {
  46. continue
  47. }
  48. masterDb := db.MasterDb{}
  49. masterDb.Set()
  50. master := masterDb.GetMaster(v.Uid)
  51. if master == nil {
  52. continue
  53. }
  54. masterAmountDb := db.MasterAmountDb{}
  55. masterAmountDb.Set()
  56. amount := masterAmountDb.GetMasterAmount(utils.IntToStr(master.Id), "playlet")
  57. if amount == nil {
  58. continue
  59. }
  60. if utils.StrToFloat64(v.Commission) <= 0 {
  61. v.SettleTime = int(time.Now().Unix())
  62. playletSaleOrderDb.PlayletVideoOrderUpdate(v.Id, &v)
  63. continue
  64. }
  65. oldAmount := amount.LastMonthAmount
  66. amount.LastMonthAmount = utils.Float64ToStr(utils.StrToFloat64(amount.LastMonthAmount) + utils.StrToFloat64(v.Commission))
  67. update := masterAmountDb.MasterAmountUpdate(master.Id, amount)
  68. if update == false {
  69. continue
  70. }
  71. var tmp = model.MasterLastMonthAmountFlow{
  72. Uid: utils.IntToStr(master.Id),
  73. Time: time.Now(),
  74. BeforeAmount: oldAmount,
  75. Amount: v.Commission,
  76. AfterAmount: amount.LastMonthAmount,
  77. Platform: "playlet",
  78. Oid: v.Oid,
  79. Title: "短剧订单结算",
  80. FlowType: "playlet_settle",
  81. ExtendUid: master.ExtendUid,
  82. }
  83. masterLastMonthAmountFlowDb := db.MasterLastMonthAmountFlowDb{}
  84. masterLastMonthAmountFlowDb.Set()
  85. masterLastMonthAmountFlowDb.MasterLastMonthAmountFlowInsert(&tmp)
  86. v.SettleTime = int(time.Now().Unix())
  87. playletSaleOrderDb.PlayletVideoOrderUpdate(v.Id, &v)
  88. }
  89. return
  90. }