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

419 lines
13 KiB

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