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

164 lines
5.5 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
  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. // todo:调用支付接口
  29. func (t *ShakeRedEnvelopeCtl) Shake() {
  30. customerId := t.MustGetInt64("customer_id")
  31. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  32. userId := t.MustGetUID()
  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. area := new(models.AreaStore)
  40. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  41. t.CheckErr(err)
  42. t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
  43. // 查询摇红包活动主id, is_pay=1给钱了,给摇
  44. envelope := new(models.ShakeRedEnvelopeActivity)
  45. exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId)
  46. t.CheckErr(err)
  47. t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
  48. activity := new(models.Activity)
  49. exist, err = models.GetById(activity, envelope.ActivityId)
  50. t.CheckErr(err)
  51. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  52. t.CheckRunning(activity.Status)
  53. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_SHAKRB, activity.Id)
  54. t.CheckErr(err)
  55. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在")
  56. record := new(models.ShakeRedEnvelopeRecord) /// 之后使用, 存入乐观锁
  57. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { // 剔除摇过红包的用户
  58. exist, err := record.ExistRecord(activity.RehearsalId, activity.Id, rule.Id, envelope.Id, userId)
  59. t.CheckErr(err)
  60. t.Assert(!exist, code.MSG_SHAKERB_RECORD_NOT_EXIST, "您已经摇过红包了,请等待下一轮.") // 不存在继续往下走
  61. }
  62. // done: 增加概率, 修改概率
  63. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  64. t.Assert(r.Float64()*100 <= rule.Probability, code.MSG_ERR, "您与红包擦肩而过") // 必须在红包概率以内才能获取
  65. // 增加session 或者乐观锁
  66. exist, err = record.GetByRuleId(ruleId, activity.RehearsalId)
  67. t.CheckErr(err)
  68. t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_EXIST, "红包已经派发完毕")
  69. // 查询用户
  70. user := new(models.User)
  71. exist, err = models.GetById(user, userId)
  72. t.CheckErr(err)
  73. t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
  74. record.IsDraw = 0
  75. // 记录红包 ---> 非彩排才能
  76. if activity.RehearsalId == 0 {
  77. //res, err := pay_service.SendRedPack("欧轩互动", user.Openid, fmt.Sprintf("感谢您参加%s", activity.Name),
  78. // "", rule.Model, "摇得越快抢得越多", int(record.Amount*100), 1, 1)
  79. ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":")
  80. res, err := pay_service.Transfer("欧轩互动-红包活动", ip[0], user.Openid, int(record.Amount*100))
  81. if err != nil {
  82. t.ERROR("您与红包擦肩而过", code.MSG_SHAKERB_NOT_HIT)
  83. return
  84. }
  85. record.TransferType = 1
  86. record.TransferNo = res.PartnerTradeNo
  87. record.IsDraw = 1
  88. }
  89. record.UserId = user.Id
  90. record.AreaName = area.Name
  91. record.Name = activity.Name
  92. row, err := record.UpdateAllColsById(record.Id)
  93. t.CheckErr(err)
  94. t.Assert(row == 1, code.MSG_ERR, "您与红包擦肩而过") // 那么 row == 1 为已经成功抢到
  95. // 通知大屏
  96. total, err := new(models.ShakeRedEnvelopeRecord).Total(activity.Id, activity.RehearsalId, envelope.Id, rule.Id)
  97. remaining, err := new(models.ShakeRedEnvelopeRecord).Count(activity.Id, activity.RehearsalId, envelope.Id, rule.Id, -1)
  98. t.CheckErr(err)
  99. customer := new(models.Customer)
  100. exist, err = models.GetById(customer, customerId)
  101. t.CheckErr(err)
  102. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  103. go ws_send_service.SendShakeRedEnvelope(fmt.Sprintf("%d", activity.Id),
  104. define.TYPE_H5USER, userId, map[string]interface{}{
  105. "customer_id": customer.Id,
  106. "user_id": user.Id,
  107. "type": "shake_rb",
  108. "data": map[string]interface{}{
  109. "amount": record.Amount,
  110. "nickname": user.Nickname,
  111. "avatar": user.Avatar,
  112. "total": total - remaining, //
  113. "remaining": remaining, //
  114. },
  115. })
  116. go ws_send_service.SendShakeRedEnvelope(fmt.Sprintf("%d", activity.Id),
  117. define.TYPE_CUSTOMER, customerId, map[string]interface{}{
  118. "customer_id": customer.Id,
  119. "user_id": user.Id,
  120. "type": "shake_rb",
  121. "data": map[string]interface{}{
  122. "amount": record.Amount,
  123. "nickname": user.Nickname,
  124. "avatar": user.Avatar,
  125. "total": total - remaining, //
  126. "remaining": remaining, //
  127. },
  128. })
  129. t.JSON(map[string]interface{}{
  130. "amount": record.Amount,
  131. "shake_red_envelope_record_id": record.Id,
  132. })
  133. }
  134. // 用户红包
  135. func (t *ShakeRedEnvelopeCtl) UserRedEnvelopes() {
  136. records, err := models.GetRedEnvelopesByUserId(t.MustGetUID())
  137. t.CheckErr(err)
  138. t.JSON(map[string]interface{}{
  139. "list": records,
  140. "total": len(records),
  141. })
  142. }