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

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