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

192 lines
6.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. package lottery_service
  2. import (
  3. "github.com/ouxuanserver/osmanthuswine/src/core"
  4. "hudongzhuanjia/models"
  5. "hudongzhuanjia/utils/define"
  6. "math/rand"
  7. "time"
  8. )
  9. type LotteryUser struct {
  10. UserId int64
  11. Username string
  12. UserPhone string
  13. Avatar string
  14. PrizeName string
  15. LadderId int64
  16. PrizeImg string
  17. }
  18. func GetLotteryUsers(ids []int64) ([]*LotteryUser, error) {
  19. users := make([]*LotteryUser, 0)
  20. err := core.GetXormAuto().Table(new(models.User)).
  21. Select("id as user_id, nickname as username, avatar, phone").
  22. In("id", ids).Find(&users)
  23. return users, err
  24. }
  25. func RandLotteryUserIds(users []int64) {
  26. r := rand.New(rand.NewSource(time.Now().Unix()))
  27. if len(users) <= 0 {
  28. return
  29. }
  30. for i := len(users) - 1; i > 0; i-- {
  31. num := r.Intn(i + 1)
  32. users[i], users[num] = users[num], users[i]
  33. }
  34. }
  35. func GetLotteryUserIds(repeat string, activityId, ruleId, ladderId, rehearsalId, areaId interface{}) ([]int64, error) {
  36. var err error
  37. userIds := make([]int64, 0)
  38. recordIds := make([]int64, 0)
  39. if repeat == define.MODULE_BESIDE_REPEAT {
  40. // 去除同规则中将用户
  41. err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("user_id").
  42. Where("lottery_draw_rule_id=? and rehearsal_id=? and area_id=? and is_delete=0",
  43. ruleId, rehearsalId, areaId).Find(&recordIds)
  44. } else {
  45. // 去除同规则中将用户
  46. err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("user_id").
  47. Where("lottery_draw_rule_ladder_id=? and rehearsal_id=? and area_id=? and is_delete=0",
  48. ladderId, rehearsalId, areaId).Find(&recordIds)
  49. }
  50. if err != nil {
  51. return nil, err
  52. }
  53. err = core.GetXormAuto().Table(new(models.SignHistory)).Select("user_id").
  54. Where("is_delete=0 and rehearsal_id=? and area_id=? and activity_id=?",
  55. rehearsalId, areaId, activityId).NotIn("user_id", recordIds).Find(&userIds)
  56. return userIds, nil
  57. }
  58. type LotteryListResult struct {
  59. LotteryDrawActivityId int64 `json:"lottery_draw_activity_id"`
  60. LotteryDrawRuleId int64 `json:"lottery_draw_rule_id"`
  61. LotteryDrawActivityName string `json:"lottery_draw_name"`
  62. LotteryDrawLadders []*LotteryLadderResult `json:"lottery_draw_ladders"`
  63. PrizeNumber int64 `json:"prize_number"`
  64. }
  65. type LotteryLadderResult struct {
  66. LotteryDrawRuleId int64 `json:"-"`
  67. LotteryDrawLadderId int64 `json:"lottery_draw_ladder_id"`
  68. Status string `json:"status"`
  69. PrizeName string `json:"prize_name"`
  70. PrizeImg string `json:"prize_img"`
  71. PrizeNumber int64 `json:"prize_number"`
  72. }
  73. func GetLotteryAndLadder(activityId, rehearsalId interface{}) ([]*LotteryListResult, error) {
  74. result := make([]*LotteryListResult, 0)
  75. err := core.GetXormAuto().Table(new(models.LotteryDrawActivity)).Alias("a").
  76. Select("a.id as lottery_draw_activity_id, r.id as lottery_draw_rule_id, a.lottery_draw_activity_name").
  77. Join("LEFT", models.AliasTableName(new(models.LotteryDrawRule), "r"),
  78. "a.id=r.lottery_draw_activity_id and r.is_delete=0").
  79. Where("a.is_delete=0 and a.activity_id=?", activityId).Find(&result)
  80. if err != nil {
  81. return nil, err
  82. }
  83. ruleIds := make([]int64, 0)
  84. for _, v := range result {
  85. ruleIds = append(ruleIds, v.LotteryDrawRuleId)
  86. }
  87. ladders := make([]*LotteryLadderResult, 0)
  88. err = core.GetXormAuto().Table(new(models.LotteryDrawRuleLadder)).
  89. Select("id as lottery_draw_ladder_id, prize_name, prize_img, prize_number, lottery_draw_rule_id, status").
  90. Where("is_delete=0").In("lottery_draw_rule_id", ruleIds).Find(&ladders)
  91. if err != nil {
  92. return nil, err
  93. }
  94. ladderIds := make([]int64, 0)
  95. for _, ladder := range ladders {
  96. ladderIds = append(ladderIds, ladder.LotteryDrawLadderId)
  97. }
  98. records := make([]map[string]int64, 0)
  99. err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Alias("r").
  100. Select("r.lottery_draw_rule_ladder_id as ladder_id, count(r.id) as num").
  101. Where("is_delete=0 and rehearsal_id=?", rehearsalId).
  102. In("r.lottery_draw_rule_ladder_id", ladderIds).GroupBy("ladder_id").Find(&records)
  103. if err != nil {
  104. return nil, err
  105. }
  106. for i := range ladders {
  107. for j := range records {
  108. if ladders[i].LotteryDrawLadderId == records[j]["ladder_id"] {
  109. ladders[i].PrizeNumber = ladders[i].PrizeNumber - records[j]["num"]
  110. }
  111. }
  112. }
  113. for i := range result {
  114. for j := range ladders {
  115. if result[i].LotteryDrawRuleId == ladders[j].LotteryDrawRuleId {
  116. result[i].PrizeNumber += ladders[j].PrizeNumber
  117. result[i].LotteryDrawLadders = append(result[i].LotteryDrawLadders, ladders[j])
  118. }
  119. }
  120. }
  121. return result, nil
  122. }
  123. type LotteryUsersResult struct {
  124. UserId int64 `json:"user_id"`
  125. Username string `json:"username"`
  126. Avatar string `json:"avatar"`
  127. }
  128. func GetLotteryUsersResultByRollNum(ladderId, rehearsalId interface{}, rollNum int) ([]*LotteryUsersResult, error) {
  129. users := make([]*LotteryUsersResult, 0)
  130. session := core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Alias("r").
  131. Join("LEFT", new(models.User).Alias("u"), "u.id=r.user_id").
  132. Where("r.is_delete=0 and r.lottery_draw_rule_ladder_id=? and "+
  133. "r.rehearsal_id=?", ladderId, rehearsalId)
  134. defer session.Close()
  135. if rollNum > 0 {
  136. session = session.Where("roll_num=?", rollNum)
  137. }
  138. err := session.Find(&users)
  139. return users, err
  140. }
  141. func GetLotteryUsersResult(areaId, activityId, rehearsalId interface{}, ids []int64) ([]*LotteryUsersResult, error) {
  142. result := make([]*LotteryUsersResult, 0)
  143. session := core.GetXormAuto().Table(new(models.SignHistory)).Alias("h").
  144. Select("h.user_id, u.nickname as username, u.avatar").Distinct("h.user_id").
  145. Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete = 0").
  146. Where("h.is_delete=0 and h.area_id=? and h.activity_id=? and h.rehearsal_id=?",
  147. areaId, activityId, rehearsalId)
  148. if len(ids) >= 0 {
  149. session = session.NotIn("h.user_id", ids)
  150. }
  151. err := session.Find(&result)
  152. if err != nil {
  153. return nil, err
  154. }
  155. return result, nil
  156. }
  157. type WinnersResult struct {
  158. UserId int64 `json:"user_id"`
  159. UserName string `json:"user_name"`
  160. UserPhone string `json:"user_phone"`
  161. Avatar string `json:"avatar"`
  162. PrizeName string `json:"prize_name"`
  163. }
  164. func GetWinnersResult(ruleId, rehearsalId interface{}) ([]*WinnersResult, error) {
  165. result := make([]*WinnersResult, 0)
  166. err := core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Alias("record").
  167. Join("LEFT", new(models.User).Alias("user"), "user.id=record.user_id and user.is_delete=0").
  168. Where("record.is_delete=0 and record.lottery_draw_rule_id=? and record.rehearsal_id=?", ruleId, rehearsalId).
  169. Find(&result)
  170. return result, err
  171. }