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

213 lines
6.7 KiB

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. activity := new(models.Activity)
  78. exist, err := models.GetById(activity, activityId)
  79. t.CheckErr(err)
  80. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  81. t.CheckRunning(activity.Status)
  82. list := make([]*models.ShakeRedEnvelopeActivity, 0)
  83. total, err := core.GetXormAuto().Where("is_delete=0 and is_pay=1 and activity_id=?", activityId).FindAndCount(&list)
  84. t.CheckErr(err)
  85. aIds := make([]int64, 0)
  86. for _, v := range list {
  87. aIds = append(aIds, v.Id)
  88. }
  89. area := new(models.AreaStore)
  90. if t.MustGetCustomerPid() == 0 {
  91. exist, err = area.GetMainAreaById(activityId)
  92. } else {
  93. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  94. }
  95. t.CheckErr(err)
  96. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  97. rules := make([]*models.ShakeRedEnvelopeRule, 0)
  98. err = core.GetXormAuto().Where("is_delete=0 and area_id=?", area.Id).In("shake_red_envelope_activity_id", aIds).Find(&rules)
  99. t.CheckErr(err)
  100. rIds := make([]int64, 0)
  101. for _, v := range rules {
  102. rIds = append(rIds, v.Id)
  103. }
  104. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  105. core.GetXormAuto().Where("is_delete=0").In("shake_red_envelope_rule_id", rIds).Find(&ladders)
  106. for index := range rules {
  107. for i := range ladders {
  108. if rules[index].Id == ladders[i].ShakeRedEnvelopeRuleId {
  109. rules[index].ShakeRedEnvelopeRuleLadders = append(rules[index].ShakeRedEnvelopeRuleLadders, ladders[i])
  110. }
  111. }
  112. }
  113. for index := range list {
  114. for i := range rules {
  115. if list[index].Id == rules[i].ShakeRedEnvelopeActivityId {
  116. list[index].ShakeRedEnvelopeRule = rules[i]
  117. break
  118. }
  119. }
  120. }
  121. for index := range rules {
  122. rules[index].Des = "根据对应得概率获得红包"
  123. }
  124. t.JSON(map[string]interface{}{
  125. "total": total,
  126. "list": list,
  127. })
  128. }
  129. //二维码
  130. func (t *ShakeRedEnvelopeCtl) Qrcode() {
  131. activityId := t.MustGetInt64("activity_id")
  132. customerId := t.MustGetUID()
  133. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  134. //将服务器得地址和activity_id,动态生成二维码
  135. qrcode, err := utils.GenH5Qrcode(define.H5ShakeRb, map[string]interface{}{
  136. "activity_id": activityId,
  137. "customer_id": customerId,
  138. "rule_id": ruleId,
  139. })
  140. t.CheckErr(err)
  141. t.JSON(map[string]interface{}{
  142. "qrcode": qrcode,
  143. })
  144. }
  145. type ShakeRedEnvelopeRecordAndUser struct {
  146. models.ShakeRedEnvelopeRecord `xorm:"extends"`
  147. User *models.User `json:"user" xorm:"extends"`
  148. }
  149. // 参数优化
  150. func (t *ShakeRedEnvelopeCtl) Winners() {
  151. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  152. rehearsalId := t.MustGetInt64("rehearsal_id")
  153. records := make([]*ShakeRedEnvelopeRecordAndUser, 0)
  154. err := core.GetXormAuto().Table(new(models.ShakeRedEnvelopeRecord)).Alias("r").
  155. Join("LEFT", models.AliasTableName(new(models.User), "u"), "r.user_id=u.id and u.is_delete=0").
  156. Where("r.shake_red_envelope_rule_id=? and is_draw<>-1 and r.is_delete=0 and r.rehearsal_id=?",
  157. ruleId, rehearsalId).OrderBy("r.created_at desc").Find(&records)
  158. t.CheckErr(err)
  159. t.JSON(map[string]interface{}{
  160. "total": len(records),
  161. "winners": records,
  162. })
  163. }
  164. // 统计摇红包参与人数和剩余红包数量
  165. func (t *ShakeRedEnvelopeCtl) Count() {
  166. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  167. rehearsalId := t.MustGetInt64("rehearsal_id")
  168. rule := new(models.ShakeRedEnvelopeRule)
  169. exist, err := models.GetById(rule, ruleId)
  170. t.CheckErr(err)
  171. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包轮次不存在")
  172. numU, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  173. ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeUser))
  174. t.CheckErr(err)
  175. numR, err := core.GetXormAuto().Where("is_delete=0 and is_draw=-1 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  176. ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeRecord))
  177. t.CheckErr(err)
  178. t.JSON(map[string]interface{}{
  179. "red_envelope_num": numR,
  180. "red_envelope_user": numU,
  181. })
  182. }