team.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package model
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. . "github.com/710leo/urlooker/modules/web/store"
  8. )
  9. type Team struct {
  10. Id int64 `json:"id"`
  11. Name string `json:"name"`
  12. Resume string `json:"resume"`
  13. Creator int64 `json:"creator"`
  14. Created time.Time `json:"-" xorm:"<-"`
  15. CreatorName string `json:"-" xorm:"-"`
  16. }
  17. func GetTeamById(id int64) (*Team, error) {
  18. obj := new(Team)
  19. has, err := Orm.Where("id=?", id).Get(obj)
  20. if err != nil {
  21. return obj, err
  22. }
  23. if !has {
  24. return obj, nil
  25. }
  26. return obj, nil
  27. }
  28. func AddTeam(name, resume string, creator int64, uids []int64) (int64, error) {
  29. if !(len(name) > 0 && creator > 0) {
  30. return 0, fmt.Errorf("团队信息有误")
  31. }
  32. session := Orm.NewSession()
  33. defer session.Close()
  34. err := session.Begin()
  35. if err != nil {
  36. return 0, err
  37. }
  38. has, err := session.Where("name=?", name).Get(new(Team))
  39. if err != nil {
  40. return 0, err
  41. }
  42. if has {
  43. return 0, fmt.Errorf("团队名称已经被占用了")
  44. }
  45. team := Team{Name: name, Resume: resume, Creator: creator}
  46. _, err = session.Insert(&team)
  47. if err != nil {
  48. session.Rollback()
  49. return 0, err
  50. }
  51. err = addUsersIntoTeam(team.Id, uids, session)
  52. if err != nil {
  53. session.Rollback()
  54. return 0, err
  55. }
  56. return team.Id, session.Commit()
  57. }
  58. func RemoveTeamById(tid int64) error {
  59. session := Orm.NewSession()
  60. defer session.Close()
  61. err := session.Begin()
  62. if err != nil {
  63. return err
  64. }
  65. _, err = session.Id(tid).Delete(new(Team))
  66. if err != nil {
  67. session.Rollback()
  68. return err
  69. }
  70. err = removeAllUsersFromTeam(tid, session)
  71. if err != nil {
  72. session.Rollback()
  73. return err
  74. }
  75. err = session.Commit()
  76. if err != nil {
  77. return err
  78. }
  79. return nil
  80. }
  81. func (this *Team) Update(uids []int64) error {
  82. session := Orm.NewSession()
  83. defer session.Close()
  84. err := session.Begin()
  85. if err != nil {
  86. return err
  87. }
  88. _, err = session.Id(this.Id).Update(this)
  89. if err != nil {
  90. session.Rollback()
  91. return err
  92. }
  93. err = updateUsersOfTeam(this.Id, uids, session)
  94. if err != nil {
  95. session.Rollback()
  96. return err
  97. }
  98. err = session.Commit()
  99. if err != nil {
  100. return err
  101. }
  102. return nil
  103. }
  104. func QueryTeams(query string, limit int) ([]*Team, error) {
  105. teams := make([]*Team, 0)
  106. if query == "" {
  107. return teams, nil
  108. }
  109. err := Orm.Where("name LIKE ?", "%"+query+"%").Limit(limit).Find(&teams)
  110. return teams, err
  111. }
  112. func GetTeamsByIds(ids string) ([]*Team, error) {
  113. teams := make([]*Team, 0)
  114. teamIdSlice := strings.Split(ids, ",")
  115. for _, tidStr := range teamIdSlice {
  116. tid, err := strconv.ParseInt(tidStr, 10, 64)
  117. if err != nil {
  118. continue
  119. }
  120. team, err := GetTeamById(tid)
  121. if err != nil {
  122. return teams, err
  123. }
  124. teams = append(teams, team)
  125. }
  126. return teams, nil
  127. }