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

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