match_active.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "sync"
  6. "go-common/app/admin/main/esports/model"
  7. arcmdl "go-common/app/service/main/archive/api"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/sync/errgroup"
  11. "go-common/library/xstr"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _arcsSize = 50
  16. _ReplyTypeAct = "25"
  17. )
  18. var (
  19. _emptyModules = make([]*model.Module, 0)
  20. _emptMatchMod = make([]*model.MatchModule, 0)
  21. _emptyActive = make([]*model.MatchModule, 0)
  22. )
  23. // AddAct .
  24. func (s *Service) AddAct(c context.Context, param *model.ParamMA) (arcs map[string][]int64, err error) {
  25. var ms []*model.Module
  26. if param.Modules != "" {
  27. if err = json.Unmarshal([]byte(param.Modules), &ms); err != nil {
  28. err = ecode.EsportsActModErr
  29. return
  30. }
  31. if arcs, err = s.checkArc(c, ms); err != nil {
  32. return
  33. }
  34. }
  35. tx := s.dao.DB.Begin()
  36. if err = tx.Model(&model.MatchActive{}).Create(&param.MatchActive).Error; err != nil {
  37. log.Error("AddAct MatchActive tx.Model Create(%+v) error(%v)", param, err)
  38. err = tx.Rollback().Error
  39. return
  40. }
  41. maID := param.ID
  42. if len(ms) > 0 {
  43. if err = tx.Model(&model.Module{}).Exec(model.BatchAddModuleSQL(maID, ms)).Error; err != nil {
  44. log.Error("AddAct Module tx.Model Create(%+v) error(%v)", param, err)
  45. err = tx.Rollback().Error
  46. return
  47. }
  48. }
  49. if err = tx.Commit().Error; err != nil {
  50. return
  51. }
  52. // register reply
  53. if err = s.dao.RegReply(c, maID, param.Adid, _ReplyTypeAct); err != nil {
  54. err = nil
  55. }
  56. return
  57. }
  58. // EditAct .
  59. func (s *Service) EditAct(c context.Context, param *model.ParamMA) (arcs map[string][]int64, err error) {
  60. var (
  61. tmpMs, upM, addM, ms []*model.Module
  62. mapMID map[int64]int64
  63. pMID map[int64]*model.Module
  64. delM []int64
  65. )
  66. if param.Modules != "" {
  67. if err = json.Unmarshal([]byte(param.Modules), &ms); err != nil {
  68. err = ecode.EsportsActModErr
  69. return
  70. }
  71. if arcs, err = s.checkArc(c, ms); err != nil {
  72. return
  73. }
  74. }
  75. // check module
  76. if err = s.dao.DB.Model(&model.Module{}).Where("ma_id=?", param.ID).Where("status=?", _notDeleted).Find(&tmpMs).Error; err != nil {
  77. log.Error("EditAct s.dao.DB.Model Find (%+v) error(%v)", param.ID, err)
  78. return
  79. }
  80. mapMID = make(map[int64]int64, len(tmpMs))
  81. for _, m := range tmpMs {
  82. mapMID[m.ID] = m.MaID
  83. }
  84. pMID = make(map[int64]*model.Module, len(ms))
  85. for _, m := range ms {
  86. if _, ok := mapMID[m.ID]; m.ID > 0 && !ok {
  87. err = ecode.EsportsActModNot
  88. return
  89. }
  90. pMID[m.ID] = m
  91. if m.ID == 0 {
  92. addM = append(addM, m)
  93. }
  94. }
  95. for _, m := range tmpMs {
  96. if mod, ok := pMID[m.ID]; ok {
  97. upM = append(upM, mod)
  98. } else {
  99. delM = append(delM, m.ID)
  100. }
  101. }
  102. // save
  103. tx := s.dao.DB.Begin()
  104. upFields := map[string]interface{}{"sid": param.Sid, "mid": param.Mid, "background": param.Background,
  105. "back_color": param.BackColor, "color_step": param.ColorStep, "live_id": param.LiveID, "intr": param.Intr,
  106. "focus": param.Focus, "url": param.URL, "status": param.Status,
  107. "h5_background": param.H5Background, "h5_back_color": param.H5BackColor,
  108. "h5_focus": param.H5Focus, "h5_url": param.H5URL,
  109. "intr_logo": param.IntrLogo, "intr_title": param.IntrTitle, "intr_text": param.IntrText}
  110. if err = tx.Model(&model.MatchActive{}).Where("id = ?", param.ID).Update(upFields).Error; err != nil {
  111. log.Error("EditAct MatchActive tx.Model Create(%+v) error(%v)", param, err)
  112. err = tx.Rollback().Error
  113. return
  114. }
  115. if len(upM) > 0 {
  116. if err = tx.Model(&model.Module{}).Exec(model.BatchEditModuleSQL(upM)).Error; err != nil {
  117. log.Error("EditAct Module tx.Model Exec(%+v) error(%v)", upM, err)
  118. err = tx.Rollback().Error
  119. return
  120. }
  121. }
  122. if len(delM) > 0 {
  123. if err = tx.Model(&model.Module{}).Where("id IN (?)", delM).Updates(map[string]interface{}{"status": _deleted}).Error; err != nil {
  124. log.Error("EditAct Module tx.Model Updates(%+v) error(%v)", delM, err)
  125. err = tx.Rollback().Error
  126. return
  127. }
  128. }
  129. if len(addM) > 0 {
  130. if err = tx.Model(&model.Module{}).Exec(model.BatchAddModuleSQL(param.ID, addM)).Error; err != nil {
  131. log.Error("EditAct Module tx.Model Create(%+v) error(%v)", addM, err)
  132. err = tx.Rollback().Error
  133. return
  134. }
  135. }
  136. err = tx.Commit().Error
  137. return
  138. }
  139. func (s *Service) checkArc(c context.Context, ms []*model.Module) (rsAids map[string][]int64, err error) {
  140. var (
  141. name string
  142. aids []int64
  143. allAids []int64
  144. tmpMap map[int64]struct{}
  145. repeatAids []int64
  146. wrongAids []int64
  147. isWrong bool
  148. )
  149. rsAids = make(map[string][]int64, 2)
  150. for _, m := range ms {
  151. // check name only .
  152. if m.Name != "" && name == m.Name {
  153. err = ecode.EsportsModNameErr
  154. return
  155. }
  156. name = m.Name
  157. if aids, err = xstr.SplitInts(m.Oids); err != nil {
  158. err = ecode.RequestErr
  159. return
  160. }
  161. tmpMap = make(map[int64]struct{})
  162. for _, aid := range aids {
  163. if _, ok := tmpMap[aid]; ok {
  164. repeatAids = append(repeatAids, aid)
  165. continue
  166. }
  167. tmpMap[aid] = struct{}{}
  168. }
  169. allAids = append(allAids, aids...)
  170. }
  171. // check aids .
  172. if wrongAids, err = s.wrongArc(c, allAids); err != nil {
  173. err = ecode.EsportsArcServerErr
  174. return
  175. }
  176. if len(repeatAids) > 0 {
  177. rsAids["repeat"] = repeatAids
  178. isWrong = true
  179. }
  180. if len(wrongAids) > 0 {
  181. rsAids["wrong"] = wrongAids
  182. isWrong = true
  183. }
  184. if isWrong {
  185. err = ecode.EsportsModArcErr
  186. }
  187. return
  188. }
  189. func (s *Service) wrongArc(c context.Context, aids []int64) (list []int64, err error) {
  190. var (
  191. arcErr error
  192. arcNormal map[int64]struct{}
  193. mutex = sync.Mutex{}
  194. )
  195. group, errCtx := errgroup.WithContext(c)
  196. aidsLen := len(aids)
  197. arcNormal = make(map[int64]struct{}, aidsLen)
  198. for i := 0; i < aidsLen; i += _arcsSize {
  199. var partAids []int64
  200. if i+_arcsSize > aidsLen {
  201. partAids = aids[i:]
  202. } else {
  203. partAids = aids[i : i+_arcsSize]
  204. }
  205. group.Go(func() (err error) {
  206. var tmpRes *arcmdl.ArcsReply
  207. if tmpRes, arcErr = s.arcClient.Arcs(errCtx, &arcmdl.ArcsRequest{Aids: partAids}); arcErr != nil {
  208. log.Error("wrongArc s.arcClient.Arcs(%v) error %v", partAids, err)
  209. return arcErr
  210. }
  211. if tmpRes != nil {
  212. for _, arc := range tmpRes.Arcs {
  213. if arc != nil && arc.IsNormal() {
  214. mutex.Lock()
  215. arcNormal[arc.Aid] = struct{}{}
  216. mutex.Unlock()
  217. }
  218. }
  219. }
  220. return nil
  221. })
  222. }
  223. if err = group.Wait(); err != nil {
  224. return
  225. }
  226. for _, aid := range aids {
  227. if _, ok := arcNormal[aid]; !ok {
  228. list = append(list, aid)
  229. }
  230. }
  231. return
  232. }
  233. // ForbidAct .
  234. func (s *Service) ForbidAct(c context.Context, id int64, state int) (err error) {
  235. if err = s.dao.DB.Model(&model.MatchActive{}).Where("id=?", id).Updates(map[string]interface{}{"status": state}).Error; err != nil {
  236. log.Error("ForbidAct MatchActive s.dao.DB.Model Updates(%d) error(%v)", id, err)
  237. }
  238. return
  239. }
  240. // ListAct .
  241. func (s *Service) ListAct(c context.Context, mid, pn, ps int64) (rs []*model.MatchModule, count int64, err error) {
  242. var (
  243. mas []*model.MatchActive
  244. maIDs, matchIDs, seasonIDs []int64
  245. mapMs map[int64][]*model.Module
  246. mapMatch map[int64]*model.Match
  247. mapSeaon map[int64]*model.Season
  248. matchTitle, matchSub, seasonTitle, seasonSub string
  249. )
  250. maDB := s.dao.DB.Model(&model.MatchActive{})
  251. if mid > 0 {
  252. maDB = maDB.Where("mid=?", mid)
  253. }
  254. maDB.Count(&count)
  255. if count == 0 {
  256. rs = _emptyActive
  257. }
  258. if err = maDB.Offset((pn - 1) * ps).Order("id ASC").Limit(ps).Find(&mas).Error; err != nil {
  259. log.Error("ListAct MatchActive s.dao.DB.Model Find error(%v)", err)
  260. return
  261. }
  262. if len(mas) == 0 {
  263. rs = _emptMatchMod
  264. return
  265. }
  266. for _, ma := range mas {
  267. maIDs = append(maIDs, ma.ID)
  268. matchIDs = append(matchIDs, ma.Mid)
  269. seasonIDs = append(seasonIDs, ma.Sid)
  270. }
  271. if ids := unique(matchIDs); len(ids) > 0 {
  272. var matchs []*model.Match
  273. if err = s.dao.DB.Model(&model.Match{}).Where("id IN (?)", ids).Find(&matchs).Error; err != nil {
  274. log.Error("ListAct match Error (%v)", err)
  275. return
  276. }
  277. mapMatch = make(map[int64]*model.Match, len(matchs))
  278. for _, v := range matchs {
  279. mapMatch[v.ID] = v
  280. }
  281. }
  282. if ids := unique(seasonIDs); len(ids) > 0 {
  283. var seasons []*model.Season
  284. if err = s.dao.DB.Model(&model.Match{}).Where("id IN (?)", ids).Find(&seasons).Error; err != nil {
  285. log.Error("ListAct season Error (%v)", err)
  286. return
  287. }
  288. mapSeaon = make(map[int64]*model.Season, len(seasonIDs))
  289. for _, v := range seasons {
  290. mapSeaon[v.ID] = v
  291. }
  292. }
  293. if mapMs, err = s.modules(maIDs, count); err != nil {
  294. log.Error("ListAct s.modules maIDs(%+v) faild(%+v)", maIDs, err)
  295. return
  296. }
  297. for _, ma := range mas {
  298. if match, ok := mapMatch[ma.Mid]; ok {
  299. matchTitle = match.Title
  300. matchSub = match.SubTitle
  301. } else {
  302. matchTitle = ""
  303. matchSub = ""
  304. }
  305. if season, ok := mapSeaon[ma.Sid]; ok {
  306. seasonTitle = season.Title
  307. seasonSub = season.SubTitle
  308. } else {
  309. seasonTitle = ""
  310. seasonSub = ""
  311. }
  312. if rsMs, ok := mapMs[ma.ID]; ok {
  313. tmpMs := rsMs
  314. rs = append(rs, &model.MatchModule{MatchActive: ma, Modules: tmpMs, MatchTitle: matchTitle, MatchSubTitle: matchSub, SeasonTitle: seasonTitle, SeasonSubTitle: seasonSub})
  315. } else {
  316. rs = append(rs, &model.MatchModule{MatchActive: ma, Modules: _emptyModules, MatchTitle: matchTitle, MatchSubTitle: matchSub, SeasonTitle: seasonTitle, SeasonSubTitle: seasonSub})
  317. }
  318. }
  319. return
  320. }
  321. func (s *Service) modules(maIDs []int64, count int64) (rs map[int64][]*model.Module, err error) {
  322. var ms []*model.Module
  323. if err = s.dao.DB.Model(&model.Module{}).Where("ma_id in(?)", maIDs).Where("status=?", _notDeleted).Find(&ms).Order("ma_id ASC").Error; err != nil {
  324. err = errors.Wrap(err, "modules map Model Find")
  325. return
  326. }
  327. rs = make(map[int64][]*model.Module, count)
  328. for _, m := range ms {
  329. tmpM := m
  330. rs[tmpM.MaID] = append(rs[tmpM.MaID], tmpM)
  331. }
  332. return
  333. }