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

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