rank.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/manager/model"
  6. "go-common/library/log"
  7. )
  8. // RankGroups gets archive groups.
  9. func (s *Service) RankGroups(c context.Context, pn, ps int) (res []*model.RankGroup, total int, err error) {
  10. start := (pn - 1) * ps
  11. if err = s.dao.DB().Where("isdel=0").Offset(start).Limit(ps).Find(&res).Error; err != nil {
  12. log.Error("s.RankGroups() get groups error(%v)", err)
  13. return
  14. }
  15. if err = s.dao.DB().Model(&model.RankGroup{}).Where("isdel=0").Count(&total).Error; err != nil {
  16. log.Error("s.RankGroups() get total error(%v)", err)
  17. return
  18. }
  19. // todo 优化,合并查询
  20. for _, g := range res {
  21. g.Auths, _ = s.rankAuths(c, g.ID)
  22. }
  23. return
  24. }
  25. // RankGroup gets rank group info.
  26. func (s *Service) RankGroup(c context.Context, id int64) (res *model.RankGroup, err error) {
  27. res = new(model.RankGroup)
  28. if err = s.dao.DB().Where("id=? and isdel=0", id).First(res).Error; err != nil {
  29. log.Error("s.RankGroup(%d) error(%v)", id, err)
  30. return
  31. }
  32. res.Auths, _ = s.rankAuths(c, id)
  33. return
  34. }
  35. // AddRankGroup adds rank group.
  36. func (s *Service) AddRankGroup(c context.Context, g *model.RankGroup, auths []int64) (id int64, err error) {
  37. if err = s.dao.DB().Create(g).Error; err != nil {
  38. log.Error("s.AddRankGroup(%+v) error(%v)", g, err)
  39. return
  40. }
  41. id = g.ID
  42. for _, a := range auths {
  43. if err = s.dao.DB().Exec("insert into rank_auths (group_id,auth_id,isdel) values (?,?,0)", id, a).Error; err != nil {
  44. log.Error("s.AddRankGroup(%d,%d) add auth error(%v)", id, a, err)
  45. return
  46. }
  47. }
  48. return
  49. }
  50. // UpdateRankGroup udpates rank group.
  51. func (s *Service) UpdateRankGroup(c context.Context, g *model.RankGroup, auths []int64) (err error) {
  52. if err = s.dao.DB().Model(g).Update(g).Error; err != nil {
  53. log.Error("s.UpdateRankGroup(%+v) error(%v)", g, err)
  54. return
  55. }
  56. // todo 优化,找出差异auth,多了删,少了加
  57. if err = s.delRankAuthAll(c, g.ID); err != nil {
  58. log.Error("s.delRankAuthAll(%d) error(%d)", g.ID, err)
  59. return
  60. }
  61. for _, a := range auths {
  62. if err = s.dao.DB().Exec("insert into rank_auths (group_id,auth_id,isdel) values (?,?,0) on duplicate key update isdel=0", g.ID, a).Error; err != nil {
  63. log.Error("s.UpdateRankGroup(%d,%d) add auth error(%v)", g.ID, a, err)
  64. return
  65. }
  66. }
  67. return
  68. }
  69. // DelRankGroup deletes rank group...
  70. func (s *Service) DelRankGroup(c context.Context, id int64) (err error) {
  71. if err = s.delRankAuthAll(c, id); err != nil {
  72. log.Error("s.delRankAuthAll(%d) error(%v)", id, err)
  73. return
  74. }
  75. if err = s.dao.DB().Model(&model.RankGroup{ID: id}).Update("isdel", 1).Error; err != nil {
  76. log.Error("s.DelRankGroup(%d) error(%v)", id, err)
  77. }
  78. return
  79. }
  80. // AddRankUser adds rank user.
  81. func (s *Service) AddRankUser(c context.Context, uid int64) (err error) {
  82. if err = s.dao.DB().Create(&model.RankUser{UID: uid}).Error; err != nil {
  83. log.Error("s.AddRankUser(%d) error(%v)", uid, err)
  84. }
  85. return
  86. }
  87. // RankUsers gets rank user list.
  88. func (s *Service) RankUsers(c context.Context, pn, ps int, username string) (res []*model.RankUserScores, total int, err error) {
  89. var (
  90. tmpUids, uids, ids []int64
  91. users []*model.RankUser
  92. )
  93. if username != "" {
  94. var (
  95. us []*model.User
  96. condition = fmt.Sprintf("%%%s%%", username)
  97. )
  98. if err = s.dao.DB().Where("username like ?", condition).Find(&us).Error; err != nil {
  99. log.Error("s.RankUsers search by username(%s) error(%v)", username, err)
  100. return
  101. }
  102. for _, u := range us {
  103. ids = append(ids, u.ID)
  104. }
  105. }
  106. if len(ids) > 0 {
  107. err = s.dao.DB().Where("uid in(?) and isdel=0", ids).Find(&users).Error
  108. } else {
  109. err = s.dao.DB().Where("isdel=0").Find(&users).Error
  110. }
  111. if err != nil {
  112. log.Error("s.RankUsers() get groups error(%v)", err)
  113. return
  114. }
  115. us := make(map[int64]map[int64]int)
  116. for _, u := range users {
  117. tmpUids = append(tmpUids, u.UID)
  118. if us[u.UID] == nil {
  119. us[u.UID] = make(map[int64]int)
  120. }
  121. if u.GroupID == 0 {
  122. continue
  123. }
  124. us[u.UID][u.GroupID] = u.Rank
  125. }
  126. dic := make(map[int64]bool)
  127. for _, i := range tmpUids {
  128. if !dic[i] {
  129. dic[i] = true
  130. uids = append(uids, i)
  131. }
  132. }
  133. names, err := s.usernames(uids)
  134. if err != nil {
  135. return
  136. }
  137. for _, uid := range uids {
  138. if names[uid] == nil {
  139. continue
  140. }
  141. u := &model.RankUserScores{
  142. UID: uid,
  143. Username: names[uid].Username,
  144. Nickname: names[uid].Nickname,
  145. Ranks: us[uid],
  146. }
  147. res = append(res, u)
  148. }
  149. total = len(res)
  150. start := (pn - 1) * ps
  151. if start >= total {
  152. res = []*model.RankUserScores{}
  153. return
  154. }
  155. end := start + ps
  156. if end > total {
  157. end = total
  158. }
  159. res = res[start:end]
  160. return
  161. }
  162. func (s *Service) usernames(uids []int64) (res map[int64]*model.User, err error) {
  163. var users []*model.User
  164. if err = s.dao.DB().Model(&model.User{}).Where("id in (?)", uids).Find(&users).Error; err != nil {
  165. log.Error("s.username(%v) error(%v)", uids, err)
  166. return
  167. }
  168. res = make(map[int64]*model.User, len(users))
  169. for _, u := range users {
  170. res[u.ID] = u
  171. }
  172. return
  173. }
  174. // SaveRankUser saves user group's rank.
  175. func (s *Service) SaveRankUser(c context.Context, uid int64, ranks map[int64]int) (err error) {
  176. for gid, rank := range ranks {
  177. if err = s.dao.DB().Exec("insert into rank_users (group_id,uid,rank) values (?,?,?) on duplicate key update rank=?,isdel=0", gid, uid, rank, rank).Error; err != nil {
  178. log.Error("s.SaveRankUser(%d,%d,%d) save user rank error(%v)", gid, uid, rank, err)
  179. return
  180. }
  181. }
  182. return
  183. }
  184. // DelRankUser deletes user.
  185. func (s *Service) DelRankUser(c context.Context, uid int64) (err error) {
  186. if err = s.dao.DB().Model(&model.RankUser{}).Where("uid=?", uid).Update("isdel", 1).Error; err != nil {
  187. log.Error("s.DelRankUser(%d) error(%v)", uid, err)
  188. }
  189. return
  190. }
  191. // delRankAuthAll deletes all rank auths by group.
  192. func (s *Service) delRankAuthAll(c context.Context, gid int64) (err error) {
  193. if err = s.dao.DB().Model(&model.RankAuth{}).Where("group_id=?", gid).Update("isdel", 1).Error; err != nil {
  194. log.Error("s.DelRankAuthAll(%d) error(%v)", gid, err)
  195. }
  196. return
  197. }
  198. // rankAuths gets auths by group.
  199. func (s *Service) rankAuths(c context.Context, gid int64) (res []*model.AuthItem, err error) {
  200. if err = s.dao.DB().Model(&model.RankAuth{}).Where("group_id=? and isdel=0", gid).Select("auth_item.id, auth_item.name, auth_item.data").Joins("left join auth_item on auth_item.id=rank_auths.auth_id").Scan(&res).Error; err != nil {
  201. log.Error("s.RankAuths(%d) error(%v)", gid, err)
  202. }
  203. return
  204. }