game.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/esports/model"
  6. "go-common/library/log"
  7. )
  8. var _emptyGameList = make([]*model.Game, 0)
  9. // GameInfo .
  10. func (s *Service) GameInfo(c context.Context, id int64) (game *model.Game, err error) {
  11. game = new(model.Game)
  12. if err = s.dao.DB.Where("id=?", id).First(&game).Error; err != nil {
  13. log.Error("GameInfo Error (%v)", err)
  14. }
  15. return
  16. }
  17. // GameList .
  18. func (s *Service) GameList(c context.Context, pn, ps int64, title string) (list []*model.Game, count int64, err error) {
  19. source := s.dao.DB.Model(&model.Game{})
  20. if title != "" {
  21. likeStr := fmt.Sprintf("%%%s%%", title)
  22. source = source.Where("title like ?", likeStr)
  23. }
  24. source.Count(&count)
  25. if err = source.Offset((pn - 1) * ps).Limit(ps).Find(&list).Error; err != nil {
  26. log.Error("GameList Error (%v)", err)
  27. }
  28. return
  29. }
  30. // AddGame .
  31. func (s *Service) AddGame(c context.Context, param *model.Game) (err error) {
  32. // TODO check name exist
  33. if err = s.dao.DB.Model(&model.Game{}).Create(param).Error; err != nil {
  34. log.Error("AddGame s.dao.DB.Model Create(%+v) error(%v)", param, err)
  35. }
  36. return
  37. }
  38. // EditGame .
  39. func (s *Service) EditGame(c context.Context, param *model.Game) (err error) {
  40. preGame := new(model.Game)
  41. if err = s.dao.DB.Where("id=?", param.ID).First(&preGame).Error; err != nil {
  42. log.Error("EditGame s.dao.DB.Where id(%d) error(%d)", param.ID, err)
  43. return
  44. }
  45. if err = s.dao.DB.Model(&model.Game{}).Update(param).Error; err != nil {
  46. log.Error("EditGame s.dao.DB.Model Update(%+v) error(%v)", param, err)
  47. }
  48. return
  49. }
  50. // ForbidGame .
  51. func (s *Service) ForbidGame(c context.Context, id int64, state int) (err error) {
  52. preGame := new(model.Game)
  53. if err = s.dao.DB.Where("id=?", id).First(&preGame).Error; err != nil {
  54. log.Error("GameForbid s.dao.DB.Where id(%d) error(%d)", id, err)
  55. return
  56. }
  57. if err = s.dao.DB.Model(&model.Game{}).Where("id=?", id).Update(map[string]int{"status": state}).Error; err != nil {
  58. log.Error("GameForbid s.dao.DB.Model error(%v)", err)
  59. }
  60. return
  61. }
  62. // gameList return game info map with oid key.
  63. func (s *Service) gameList(c context.Context, typ int, oids []int64) (list map[int64][]*model.Game, err error) {
  64. var (
  65. gidMaps []*model.GIDMap
  66. gids []int64
  67. games []*model.Game
  68. )
  69. if len(oids) == 0 {
  70. return
  71. }
  72. if err = s.dao.DB.Model(&model.GIDMap{}).Where("is_deleted=?", _notDeleted).Where("type=?", typ).Where("oid IN(?)", oids).Find(&gidMaps).Error; err != nil {
  73. log.Error("gameList gidMap Error (%v)", err)
  74. return
  75. }
  76. if len(gidMaps) == 0 {
  77. return
  78. }
  79. gidMap := make(map[int64]int64, len(gidMaps))
  80. oidGidMap := make(map[int64][]int64, len(gidMaps))
  81. for _, v := range gidMaps {
  82. oidGidMap[v.Oid] = append(oidGidMap[v.Oid], v.Gid)
  83. if _, ok := gidMap[v.Gid]; ok {
  84. continue
  85. }
  86. gids = append(gids, v.Gid)
  87. gidMap[v.Gid] = v.Gid
  88. }
  89. if err = s.dao.DB.Model(&model.Game{}).Where("status=?", _statusOn).Where("id IN(?)", gids).Find(&games).Error; err != nil {
  90. log.Error("gameList games Error (%v)", err)
  91. return
  92. }
  93. if len(games) == 0 {
  94. return
  95. }
  96. gameMap := make(map[int64]*model.Game, len(games))
  97. for _, v := range games {
  98. gameMap[v.ID] = v
  99. }
  100. list = make(map[int64][]*model.Game, len(oids))
  101. for _, oid := range oids {
  102. if ids, ok := oidGidMap[oid]; ok {
  103. for _, id := range ids {
  104. if game, ok := gameMap[id]; ok {
  105. list[oid] = append(list[oid], game)
  106. }
  107. }
  108. }
  109. }
  110. return
  111. }
  112. // Types return data page game types.
  113. func (s *Service) Types(c context.Context) (list map[int64]string, err error) {
  114. list = make(map[int64]string, len(s.c.GameTypes))
  115. for _, tp := range s.c.GameTypes {
  116. list[tp.ID] = tp.Name
  117. }
  118. return
  119. }