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

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