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

284 lines
9.0 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
  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. // 彩排不用发送到直播间
  74. if activity.RehearsalId == 0 {
  75. live := new(models.LiveConfig)
  76. exist, err := live.GetByActivityId(activity.Id)
  77. t.CheckErr(err)
  78. if exist && live.HaveModule(define.MODULE_SHAKRB) {
  79. go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackStart,
  80. map[string]interface{}{
  81. "customer_id": uid,
  82. "shake_red_envelope_rule_id": rule.Id,
  83. "timestamp": time.Now().Unix(),
  84. })
  85. }
  86. }
  87. t.SUCCESS("success")
  88. }
  89. //停止摇红包
  90. func (t *ShakeRedEnvelopeCtl) Stop() {
  91. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  92. uid := t.MustGetUID()
  93. rule := new(models.ShakeRedEnvelopeRule)
  94. exist, err := models.Get(rule, ruleId)
  95. t.CheckErr(err)
  96. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在")
  97. envelope := new(models.ShakeRedEnvelopeActivity)
  98. exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId)
  99. t.CheckErr(err)
  100. t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
  101. activity := new(models.Activity)
  102. exist, err = models.Get(activity, envelope.ActivityId)
  103. t.CheckErr(err)
  104. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  105. t.CheckRunning(activity.Status)
  106. rule.ShakeRedEnvelopeStatus = define.StatusEnding
  107. _, err = rule.UpdateStatus(ruleId, rule.ShakeRedEnvelopeStatus)
  108. t.CheckErr(err)
  109. if activity.RehearsalId == 0 {
  110. live := new(models.LiveConfig)
  111. exist, err := live.GetByActivityId(activity.Id)
  112. t.CheckErr(err)
  113. if exist && live.HaveModule(define.MODULE_SHAKRB) {
  114. go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackEnd,
  115. map[string]interface{}{
  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.MustGetInt64("activity_id")
  126. customerId := t.MustGetUID()
  127. customer := new(models.Customer)
  128. exist, err := models.Get(customer, customerId)
  129. t.CheckErr(err)
  130. t.Assert(exist, code.MSG_CUSTOMER_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([]int64, 0)
  140. for _, v := range list {
  141. aIds = append(aIds, v.Id)
  142. }
  143. area := new(models.AreaStore)
  144. if customer.Pid == 0 {
  145. exist, err = area.GetMainAreaById(activityId)
  146. } else {
  147. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  148. }
  149. t.CheckErr(err)
  150. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  151. rules := make([]*models.ShakeRedEnvelopeRule, 0)
  152. err = core.GetXormAuto().Where("is_delete=0 and area_id=?", area.Id).In("shake_red_envelope_activity_id", aIds).Find(&rules)
  153. t.CheckErr(err)
  154. rIds := make([]int64, 0)
  155. for _, v := range rules {
  156. rIds = append(rIds, v.Id)
  157. }
  158. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  159. core.GetXormAuto().Where("is_delete=0").In("shake_red_envelope_rule_id", rIds).Find(&ladders)
  160. for index := range rules {
  161. for i := range ladders {
  162. if rules[index].Id == ladders[i].ShakeRedEnvelopeRuleId {
  163. rules[index].ShakeRedEnvelopeRuleLadders = append(rules[index].ShakeRedEnvelopeRuleLadders, ladders[i])
  164. }
  165. }
  166. }
  167. for index := range list {
  168. for i := range rules {
  169. if list[index].Id == rules[i].ShakeRedEnvelopeActivityId {
  170. list[index].ShakeRedEnvelopeRule = rules[i]
  171. break
  172. }
  173. }
  174. }
  175. for index := range rules {
  176. rules[index].Des = "根据对应得概率获得红包"
  177. }
  178. t.JSON(map[string]interface{}{
  179. "total": total,
  180. "list": list,
  181. })
  182. }
  183. //二维码
  184. func (t *ShakeRedEnvelopeCtl) Qrcode() {
  185. activityId := t.MustGetInt64("activity_id")
  186. customerId := t.MustGetUID()
  187. rehearsalId := t.MustGetInt64("rehearsal_id")
  188. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  189. area := new(models.AreaStore)
  190. exist, err := area.GetByCustomerId(customerId, activityId)
  191. t.CheckErr(err)
  192. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  193. //将服务器得地址和activity_id,动态生成二维码
  194. qrcode, err := utils.GenH5Qrcode(define.H5ShakeRb, map[string]interface{}{
  195. "activity_id": activityId,
  196. "area_id": area.Id,
  197. "customer_id": customerId,
  198. "rule_id": ruleId,
  199. "rehearsal_id": rehearsalId,
  200. })
  201. t.CheckErr(err)
  202. t.JSON(map[string]interface{}{
  203. "qrcode": qrcode,
  204. })
  205. }
  206. type ShakeRedEnvelopeRecordAndUser struct {
  207. models.ShakeRedEnvelopeRecord `xorm:"extends"`
  208. User *models.User `json:"user" xorm:"extends"`
  209. }
  210. // 参数优化
  211. func (t *ShakeRedEnvelopeCtl) Winners() {
  212. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  213. rehearsalId := t.MustGetInt64("rehearsal_id")
  214. records := make([]*ShakeRedEnvelopeRecordAndUser, 0)
  215. err := core.GetXormAuto().Table(new(models.ShakeRedEnvelopeRecord)).Alias("r").
  216. Join("LEFT", models.AliasTableName(new(models.User), "u"), "r.user_id=u.id and u.is_delete=0").
  217. Where("r.shake_red_envelope_rule_id=? and is_draw<>-1 and r.is_delete=0 and r.rehearsal_id=?",
  218. ruleId, rehearsalId).OrderBy("r.created_at desc").Find(&records)
  219. t.CheckErr(err)
  220. t.JSON(map[string]interface{}{
  221. "total": len(records),
  222. "winners": records,
  223. })
  224. }
  225. // 统计摇红包参与人数和剩余红包数量
  226. func (t *ShakeRedEnvelopeCtl) Count() {
  227. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  228. rehearsalId := t.MustGetInt64("rehearsal_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. numU, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  234. ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeUser))
  235. t.CheckErr(err)
  236. numR, err := core.GetXormAuto().Where("is_delete=0 and is_draw=-1 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  237. ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeRecord))
  238. t.CheckErr(err)
  239. t.JSON(map[string]interface{}{
  240. "red_envelope_num": numR,
  241. "red_envelope_user": numU,
  242. })
  243. }