rel_team_user.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package model
  2. import (
  3. "github.com/go-xorm/xorm"
  4. . "github.com/710leo/urlooker/modules/web/store"
  5. )
  6. type RelTeamUser struct {
  7. Id int64 `json:"id"`
  8. Tid int64 `json:"tid"`
  9. Uid int64 `json:"uid"`
  10. }
  11. func TeamsOfUser(query string, uid int64, limit, offset int) ([]*Team, error) {
  12. teams := make([]*Team, 0)
  13. var err error
  14. if query != "" {
  15. tn := "%" + query + "%"
  16. err = Orm.Sql("SELECT * FROM team WHERE name LIKE ? AND ( id IN (SELECT DISTINCT tid FROM rel_team_user WHERE uid=? ) OR creator= ?) ORDER BY name LIMIT ?,?", tn, uid, uid, offset, limit).Find(&teams)
  17. } else {
  18. err = Orm.Sql("SELECT * FROM team WHERE (id IN (SELECT DISTINCT tid FROM rel_team_user WHERE uid=?)) OR creator = ? ORDER BY name LIMIT ?,?", uid, uid, offset, limit).Find(&teams)
  19. }
  20. return teams, err
  21. }
  22. func TeamCountOfUser(query string, uid int64) (int64, error) {
  23. if query != "" {
  24. tn := "%" + query + "%"
  25. return Orm.Where("name LIKE ? AND ( id IN (SELECT DISTINCT tid FROM rel_team_user WHERE uid=? ))", tn, uid).Count(new(Team))
  26. } else {
  27. return Orm.Where("id IN (SELECT tid FROM rel_team_user WHERE uid=? )", uid).Count(new(Team))
  28. }
  29. }
  30. func UsersOfTeam(tid int64) ([]*User, error) {
  31. users := make([]*User, 0)
  32. err := Orm.Cols("id", "name", "cnname", "email", "phone", "wechat").Sql("SELECT * FROM user WHERE id IN ( SELECT uid FROM rel_team_user WHERE tid=? )", tid).Find(&users)
  33. if err != nil {
  34. return users, err
  35. }
  36. return users, nil
  37. }
  38. func UsersInfoOfTeam(tid int64) ([]*User, error) {
  39. users := make([]*User, 0)
  40. err := Orm.Cols("name", "cnname", "email", "phone", "wechat").Sql("SELECT * FROM user WHERE id IN ( SELECT uid FROM rel_team_user WHERE tid=? )", tid).Find(&users)
  41. if err != nil {
  42. return users, err
  43. }
  44. return users, nil
  45. }
  46. func IsMemberOfTeam(uid, tid int64) (bool, error) {
  47. users, err := UsersOfTeam(tid)
  48. if err != nil {
  49. return false, err
  50. }
  51. for _, user := range users {
  52. if user != nil && user.Id == uid {
  53. return true, nil
  54. }
  55. }
  56. return false, nil
  57. }
  58. func IsCreatorOfTeam(uid, tid int64) (bool, error) {
  59. team, err := GetTeamById(tid)
  60. if err != nil {
  61. return false, err
  62. }
  63. if team.Creator == uid {
  64. return true, nil
  65. }
  66. return false, nil
  67. }
  68. func updateUsersOfTeam(tid int64, uids []int64, session *xorm.Session) error {
  69. err := removeAllUsersFromTeam(tid, session)
  70. if err != nil {
  71. return err
  72. }
  73. err = addUsersIntoTeam(tid, uids, session)
  74. if err != nil {
  75. return err
  76. }
  77. return err
  78. }
  79. func addUsersIntoTeam(tid int64, uids []int64, session *xorm.Session) error {
  80. for _, uid := range uids {
  81. relTeamUser := &RelTeamUser{Tid: tid, Uid: uid}
  82. _, err := session.Insert(relTeamUser)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. func removeAllUsersFromTeam(tid int64, session *xorm.Session) error {
  90. _, err := session.Where("tid=?", tid).Delete(new(RelTeamUser))
  91. return err
  92. }