archive.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/esports/model"
  5. accwarden "go-common/app/service/main/account/api"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "go-common/library/net/metadata"
  9. "github.com/jinzhu/gorm"
  10. )
  11. const (
  12. _typeAdd = "add"
  13. _typeEdit = "edit"
  14. )
  15. var _emptyArcList = make([]*model.ArcResult, 0)
  16. // ArcList archive list.
  17. func (s *Service) ArcList(c context.Context, arg *model.ArcListParam) (arcs []*model.ArcResult, total int, err error) {
  18. var (
  19. list []*model.SearchArc
  20. gids, tids, matchIDs, teamIDs, mids []int64
  21. games []*model.Game
  22. tags []*model.Tag
  23. matchs []*model.Match
  24. teams []*model.Team
  25. gameMap map[int64]*model.Game
  26. tagMap map[int64]*model.Tag
  27. matchMap map[int64]*model.Match
  28. teamMap map[int64]*model.Team
  29. infosReply *accwarden.InfosReply
  30. ip = metadata.String(c, metadata.RemoteIP)
  31. )
  32. if list, total, err = s.dao.SearchArc(c, arg); err != nil {
  33. return
  34. }
  35. if len(list) == 0 {
  36. arcs = _emptyArcList
  37. return
  38. }
  39. for _, arc := range list {
  40. if len(arc.Gid) > 0 {
  41. gids = append(gids, arc.Gid...)
  42. }
  43. if len(arc.Tags) > 0 {
  44. tids = append(tids, arc.Tags...)
  45. }
  46. if len(arc.Matchs) > 0 {
  47. matchIDs = append(matchIDs, arc.Matchs...)
  48. }
  49. if len(arc.Teams) > 0 {
  50. teamIDs = append(teamIDs, arc.Teams...)
  51. }
  52. if arc.Mid > 0 {
  53. mids = append(mids, arc.Mid)
  54. }
  55. }
  56. if len(gids) > 0 {
  57. if err = s.dao.DB.Model(&model.Game{}).Where("id IN (?)", unique(gids)).Find(&games).Error; err != nil {
  58. log.Error("ArcList Game gids(%+v) error(%v)", gids, err)
  59. return
  60. }
  61. if gl := len(games); gl > 0 {
  62. gameMap = make(map[int64]*model.Game, gl)
  63. for _, v := range games {
  64. gameMap[v.ID] = v
  65. }
  66. }
  67. }
  68. if len(tids) > 0 {
  69. if err = s.dao.DB.Model(&model.Tag{}).Where("id IN (?)", unique(tids)).Find(&tags).Error; err != nil {
  70. log.Error("ArcList Tag tids(%+v) error(%v)", tids, err)
  71. return
  72. }
  73. if tl := len(tags); tl > 0 {
  74. tagMap = make(map[int64]*model.Tag, tl)
  75. for _, v := range tags {
  76. tagMap[v.ID] = v
  77. }
  78. }
  79. }
  80. if len(matchIDs) > 0 {
  81. if err = s.dao.DB.Model(&model.Match{}).Where("id IN (?)", unique(matchIDs)).Find(&matchs).Error; err != nil {
  82. log.Error("ArcList Match ids(%+v) error(%v)", tids, err)
  83. return
  84. }
  85. if tl := len(matchs); tl > 0 {
  86. matchMap = make(map[int64]*model.Match, tl)
  87. for _, v := range matchs {
  88. matchMap[v.ID] = v
  89. }
  90. }
  91. }
  92. if len(teamIDs) > 0 {
  93. if err = s.dao.DB.Model(&model.Team{}).Where("id IN (?)", unique(teamIDs)).Find(&teams).Error; err != nil {
  94. log.Error("ArcList Team ids(%+v) error(%v)", tids, err)
  95. return
  96. }
  97. if tl := len(teams); tl > 0 {
  98. teamMap = make(map[int64]*model.Team, tl)
  99. for _, v := range teams {
  100. teamMap[v.ID] = v
  101. }
  102. }
  103. }
  104. if len(mids) > 0 {
  105. if infosReply, err = s.accClient.Infos3(c, &accwarden.MidsReq{Mids: unique(mids), RealIp: ip}); err != nil {
  106. log.Error("账号Infos3:grpc错误", "s.accClient.Infos3 error(%v)", err)
  107. return
  108. }
  109. }
  110. for _, v := range list {
  111. arcRes := &model.ArcResult{
  112. Aid: v.Aid,
  113. TypeID: v.TypeID,
  114. Title: v.Title,
  115. State: v.State,
  116. Mid: v.Mid,
  117. Years: v.Year,
  118. }
  119. if infosReply != nil && len(infosReply.Infos) > 0 {
  120. if info, ok := infosReply.Infos[v.Mid]; ok && info != nil {
  121. arcRes.Uname = info.Name
  122. }
  123. }
  124. if len(gameMap) > 0 {
  125. for _, gid := range v.Gid {
  126. if game, ok := gameMap[gid]; ok {
  127. arcRes.Games = append(arcRes.Games, game)
  128. }
  129. }
  130. }
  131. if len(tagMap) > 0 {
  132. for _, tid := range v.Tags {
  133. if tag, ok := tagMap[tid]; ok {
  134. arcRes.Tags = append(arcRes.Tags, tag)
  135. }
  136. }
  137. }
  138. if len(matchMap) > 0 {
  139. for _, tid := range v.Matchs {
  140. if match, ok := matchMap[tid]; ok {
  141. arcRes.Matchs = append(arcRes.Matchs, match)
  142. }
  143. }
  144. }
  145. if len(teamMap) > 0 {
  146. for _, tid := range v.Teams {
  147. if team, ok := teamMap[tid]; ok {
  148. arcRes.Teams = append(arcRes.Teams, team)
  149. }
  150. }
  151. }
  152. if len(arcRes.Games) == 0 {
  153. arcRes.Games = _emptyGameList
  154. }
  155. if len(arcRes.Tags) == 0 {
  156. arcRes.Tags = _emptyTagList
  157. }
  158. if len(arcRes.Matchs) == 0 {
  159. arcRes.Matchs = _emptyMatchList
  160. }
  161. if len(arcRes.Teams) == 0 {
  162. arcRes.Teams = _emptyTeamList
  163. }
  164. arcs = append(arcs, arcRes)
  165. }
  166. return
  167. }
  168. // EditArc edit archive.
  169. func (s *Service) EditArc(c context.Context, arg *model.ArcImportParam) (err error) {
  170. var preArc *model.Arc
  171. if err = s.dao.DB.Model(&model.Arc{}).Where("aid = ?", arg.Aid).Where("is_deleted=?", _notDeleted).First(&preArc).Error; err != nil {
  172. log.Error("EditArc check aid Error (%v)", err)
  173. return
  174. }
  175. var data *model.ArcRelation
  176. if data, err = s.arcRelationChanges(c, arg, _typeEdit); err != nil {
  177. return
  178. }
  179. tx := s.dao.DB.Begin()
  180. if err = tx.Error; err != nil {
  181. log.Error("s.dao.DB.Begin error(%v)", err)
  182. return
  183. }
  184. if err = upArcRelation(tx, data); err != nil {
  185. err = tx.Rollback().Error
  186. return
  187. }
  188. err = tx.Commit().Error
  189. return
  190. }
  191. // BatchAddArc batch add archive.
  192. func (s *Service) BatchAddArc(c context.Context, param *model.ArcAddParam) (err error) {
  193. var (
  194. arcs []*model.Arc
  195. upAddAids, addAids, changeAids []int64
  196. )
  197. if err = s.dao.DB.Model(&model.Arc{}).Where("aid IN (?)", param.Aids).Find(&arcs).Error; err != nil {
  198. log.Error("BatchAddArc check aids Error (%v)", err)
  199. return
  200. }
  201. if len(arcs) > 0 {
  202. arcMap := make(map[int64]*model.Arc, len(param.Aids))
  203. for _, v := range arcs {
  204. arcMap[v.Aid] = v
  205. }
  206. for _, aid := range param.Aids {
  207. if arc, ok := arcMap[aid]; ok {
  208. if arc.IsDeleted == _deleted {
  209. upAddAids = append(upAddAids, arc.ID)
  210. changeAids = append(changeAids, arc.Aid)
  211. }
  212. } else {
  213. addAids = append(addAids, aid)
  214. changeAids = append(changeAids, aid)
  215. }
  216. }
  217. } else {
  218. addAids = param.Aids
  219. changeAids = param.Aids
  220. }
  221. tx := s.dao.DB.Begin()
  222. if err = tx.Error; err != nil {
  223. log.Error("s.dao.DB.Begin error(%v)", err)
  224. return
  225. }
  226. if len(addAids) > 0 {
  227. if err = tx.Model(&model.Arc{}).Exec(model.ArcBatchAddSQL(addAids)).Error; err != nil {
  228. log.Error("BatchAddArc Arc tx.Model Exec(%+v) error(%v)", addAids, err)
  229. err = tx.Rollback().Error
  230. return
  231. }
  232. }
  233. if len(changeAids) > 0 {
  234. arcRelation := new(model.ArcRelation)
  235. for _, aid := range changeAids {
  236. var data *model.ArcRelation
  237. arg := &model.ArcImportParam{
  238. Aid: aid,
  239. Gids: param.Gids,
  240. MatchIDs: param.MatchIDs,
  241. TagIDs: param.TagIDs,
  242. TeamIDs: param.TeamIDs,
  243. Years: param.Years,
  244. }
  245. if data, err = s.arcRelationChanges(c, arg, _typeAdd); err != nil {
  246. return
  247. }
  248. arcRelation.UpAddGids = append(arcRelation.UpAddGids, data.UpAddGids...)
  249. arcRelation.UpDelGids = append(arcRelation.UpDelGids, data.UpDelGids...)
  250. arcRelation.AddGids = append(arcRelation.AddGids, data.AddGids...)
  251. arcRelation.UpAddMatchs = append(arcRelation.UpAddMatchs, data.UpAddMatchs...)
  252. arcRelation.UpDelMatchs = append(arcRelation.UpDelMatchs, data.UpDelMatchs...)
  253. arcRelation.AddMatchs = append(arcRelation.AddMatchs, data.AddMatchs...)
  254. arcRelation.UpAddTags = append(arcRelation.UpAddTags, data.UpAddTags...)
  255. arcRelation.UpDelTags = append(arcRelation.UpDelTags, data.UpDelTags...)
  256. arcRelation.AddTags = append(arcRelation.AddTags, data.AddTags...)
  257. arcRelation.UpAddTeams = append(arcRelation.UpAddTeams, data.UpAddTeams...)
  258. arcRelation.UpDelTeams = append(arcRelation.UpDelTeams, data.UpDelTeams...)
  259. arcRelation.AddTeams = append(arcRelation.AddTeams, data.AddTeams...)
  260. arcRelation.UpAddYears = append(arcRelation.UpAddYears, data.UpAddYears...)
  261. arcRelation.UpDelYears = append(arcRelation.UpDelYears, data.UpDelYears...)
  262. arcRelation.AddYears = append(arcRelation.AddYears, data.AddYears...)
  263. }
  264. if err = upArcRelation(tx, arcRelation); err != nil {
  265. err = tx.Rollback().Error
  266. return
  267. }
  268. }
  269. if len(upAddAids) > 0 {
  270. if err = tx.Model(&model.Arc{}).Where("id IN (?)", upAddAids).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  271. log.Error("BatchAddArc Save(%+v) error(%v)", upAddAids, err)
  272. err = tx.Rollback().Error
  273. return
  274. }
  275. }
  276. err = tx.Commit().Error
  277. return
  278. }
  279. // BatchEditArc batch add arc.
  280. func (s *Service) BatchEditArc(c context.Context, param *model.ArcAddParam) (err error) {
  281. var (
  282. arcs []*model.Arc
  283. changeAids []int64
  284. )
  285. if err = s.dao.DB.Model(&model.Arc{}).Where("aid IN (?)", param.Aids).Find(&arcs).Error; err != nil {
  286. log.Error("BatchEditArc check aids Error (%v)", err)
  287. return
  288. }
  289. if len(arcs) > 0 {
  290. arcMap := make(map[int64]*model.Arc, len(param.Aids))
  291. for _, v := range arcs {
  292. arcMap[v.Aid] = v
  293. }
  294. for _, aid := range param.Aids {
  295. if arc, ok := arcMap[aid]; ok {
  296. if arc.IsDeleted == _notDeleted {
  297. changeAids = append(changeAids, arc.Aid)
  298. }
  299. }
  300. }
  301. }
  302. if len(changeAids) == 0 {
  303. err = ecode.RequestErr
  304. return
  305. }
  306. arcRelation := new(model.ArcRelation)
  307. tx := s.dao.DB.Begin()
  308. if err = tx.Error; err != nil {
  309. log.Error("s.dao.DB.Begin error(%v)", err)
  310. return
  311. }
  312. for _, aid := range changeAids {
  313. var data *model.ArcRelation
  314. arg := &model.ArcImportParam{
  315. Aid: aid,
  316. Gids: param.Gids,
  317. MatchIDs: param.MatchIDs,
  318. TagIDs: param.TagIDs,
  319. TeamIDs: param.TeamIDs,
  320. Years: param.Years,
  321. }
  322. if data, err = s.arcRelationChanges(c, arg, _typeEdit); err != nil {
  323. return
  324. }
  325. arcRelation.UpAddGids = append(arcRelation.UpAddGids, data.UpAddGids...)
  326. arcRelation.UpDelGids = append(arcRelation.UpDelGids, data.UpDelGids...)
  327. arcRelation.AddGids = append(arcRelation.AddGids, data.AddGids...)
  328. arcRelation.UpAddMatchs = append(arcRelation.UpAddMatchs, data.UpAddMatchs...)
  329. arcRelation.UpDelMatchs = append(arcRelation.UpDelMatchs, data.UpDelMatchs...)
  330. arcRelation.AddMatchs = append(arcRelation.AddMatchs, data.AddMatchs...)
  331. arcRelation.UpAddTags = append(arcRelation.UpAddTags, data.UpAddTags...)
  332. arcRelation.UpDelTags = append(arcRelation.UpDelTags, data.UpDelTags...)
  333. arcRelation.AddTags = append(arcRelation.AddTags, data.AddTags...)
  334. arcRelation.UpAddTeams = append(arcRelation.UpAddTeams, data.UpAddTeams...)
  335. arcRelation.UpDelTeams = append(arcRelation.UpDelTeams, data.UpDelTeams...)
  336. arcRelation.AddTeams = append(arcRelation.AddTeams, data.AddTeams...)
  337. arcRelation.UpAddYears = append(arcRelation.UpAddYears, data.UpAddYears...)
  338. arcRelation.UpDelYears = append(arcRelation.UpDelYears, data.UpDelYears...)
  339. arcRelation.AddYears = append(arcRelation.AddYears, data.AddYears...)
  340. }
  341. if err = upArcRelation(tx, arcRelation); err != nil {
  342. err = tx.Rollback().Error
  343. return
  344. }
  345. err = tx.Commit().Error
  346. return
  347. }
  348. // ArcImportCSV archive import.
  349. func (s *Service) ArcImportCSV(c context.Context, list []*model.ArcImportParam) (err error) {
  350. var (
  351. aids, addAids, changeAids, saveAids []int64
  352. arcs []*model.Arc
  353. )
  354. listMap := make(map[int64]*model.ArcImportParam, len(aids))
  355. for _, v := range list {
  356. aids = append(aids, v.Aid)
  357. listMap[v.Aid] = v
  358. }
  359. if err = s.dao.DB.Model(&model.Arc{}).Where("aid IN (?)", aids).Find(&arcs).Error; err != nil {
  360. log.Error("arcImport check aids Error (%v)", err)
  361. return
  362. }
  363. if len(arcs) > 0 {
  364. arcMap := make(map[int64]*model.Arc, len(aids))
  365. for _, v := range arcs {
  366. arcMap[v.Aid] = v
  367. }
  368. for _, aid := range aids {
  369. if arc, ok := arcMap[aid]; ok {
  370. if arc.IsDeleted == _deleted {
  371. saveAids = append(saveAids, arc.ID)
  372. changeAids = append(changeAids, arc.Aid)
  373. }
  374. } else {
  375. addAids = append(addAids, aid)
  376. changeAids = append(changeAids, aid)
  377. }
  378. }
  379. } else {
  380. addAids = aids
  381. changeAids = aids
  382. }
  383. tx := s.dao.DB.Begin()
  384. if err = tx.Error; err != nil {
  385. log.Error("s.dao.DB.Begin error(%v)", err)
  386. return
  387. }
  388. if len(addAids) > 0 {
  389. if err = tx.Model(&model.Arc{}).Exec(model.ArcBatchAddSQL(addAids)).Error; err != nil {
  390. log.Error("arcImport Arc tx.Model Exec(%+v) error(%v)", addAids, err)
  391. err = tx.Rollback().Error
  392. return
  393. }
  394. }
  395. if len(saveAids) > 0 {
  396. if err = tx.Model(&model.Arc{}).Where("id IN (?)", saveAids).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  397. log.Error("arcImport Save(%+v) error(%v)", saveAids, err)
  398. err = tx.Rollback().Error
  399. return
  400. }
  401. }
  402. if len(changeAids) > 0 {
  403. arcRelation := new(model.ArcRelation)
  404. for _, aid := range changeAids {
  405. if arc, ok := listMap[aid]; ok {
  406. var data *model.ArcRelation
  407. arg := &model.ArcImportParam{
  408. Aid: aid,
  409. Gids: arc.Gids,
  410. MatchIDs: arc.MatchIDs,
  411. TagIDs: arc.TagIDs,
  412. TeamIDs: arc.TeamIDs,
  413. Years: arc.Years,
  414. }
  415. if data, err = s.arcRelationChanges(c, arg, _typeAdd); err != nil {
  416. return
  417. }
  418. arcRelation.UpAddGids = append(arcRelation.UpAddGids, data.UpAddGids...)
  419. arcRelation.UpDelGids = append(arcRelation.UpDelGids, data.UpDelGids...)
  420. arcRelation.AddGids = append(arcRelation.AddGids, data.AddGids...)
  421. arcRelation.UpAddMatchs = append(arcRelation.UpAddMatchs, data.UpAddMatchs...)
  422. arcRelation.UpDelMatchs = append(arcRelation.UpDelMatchs, data.UpDelMatchs...)
  423. arcRelation.AddMatchs = append(arcRelation.AddMatchs, data.AddMatchs...)
  424. arcRelation.UpAddTags = append(arcRelation.UpAddTags, data.UpAddTags...)
  425. arcRelation.UpDelTags = append(arcRelation.UpDelTags, data.UpDelTags...)
  426. arcRelation.AddTags = append(arcRelation.AddTags, data.AddTags...)
  427. arcRelation.UpAddTeams = append(arcRelation.UpAddTeams, data.UpAddTeams...)
  428. arcRelation.UpDelTeams = append(arcRelation.UpDelTeams, data.UpDelTeams...)
  429. arcRelation.AddTeams = append(arcRelation.AddTeams, data.AddTeams...)
  430. arcRelation.UpAddYears = append(arcRelation.UpAddYears, data.UpAddYears...)
  431. arcRelation.UpDelYears = append(arcRelation.UpDelYears, data.UpDelYears...)
  432. arcRelation.AddYears = append(arcRelation.AddYears, data.AddYears...)
  433. }
  434. }
  435. if err = upArcRelation(tx, arcRelation); err != nil {
  436. err = tx.Rollback().Error
  437. return
  438. }
  439. }
  440. err = tx.Commit().Error
  441. return
  442. }
  443. func (s *Service) arcRelationChanges(c context.Context, arg *model.ArcImportParam, typ string) (data *model.ArcRelation, err error) {
  444. data = new(model.ArcRelation)
  445. // add game map
  446. if len(arg.Gids) > 0 {
  447. var gidMaps []*model.GIDMap
  448. if err = s.dao.DB.Model(&model.GIDMap{}).Where("oid=?", arg.Aid).Where("gid IN (?)", arg.Gids).Where("type=?", model.TypeArc).Find(&gidMaps).Error; err != nil {
  449. log.Error("arcRelationChanges GIDMap s.dao.DB.Model MatchMap(%+v) error(%v)", arg.Gids, err)
  450. return
  451. }
  452. if len(gidMaps) > 0 {
  453. gidMap := make(map[int64]*model.GIDMap, len(gidMaps))
  454. for _, v := range gidMaps {
  455. gidMap[v.Gid] = v
  456. }
  457. for _, gid := range arg.Gids {
  458. if gidItem, ok := gidMap[gid]; ok {
  459. if gidItem.IsDeleted == _deleted {
  460. data.UpAddGids = append(data.UpAddGids, gidItem.ID)
  461. }
  462. } else {
  463. data.AddGids = append(data.AddGids, &model.GIDMap{Type: model.TypeArc, Gid: gid, Oid: arg.Aid})
  464. }
  465. }
  466. } else {
  467. for _, gid := range arg.Gids {
  468. data.AddGids = append(data.AddGids, &model.GIDMap{Type: model.TypeArc, Gid: gid, Oid: arg.Aid})
  469. }
  470. }
  471. }
  472. // add match map
  473. if len(arg.MatchIDs) > 0 {
  474. var matchs []*model.MatchMap
  475. if err = s.dao.DB.Model(&model.MatchMap{}).Where("aid=?", arg.Aid).Where("mid IN (?)", arg.MatchIDs).Find(&matchs).Error; err != nil {
  476. log.Error("arcRelationChanges Arc s.dao.DB.Model MatchMap(%+v) error(%v)", arg.MatchIDs, err)
  477. return
  478. }
  479. if len(matchs) > 0 {
  480. matchMap := make(map[int64]*model.MatchMap, len(matchs))
  481. for _, v := range matchs {
  482. matchMap[v.Mid] = v
  483. }
  484. for _, mid := range arg.MatchIDs {
  485. if match, ok := matchMap[mid]; ok {
  486. if match.IsDeleted == _deleted {
  487. data.UpAddMatchs = append(data.UpAddMatchs, match.ID)
  488. }
  489. } else {
  490. data.AddMatchs = append(data.AddMatchs, &model.MatchMap{Mid: mid, Aid: arg.Aid})
  491. }
  492. }
  493. } else {
  494. for _, mid := range arg.MatchIDs {
  495. data.AddMatchs = append(data.AddMatchs, &model.MatchMap{Mid: mid, Aid: arg.Aid})
  496. }
  497. }
  498. }
  499. // add tags map
  500. if len(arg.TagIDs) > 0 {
  501. var tags []*model.TagMap
  502. if err = s.dao.DB.Model(&model.TagMap{}).Where("aid=?", arg.Aid).Where("tid IN (?)", arg.TagIDs).Find(&tags).Error; err != nil {
  503. log.Error("arcRelationChanges Arc s.dao.DB.Model TagMap(%+v) error(%v)", arg.TagIDs, err)
  504. return
  505. }
  506. if len(tags) > 0 {
  507. tagMap := make(map[int64]*model.TagMap, len(tags))
  508. for _, v := range tags {
  509. tagMap[v.Tid] = v
  510. }
  511. for _, tid := range arg.TagIDs {
  512. if tag, ok := tagMap[tid]; ok {
  513. if tag.IsDeleted == _deleted {
  514. data.UpAddTags = append(data.UpAddTags, tag.ID)
  515. }
  516. } else {
  517. data.AddTags = append(data.AddTags, &model.TagMap{Tid: tid, Aid: arg.Aid})
  518. }
  519. }
  520. } else {
  521. for _, tid := range arg.TagIDs {
  522. data.AddTags = append(data.AddTags, &model.TagMap{Tid: tid, Aid: arg.Aid})
  523. }
  524. }
  525. }
  526. // add teams map
  527. if len(arg.TeamIDs) > 0 {
  528. var teams []*model.TeamMap
  529. if err = s.dao.DB.Model(&model.TeamMap{}).Where("aid=?", arg.Aid).Where("tid IN (?)", arg.TeamIDs).Find(&teams).Error; err != nil {
  530. log.Error("arcRelationChanges Arc s.dao.DB.Model TeamMap(%+v) error(%v)", arg.TeamIDs, err)
  531. return
  532. }
  533. if len(teams) > 0 {
  534. teamMap := make(map[int64]*model.TeamMap, len(teams))
  535. for _, v := range teams {
  536. teamMap[v.Tid] = v
  537. }
  538. for _, teamID := range arg.TeamIDs {
  539. if team, ok := teamMap[teamID]; ok {
  540. if team.IsDeleted == _deleted {
  541. data.UpAddTeams = append(data.UpAddTeams, team.ID)
  542. }
  543. } else {
  544. data.AddTeams = append(data.AddTeams, &model.TeamMap{Tid: teamID, Aid: arg.Aid})
  545. }
  546. }
  547. } else {
  548. for _, teamID := range arg.TeamIDs {
  549. data.AddTeams = append(data.AddTeams, &model.TeamMap{Tid: teamID, Aid: arg.Aid})
  550. }
  551. }
  552. }
  553. // add year map
  554. if len(arg.Years) > 0 {
  555. var yearMaps []*model.YearMap
  556. if err = s.dao.DB.Model(&model.YearMap{}).Where("aid=?", arg.Aid).Where("year IN (?)", arg.Years).Find(&yearMaps).Error; err != nil {
  557. log.Error("arcRelationChanges Arc s.dao.DB.Model YearMap(%+v) error(%v)", arg.Years, err)
  558. return
  559. }
  560. if len(yearMaps) > 0 {
  561. yearMap := make(map[int64]*model.YearMap, len(yearMaps))
  562. for _, v := range yearMaps {
  563. yearMap[v.Year] = v
  564. }
  565. for _, yearID := range arg.Years {
  566. if year, ok := yearMap[yearID]; ok {
  567. if year.IsDeleted == _deleted {
  568. data.UpAddYears = append(data.UpAddYears, year.ID)
  569. }
  570. } else {
  571. data.AddYears = append(data.AddYears, &model.YearMap{Year: yearID, Aid: arg.Aid})
  572. }
  573. }
  574. } else {
  575. for _, yearID := range arg.Years {
  576. data.AddYears = append(data.AddYears, &model.YearMap{Year: yearID, Aid: arg.Aid})
  577. }
  578. }
  579. }
  580. if typ == _typeEdit {
  581. var (
  582. gidMaps []*model.GIDMap
  583. matchs []*model.MatchMap
  584. tags []*model.TagMap
  585. teams []*model.TeamMap
  586. years []*model.YearMap
  587. )
  588. // gid map
  589. if err = s.dao.DB.Model(&model.GIDMap{}).Where("oid=?", arg.Aid).Where("type=?", model.TypeArc).Where("is_deleted=?", _notDeleted).Find(&gidMaps).Error; err != nil {
  590. log.Error("arcRelationChanges GIDMap s.dao.DB.Model MatchMap(%+v) error(%v)", arg.Gids, err)
  591. return
  592. }
  593. if len(gidMaps) > 0 {
  594. if len(arg.Gids) > 0 {
  595. gidMap := make(map[int64]int64, len(arg.Gids))
  596. for _, gid := range arg.Gids {
  597. gidMap[gid] = gid
  598. }
  599. for _, v := range gidMaps {
  600. if _, ok := gidMap[v.Gid]; !ok {
  601. data.UpDelGids = append(data.UpDelGids, v.ID)
  602. }
  603. }
  604. } else {
  605. for _, v := range gidMaps {
  606. data.UpDelGids = append(data.UpDelGids, v.ID)
  607. }
  608. }
  609. }
  610. // match
  611. if err = s.dao.DB.Model(&model.MatchMap{}).Where("aid=?", arg.Aid).Where("is_deleted=?", _notDeleted).Find(&matchs).Error; err != nil {
  612. log.Error("arcRelationChanges Arc s.dao.DB.Model MatchMap(%+v) error(%v)", arg.MatchIDs, err)
  613. return
  614. }
  615. if len(matchs) > 0 {
  616. if len(arg.MatchIDs) > 0 {
  617. matchIDMap := make(map[int64]int64, len(arg.MatchIDs))
  618. for _, id := range arg.MatchIDs {
  619. matchIDMap[id] = id
  620. }
  621. for _, v := range matchs {
  622. if _, ok := matchIDMap[v.Mid]; !ok {
  623. data.UpDelMatchs = append(data.UpDelMatchs, v.ID)
  624. }
  625. }
  626. } else {
  627. for _, v := range matchs {
  628. data.UpDelMatchs = append(data.UpDelMatchs, v.ID)
  629. }
  630. }
  631. }
  632. // tag
  633. if err = s.dao.DB.Model(&model.TagMap{}).Where("aid=?", arg.Aid).Where("is_deleted=?", _notDeleted).Find(&tags).Error; err != nil {
  634. log.Error("arcRelationChanges Arc s.dao.DB.Model TagMap(%+v) error(%v)", arg.TagIDs, err)
  635. return
  636. }
  637. if len(tags) > 0 {
  638. if len(arg.TagIDs) > 0 {
  639. tagIDMap := make(map[int64]int64, len(arg.TagIDs))
  640. for _, id := range arg.TagIDs {
  641. tagIDMap[id] = id
  642. }
  643. for _, v := range tags {
  644. if _, ok := tagIDMap[v.Tid]; !ok {
  645. data.UpDelTags = append(data.UpDelTags, v.ID)
  646. }
  647. }
  648. } else {
  649. for _, v := range tags {
  650. data.UpDelTags = append(data.UpDelTags, v.ID)
  651. }
  652. }
  653. }
  654. // team
  655. if err = s.dao.DB.Model(&model.TeamMap{}).Where("aid=?", arg.Aid).Where("is_deleted=?", _notDeleted).Find(&teams).Error; err != nil {
  656. log.Error("arcRelationChanges Arc s.dao.DB.Model MatchMap(%+v) error(%v)", arg.MatchIDs, err)
  657. return
  658. }
  659. if len(teams) > 0 {
  660. if len(arg.TeamIDs) > 0 {
  661. teamIDMap := make(map[int64]int64, len(arg.TeamIDs))
  662. for _, id := range arg.TeamIDs {
  663. teamIDMap[id] = id
  664. }
  665. for _, v := range teams {
  666. if _, ok := teamIDMap[v.Tid]; !ok {
  667. data.UpDelTeams = append(data.UpDelTeams, v.ID)
  668. }
  669. }
  670. } else {
  671. for _, v := range teams {
  672. data.UpDelTeams = append(data.UpDelTeams, v.ID)
  673. }
  674. }
  675. }
  676. // year
  677. if err = s.dao.DB.Model(&model.YearMap{}).Where("aid=?", arg.Aid).Where("is_deleted=?", _notDeleted).Find(&years).Error; err != nil {
  678. log.Error("arcRelationChanges Arc s.dao.DB.Model MatchMap(%+v) error(%v)", arg.MatchIDs, err)
  679. return
  680. }
  681. if len(years) > 0 {
  682. if len(arg.Years) > 0 {
  683. yearMap := make(map[int64]int64, len(arg.Years))
  684. for _, id := range arg.Years {
  685. yearMap[id] = id
  686. }
  687. for _, v := range years {
  688. if _, ok := yearMap[v.Year]; !ok {
  689. data.UpDelYears = append(data.UpDelYears, v.ID)
  690. }
  691. }
  692. } else {
  693. for _, v := range years {
  694. data.UpDelYears = append(data.UpDelYears, v.ID)
  695. }
  696. }
  697. }
  698. }
  699. return
  700. }
  701. // BatchDelArc batch del archive.
  702. func (s *Service) BatchDelArc(c context.Context, aids []int64) (err error) {
  703. tx := s.dao.DB.Begin()
  704. if err = tx.Error; err != nil {
  705. log.Error("s.dao.DB.Begin error(%v)", err)
  706. return
  707. }
  708. if err = tx.Model(&model.Arc{}).Where("aid IN (?)", aids).Update(map[string]int{"is_deleted": _deleted}).Error; err != nil {
  709. log.Error("BatchDelArc Arc s.dao.DB.Model Update(%+v) error(%v)", aids, err)
  710. err = tx.Rollback().Error
  711. return
  712. }
  713. if err = tx.Model(&model.GIDMap{}).Where("oid IN (?)", aids).Update(map[string]int{"is_deleted": _deleted}).Error; err != nil {
  714. log.Error("BatchDelArc GIDMap s.dao.DB.Model Update(%+v) error(%v)", aids, err)
  715. err = tx.Rollback().Error
  716. return
  717. }
  718. err = tx.Commit().Error
  719. return
  720. }
  721. func upArcRelation(tx *gorm.DB, data *model.ArcRelation) (err error) {
  722. if len(data.AddGids) > 0 {
  723. if err = tx.Model(&model.GIDMap{}).Exec(model.GidBatchAddSQL(data.AddGids)).Error; err != nil {
  724. log.Error("upArcRelation GIDMap tx.Model Exec(%+v) error(%v)", data.AddGids, err)
  725. return
  726. }
  727. }
  728. if len(data.UpAddGids) > 0 {
  729. if err = tx.Model(&model.GIDMap{}).Where("id IN (?)", data.UpAddGids).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  730. log.Error("upArcRelation Tag tx.Model Updates(%+v) error(%v)", data.UpAddGids, err)
  731. return
  732. }
  733. }
  734. if len(data.UpDelGids) > 0 {
  735. if err = tx.Model(&model.GIDMap{}).Where("id IN (?)", data.UpDelGids).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  736. log.Error("upArcRelation Tag tx.Model Updates(%+v) error(%v)", data.UpDelGids, err)
  737. return
  738. }
  739. }
  740. if len(data.AddMatchs) > 0 {
  741. if err = tx.Model(&model.MatchMap{}).Exec(model.BatchAddMachMapSQL(data.AddMatchs)).Error; err != nil {
  742. log.Error("upArcRelation Match tx.Model Exec(%+v) error(%v)", data.AddMatchs, err)
  743. return
  744. }
  745. }
  746. if len(data.UpAddMatchs) > 0 {
  747. if err = tx.Model(&model.MatchMap{}).Where("id IN (?)", data.UpAddMatchs).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  748. log.Error("upArcRelation Match tx.Model Updates(%+v) error(%v)", data.UpAddMatchs, err)
  749. return
  750. }
  751. }
  752. if len(data.UpDelMatchs) > 0 {
  753. if err = tx.Model(&model.MatchMap{}).Where("id IN (?)", data.UpDelMatchs).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  754. log.Error("upArcRelation Match tx.Model Updates(%+v) error(%v)", data.UpDelMatchs, err)
  755. return
  756. }
  757. }
  758. if len(data.AddTags) > 0 {
  759. if err = tx.Model(&model.TagMap{}).Exec(model.BatchAddTagMapSQL(data.AddTags)).Error; err != nil {
  760. log.Error("upArcRelation Tag tx.Model Exec(%+v) error(%v)", data.AddTags, err)
  761. return
  762. }
  763. }
  764. if len(data.UpAddTags) > 0 {
  765. if err = tx.Model(&model.TagMap{}).Where("id IN (?)", data.UpAddTags).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  766. log.Error("upArcRelation Tag tx.Model Updates(%+v) error(%v)", data.UpAddTags, err)
  767. return
  768. }
  769. }
  770. if len(data.UpDelTags) > 0 {
  771. if err = tx.Model(&model.TagMap{}).Where("id IN (?)", data.UpDelTags).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  772. log.Error("upArcRelation Tag tx.Model Updates(%+v) error(%v)", data.UpDelTags, err)
  773. return
  774. }
  775. }
  776. if len(data.AddTeams) > 0 {
  777. if err = tx.Model(&model.TeamMap{}).Exec(model.BatchAddTeamMapSQL(data.AddTeams)).Error; err != nil {
  778. log.Error("upArcRelation Team tx.Model Exec(%+v) error(%v)", data.AddTeams, err)
  779. return
  780. }
  781. }
  782. if len(data.UpAddTeams) > 0 {
  783. if err = tx.Model(&model.TeamMap{}).Where("id IN (?)", data.UpAddTeams).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  784. log.Error("upArcRelation Team tx.Model Updates(%+v) error(%v)", data.UpAddTags, err)
  785. return
  786. }
  787. }
  788. if len(data.UpDelTeams) > 0 {
  789. if err = tx.Model(&model.TeamMap{}).Where("id IN (?)", data.UpDelTeams).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  790. log.Error("upArcRelation Team tx.Model Updates(%+v) error(%v)", data.UpDelTags, err)
  791. return
  792. }
  793. }
  794. if len(data.AddYears) > 0 {
  795. if err = tx.Model(&model.YearMap{}).Exec(model.BatchAddYearMapSQL(data.AddYears)).Error; err != nil {
  796. log.Error("upArcRelation Year tx.Model Exec(%+v) error(%v)", data.AddYears, err)
  797. return
  798. }
  799. }
  800. if len(data.UpAddYears) > 0 {
  801. if err = tx.Model(&model.YearMap{}).Where("id IN (?)", data.UpAddYears).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
  802. log.Error("upArcRelation Year tx.Model Updates(%+v) error(%v)", data.UpAddTags, err)
  803. return
  804. }
  805. }
  806. if len(data.UpDelYears) > 0 {
  807. if err = tx.Model(&model.YearMap{}).Where("id IN (?)", data.UpDelYears).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
  808. log.Error("upArcRelation Year tx.Model Updates(%+v) error(%v)", data.UpDelTags, err)
  809. return
  810. }
  811. }
  812. return
  813. }