互动
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.

154 lines
5.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package client
  2. import (
  3. "fmt"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/models"
  6. activity_service "hudongzhuanjia/services/activity"
  7. pay_service "hudongzhuanjia/services/pay"
  8. ws_send_service "hudongzhuanjia/services/ws_send"
  9. "hudongzhuanjia/utils/code"
  10. "hudongzhuanjia/utils/define"
  11. "math/rand"
  12. "time"
  13. )
  14. type ShakeRedEnvelopeCtl struct {
  15. controllers.AuthorCtl
  16. }
  17. func (t *ShakeRedEnvelopeCtl) Status() {
  18. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  19. rule := new(models.ShakeRedEnvelopeRule)
  20. exist, err := models.Get(rule, ruleId)
  21. t.CheckErr(err)
  22. t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_EXIST, "摇红包规则不存在")
  23. t.JSON(map[string]interface{}{
  24. "status": rule.ShakeRedEnvelopeStatus,
  25. })
  26. }
  27. func (t *ShakeRedEnvelopeCtl) Shake() {
  28. customerId := t.MustGetInt64("customer_id")
  29. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  30. userId := t.MustGetUID()
  31. areaId := t.MustGetAreaId()
  32. // 查询摇红包规则
  33. rule := new(models.ShakeRedEnvelopeRule)
  34. exist, err := models.Get(rule, ruleId)
  35. t.CheckErr(err)
  36. t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "摇红包规则不存在")
  37. t.CheckRunning(rule.ShakeRedEnvelopeStatus)
  38. // 查询摇红包活动主id, is_pay=1给钱了,给摇
  39. envelope := new(models.ShakeRedEnvelopeActivity)
  40. exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId)
  41. t.CheckErr(err)
  42. t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
  43. activity := new(models.Activity)
  44. exist, err = models.Get(activity, envelope.ActivityId)
  45. t.CheckErr(err)
  46. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  47. t.CheckRunning(activity.Status)
  48. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_SHAKRB, activity.Id)
  49. t.CheckErr(err)
  50. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在")
  51. record := new(models.ShakeRedEnvelopeRecord) /// 之后使用, 存入乐观锁
  52. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { // 剔除摇过红包的用户
  53. exist, err = record.ExistRecord(activity.RehearsalId, rule.Id, userId)
  54. t.CheckErr(err)
  55. t.Assert(!exist, code.MSG_SHAKERB_RECORD_NOT_HIT, "您已经摇过红包了,请等待下一轮.") // 不存在继续往下走
  56. }
  57. // done: 增加概率, 修改概率
  58. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  59. t.Assert(r.Float64()*100 <= rule.Probability, code.MSG_SHAKERB_RECORD_NOT_HIT, "您与红包擦肩而过") // 必须在红包概率以内才能获取
  60. // 增加session 或者乐观锁
  61. exist, err = record.GetByRuleId(ruleId, activity.RehearsalId, 0)
  62. t.CheckErr(err)
  63. t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包已经派发完毕")
  64. // 查询用户
  65. user := new(models.User)
  66. exist, err = models.Get(user, userId)
  67. t.CheckErr(err)
  68. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  69. record.IsDraw = 0
  70. record.UserId = userId
  71. record.AreaId = areaId
  72. row, err := models.Update(record.Id, record, "is_draw", "user_id", "area_id")
  73. t.CheckErr(err)
  74. t.Assert(row == 1, code.MSG_SHAKERB_RECORD_NOT_HIT, "您与红包擦肩而过") // 那么 row == 1 为已经成功抢到
  75. // 记录红包 ---> 非彩排才能
  76. if activity.RehearsalId == 0 {
  77. result, err := pay_service.SendRedPack("欧轩互动", user.Openid, "红包只会越抢越多",
  78. "直播抢红包活动", "抢的多,赚得多", int(record.Amount*100), 1, 2)
  79. if err != nil {
  80. t.ERROR("红包被领完了", code.MSG_SHAKERB_RECORD_NOT_HIT)
  81. }
  82. record.MchBillno = result.MchBillno
  83. models.Update(record.Id, record, "mch_billno")
  84. }
  85. // 通知大屏
  86. total, err := new(models.ShakeRedEnvelopeRecord).Total(activity.Id, activity.RehearsalId, envelope.Id, rule.Id)
  87. remaining, err := new(models.ShakeRedEnvelopeRecord).Count(activity.Id, activity.RehearsalId, envelope.Id, rule.Id, -1)
  88. t.CheckErr(err)
  89. go ws_send_service.SendShakeRedEnvelope(fmt.Sprintf("%d", activity.Id),
  90. define.TYPE_H5USER, userId, map[string]interface{}{
  91. "customer_id": customerId,
  92. "user_id": user.Id,
  93. "type": "shake_rb",
  94. "data": map[string]interface{}{
  95. "amount": record.Amount,
  96. "nickname": user.Nickname,
  97. "avatar": user.Avatar,
  98. "total": total - remaining, //
  99. "remaining": remaining, //
  100. },
  101. })
  102. go ws_send_service.SendShakeRedEnvelope(fmt.Sprintf("%d", activity.Id),
  103. define.TYPE_CUSTOMER, customerId, map[string]interface{}{
  104. "customer_id": customerId,
  105. "user_id": user.Id,
  106. "type": "shake_rb",
  107. "data": map[string]interface{}{
  108. "amount": record.Amount,
  109. "nickname": user.Nickname,
  110. "avatar": user.Avatar,
  111. "total": total - remaining, //
  112. "remaining": remaining, //
  113. },
  114. })
  115. t.JSON(map[string]interface{}{
  116. "amount": record.Amount,
  117. "shake_red_envelope_record_id": record.Id,
  118. })
  119. }
  120. // 用户红包
  121. func (t *ShakeRedEnvelopeCtl) UserRedEnvelopes() {
  122. uid := t.MustGetUID()
  123. activityId := t.MustGetInt64("activity_id")
  124. activity := new(models.Activity)
  125. exist, err := models.Get(activity, activityId)
  126. t.CheckErr(err)
  127. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "活动不存在")
  128. records, err := models.GetRedEnvelopesByUserId(uid, activityId, activity.RehearsalId)
  129. t.CheckErr(err)
  130. t.JSON(map[string]interface{}{
  131. "list": records,
  132. "total": len(records),
  133. })
  134. }