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

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