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

274 lines
8.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
  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. go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackStart,
  76. map[string]interface{}{
  77. "customer_id": uid,
  78. "shake_red_envelope_rule_id": rule.Id,
  79. "timestamp": time.Now().Unix(),
  80. })
  81. }
  82. t.SUCCESS("success")
  83. }
  84. //停止摇红包
  85. func (t *ShakeRedEnvelopeCtl) Stop() {
  86. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  87. uid := t.MustGetUID()
  88. rule := new(models.ShakeRedEnvelopeRule)
  89. exist, err := models.Get(rule, ruleId)
  90. t.CheckErr(err)
  91. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在")
  92. envelope := new(models.ShakeRedEnvelopeActivity)
  93. exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId)
  94. t.CheckErr(err)
  95. t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
  96. activity := new(models.Activity)
  97. exist, err = models.Get(activity, envelope.ActivityId)
  98. t.CheckErr(err)
  99. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  100. t.CheckRunning(activity.Status)
  101. rule.ShakeRedEnvelopeStatus = define.StatusEnding
  102. _, err = rule.UpdateStatus(ruleId, rule.ShakeRedEnvelopeStatus)
  103. t.CheckErr(err)
  104. if activity.RehearsalId == 0 {
  105. go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im_service.NoticeShakeRedPackEnd,
  106. map[string]interface{}{
  107. "customer_id": uid,
  108. "shake_red_envelope_rule_id": rule.Id,
  109. "timestamp": time.Now().Unix(),
  110. })
  111. }
  112. t.SUCCESS("success")
  113. }
  114. func (t *ShakeRedEnvelopeCtl) List() {
  115. activityId := t.MustGetInt64("activity_id")
  116. customerId := t.MustGetUID()
  117. customer := new(models.Customer)
  118. exist, err := models.Get(customer, customerId)
  119. t.CheckErr(err)
  120. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  121. activity := new(models.Activity)
  122. exist, err = models.Get(activity, activityId)
  123. t.CheckErr(err)
  124. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  125. t.CheckRunning(activity.Status)
  126. list := make([]*models.ShakeRedEnvelopeActivity, 0)
  127. total, err := core.GetXormAuto().Where("is_delete=0 and is_pay=1 and activity_id=?", activityId).FindAndCount(&list)
  128. t.CheckErr(err)
  129. aIds := make([]int64, 0)
  130. for _, v := range list {
  131. aIds = append(aIds, v.Id)
  132. }
  133. area := new(models.AreaStore)
  134. if customer.Pid == 0 {
  135. exist, err = area.GetMainAreaById(activityId)
  136. } else {
  137. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  138. }
  139. t.CheckErr(err)
  140. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  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. rehearsalId := t.MustGetInt64("rehearsal_id")
  204. records := make([]*ShakeRedEnvelopeRecordAndUser, 0)
  205. err := core.GetXormAuto().Table(new(models.ShakeRedEnvelopeRecord)).Alias("r").
  206. Join("LEFT", models.AliasTableName(new(models.User), "u"), "r.user_id=u.id and u.is_delete=0").
  207. Where("r.shake_red_envelope_rule_id=? and is_draw<>-1 and r.is_delete=0 and r.rehearsal_id=?",
  208. ruleId, rehearsalId).OrderBy("r.created_at desc").Find(&records)
  209. t.CheckErr(err)
  210. t.JSON(map[string]interface{}{
  211. "total": len(records),
  212. "winners": records,
  213. })
  214. }
  215. // 统计摇红包参与人数和剩余红包数量
  216. func (t *ShakeRedEnvelopeCtl) Count() {
  217. ruleId := t.MustGetInt64("shake_red_envelope_rule_id")
  218. rehearsalId := t.MustGetInt64("rehearsal_id")
  219. rule := new(models.ShakeRedEnvelopeRule)
  220. exist, err := models.Get(rule, ruleId)
  221. t.CheckErr(err)
  222. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包轮次不存在")
  223. numU, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  224. ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeUser))
  225. t.CheckErr(err)
  226. numR, err := core.GetXormAuto().Where("is_delete=0 and is_draw=-1 and shake_red_envelope_rule_id=? and rehearsal_id=?",
  227. ruleId, rehearsalId).Count(new(models.ShakeRedEnvelopeRecord))
  228. t.CheckErr(err)
  229. t.JSON(map[string]interface{}{
  230. "red_envelope_num": numR,
  231. "red_envelope_user": numU,
  232. })
  233. }