contest.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/admin/main/esports/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. var (
  10. _emptyContestList = make([]*model.Contest, 0)
  11. _emptyContestData = make([]*model.ContestData, 0)
  12. )
  13. const (
  14. _sortDesc = 1
  15. _sortASC = 2
  16. _ReplyTypeContest = "27"
  17. )
  18. // ContestInfo .
  19. func (s *Service) ContestInfo(c context.Context, id int64) (data *model.ContestInfo, err error) {
  20. var (
  21. gameMap map[int64][]*model.Game
  22. teamMap map[int64]*model.Team
  23. teamIDs []int64
  24. hasTeam bool
  25. )
  26. contest := new(model.Contest)
  27. if err = s.dao.DB.Where("id=?", id).First(&contest).Error; err != nil {
  28. log.Error("ContestInfo Error (%v)", err)
  29. return
  30. }
  31. if gameMap, err = s.gameList(c, model.TypeContest, []int64{id}); err != nil {
  32. return
  33. }
  34. if contest.HomeID > 0 {
  35. teamIDs = append(teamIDs, contest.HomeID)
  36. }
  37. if contest.AwayID > 0 {
  38. teamIDs = append(teamIDs, contest.AwayID)
  39. }
  40. if ids := unique(teamIDs); len(ids) > 0 {
  41. var teams []*model.Team
  42. if err = s.dao.DB.Model(&model.Team{}).Where("id IN (?)", ids).Find(&teams).Error; err != nil {
  43. log.Error("ContestList team Error (%v)", err)
  44. return
  45. }
  46. if len(teams) > 0 {
  47. hasTeam = true
  48. }
  49. teamMap = make(map[int64]*model.Team, len(teams))
  50. for _, v := range teams {
  51. teamMap[v.ID] = v
  52. }
  53. }
  54. data = &model.ContestInfo{Contest: contest}
  55. if len(gameMap) > 0 {
  56. if games, ok := gameMap[id]; ok {
  57. data.Games = games
  58. }
  59. }
  60. if len(data.Games) == 0 {
  61. data.Games = _emptyGameList
  62. }
  63. if hasTeam {
  64. if team, ok := teamMap[contest.HomeID]; ok {
  65. data.HomeName = team.Title
  66. }
  67. if team, ok := teamMap[contest.AwayID]; ok {
  68. data.AwayName = team.Title
  69. }
  70. }
  71. var cDatas []*model.ContestData
  72. if err = s.dao.DB.Model(&model.ContestData{}).Where(map[string]interface{}{"is_deleted": _notDeleted}).Where("cid IN (?)", []int64{id}).Find(&cDatas).Error; err != nil {
  73. log.Error("ContestInfo Find ContestData Error (%v)", err)
  74. return
  75. }
  76. data.Data = cDatas
  77. return
  78. }
  79. // ContestList .
  80. func (s *Service) ContestList(c context.Context, mid, sid, pn, ps, srt int64) (list []*model.ContestInfo, count int64, err error) {
  81. var contests []*model.Contest
  82. source := s.dao.DB.Model(&model.Contest{})
  83. if srt == _sortDesc {
  84. source = source.Order("stime DESC")
  85. } else if srt == _sortASC {
  86. source = source.Order("stime ASC")
  87. }
  88. if mid > 0 {
  89. source = source.Where("mid=?", mid)
  90. }
  91. if sid > 0 {
  92. source = source.Where("sid=?", sid)
  93. }
  94. source.Count(&count)
  95. if err = source.Offset((pn - 1) * ps).Limit(ps).Find(&contests).Error; err != nil {
  96. log.Error("ContestList Error (%v)", err)
  97. return
  98. }
  99. if len(contests) == 0 {
  100. contests = _emptyContestList
  101. return
  102. }
  103. if list, err = s.contestInfos(c, contests, true); err != nil {
  104. log.Error("s.contestInfos Error (%v)", err)
  105. }
  106. return
  107. }
  108. func (s *Service) contestInfos(c context.Context, contests []*model.Contest, useGame bool) (list []*model.ContestInfo, err error) {
  109. var (
  110. conIDs, teamIDs []int64
  111. gameMap map[int64][]*model.Game
  112. teamMap map[int64]*model.Team
  113. cDataMap map[int64][]*model.ContestData
  114. hasGame, hasTeam, hasCData bool
  115. )
  116. for _, v := range contests {
  117. conIDs = append(conIDs, v.ID)
  118. if v.HomeID > 0 {
  119. teamIDs = append(teamIDs, v.HomeID)
  120. }
  121. if v.AwayID > 0 {
  122. teamIDs = append(teamIDs, v.AwayID)
  123. }
  124. if v.SuccessTeam > 0 {
  125. teamIDs = append(teamIDs, v.SuccessTeam)
  126. }
  127. }
  128. if useGame {
  129. if gameMap, err = s.gameList(c, model.TypeContest, conIDs); err != nil {
  130. return
  131. } else if len(gameMap) > 0 {
  132. hasGame = true
  133. }
  134. }
  135. if ids := unique(teamIDs); len(ids) > 0 {
  136. var teams []*model.Team
  137. if err = s.dao.DB.Model(&model.Team{}).Where("id IN (?)", ids).Find(&teams).Error; err != nil {
  138. log.Error("ContestList team Error (%v)", err)
  139. return
  140. }
  141. if len(teams) > 0 {
  142. hasTeam = true
  143. }
  144. teamMap = make(map[int64]*model.Team, len(teams))
  145. for _, v := range teams {
  146. teamMap[v.ID] = v
  147. }
  148. }
  149. if len(conIDs) > 0 {
  150. var cDatas []*model.ContestData
  151. if err = s.dao.DB.Model(&model.ContestData{}).Where(map[string]interface{}{"is_deleted": _notDeleted}).Where("cid IN (?)", conIDs).Find(&cDatas).Error; err != nil {
  152. log.Error("ContestList Find ContestData Error (%v)", err)
  153. return
  154. }
  155. if len(cDatas) > 0 {
  156. hasCData = true
  157. }
  158. cDataMap = make(map[int64][]*model.ContestData, len(cDatas))
  159. for _, v := range cDatas {
  160. cDataMap[v.CID] = append(cDataMap[v.CID], v)
  161. }
  162. }
  163. for _, v := range contests {
  164. contest := &model.ContestInfo{Contest: v}
  165. if hasGame {
  166. if games, ok := gameMap[v.ID]; ok {
  167. contest.Games = games
  168. }
  169. }
  170. if len(contest.Games) == 0 {
  171. contest.Games = _emptyGameList
  172. }
  173. if hasTeam {
  174. if team, ok := teamMap[v.HomeID]; ok {
  175. contest.HomeName = team.Title
  176. }
  177. if team, ok := teamMap[v.AwayID]; ok {
  178. contest.AwayName = team.Title
  179. }
  180. if team, ok := teamMap[v.SuccessTeam]; ok {
  181. contest.SuccessName = team.Title
  182. }
  183. }
  184. if hasCData {
  185. if cData, ok := cDataMap[v.ID]; ok {
  186. contest.Data = cData
  187. }
  188. } else {
  189. contest.Data = _emptyContestData
  190. }
  191. list = append(list, contest)
  192. }
  193. return
  194. }
  195. // AddContest .
  196. func (s *Service) AddContest(c context.Context, param *model.Contest, gids []int64) (err error) {
  197. // TODO check name exist
  198. // check sid
  199. season := new(model.Season)
  200. if err = s.dao.DB.Where("id=?", param.Sid).Where("status=?", _statusOn).First(&season).Error; err != nil {
  201. log.Error("AddContest s.dao.DB.Where id(%d) error(%d)", param.Sid, err)
  202. return
  203. }
  204. // check mid
  205. match := new(model.Match)
  206. if err = s.dao.DB.Where("id=?", param.Mid).Where("status=?", _statusOn).First(&match).Error; err != nil {
  207. log.Error("AddContest s.dao.DB.Where id(%d) error(%d)", param.Mid, err)
  208. return
  209. }
  210. // check game idsEsportsCDataErr
  211. var (
  212. games []*model.Game
  213. gidMaps []*model.GIDMap
  214. contestData []*model.ContestData
  215. )
  216. if param.DataType == 0 {
  217. param.Data = ""
  218. param.MatchID = 0
  219. }
  220. if param.Data != "" {
  221. if err = json.Unmarshal([]byte(param.Data), &contestData); err != nil {
  222. err = ecode.EsportsContestDataErr
  223. return
  224. }
  225. }
  226. if err = s.dao.DB.Model(&model.Game{}).Where("status=?", _statusOn).Where("id IN (?)", gids).Find(&games).Error; err != nil {
  227. log.Error("AddContest check game ids Error (%v)", err)
  228. return
  229. }
  230. if len(games) == 0 {
  231. log.Error("AddContest games(%v) not found", gids)
  232. err = ecode.RequestErr
  233. return
  234. }
  235. tx := s.dao.DB.Begin()
  236. if err = tx.Model(&model.Contest{}).Create(param).Error; err != nil {
  237. log.Error("AddContest tx.Model Create(%+v) error(%v)", param, err)
  238. err = tx.Rollback().Error
  239. return
  240. }
  241. for _, v := range games {
  242. gidMaps = append(gidMaps, &model.GIDMap{Type: model.TypeContest, Oid: param.ID, Gid: v.ID})
  243. }
  244. if err = tx.Model(&model.GIDMap{}).Exec(model.GidBatchAddSQL(gidMaps)).Error; err != nil {
  245. log.Error("AddContest tx.Model Create(%+v) error(%v)", param, err)
  246. err = tx.Rollback().Error
  247. return
  248. }
  249. if len(contestData) > 0 {
  250. if err = tx.Model(&model.Module{}).Exec(model.BatchAddCDataSQL(param.ID, contestData)).Error; err != nil {
  251. log.Error("AddContest Module tx.Model Create(%+v) error(%v)", param, err)
  252. err = tx.Rollback().Error
  253. return
  254. }
  255. }
  256. if err = tx.Commit().Error; err != nil {
  257. return
  258. }
  259. // register reply
  260. if err = s.dao.RegReply(c, param.ID, param.Adid, _ReplyTypeContest); err != nil {
  261. err = nil
  262. }
  263. return
  264. }
  265. // EditContest .
  266. func (s *Service) EditContest(c context.Context, param *model.Contest, gids []int64) (err error) {
  267. var (
  268. games []*model.Game
  269. preGidMaps, addGidMaps []*model.GIDMap
  270. upGidMapAdd, upGidMapDel []int64
  271. )
  272. // TODO check name exist
  273. // check sid
  274. season := new(model.Season)
  275. if err = s.dao.DB.Where("id=?", param.Sid).Where("status=?", _statusOn).First(&season).Error; err != nil {
  276. log.Error("EditContest s.dao.DB.Where id(%d) error(%d)", param.Sid, err)
  277. return
  278. }
  279. // check mid
  280. match := new(model.Match)
  281. if err = s.dao.DB.Where("id=?", param.Mid).Where("status=?", _statusOn).First(&match).Error; err != nil {
  282. log.Error("EditContest s.dao.DB.Where id(%d) error(%d)", param.Mid, err)
  283. return
  284. }
  285. preData := new(model.Contest)
  286. if err = s.dao.DB.Where("id=?", param.ID).First(&preData).Error; err != nil {
  287. log.Error("EditContest s.dao.DB.Where id(%d) error(%d)", param.ID, err)
  288. return
  289. }
  290. if err = s.dao.DB.Model(&model.Game{}).Where("status=?", _statusOn).Where("id IN (?)", gids).Find(&games).Error; err != nil {
  291. log.Error("EditContest check game ids Error (%v)", err)
  292. return
  293. }
  294. if len(games) == 0 {
  295. log.Error("EditContest games(%v) not found", gids)
  296. err = ecode.RequestErr
  297. return
  298. }
  299. if err = s.dao.DB.Model(&model.GIDMap{}).Where("oid=?", param.ID).Where("type=?", model.TypeContest).Find(&preGidMaps).Error; err != nil {
  300. log.Error("EditContest games(%v) not found", gids)
  301. return
  302. }
  303. var (
  304. newCData []*model.ContestData
  305. )
  306. if param.DataType == 0 {
  307. param.Data = ""
  308. param.MatchID = 0
  309. }
  310. if param.Data != "" {
  311. if err = json.Unmarshal([]byte(param.Data), &newCData); err != nil {
  312. err = ecode.EsportsContestDataErr
  313. return
  314. }
  315. }
  316. gidsMap := make(map[int64]int64, len(gids))
  317. preGidsMap := make(map[int64]int64, len(preGidMaps))
  318. for _, v := range gids {
  319. gidsMap[v] = v
  320. }
  321. for _, v := range preGidMaps {
  322. preGidsMap[v.Gid] = v.Gid
  323. if _, ok := gidsMap[v.Gid]; ok {
  324. if v.IsDeleted == 1 {
  325. upGidMapAdd = append(upGidMapAdd, v.ID)
  326. }
  327. } else {
  328. upGidMapDel = append(upGidMapDel, v.ID)
  329. }
  330. }
  331. for _, gid := range gids {
  332. if _, ok := preGidsMap[gid]; !ok {
  333. addGidMaps = append(addGidMaps, &model.GIDMap{Type: model.TypeContest, Oid: param.ID, Gid: gid})
  334. }
  335. }
  336. tx := s.dao.DB.Begin()
  337. if err = tx.Error; err != nil {
  338. log.Error("s.dao.DB.Begin error(%v)", err)
  339. return
  340. }
  341. if err = tx.Model(&model.Contest{}).Save(param).Error; err != nil {
  342. log.Error("EditContest Update(%+v) error(%v)", param, err)
  343. err = tx.Rollback().Error
  344. return
  345. }
  346. if len(upGidMapAdd) > 0 {
  347. if err = tx.Model(&model.GIDMap{}).Where("id IN (?)", upGidMapAdd).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  348. log.Error("EditContest GIDMap Add(%+v) error(%v)", upGidMapAdd, err)
  349. err = tx.Rollback().Error
  350. return
  351. }
  352. }
  353. if len(upGidMapDel) > 0 {
  354. if err = tx.Model(&model.GIDMap{}).Where("id IN (?)", upGidMapDel).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  355. log.Error("EditContest GIDMap Del(%+v) error(%v)", upGidMapDel, err)
  356. err = tx.Rollback().Error
  357. return
  358. }
  359. }
  360. if len(addGidMaps) > 0 {
  361. if err = tx.Model(&model.GIDMap{}).Exec(model.GidBatchAddSQL(addGidMaps)).Error; err != nil {
  362. log.Error("EditContest GIDMap Create(%+v) error(%v)", addGidMaps, err)
  363. err = tx.Rollback().Error
  364. return
  365. }
  366. }
  367. var (
  368. mapOldCData, mapNewCData map[int64]*model.ContestData
  369. upCData, addCData, oldCData []*model.ContestData
  370. delCData []int64
  371. )
  372. if len(newCData) > 0 {
  373. // check module
  374. if err = s.dao.DB.Model(&model.ContestData{}).Where("cid=?", param.ID).Where("is_deleted=?", _notDeleted).Find(&oldCData).Error; err != nil {
  375. log.Error("EditContest s.dao.DB.Model Find (%+v) error(%v)", param.ID, err)
  376. return
  377. }
  378. mapOldCData = make(map[int64]*model.ContestData, len(oldCData))
  379. for _, v := range oldCData {
  380. mapOldCData[v.ID] = v
  381. }
  382. //新数据在老数据中 更新老数据。新的数据不在老数据 添加新数据
  383. for _, cData := range newCData {
  384. if _, ok := mapOldCData[cData.ID]; ok {
  385. upCData = append(upCData, cData)
  386. } else {
  387. addCData = append(addCData, cData)
  388. }
  389. }
  390. mapNewCData = make(map[int64]*model.ContestData, len(oldCData))
  391. for _, v := range newCData {
  392. mapNewCData[v.ID] = v
  393. }
  394. //老数据在新中 上面已经处理。老数据不在新数据中 删除老数据
  395. for _, cData := range oldCData {
  396. if _, ok := mapNewCData[cData.ID]; !ok {
  397. delCData = append(delCData, cData.ID)
  398. }
  399. }
  400. if len(upCData) > 0 {
  401. if err = tx.Model(&model.ContestData{}).Exec(model.BatchEditCDataSQL(upCData)).Error; err != nil {
  402. log.Error("EditContest s.dao.DB.Model tx.Model Exec(%+v) error(%v)", upCData, err)
  403. err = tx.Rollback().Error
  404. return
  405. }
  406. }
  407. if len(delCData) > 0 {
  408. if err = tx.Model(&model.ContestData{}).Where("id IN (?)", delCData).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  409. log.Error("EditContest s.dao.DB.Model Updates(%+v) error(%v)", delCData, err)
  410. err = tx.Rollback().Error
  411. return
  412. }
  413. }
  414. if len(addCData) > 0 {
  415. if err = tx.Model(&model.ContestData{}).Exec(model.BatchAddCDataSQL(param.ID, addCData)).Error; err != nil {
  416. log.Error("EditContest s.dao.DB.Model Create(%+v) error(%v)", addCData, err)
  417. err = tx.Rollback().Error
  418. return
  419. }
  420. }
  421. } else {
  422. if err = tx.Model(&model.ContestData{}).Where("cid = ?", param.ID).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  423. log.Error("EditContest s.dao.DB.Model Updates(%+v) error(%v)", param.ID, err)
  424. err = tx.Rollback().Error
  425. return
  426. }
  427. }
  428. err = tx.Commit().Error
  429. return
  430. }
  431. // ForbidContest .
  432. func (s *Service) ForbidContest(c context.Context, id int64, state int) (err error) {
  433. preContest := new(model.Contest)
  434. if err = s.dao.DB.Where("id=?", id).First(&preContest).Error; err != nil {
  435. log.Error("ContestForbid s.dao.DB.Where id(%d) error(%d)", id, err)
  436. return
  437. }
  438. if err = s.dao.DB.Model(&model.Contest{}).Where("id=?", id).Update(map[string]int{"status": state}).Error; err != nil {
  439. log.Error("ContestForbid s.dao.DB.Model error(%v)", err)
  440. }
  441. return
  442. }