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

336 lines
10 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
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. activity_service "hudongzhuanjia/services/activity"
  7. im_service "hudongzhuanjia/services/im"
  8. lottery_service "hudongzhuanjia/services/lottery"
  9. "hudongzhuanjia/utils/code"
  10. "hudongzhuanjia/utils/define"
  11. "time"
  12. )
  13. //抽奖
  14. type LotteryDrawCtl struct {
  15. controllers.AuthorCtl
  16. }
  17. //开始抽奖
  18. func (t *LotteryDrawCtl) Start() {
  19. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  20. activityId := t.MustGetInt64("activity_id")
  21. ladder := new(models.LotteryDrawRuleLadder)
  22. exist, err := models.Get(ladder, ladderId)
  23. t.CheckErr(err)
  24. t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
  25. if ladder.Status != define.StatusNotBegin {
  26. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  27. }
  28. ladder.Status = define.StatusRunning
  29. ladder.UpdatedAt = time.Now()
  30. ladder.RollNum = 0
  31. _, err = models.Update(ladder.Id, ladder, "status", "roll_num", "updated_at")
  32. t.CheckErr(err)
  33. // 通知直播开始抽奖
  34. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStart,
  35. map[string]interface{}{
  36. "lottery_draw_ladder_id": ladder.Id,
  37. "timestamp": time.Now().Unix(),
  38. "desc": "开始抽奖",
  39. })
  40. t.SUCCESS("操作成功")
  41. }
  42. // 滚动
  43. func (t *LotteryDrawCtl) StartRoll() {
  44. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  45. activityId := t.MustGetInt64("activity_id")
  46. num := t.MustGetInt64("num")
  47. ladder := new(models.LotteryDrawRuleLadder)
  48. exist, err := models.Get(ladder, ladderId)
  49. t.CheckErr(err)
  50. t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
  51. if ladder.Status != define.StatusRunning {
  52. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  53. }
  54. ladder.RollNum += 1
  55. _, err = models.Update(ladder.Id, ladder, "roll_num")
  56. // 通知直播开始抽奖
  57. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawRollStart,
  58. map[string]interface{}{
  59. "lottery_draw_ladder_id": ladder.Id,
  60. "timestamp": time.Now().Unix(),
  61. "roll_num": ladder.RollNum,
  62. "desc": "开始滚动",
  63. "number": num,
  64. })
  65. t.SUCCESS("操作成功")
  66. }
  67. func (t *LotteryDrawCtl) StopRoll() {
  68. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  69. activityId := t.MustGetInt64("activity_id")
  70. num := t.MustGetInt64("num")
  71. ladder := new(models.LotteryDrawRuleLadder)
  72. exist, err := models.Get(ladder, ladderId)
  73. t.CheckErr(err)
  74. t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
  75. if ladder.Status != define.StatusRunning {
  76. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  77. }
  78. // 通知直播开始抽奖
  79. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawRollStop,
  80. map[string]interface{}{
  81. "lottery_draw_ladder_id": ladder.Id,
  82. "timestamp": time.Now().Unix(),
  83. "roll_num": ladder.RollNum,
  84. "desc": "停止滚动",
  85. "number": num,
  86. })
  87. t.SUCCESS("操作成功")
  88. }
  89. //停止抽奖
  90. func (t *LotteryDrawCtl) Stop() {
  91. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  92. activityId := t.MustGetInt64("activity_id")
  93. ladder := new(models.LotteryDrawRuleLadder)
  94. exist, err := models.Get(ladder, ladderId)
  95. t.CheckErr(err)
  96. t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
  97. if ladder.Status != define.StatusRunning {
  98. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  99. }
  100. ladder.Status = define.StatusEnding
  101. ladder.UpdatedAt = time.Now()
  102. ladder.RollNum = 0
  103. _, err = models.Update(ladder.Id, ladder, "status", "updated_at", "roll_num")
  104. t.CheckErr(err)
  105. // 通知直播开始抽奖
  106. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStop,
  107. map[string]interface{}{
  108. "lottery_draw_ladder_id": ladder.Id,
  109. "timestamp": time.Now().Unix(),
  110. "desc": "结束抽奖",
  111. })
  112. t.SUCCESS("操作成功")
  113. }
  114. //获取所有抽奖活动列表
  115. func (t *LotteryDrawCtl) List() {
  116. activityId := t.MustGetInt64("activity_id")
  117. customerId := t.MustGetUID()
  118. customer := new(models.Customer)
  119. exist, err := models.Get(customer, customerId)
  120. t.CheckErr(err)
  121. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  122. activity := new(models.Activity)
  123. exist, err = models.Get(activity, activityId)
  124. t.CheckErr(err)
  125. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  126. result, err := lottery_service.GetLotteryAndLadder(activityId, activity.RehearsalId)
  127. t.JSON(map[string]interface{}{
  128. "total": len(result),
  129. "list": result,
  130. })
  131. }
  132. //抽奖奖品
  133. func (t *LotteryDrawCtl) Prize() {
  134. ruleId := t.MustGetInt64("lottery_draw_rule_id")
  135. list, err := models.GetLotteryDrawLadderByRuleId(ruleId)
  136. t.CheckErr(err)
  137. for index := range list {
  138. list[index].Des = "在该活动的所有用户中随机抽奖品数量的用户"
  139. }
  140. t.JSON(map[string]interface{}{
  141. "total": len(list),
  142. "lise": list,
  143. })
  144. }
  145. // 抽奖用户
  146. func (t *LotteryDrawCtl) Users() {
  147. activityId := t.MustGetInt64("activity_id")
  148. ruleId := t.MustGetInt64("lottery_draw_rule_id")
  149. customerId := t.MustGetUID()
  150. customer := new(models.Customer)
  151. exist, err := models.Get(customer, customerId)
  152. t.CheckErr(err)
  153. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  154. activity := new(models.Activity)
  155. exist, err = models.Get(activity, activityId)
  156. t.CheckErr(err)
  157. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  158. area := new(models.AreaStore)
  159. exist, err = area.GetByCustomerId(customerId, activityId)
  160. t.CheckErr(err)
  161. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  162. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_LOTTERY, activityId)
  163. t.CheckErr(err)
  164. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在")
  165. recordIds := make([]int64, 0)
  166. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  167. // 去重标志
  168. recordIds, err = models.GetUserIdsByLotteryDrawRuleId(ruleId, activity.RehearsalId, area.Id)
  169. t.CheckErr(err)
  170. }
  171. result, err := lottery_service.GetLotteryUsersResult(area.Id, activity.Id, activity.RehearsalId, recordIds)
  172. t.CheckErr(err)
  173. t.JSON(map[string]interface{}{
  174. "total": len(result),
  175. "list": result,
  176. })
  177. }
  178. //抽奖动作
  179. func (t *LotteryDrawCtl) Lottery() {
  180. activityId := t.MustGetInt64("activity_id")
  181. ruleId := t.MustGetInt64("lottery_draw_rule_id")
  182. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  183. number := t.MustGetInt("number") // 奖品数量
  184. customerId := t.MustGetUID()
  185. customer := new(models.Customer)
  186. exist, err := models.Get(customer, customerId)
  187. t.CheckErr(err)
  188. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  189. activity := new(models.Activity)
  190. exist, err = models.Get(activity, activityId)
  191. t.CheckErr(err)
  192. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  193. t.CheckRunning(activity.Status)
  194. // 多地区设置
  195. area := new(models.AreaStore)
  196. exist, err = area.GetByCustomerId(customerId, activityId)
  197. t.CheckErr(err)
  198. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  199. rule := new(models.LotteryDrawRule)
  200. exist, err = models.Get(rule, ruleId)
  201. t.CheckErr(err)
  202. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "抽奖规则不存在")
  203. // 查询奖品
  204. ladder := new(models.LotteryDrawRuleLadder)
  205. exist, err = models.Get(ladder, ladderId)
  206. t.CheckErr(err)
  207. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "抽奖等级不存在")
  208. t.CheckRunning(ladder.Status)
  209. count, err := new(models.LotteryDrawRecord).CountRecord(ruleId, ladder.Id, activity.RehearsalId, area.Id)
  210. t.CheckErr(err)
  211. prizeNum := ladder.PrizeNumber - int(count)
  212. if prizeNum <= 0 || prizeNum < number { // 需要抽奖的数量比数据库存在数量多
  213. t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH)
  214. }
  215. module, exist, err := activity_service.GetModuleService(define.MODULE_LOTTERY, activityId)
  216. t.CheckErr(err)
  217. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在")
  218. // 取设置
  219. userIds, err := lottery_service.GetLotteryUserIds(module.BesideRepeat, activityId, ruleId, ladder.Id, activity.RehearsalId, area.Id)
  220. if len(userIds) < number {
  221. t.ERROR("抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH)
  222. }
  223. lottery_service.RandLotteryUserIds(userIds) // 打乱需要中奖人员
  224. winnerIds := userIds[:number]
  225. winners, err := lottery_service.GetLotteryUsers(winnerIds)
  226. t.CheckErr(err)
  227. t.CheckErr(err)
  228. for i := range winners {
  229. // 补全信息
  230. winners[i].PrizeName = ladder.PrizeName
  231. winners[i].LadderId = ladder.Id
  232. winners[i].PrizeImg = ladder.PrizeImg
  233. // 普通抽奖
  234. userPrize := new(models.UserPrize)
  235. userPrize.ActivityId = activityId
  236. userPrize.RehearsalId = activity.RehearsalId
  237. userPrize.ActivityName = activity.Name
  238. userPrize.UserId = winners[i].UserId
  239. userPrize.PrizeImg = ladder.PrizeImg
  240. userPrize.PrizeName = ladder.PrizeName
  241. userPrize.PrizeType = 1
  242. userPrize.IsDelete = false
  243. userPrize.CreatedAt = time.Now()
  244. userPrize.UpdatedAt = time.Now()
  245. _, err = models.Add(userPrize)
  246. t.CheckErr(err)
  247. record := new(models.LotteryDrawRecord)
  248. record.UserPrizeId = userPrize.Id
  249. record.ActivityId = activityId
  250. record.RehearsalId = activity.RehearsalId
  251. record.LotteryDrawActivityId = rule.LotteryDrawActivityId
  252. record.LotteryDrawRuleId = rule.Id
  253. record.UserId = winners[i].UserId
  254. record.UserName = winners[i].Username
  255. record.UserPhone = winners[i].UserPhone
  256. record.LotteryDrawRuleLadderId = ladder.Id
  257. record.PrizeName = ladder.PrizeName
  258. record.AreaId = area.Id
  259. record.AreaName = area.Name
  260. record.RollNum = ladder.RollNum
  261. record.IsDelete = false
  262. record.CreatedAt = time.Now()
  263. record.UpdatedAt = time.Now()
  264. _, err = models.Add(record)
  265. t.CheckErr(err)
  266. }
  267. // 通知直播开始抽奖
  268. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawResult,
  269. map[string]interface{}{
  270. "lottery_draw_ladder_id": ladder.Id,
  271. "winners": winners,
  272. "roll_num": ladder.RollNum,
  273. "timestamp": time.Now().Unix(),
  274. "desc": "抽奖结果",
  275. })
  276. t.JSON(winners)
  277. }
  278. //获取中奖名单
  279. func (t *LotteryDrawCtl) ListOfWinners() {
  280. ruleId := t.MustGetInt64("lottery_draw_rule_id")
  281. rehearsalId := t.MustGetInt64("rehearsal_id")
  282. result, err := lottery_service.GetWinnersResult(ruleId, rehearsalId)
  283. t.CheckErr(err)
  284. t.JSON(map[string]interface{}{
  285. "total": len(result),
  286. "list": result,
  287. })
  288. }