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

114 lines
2.8 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 < 20 {
  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. "is_commission": "1",
  22. "to_settle_time": time.Unix(month["start"], 0).Format("2006-01-02 15:04:05"),
  23. "p": "1",
  24. "size": "100",
  25. }
  26. playletSaleOrderDb := db.PlayletSaleOrderDb{}
  27. playletSaleOrderDb.Set()
  28. order := playletSaleOrderDb.GetPlayletVideoOrderList(arg)
  29. if len(order) == 0 {
  30. return
  31. }
  32. fmt.Println(utils.SerializeStr(order))
  33. for _, v := range order {
  34. fmt.Println("=================00")
  35. // 加锁 防止并发提取
  36. mutexKey := fmt.Sprintf("playlet_order_settle3:%d", v.Id)
  37. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 1800, "NX")
  38. if err != nil {
  39. fmt.Println("=================0")
  40. fmt.Println(err)
  41. continue
  42. }
  43. if withdrawAvailable != "OK" {
  44. fmt.Println("=================1")
  45. continue
  46. }
  47. if v.SettleTime > 0 {
  48. fmt.Println("=================2")
  49. continue
  50. }
  51. //加到上月结算
  52. if v.Uid == "" || v.Uid == "0" {
  53. fmt.Println("=================3")
  54. continue
  55. }
  56. masterDb := db.MasterDb{}
  57. masterDb.Set()
  58. master := masterDb.GetMaster(v.Uid)
  59. if master == nil {
  60. fmt.Println("=================4")
  61. continue
  62. }
  63. masterAmountDb := db.MasterAmountDb{}
  64. masterAmountDb.Set()
  65. amount := masterAmountDb.GetMasterAmountByExtendUid(utils.IntToStr(master.Id), v.ExtendUid, "playlet")
  66. if amount == nil {
  67. fmt.Println("=================5")
  68. continue
  69. }
  70. if utils.StrToFloat64(v.Commission) <= 0 {
  71. v.SettleTime = int(time.Now().Unix())
  72. playletSaleOrderDb.PlayletVideoOrderUpdate(v.Id, &v)
  73. fmt.Println("=================6")
  74. continue
  75. }
  76. oldAmount := amount.LastMonthAmount
  77. amount.LastMonthAmount = utils.Float64ToStr(utils.StrToFloat64(amount.LastMonthAmount) + utils.StrToFloat64(v.Commission))
  78. update := masterAmountDb.MasterAmountUpdate(amount.Id, amount)
  79. if update == false {
  80. fmt.Println("=================7")
  81. continue
  82. }
  83. var tmp = model.MasterLastMonthAmountFlow{
  84. Uid: utils.IntToStr(master.Id),
  85. Time: time.Now(),
  86. BeforeAmount: oldAmount,
  87. Amount: v.Commission,
  88. AfterAmount: amount.LastMonthAmount,
  89. Platform: "playlet",
  90. Oid: v.Oid,
  91. Title: "短剧订单结算",
  92. FlowType: "playlet_settle",
  93. ExtendUid: v.ExtendUid,
  94. }
  95. masterLastMonthAmountFlowDb := db.MasterLastMonthAmountFlowDb{}
  96. masterLastMonthAmountFlowDb.Set()
  97. masterLastMonthAmountFlowDb.MasterLastMonthAmountFlowInsert(&tmp)
  98. v.SettleTime = int(time.Now().Unix())
  99. playletSaleOrderDb.PlayletVideoOrderUpdate(v.Id, &v)
  100. }
  101. return
  102. }