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

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