season.go 5.8 KB

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