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

492 lines
16 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
  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. "github.com/ouxuanserver/osmanthuswine/src/core"
  13. )
  14. //抽奖
  15. type LotteryDrawCtl struct {
  16. controllers.AuthorCtl
  17. }
  18. //开始抽奖
  19. func (t *LotteryDrawCtl) Start() {
  20. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  21. activityId := t.MustGetInt64("activity_id")
  22. ladder := new(models.LotteryDrawRuleLadder)
  23. exist, err := models.Get(ladder, ladderId)
  24. t.CheckErr(err)
  25. t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
  26. if ladder.Status != define.StatusNotBegin {
  27. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  28. }
  29. ladder.Status = define.StatusRunning
  30. ladder.UpdatedAt = time.Now()
  31. t.CheckErr(models.Save(map[string]interface{}{
  32. "id=": ladderId,
  33. }, ladder, "status", "updated_at"))
  34. // 通知直播开始抽奖
  35. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStart,
  36. map[string]interface{}{
  37. "lottery_draw_ladder_id": ladder.Id,
  38. "timestamp": time.Now().Unix(),
  39. "desc": "开始抽奖",
  40. })
  41. t.SUCCESS("操作成功")
  42. }
  43. // 滚动
  44. func (t *LotteryDrawCtl) StartRoll() {
  45. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  46. activityId := t.MustGetInt64("activity_id")
  47. num := t.MustGetInt64("num")
  48. ladder := new(models.LotteryDrawRuleLadder)
  49. exist, err := models.Get(ladder, ladderId)
  50. t.CheckErr(err)
  51. t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
  52. if ladder.Status != define.StatusRunning {
  53. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  54. }
  55. // 通知直播开始抽奖
  56. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawRollStart,
  57. map[string]interface{}{
  58. "lottery_draw_ladder_id": ladder.Id,
  59. "timestamp": time.Now().Unix(),
  60. "desc": "开始滚动",
  61. "number": num,
  62. })
  63. t.SUCCESS("操作成功")
  64. }
  65. func (t *LotteryDrawCtl) StopRoll() {
  66. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  67. activityId := t.MustGetInt64("activity_id")
  68. num := t.MustGetInt64("num")
  69. ladder := new(models.LotteryDrawRuleLadder)
  70. exist, err := models.Get(ladder, ladderId)
  71. t.CheckErr(err)
  72. t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
  73. if ladder.Status != define.StatusRunning {
  74. t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR)
  75. }
  76. // 通知直播开始抽奖
  77. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawRollStop,
  78. map[string]interface{}{
  79. "lottery_draw_ladder_id": ladder.Id,
  80. "timestamp": time.Now().Unix(),
  81. "desc": "停止滚动",
  82. "number": num,
  83. })
  84. t.SUCCESS("操作成功")
  85. }
  86. //停止抽奖
  87. func (t *LotteryDrawCtl) Stop() {
  88. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  89. activityId := t.MustGetInt64("activity_id")
  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. ladder.Status = define.StatusEnding
  98. ladder.UpdatedAt = time.Now()
  99. t.CheckErr(models.Save(map[string]interface{}{
  100. "id=": ladderId,
  101. }, ladder, "status", "updated_at"))
  102. // 通知直播开始抽奖
  103. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStop,
  104. map[string]interface{}{
  105. "lottery_draw_ladder_id": ladder.Id,
  106. "timestamp": time.Now().Unix(),
  107. "desc": "结束抽奖",
  108. })
  109. t.SUCCESS("操作成功")
  110. }
  111. type LotteryListResult struct {
  112. LotteryDrawActivityId int64 `json:"lottery_draw_activity_id"`
  113. LotteryDrawRuleId int64 `json:"lottery_draw_rule_id"`
  114. LotteryDrawActivityName string `json:"lottery_draw_name"`
  115. LotteryDrawLadders []*LotteryLadderResult `json:"lottery_draw_ladders"`
  116. PrizeNumber int64 `json:"prize_number"`
  117. }
  118. type LotteryLadderResult struct {
  119. LotteryDrawRuleId int64 `json:"-"`
  120. LotteryDrawLadderId int64 `json:"lottery_draw_ladder_id"`
  121. Status string `json:"status"`
  122. PrizeName string `json:"prize_name"`
  123. PrizeImg string `json:"prize_img"`
  124. PrizeNumber int64 `json:"prize_number"`
  125. }
  126. //获取所有抽奖活动列表
  127. func (t *LotteryDrawCtl) List() {
  128. activityId := t.MustGetInt64("activity_id")
  129. customerId := t.MustGetUID()
  130. customer := new(models.Customer)
  131. exist, err := models.Get(customer, customerId)
  132. t.CheckErr(err)
  133. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  134. activity := new(models.Activity)
  135. exist, err = models.Get(activity, activityId)
  136. t.CheckErr(err)
  137. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  138. result := make([]*LotteryListResult, 0)
  139. err = core.GetXormAuto().Table(new(models.LotteryDrawActivity)).Alias("a").
  140. Select("a.id as lottery_draw_activity_id, r.id as lottery_draw_rule_id, a.lottery_draw_activity_name").
  141. Join("LEFT", models.AliasTableName(new(models.LotteryDrawRule), "r"),
  142. "a.id=r.lottery_draw_activity_id and r.is_delete=0").
  143. Where("a.is_delete=0 and a.activity_id=?", activityId).Find(&result)
  144. t.CheckErr(err)
  145. // 多地区的一个坑:分配给主会场的area_id不是主账号的area_id
  146. area := new(models.AreaStore)
  147. if customer.Pid == 0 {
  148. exist, err = area.GetMainAreaById(activityId)
  149. } else {
  150. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  151. }
  152. t.CheckErr(err)
  153. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  154. ruleIds := make([]int64, 0)
  155. for _, v := range result {
  156. ruleIds = append(ruleIds, v.LotteryDrawRuleId)
  157. }
  158. ladders := make([]*LotteryLadderResult, 0)
  159. err = core.GetXormAuto().Table(new(models.LotteryDrawRuleLadder)).
  160. Select("id as lottery_draw_ladder_id, prize_name, prize_img, prize_number, lottery_draw_rule_id, status").
  161. Where("is_delete=0").In("lottery_draw_rule_id", ruleIds).Find(&ladders)
  162. t.CheckErr(err)
  163. ladderIds := make([]int64, 0)
  164. for _, ladder := range ladders {
  165. ladderIds = append(ladderIds, ladder.LotteryDrawLadderId)
  166. }
  167. records := make([]map[string]int64, 0)
  168. err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Alias("r").
  169. Select("r.lottery_draw_rule_ladder_id as ladder_id, count(r.id) as num").
  170. Where("is_delete=0 and rehearsal_id=?", activity.RehearsalId).
  171. In("r.lottery_draw_rule_ladder_id", ladderIds).GroupBy("ladder_id").Find(&records)
  172. t.CheckErr(err)
  173. for i := range ladders {
  174. for j := range records {
  175. if ladders[i].LotteryDrawLadderId == records[j]["ladder_id"] {
  176. ladders[i].PrizeNumber = ladders[i].PrizeNumber - records[j]["num"]
  177. }
  178. }
  179. }
  180. for i := range result {
  181. for j := range ladders {
  182. if result[i].LotteryDrawRuleId == ladders[j].LotteryDrawRuleId {
  183. result[i].PrizeNumber += ladders[j].PrizeNumber
  184. result[i].LotteryDrawLadders = append(result[i].LotteryDrawLadders, ladders[j])
  185. }
  186. }
  187. }
  188. core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("count()")
  189. t.JSON(map[string]interface{}{
  190. "total": len(result),
  191. "list": result,
  192. })
  193. }
  194. //抽奖奖品
  195. func (t *LotteryDrawCtl) Prize() {
  196. ruleId := t.MustGetInt64("lottery_draw_rule_id")
  197. list := make([]*models.LotteryDrawRuleLadder, 0)
  198. err := core.GetXormAuto().Where("is_delete=0 and lottery_draw_rule_id=?", ruleId).Find(&list)
  199. t.CheckErr(err)
  200. for index := range list {
  201. list[index].Des = "在该活动的所有用户中随机抽奖品数量的用户"
  202. }
  203. t.JSON(map[string]interface{}{
  204. "total": len(list),
  205. "lise": list,
  206. })
  207. }
  208. type LotteryUsersResult struct {
  209. UserId int64 `json:"user_id"`
  210. Username string `json:"username"`
  211. Avatar string `json:"avatar"`
  212. }
  213. // 抽奖用户
  214. func (t *LotteryDrawCtl) Users() {
  215. activityId := t.MustGetInt64("activity_id")
  216. ruleId := t.MustGetInt64("lottery_draw_rule_id")
  217. customerId := t.MustGetUID()
  218. customer := new(models.Customer)
  219. exist, err := models.Get(customer, customerId)
  220. t.CheckErr(err)
  221. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  222. activity := new(models.Activity)
  223. exist, err = models.Get(activity, activityId)
  224. t.CheckErr(err)
  225. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  226. area := new(models.AreaStore)
  227. if customer.Pid == 0 {
  228. exist, err = area.GetMainAreaById(activityId)
  229. } else {
  230. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  231. }
  232. t.CheckErr(err)
  233. t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
  234. result := make([]*LotteryUsersResult, 0)
  235. session := core.GetXormAuto().Table(new(models.SignHistory)).Alias("h").
  236. Select("h.user_id, u.nickname as username, u.avatar").Distinct("h.user_id").
  237. Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete = 0").
  238. Where("h.is_delete=0 and h.area_id=? and h.activity_id=? and h.rehearsal_id=?",
  239. area.Id, activityId, activity.RehearsalId)
  240. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_LOTTERY, activityId)
  241. t.CheckErr(err)
  242. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在")
  243. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  244. // 去重标志
  245. recordIds := make([]int64, 0)
  246. err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("user_id").
  247. Where("lottery_draw_rule_id=? and rehearsal_id=? and area_id=? and is_delete=0",
  248. ruleId, activity.RehearsalId, area.Id).Find(&recordIds)
  249. t.CheckErr(err)
  250. session = session.NotIn("h.user_id", recordIds)
  251. }
  252. err = session.Find(&result)
  253. t.CheckErr(err)
  254. t.JSON(map[string]interface{}{
  255. "total": len(result),
  256. "list": result,
  257. })
  258. }
  259. //抽奖动作
  260. func (t *LotteryDrawCtl) Lottery() {
  261. activityId := t.MustGetInt64("activity_id")
  262. ruleId := t.MustGetInt64("lottery_draw_rule_id")
  263. ladderId := t.MustGetInt64("lottery_draw_ladder_id")
  264. number := t.MustGetInt("number") // 奖品数量
  265. customerId := t.MustGetUID()
  266. customer := new(models.Customer)
  267. exist, err := models.Get(customer, customerId)
  268. t.CheckErr(err)
  269. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  270. activity := new(models.Activity)
  271. exist, err = models.Get(activity, activityId)
  272. t.CheckErr(err)
  273. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  274. t.CheckRunning(activity.Status)
  275. // 多地区设置
  276. area := new(models.AreaStore)
  277. if customer.Pid == 0 {
  278. exist, err = area.GetMainAreaById(activity.Id)
  279. } else {
  280. exist, err = area.GetAreaStoreById(t.MustGetAreaId())
  281. }
  282. t.CheckErr(err)
  283. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在")
  284. rule := new(models.LotteryDrawRule)
  285. exist, err = models.Get(rule, ruleId)
  286. t.CheckErr(err)
  287. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "抽奖规则不存在")
  288. // 查询奖品
  289. ladder := new(models.LotteryDrawRuleLadder)
  290. exist, err = models.Get(ladder, ladderId)
  291. t.CheckErr(err)
  292. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "抽奖等级不存在")
  293. t.CheckRunning(ladder.Status)
  294. count, err := core.GetXormAuto().Where("lottery_draw_rule_id=? and lottery_draw_rule_ladder_id=? "+
  295. "and rehearsal_id=? and area_id=? and is_delete=0", ruleId, ladderId, activity.RehearsalId, area.Id).
  296. Count(new(models.LotteryDrawRecord))
  297. t.CheckErr(err)
  298. prizeNum := ladder.PrizeNumber - int(count)
  299. if prizeNum <= 0 || prizeNum < number { // 需要抽奖的数量比数据库存在数量多
  300. t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH)
  301. }
  302. //根据主活动id和地区id获取所有的用户
  303. signUpIds := make([]int64, 0)
  304. err = core.GetXormAuto().Table(new(models.SignUp)).Select("id").Where("activity_id=? and is_delete=0",
  305. activityId).Find(&signUpIds)
  306. t.CheckErr(err)
  307. if len(signUpIds) <= 0 {
  308. t.ERROR("暂无签到用户", code.MSG_DATA_NOT_EXIST)
  309. }
  310. moduleService, exist, err := activity_service.GetModuleService(define.MODULE_LOTTERY, activityId)
  311. t.CheckErr(err)
  312. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在")
  313. // 取设置
  314. lotteryUsers := make([]*lottery_service.LotteryUser, 0)
  315. if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
  316. // 去除同规则中将用户
  317. recordIds := make([]int64, 0)
  318. err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("user_id").
  319. Where("lottery_draw_rule_id=? and rehearsal_id=? and area_id=? and is_delete=0",
  320. ruleId, activity.RehearsalId, area.Id).Find(&recordIds)
  321. t.CheckErr(err)
  322. //查询已经中奖的用户,剔除已经中奖的用户
  323. err = core.GetXormAuto().Table(new(models.SignHistory)).Select("user_id").
  324. Where("is_delete=0 and rehearsal_id=? and area_id=?",
  325. activity.RehearsalId, area.Id).In("sign_rule_id", signUpIds).
  326. NotIn("user_id", recordIds).Find(&lotteryUsers)
  327. t.CheckErr(err)
  328. } else {
  329. // 同轮次剔除中奖用户
  330. // 去除同规则中将用户
  331. recordIds := make([]int64, 0)
  332. err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("user_id").
  333. Where("lottery_draw_rule_ladder_id=? and rehearsal_id=? and area_id=? and is_delete=0",
  334. ladderId, activity.RehearsalId, area.Id).Find(&recordIds)
  335. t.CheckErr(err)
  336. err = core.GetXormAuto().Table(new(models.SignHistory)).Select("user_id").
  337. Where("is_delete=0 and rehearsal_id=? and area_id=?",
  338. activity.RehearsalId, area.Id).In("sign_rule_id", signUpIds).
  339. NotIn("user_id", recordIds).Find(&lotteryUsers)
  340. t.CheckErr(err)
  341. }
  342. if len(lotteryUsers) < number {
  343. t.ERROR("抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH)
  344. }
  345. lottery_service.RandLotteryUser(lotteryUsers) // 打乱需要中奖人员
  346. winners := lotteryUsers[:number]
  347. userIds := make([]int64, 0)
  348. for _, winner := range winners {
  349. userIds = append(userIds, winner.UserId)
  350. }
  351. users := make([]*models.User, 0)
  352. err = core.GetXormAuto().Where("is_delete=0").In("id", userIds).Find(&users)
  353. t.CheckErr(err)
  354. for i := range winners {
  355. // 补全信息
  356. for j := range users {
  357. if winners[i].UserId == users[j].Id {
  358. winners[i].Username = users[j].Nickname
  359. winners[i].Avatar = users[j].Avatar
  360. winners[i].UserPhone = users[j].Phone
  361. winners[i].PrizeName = ladder.PrizeName
  362. winners[i].LadderId = ladder.Id
  363. winners[i].PrizeImg = ladder.PrizeImg
  364. break
  365. }
  366. }
  367. // 普通抽奖
  368. userPrize := new(models.UserPrize)
  369. userPrize.ActivityId = activityId
  370. userPrize.RehearsalId = activity.RehearsalId
  371. userPrize.ActivityName = activity.Name
  372. userPrize.UserId = winners[i].UserId
  373. userPrize.PrizeImg = ladder.PrizeImg
  374. userPrize.PrizeName = ladder.PrizeName
  375. userPrize.PrizeType = 1
  376. userPrize.IsDelete = false
  377. userPrize.CreatedAt = time.Now()
  378. userPrize.UpdatedAt = time.Now()
  379. _, err = core.GetXormAuto().Insert(userPrize)
  380. t.CheckErr(err)
  381. record := new(models.LotteryDrawRecord)
  382. record.UserPrizeId = userPrize.Id
  383. record.ActivityId = activityId
  384. record.RehearsalId = activity.RehearsalId
  385. record.LotteryDrawActivityId = rule.LotteryDrawActivityId
  386. record.LotteryDrawRuleId = rule.Id
  387. record.UserId = winners[i].UserId
  388. record.UserName = winners[i].Username
  389. record.UserPhone = winners[i].UserPhone
  390. record.LotteryDrawRuleLadderId = ladder.Id
  391. record.PrizeName = ladder.PrizeName
  392. record.AreaId = area.Id
  393. record.AreaName = area.Name
  394. record.IsDelete = false
  395. record.CreatedAt = time.Now()
  396. record.UpdatedAt = time.Now()
  397. _, err = core.GetXormAuto().InsertOne(record)
  398. t.CheckErr(err)
  399. }
  400. // 通知直播开始抽奖
  401. im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawResult,
  402. map[string]interface{}{
  403. "lottery_draw_ladder_id": ladder.Id,
  404. "winners": winners,
  405. "timestamp": time.Now().Unix(),
  406. "desc": "抽奖结果",
  407. })
  408. t.JSON(winners)
  409. }
  410. type WinnersResult struct {
  411. UserId int64 `json:"user_id"`
  412. UserName string `json:"user_name"`
  413. UserPhone string `json:"user_phone"`
  414. Avatar string `json:"avatar"`
  415. PrizeName string `json:"prize_name"`
  416. }
  417. //获取中奖名单
  418. func (t *LotteryDrawCtl) ListOfWinners() {
  419. ruleId := t.MustGetInt64("lottery_draw_rule_id")
  420. rehearsalId := t.MustGetInt64("rehearsal_id")
  421. result := make([]*WinnersResult, 0)
  422. err := core.GetXormAuto().Table("ox_lottery_draw_record").Alias("record").
  423. Join("LEFT", new(models.User).Alias("user"), "user.id=record.user_id and user.is_delete=0").
  424. Where("record.is_delete=0 and record.lottery_draw_rule_id=? and record.rehearsal_id=?", ruleId, rehearsalId).
  425. Find(&result)
  426. t.CheckErr(err)
  427. t.JSON(map[string]interface{}{
  428. "total": len(result),
  429. "list": result,
  430. })
  431. }