team.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/esports/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. var _emptyTeamList = make([]*model.Team, 0)
  10. // TeamInfo .
  11. func (s *Service) TeamInfo(c context.Context, id int64) (data *model.TeamInfo, err error) {
  12. var gameMap map[int64][]*model.Game
  13. team := new(model.Team)
  14. if err = s.dao.DB.Where("id=?", id).Where("is_deleted=?", _notDeleted).First(&team).Error; err != nil {
  15. log.Error("TeamInfo Error (%v)", err)
  16. return
  17. }
  18. if gameMap, err = s.gameList(c, model.TypeTeam, []int64{id}); err != nil {
  19. return
  20. }
  21. if games, ok := gameMap[id]; ok {
  22. data = &model.TeamInfo{Team: team, Games: games}
  23. } else {
  24. data = &model.TeamInfo{Team: team, Games: _emptyGameList}
  25. }
  26. return
  27. }
  28. // TeamList .
  29. func (s *Service) TeamList(c context.Context, pn, ps int64, title string, status int) (list []*model.TeamInfo, count int64, err error) {
  30. var (
  31. teams []*model.Team
  32. teamIDs []int64
  33. gameMap map[int64][]*model.Game
  34. )
  35. source := s.dao.DB.Model(&model.Team{})
  36. if status != _statusAll {
  37. source = source.Where("is_deleted=?", _notDeleted)
  38. }
  39. if title != "" {
  40. likeStr := fmt.Sprintf("%%%s%%", title)
  41. source = source.Where("title like ?", likeStr)
  42. }
  43. source.Count(&count)
  44. if err = source.Offset((pn - 1) * ps).Limit(ps).Find(&teams).Error; err != nil {
  45. log.Error("TeamList Error (%v)", err)
  46. return
  47. }
  48. if len(teams) == 0 {
  49. return
  50. }
  51. for _, v := range teams {
  52. teamIDs = append(teamIDs, v.ID)
  53. }
  54. if gameMap, err = s.gameList(c, model.TypeTeam, teamIDs); err != nil {
  55. return
  56. }
  57. for _, v := range teams {
  58. if games, ok := gameMap[v.ID]; ok {
  59. list = append(list, &model.TeamInfo{Team: v, Games: games})
  60. } else {
  61. list = append(list, &model.TeamInfo{Team: v, Games: _emptyGameList})
  62. }
  63. }
  64. return
  65. }
  66. // AddTeam .
  67. func (s *Service) AddTeam(c context.Context, param *model.Team, gids []int64) (err error) {
  68. var (
  69. games []*model.Game
  70. gidMaps []*model.GIDMap
  71. )
  72. if err = s.dao.DB.Model(&model.Game{}).Where("status=?", _statusOn).Where("id IN (?)", gids).Find(&games).Error; err != nil {
  73. log.Error("AddTeam check game ids Error (%v)", err)
  74. return
  75. }
  76. if len(games) == 0 {
  77. log.Error("AddTeam games(%v) not found", gids)
  78. err = ecode.RequestErr
  79. return
  80. }
  81. tx := s.dao.DB.Begin()
  82. if err = tx.Error; err != nil {
  83. log.Error("s.dao.DB.Begin error(%v)", err)
  84. return
  85. }
  86. if err = tx.Model(&model.Team{}).Create(param).Error; err != nil {
  87. log.Error("AddTeam s.dao.DB.Model Create(%+v) error(%v)", param, err)
  88. err = tx.Rollback().Error
  89. return
  90. }
  91. for _, v := range games {
  92. gidMaps = append(gidMaps, &model.GIDMap{Type: model.TypeTeam, Oid: param.ID, Gid: v.ID})
  93. }
  94. if err = tx.Model(&model.GIDMap{}).Exec(model.GidBatchAddSQL(gidMaps)).Error; err != nil {
  95. log.Error("AddTeam s.dao.DB.Model Create(%+v) error(%v)", param, err)
  96. err = tx.Rollback().Error
  97. return
  98. }
  99. err = tx.Commit().Error
  100. return
  101. }
  102. // EditTeam .
  103. func (s *Service) EditTeam(c context.Context, param *model.Team, gids []int64) (err error) {
  104. var (
  105. games []*model.Game
  106. preGidMaps, addGidMaps []*model.GIDMap
  107. upGidMapAdd, upGidMapDel []int64
  108. )
  109. preData := new(model.Team)
  110. if err = s.dao.DB.Where("id=?", param.ID).First(&preData).Error; err != nil {
  111. log.Error("EditTeam s.dao.DB.Where id(%d) error(%d)", param.ID, err)
  112. return
  113. }
  114. if err = s.dao.DB.Model(&model.Game{}).Where("status=?", _statusOn).Where("id IN (?)", gids).Find(&games).Error; err != nil {
  115. log.Error("EditTeam check game ids Error (%v)", err)
  116. return
  117. }
  118. if len(games) == 0 {
  119. log.Error("EditTeam games(%v) not found", gids)
  120. err = ecode.RequestErr
  121. return
  122. }
  123. if err = s.dao.DB.Model(&model.GIDMap{}).Where("oid=?", param.ID).Where("type=?", model.TypeTeam).Find(&preGidMaps).Error; err != nil {
  124. log.Error("EditTeam games(%v) not found", gids)
  125. return
  126. }
  127. gidsMap := make(map[int64]int64, len(gids))
  128. preGidsMap := make(map[int64]int64, len(preGidMaps))
  129. for _, v := range gids {
  130. gidsMap[v] = v
  131. }
  132. for _, v := range preGidMaps {
  133. preGidsMap[v.Gid] = v.Gid
  134. if _, ok := gidsMap[v.Gid]; ok {
  135. if v.IsDeleted == 1 {
  136. upGidMapAdd = append(upGidMapAdd, v.ID)
  137. }
  138. } else {
  139. upGidMapDel = append(upGidMapDel, v.ID)
  140. }
  141. }
  142. for _, gid := range gids {
  143. if _, ok := preGidsMap[gid]; !ok {
  144. addGidMaps = append(addGidMaps, &model.GIDMap{Type: model.TypeTeam, Oid: param.ID, Gid: gid})
  145. }
  146. }
  147. tx := s.dao.DB.Begin()
  148. if err = tx.Error; err != nil {
  149. log.Error("s.dao.DB.Begin error(%v)", err)
  150. return
  151. }
  152. if err = tx.Model(&model.Team{}).Save(param).Error; err != nil {
  153. log.Error("EditTeam Team Update(%+v) error(%v)", param, err)
  154. err = tx.Rollback().Error
  155. return
  156. }
  157. if len(upGidMapAdd) > 0 {
  158. if err = tx.Model(&model.GIDMap{}).Where("id IN (?)", upGidMapAdd).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  159. log.Error("EditTeam GIDMap Add(%+v) error(%v)", upGidMapAdd, err)
  160. err = tx.Rollback().Error
  161. return
  162. }
  163. }
  164. if len(upGidMapDel) > 0 {
  165. if err = tx.Model(&model.GIDMap{}).Where("id IN (?)", upGidMapDel).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  166. log.Error("EditTeam GIDMap Del(%+v) error(%v)", upGidMapDel, err)
  167. err = tx.Rollback().Error
  168. return
  169. }
  170. }
  171. if len(addGidMaps) > 0 {
  172. if err = tx.Model(&model.GIDMap{}).Exec(model.GidBatchAddSQL(addGidMaps)).Error; err != nil {
  173. log.Error("EditTeam GIDMap Create(%+v) error(%v)", addGidMaps, err)
  174. err = tx.Rollback().Error
  175. return
  176. }
  177. }
  178. err = tx.Commit().Error
  179. return
  180. }
  181. // ForbidTeam .
  182. func (s *Service) ForbidTeam(c context.Context, id int64, state int) (err error) {
  183. preTeam := new(model.Team)
  184. if err = s.dao.DB.Where("id=?", id).First(&preTeam).Error; err != nil {
  185. log.Error("TeamForbid s.dao.DB.Where id(%d) error(%d)", id, err)
  186. return
  187. }
  188. if err = s.dao.DB.Model(&model.Team{}).Where("id=?", id).Update(map[string]int{"is_deleted": state}).Error; err != nil {
  189. log.Error("TeamForbid s.dao.DB.Model error(%v)", err)
  190. }
  191. return
  192. }