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

219 lines
6.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package pc
  2. import (
  3. "fmt"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. red_envelope_service "hudongzhuanjia/services/red_envelope"
  8. "hudongzhuanjia/utils"
  9. "hudongzhuanjia/utils/code"
  10. "hudongzhuanjia/utils/define"
  11. )
  12. type ShakeRedEnvelopeCtl struct {
  13. controllers.AuthorCtl
  14. }
  15. func (t *ShakeRedEnvelopeCtl) Ready() {
  16. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  17. rule := new(models.ShakeRedEnvelopeRule)
  18. exist, err := models.GetById(rule, ruleId)
  19. t.CheckErr(err)
  20. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包不存在")
  21. if rule.ShakeRedEnvelopeStatus != define.StatusNotBegin {
  22. t.ERROR(fmt.Sprintf("该活动%s", rule.ShakeRedEnvelopeStatus), code.MSG_ERR)
  23. }
  24. envelope := new(models.ShakeRedEnvelopeActivity)
  25. exist, err = models.GetById(envelope, rule.ShakeRedEnvelopeActivityId)
  26. t.CheckErr(err)
  27. t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
  28. activity := new(models.Activity)
  29. exist, err = models.GetById(activity, envelope.ActivityId)
  30. t.CheckErr(err)
  31. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  32. t.CheckRunning(activity.Status)
  33. // done: 把其他的准备中的状态改为未开始
  34. // 又一个坑, 红包的 轮次根据 activity_id
  35. sids := make([]int64, 0)
  36. err = core.GetXormAuto().Table(new(models.ShakeRedEnvelopeActivity)).Select("id").
  37. Where("is_delete=0 and activity_id=?", activity.Id).Find(&sids)
  38. t.CheckErr(err)
  39. _, err = rule.UpdateToStatusBySids(sids, define.StatusReady, define.StatusNotBegin)
  40. t.CheckErr(err)
  41. // 初始化红包
  42. err = red_envelope_service.GenRedEnvelope(activity.Id, activity.RehearsalId, rule)
  43. t.CheckErr(err)
  44. _, err = rule.UpdateStatus(ruleId, define.StatusReady)
  45. t.CheckErr(err)
  46. t.SUCCESS("success")
  47. }
  48. //开始摇红包
  49. func (t *ShakeRedEnvelopeCtl) Start() {
  50. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  51. //根据摇红包规则id查找该条记录并且更给状态
  52. rule := new(models.ShakeRedEnvelopeRule)
  53. exist, err := models.GetById(rule, ruleId)
  54. t.CheckErr(err)
  55. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包不存在")
  56. if rule.ShakeRedEnvelopeStatus != define.StatusReady {
  57. t.ERROR(fmt.Sprintf("该活动%s", rule.ShakeRedEnvelopeStatus), code.MSG_ERR)
  58. }
  59. _, err = rule.UpdateStatus(ruleId, define.StatusRunning)
  60. t.CheckErr(err)
  61. t.SUCCESS("success")
  62. }
  63. //停止摇红包
  64. func (t *ShakeRedEnvelopeCtl) Stop() {
  65. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  66. rule := new(models.ShakeRedEnvelopeRule)
  67. exist, err := models.GetById(rule, ruleId)
  68. t.CheckErr(err)
  69. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在")
  70. rule.ShakeRedEnvelopeStatus = define.StatusEnding
  71. _, err = rule.UpdateStatus(ruleId, rule.ShakeRedEnvelopeStatus)
  72. t.CheckErr(err)
  73. t.SUCCESS("success")
  74. }
  75. func (t *ShakeRedEnvelopeCtl) List() {
  76. activityId := t.MustGetInt64("activity_id")
  77. customerId := t.MustGetUID()
  78. customer := new(models.Customer)
  79. exist, err := models.GetById(customer, customerId)
  80. t.CheckErr(err)
  81. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  82. activity := new(models.Activity)
  83. exist, err = models.GetById(activity, activityId)
  84. t.CheckErr(err)
  85. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  86. t.CheckRunning(activity.Status)
  87. list := make([]*models.ShakeRedEnvelopeActivity, 0)
  88. total, err := core.GetXormAuto().Where("is_delete=0 and is_pay=1 and activity_id=?", activityId).FindAndCount(&list)
  89. t.CheckErr(err)
  90. aIds := make([]int64, 0)
  91. for _, v := range list {
  92. aIds = append(aIds, v.Id)
  93. }
  94. area := new(models.AreaStore)
  95. if customer.Pid == 0 {
  96. exist, err = area.GetMainAreaById(activityId)
  97. } else {
  98. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  99. }
  100. t.CheckErr(err)
  101. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  102. rules := make([]*models.ShakeRedEnvelopeRule, 0)
  103. err = core.GetXormAuto().Where("is_delete=0 and area_id=?", area.Id).In("shake_red_envelope_activity_id", aIds).Find(&rules)
  104. t.CheckErr(err)
  105. rIds := make([]int64, 0)
  106. for _, v := range rules {
  107. rIds = append(rIds, v.Id)
  108. }
  109. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  110. core.GetXormAuto().Where("is_delete=0").In("shake_red_envelope_rule_id", rIds).Find(&ladders)
  111. for index := range rules {
  112. for i := range ladders {
  113. if rules[index].Id == ladders[i].ShakeRedEnvelopeRuleId {
  114. rules[index].ShakeRedEnvelopeRuleLadders = append(rules[index].ShakeRedEnvelopeRuleLadders, ladders[i])
  115. }
  116. }
  117. }
  118. for index := range list {
  119. for i := range rules {
  120. if list[index].Id == rules[i].ShakeRedEnvelopeActivityId {
  121. list[index].ShakeRedEnvelopeRule = rules[i]
  122. break
  123. }
  124. }
  125. }
  126. for index := range rules {
  127. rules[index].Des = "根据对应得概率获得红包"
  128. }
  129. t.JSON(map[string]interface{}{
  130. "total": total,
  131. "list": list,
  132. })
  133. }
  134. //二维码
  135. func (t *ShakeRedEnvelopeCtl) Qrcode() {
  136. activityId := t.MustGetInt64("activity_id")
  137. customerId := t.MustGetUID()
  138. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  139. //将服务器得地址和activity_id,动态生成二维码
  140. qrcode, err := utils.GenH5Qrcode(define.H5ShakeRb, map[string]interface{}{
  141. "activity_id": activityId,
  142. "customer_id": customerId,
  143. "rule_id": ruleId,
  144. })
  145. t.CheckErr(err)
  146. t.JSON(map[string]interface{}{
  147. "qrcode": qrcode,
  148. })
  149. }
  150. type ShakeRedEnvelopeRecordAndUser struct {
  151. models.ShakeRedEnvelopeRecord `xorm:"extends"`
  152. User *models.User `json:"user" xorm:"extends"`
  153. }
  154. // 参数优化
  155. func (t *ShakeRedEnvelopeCtl) Winners() {
  156. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  157. rehearsalId := t.MustGetInt64("rehearsal_id")
  158. records := make([]*ShakeRedEnvelopeRecordAndUser, 0)
  159. err := core.GetXormAuto().Table(new(models.ShakeRedEnvelopeRecord)).Alias("r").
  160. Join("LEFT", models.AliasTableName(new(models.User), "u"), "r.user_id=u.id and u.is_delete=0").
  161. Where("r.shake_red_envelope_rule_id=? and is_draw<>-1 and r.is_delete=0 and r.rehearsal_id=?",
  162. ruleId, rehearsalId).OrderBy("r.created_at desc").Find(&records)
  163. t.CheckErr(err)
  164. t.JSON(map[string]interface{}{
  165. "total": len(records),
  166. "winners": records,
  167. })
  168. }
  169. // 统计摇红包参与人数和剩余红包数量
  170. func (t *ShakeRedEnvelopeCtl) Count() {
  171. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  172. rehearsalId := t.MustGetInt64("rehearsal_id")
  173. rule := new(models.ShakeRedEnvelopeRule)
  174. exist, err := models.GetById(rule, ruleId)
  175. t.CheckErr(err)
  176. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包轮次不存在")
  177. numU, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  178. ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeUser))
  179. t.CheckErr(err)
  180. numR, err := core.GetXormAuto().Where("is_delete=0 and is_draw=-1 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  181. ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeRecord))
  182. t.CheckErr(err)
  183. t.JSON(map[string]interface{}{
  184. "red_envelope_num": numR,
  185. "red_envelope_user": numU,
  186. })
  187. }