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

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