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

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