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

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