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

299 lines
9.6 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
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. 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. _, err = rule.UpdateStatus(ruleId, define.StatusReady)
  42. t.CheckErr(err)
  43. t.SUCCESS("success")
  44. }
  45. //开始摇红包
  46. func (t *ShakeRedEnvelopeCtl) Start() {
  47. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  48. uid := t.MustGetUID()
  49. //根据摇红包规则id查找该条记录并且更给状态
  50. rule := new(models.ShakeRedEnvelopeRule)
  51. exist, err := models.Get(rule, ruleId)
  52. t.CheckErr(err)
  53. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包不存在")
  54. envelope := new(models.ShakeRedEnvelopeActivity)
  55. exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId)
  56. t.CheckErr(err)
  57. t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
  58. activity := new(models.Activity)
  59. exist, err = models.Get(activity, envelope.ActivityId)
  60. t.CheckErr(err)
  61. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  62. t.CheckRunning(activity.Status)
  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. err = red_envelope_service.GenRedEnvelope(activity.Id, activity.Name, activity.RehearsalId, rule, activity.ArchId)
  70. t.CheckErr(err)
  71. // 彩排不用发送到直播间
  72. if activity.RehearsalId == 0 {
  73. live := new(models.LiveConfig)
  74. exist, err := live.GetByActivityId(activity.Id)
  75. t.CheckErr(err)
  76. if exist && live.HaveModule(define.MODULE_SHAKRB) {
  77. go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackStart,
  78. map[string]interface{}{
  79. "customer_id": uid,
  80. "shake_red_envelope_rule_id": rule.Id,
  81. "timestamp": time.Now().Unix(),
  82. })
  83. }
  84. }
  85. t.SUCCESS("success")
  86. }
  87. //停止摇红包
  88. func (t *ShakeRedEnvelopeCtl) Stop() {
  89. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  90. uid := t.MustGetUID()
  91. rule := new(models.ShakeRedEnvelopeRule)
  92. exist, err := models.Get(rule, ruleId)
  93. t.CheckErr(err)
  94. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在")
  95. envelope := new(models.ShakeRedEnvelopeActivity)
  96. exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId)
  97. t.CheckErr(err)
  98. t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
  99. activity := new(models.Activity)
  100. exist, err = models.Get(activity, envelope.ActivityId)
  101. t.CheckErr(err)
  102. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  103. t.CheckRunning(activity.Status)
  104. rule.ShakeRedEnvelopeStatus = define.StatusEnding
  105. _, err = rule.UpdateStatus(ruleId, rule.ShakeRedEnvelopeStatus)
  106. t.CheckErr(err)
  107. if activity.RehearsalId == 0 {
  108. live := new(models.LiveConfig)
  109. exist, err := live.GetByActivityId(activity.Id)
  110. t.CheckErr(err)
  111. if exist && live.HaveModule(define.MODULE_SHAKRB) {
  112. go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackEnd,
  113. map[string]interface{}{
  114. "customer_id": uid,
  115. "shake_red_envelope_rule_id": rule.Id,
  116. "timestamp": time.Now().Unix(),
  117. })
  118. }
  119. }
  120. t.SUCCESS("success")
  121. }
  122. func (t *ShakeRedEnvelopeCtl) List() {
  123. activityId := t.MustGetInt64("activity_id")
  124. customerId := t.MustGetUID()
  125. area := new(models.AreaStore)
  126. exist, err := area.GetByCustomerId(customerId, activityId)
  127. t.CheckErr(err)
  128. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  129. activity := new(models.Activity)
  130. exist, err = models.Get(activity, activityId)
  131. t.CheckErr(err)
  132. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  133. t.CheckRunning(activity.Status)
  134. list := make([]*models.ShakeRedEnvelopeActivity, 0)
  135. total, err := core.GetXormAuto().Where("is_delete=0 and is_pay=1 and activity_id=?", activityId).FindAndCount(&list)
  136. t.CheckErr(err)
  137. aIds := make([]int64, 0)
  138. for _, v := range list {
  139. aIds = append(aIds, v.Id)
  140. }
  141. rules := make([]*models.ShakeRedEnvelopeRule, 0)
  142. err = core.GetXormAuto().Where("is_delete=0 and area_id=?", area.Id).In("shake_red_envelope_activity_id", aIds).Find(&rules)
  143. t.CheckErr(err)
  144. rIds := make([]int64, 0)
  145. for _, v := range rules {
  146. rIds = append(rIds, v.Id)
  147. }
  148. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  149. core.GetXormAuto().Where("is_delete=0").In("shake_red_envelope_rule_id", rIds).Find(&ladders)
  150. for index := range rules {
  151. for i := range ladders {
  152. if rules[index].Id == ladders[i].ShakeRedEnvelopeRuleId {
  153. rules[index].ShakeRedEnvelopeRuleLadders = append(rules[index].ShakeRedEnvelopeRuleLadders, ladders[i])
  154. }
  155. }
  156. }
  157. for index := range list {
  158. for i := range rules {
  159. if list[index].Id == rules[i].ShakeRedEnvelopeActivityId {
  160. list[index].ShakeRedEnvelopeRule = rules[i]
  161. break
  162. }
  163. }
  164. }
  165. for index := range rules {
  166. rules[index].Des = "根据对应得概率获得红包"
  167. }
  168. t.JSON(map[string]interface{}{
  169. "total": total,
  170. "list": list,
  171. })
  172. }
  173. //二维码
  174. func (t *ShakeRedEnvelopeCtl) Qrcode() {
  175. activityId := t.MustGetInt64("activity_id")
  176. customerId := t.MustGetUID()
  177. rehearsalId := t.MustGetInt64("rehearsal_id")
  178. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  179. area := new(models.AreaStore)
  180. exist, err := area.GetByCustomerId(customerId, activityId)
  181. t.CheckErr(err)
  182. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  183. //将服务器得地址和activity_id,动态生成二维码
  184. qrcode, err := utils.GenH5Qrcode(define.H5ShakeRb, map[string]interface{}{
  185. "activity_id": activityId,
  186. "area_id": area.Id,
  187. "customer_id": customerId,
  188. "rule_id": ruleId,
  189. "rehearsal_id": rehearsalId,
  190. })
  191. t.CheckErr(err)
  192. t.JSON(map[string]interface{}{
  193. "qrcode": qrcode,
  194. })
  195. }
  196. type ShakeRedEnvelopeRecordAndUser struct {
  197. models.ShakeRedEnvelopeRecord `xorm:"extends"`
  198. User *models.User `json:"user" xorm:"extends"`
  199. }
  200. // 参数优化
  201. func (t *ShakeRedEnvelopeCtl) Winners() {
  202. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  203. rule := &models.ShakeRedEnvelopeRule{}
  204. exist, err := models.Get(rule, ruleId)
  205. t.CheckErr(err)
  206. t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则不存在")
  207. redEnvelope := &models.ShakeRedEnvelopeActivity{}
  208. exist, err = models.Get(redEnvelope, rule.ShakeRedEnvelopeActivityId)
  209. t.CheckErr(err)
  210. t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包活动不存在")
  211. activity := &models.Activity{}
  212. exist, err = models.Get(activity, redEnvelope.ActivityId)
  213. t.CheckErr(err)
  214. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  215. records := make([]*ShakeRedEnvelopeRecordAndUser, 0)
  216. err = core.GetXormAuto().Table(new(models.ShakeRedEnvelopeRecord)).Alias("r").
  217. Join("LEFT", models.Alias(new(models.User), "u"), "r.user_id=u.id and u.is_delete=0").
  218. Where("r.shake_red_envelope_rule_id=? and is_draw<>-1 and r.is_delete=0 and r.rehearsal_id=? and r.arch_id=?",
  219. ruleId, activity.RehearsalId, activity.ArchId).OrderBy("r.created_at desc").Find(&records)
  220. t.CheckErr(err)
  221. t.JSON(map[string]interface{}{
  222. "total": len(records),
  223. "winners": records,
  224. })
  225. }
  226. // 统计摇红包参与人数和剩余红包数量
  227. func (t *ShakeRedEnvelopeCtl) Count() {
  228. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  229. rule := new(models.ShakeRedEnvelopeRule)
  230. exist, err := models.Get(rule, ruleId)
  231. t.CheckErr(err)
  232. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包轮次不存在")
  233. redEnvelope := &models.ShakeRedEnvelopeActivity{}
  234. exist, err = models.Get(redEnvelope, rule.ShakeRedEnvelopeActivityId)
  235. t.CheckErr(err)
  236. t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包活动不存在")
  237. activity := &models.Activity{}
  238. exist, err = models.Get(activity, redEnvelope.ActivityId)
  239. t.CheckErr(err)
  240. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  241. numU, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? and rehearsal_id=? and arch_id=?",
  242. ruleId, activity.RehearsalId, activity.ArchId).Count(new(models.ShakeRedEnvelopeUser))
  243. t.CheckErr(err)
  244. numR, err := core.GetXormAuto().Where("is_delete=0 and is_draw=-1 and shake_red_envelope_rule_id=? "+
  245. " and rehearsal_id=? and arch_id=?", ruleId, activity.RehearsalId, activity.ArchId).
  246. Count(new(models.ShakeRedEnvelopeRecord))
  247. t.CheckErr(err)
  248. t.JSON(map[string]interface{}{
  249. "red_envelope_num": numR,
  250. "red_envelope_user": numU,
  251. })
  252. }