match.go 5.8 KB

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