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

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