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

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