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

390 lines
13 KiB

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