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

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