123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826 |
- package service
- import (
- "context"
- "go-common/app/admin/main/esports/model"
- accwarden "go-common/app/service/main/account/api"
- "go-common/library/ecode"
- "go-common/library/log"
- "go-common/library/net/metadata"
- "github.com/jinzhu/gorm"
- )
- const (
- _typeAdd = "add"
- _typeEdit = "edit"
- )
- var _emptyArcList = make([]*model.ArcResult, 0)
- // ArcList archive list.
- func (s *Service) ArcList(c context.Context, arg *model.ArcListParam) (arcs []*model.ArcResult, total int, err error) {
- var (
- list []*model.SearchArc
- gids, tids, matchIDs, teamIDs, mids []int64
- games []*model.Game
- tags []*model.Tag
- matchs []*model.Match
- teams []*model.Team
- gameMap map[int64]*model.Game
- tagMap map[int64]*model.Tag
- matchMap map[int64]*model.Match
- teamMap map[int64]*model.Team
- infosReply *accwarden.InfosReply
- ip = metadata.String(c, metadata.RemoteIP)
- )
- if list, total, err = s.dao.SearchArc(c, arg); err != nil {
- return
- }
- if len(list) == 0 {
- arcs = _emptyArcList
- return
- }
- for _, arc := range list {
- if len(arc.Gid) > 0 {
- gids = append(gids, arc.Gid...)
- }
- if len(arc.Tags) > 0 {
- tids = append(tids, arc.Tags...)
- }
- if len(arc.Matchs) > 0 {
- matchIDs = append(matchIDs, arc.Matchs...)
- }
- if len(arc.Teams) > 0 {
- teamIDs = append(teamIDs, arc.Teams...)
- }
- if arc.Mid > 0 {
- mids = append(mids, arc.Mid)
- }
- }
- if len(gids) > 0 {
- if err = s.dao.DB.Model(&model.Game{}).Where("id IN (?)", unique(gids)).Find(&games).Error; err != nil {
- log.Error("ArcList Game gids(%+v) error(%v)", gids, err)
- return
- }
- if gl := len(games); gl > 0 {
- gameMap = make(map[int64]*model.Game, gl)
- for _, v := range games {
- gameMap[v.ID] = v
- }
- }
- }
- if len(tids) > 0 {
- if err = s.dao.DB.Model(&model.Tag{}).Where("id IN (?)", unique(tids)).Find(&tags).Error; err != nil {
- log.Error("ArcList Tag tids(%+v) error(%v)", tids, err)
- return
- }
- if tl := len(tags); tl > 0 {
- tagMap = make(map[int64]*model.Tag, tl)
- for _, v := range tags {
- tagMap[v.ID] = v
- }
- }
- }
- if len(matchIDs) > 0 {
- if err = s.dao.DB.Model(&model.Match{}).Where("id IN (?)", unique(matchIDs)).Find(&matchs).Error; err != nil {
- log.Error("ArcList Match ids(%+v) error(%v)", tids, err)
- return
- }
- if tl := len(matchs); tl > 0 {
- matchMap = make(map[int64]*model.Match, tl)
- for _, v := range matchs {
- matchMap[v.ID] = v
- }
- }
- }
- if len(teamIDs) > 0 {
- if err = s.dao.DB.Model(&model.Team{}).Where("id IN (?)", unique(teamIDs)).Find(&teams).Error; err != nil {
- log.Error("ArcList Team ids(%+v) error(%v)", tids, err)
- return
- }
- if tl := len(teams); tl > 0 {
- teamMap = make(map[int64]*model.Team, tl)
- for _, v := range teams {
- teamMap[v.ID] = v
- }
- }
- }
- if len(mids) > 0 {
- if infosReply, err = s.accClient.Infos3(c, &accwarden.MidsReq{Mids: unique(mids), RealIp: ip}); err != nil {
- log.Error("账号Infos3:grpc错误", "s.accClient.Infos3 error(%v)", err)
- return
- }
- }
- for _, v := range list {
- arcRes := &model.ArcResult{
- Aid: v.Aid,
- TypeID: v.TypeID,
- Title: v.Title,
- State: v.State,
- Mid: v.Mid,
- Years: v.Year,
- }
- if infosReply != nil && len(infosReply.Infos) > 0 {
- if info, ok := infosReply.Infos[v.Mid]; ok && info != nil {
- arcRes.Uname = info.Name
- }
- }
- if len(gameMap) > 0 {
- for _, gid := range v.Gid {
- if game, ok := gameMap[gid]; ok {
- arcRes.Games = append(arcRes.Games, game)
- }
- }
- }
- if len(tagMap) > 0 {
- for _, tid := range v.Tags {
- if tag, ok := tagMap[tid]; ok {
- arcRes.Tags = append(arcRes.Tags, tag)
- }
- }
- }
- if len(matchMap) > 0 {
- for _, tid := range v.Matchs {
- if match, ok := matchMap[tid]; ok {
- arcRes.Matchs = append(arcRes.Matchs, match)
- }
- }
- }
- if len(teamMap) > 0 {
- for _, tid := range v.Teams {
- if team, ok := teamMap[tid]; ok {
- arcRes.Teams = append(arcRes.Teams, team)
- }
- }
- }
- if len(arcRes.Games) == 0 {
- arcRes.Games = _emptyGameList
- }
- if len(arcRes.Tags) == 0 {
- arcRes.Tags = _emptyTagList
- }
- if len(arcRes.Matchs) == 0 {
- arcRes.Matchs = _emptyMatchList
- }
- if len(arcRes.Teams) == 0 {
- arcRes.Teams = _emptyTeamList
- }
- arcs = append(arcs, arcRes)
- }
- return
- }
- // EditArc edit archive.
- func (s *Service) EditArc(c context.Context, arg *model.ArcImportParam) (err error) {
- var preArc *model.Arc
- if err = s.dao.DB.Model(&model.Arc{}).Where("aid = ?", arg.Aid).Where("is_deleted=?", _notDeleted).First(&preArc).Error; err != nil {
- log.Error("EditArc check aid Error (%v)", err)
- return
- }
- var data *model.ArcRelation
- if data, err = s.arcRelationChanges(c, arg, _typeEdit); err != nil {
- return
- }
- tx := s.dao.DB.Begin()
- if err = tx.Error; err != nil {
- log.Error("s.dao.DB.Begin error(%v)", err)
- return
- }
- if err = upArcRelation(tx, data); err != nil {
- err = tx.Rollback().Error
- return
- }
- err = tx.Commit().Error
- return
- }
- // BatchAddArc batch add archive.
- func (s *Service) BatchAddArc(c context.Context, param *model.ArcAddParam) (err error) {
- var (
- arcs []*model.Arc
- upAddAids, addAids, changeAids []int64
- )
- if err = s.dao.DB.Model(&model.Arc{}).Where("aid IN (?)", param.Aids).Find(&arcs).Error; err != nil {
- log.Error("BatchAddArc check aids Error (%v)", err)
- return
- }
- if len(arcs) > 0 {
- arcMap := make(map[int64]*model.Arc, len(param.Aids))
- for _, v := range arcs {
- arcMap[v.Aid] = v
- }
- for _, aid := range param.Aids {
- if arc, ok := arcMap[aid]; ok {
- if arc.IsDeleted == _deleted {
- upAddAids = append(upAddAids, arc.ID)
- changeAids = append(changeAids, arc.Aid)
- }
- } else {
- addAids = append(addAids, aid)
- changeAids = append(changeAids, aid)
- }
- }
- } else {
- addAids = param.Aids
- changeAids = param.Aids
- }
- tx := s.dao.DB.Begin()
- if err = tx.Error; err != nil {
- log.Error("s.dao.DB.Begin error(%v)", err)
- return
- }
- if len(addAids) > 0 {
- if err = tx.Model(&model.Arc{}).Exec(model.ArcBatchAddSQL(addAids)).Error; err != nil {
- log.Error("BatchAddArc Arc tx.Model Exec(%+v) error(%v)", addAids, err)
- err = tx.Rollback().Error
- return
- }
- }
- if len(changeAids) > 0 {
- arcRelation := new(model.ArcRelation)
- for _, aid := range changeAids {
- var data *model.ArcRelation
- arg := &model.ArcImportParam{
- Aid: aid,
- Gids: param.Gids,
- MatchIDs: param.MatchIDs,
- TagIDs: param.TagIDs,
- TeamIDs: param.TeamIDs,
- Years: param.Years,
- }
- if data, err = s.arcRelationChanges(c, arg, _typeAdd); err != nil {
- return
- }
- arcRelation.UpAddGids = append(arcRelation.UpAddGids, data.UpAddGids...)
- arcRelation.UpDelGids = append(arcRelation.UpDelGids, data.UpDelGids...)
- arcRelation.AddGids = append(arcRelation.AddGids, data.AddGids...)
- arcRelation.UpAddMatchs = append(arcRelation.UpAddMatchs, data.UpAddMatchs...)
- arcRelation.UpDelMatchs = append(arcRelation.UpDelMatchs, data.UpDelMatchs...)
- arcRelation.AddMatchs = append(arcRelation.AddMatchs, data.AddMatchs...)
- arcRelation.UpAddTags = append(arcRelation.UpAddTags, data.UpAddTags...)
- arcRelation.UpDelTags = append(arcRelation.UpDelTags, data.UpDelTags...)
- arcRelation.AddTags = append(arcRelation.AddTags, data.AddTags...)
- arcRelation.UpAddTeams = append(arcRelation.UpAddTeams, data.UpAddTeams...)
- arcRelation.UpDelTeams = append(arcRelation.UpDelTeams, data.UpDelTeams...)
- arcRelation.AddTeams = append(arcRelation.AddTeams, data.AddTeams...)
- arcRelation.UpAddYears = append(arcRelation.UpAddYears, data.UpAddYears...)
- arcRelation.UpDelYears = append(arcRelation.UpDelYears, data.UpDelYears...)
- arcRelation.AddYears = append(arcRelation.AddYears, data.AddYears...)
- }
- if err = upArcRelation(tx, arcRelation); err != nil {
- err = tx.Rollback().Error
- return
- }
- }
- if len(upAddAids) > 0 {
- if err = tx.Model(&model.Arc{}).Where("id IN (?)", upAddAids).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
- log.Error("BatchAddArc Save(%+v) error(%v)", upAddAids, err)
- err = tx.Rollback().Error
- return
- }
- }
- err = tx.Commit().Error
- return
- }
- // BatchEditArc batch add arc.
- func (s *Service) BatchEditArc(c context.Context, param *model.ArcAddParam) (err error) {
- var (
- arcs []*model.Arc
- changeAids []int64
- )
- if err = s.dao.DB.Model(&model.Arc{}).Where("aid IN (?)", param.Aids).Find(&arcs).Error; err != nil {
- log.Error("BatchEditArc check aids Error (%v)", err)
- return
- }
- if len(arcs) > 0 {
- arcMap := make(map[int64]*model.Arc, len(param.Aids))
- for _, v := range arcs {
- arcMap[v.Aid] = v
- }
- for _, aid := range param.Aids {
- if arc, ok := arcMap[aid]; ok {
- if arc.IsDeleted == _notDeleted {
- changeAids = append(changeAids, arc.Aid)
- }
- }
- }
- }
- if len(changeAids) == 0 {
- err = ecode.RequestErr
- return
- }
- arcRelation := new(model.ArcRelation)
- tx := s.dao.DB.Begin()
- if err = tx.Error; err != nil {
- log.Error("s.dao.DB.Begin error(%v)", err)
- return
- }
- for _, aid := range changeAids {
- var data *model.ArcRelation
- arg := &model.ArcImportParam{
- Aid: aid,
- Gids: param.Gids,
- MatchIDs: param.MatchIDs,
- TagIDs: param.TagIDs,
- TeamIDs: param.TeamIDs,
- Years: param.Years,
- }
- if data, err = s.arcRelationChanges(c, arg, _typeEdit); err != nil {
- return
- }
- arcRelation.UpAddGids = append(arcRelation.UpAddGids, data.UpAddGids...)
- arcRelation.UpDelGids = append(arcRelation.UpDelGids, data.UpDelGids...)
- arcRelation.AddGids = append(arcRelation.AddGids, data.AddGids...)
- arcRelation.UpAddMatchs = append(arcRelation.UpAddMatchs, data.UpAddMatchs...)
- arcRelation.UpDelMatchs = append(arcRelation.UpDelMatchs, data.UpDelMatchs...)
- arcRelation.AddMatchs = append(arcRelation.AddMatchs, data.AddMatchs...)
- arcRelation.UpAddTags = append(arcRelation.UpAddTags, data.UpAddTags...)
- arcRelation.UpDelTags = append(arcRelation.UpDelTags, data.UpDelTags...)
- arcRelation.AddTags = append(arcRelation.AddTags, data.AddTags...)
- arcRelation.UpAddTeams = append(arcRelation.UpAddTeams, data.UpAddTeams...)
- arcRelation.UpDelTeams = append(arcRelation.UpDelTeams, data.UpDelTeams...)
- arcRelation.AddTeams = append(arcRelation.AddTeams, data.AddTeams...)
- arcRelation.UpAddYears = append(arcRelation.UpAddYears, data.UpAddYears...)
- arcRelation.UpDelYears = append(arcRelation.UpDelYears, data.UpDelYears...)
- arcRelation.AddYears = append(arcRelation.AddYears, data.AddYears...)
- }
- if err = upArcRelation(tx, arcRelation); err != nil {
- err = tx.Rollback().Error
- return
- }
- err = tx.Commit().Error
- return
- }
- // ArcImportCSV archive import.
- func (s *Service) ArcImportCSV(c context.Context, list []*model.ArcImportParam) (err error) {
- var (
- aids, addAids, changeAids, saveAids []int64
- arcs []*model.Arc
- )
- listMap := make(map[int64]*model.ArcImportParam, len(aids))
- for _, v := range list {
- aids = append(aids, v.Aid)
- listMap[v.Aid] = v
- }
- if err = s.dao.DB.Model(&model.Arc{}).Where("aid IN (?)", aids).Find(&arcs).Error; err != nil {
- log.Error("arcImport check aids Error (%v)", err)
- return
- }
- if len(arcs) > 0 {
- arcMap := make(map[int64]*model.Arc, len(aids))
- for _, v := range arcs {
- arcMap[v.Aid] = v
- }
- for _, aid := range aids {
- if arc, ok := arcMap[aid]; ok {
- if arc.IsDeleted == _deleted {
- saveAids = append(saveAids, arc.ID)
- changeAids = append(changeAids, arc.Aid)
- }
- } else {
- addAids = append(addAids, aid)
- changeAids = append(changeAids, aid)
- }
- }
- } else {
- addAids = aids
- changeAids = aids
- }
- tx := s.dao.DB.Begin()
- if err = tx.Error; err != nil {
- log.Error("s.dao.DB.Begin error(%v)", err)
- return
- }
- if len(addAids) > 0 {
- if err = tx.Model(&model.Arc{}).Exec(model.ArcBatchAddSQL(addAids)).Error; err != nil {
- log.Error("arcImport Arc tx.Model Exec(%+v) error(%v)", addAids, err)
- err = tx.Rollback().Error
- return
- }
- }
- if len(saveAids) > 0 {
- if err = tx.Model(&model.Arc{}).Where("id IN (?)", saveAids).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
- log.Error("arcImport Save(%+v) error(%v)", saveAids, err)
- err = tx.Rollback().Error
- return
- }
- }
- if len(changeAids) > 0 {
- arcRelation := new(model.ArcRelation)
- for _, aid := range changeAids {
- if arc, ok := listMap[aid]; ok {
- var data *model.ArcRelation
- arg := &model.ArcImportParam{
- Aid: aid,
- Gids: arc.Gids,
- MatchIDs: arc.MatchIDs,
- TagIDs: arc.TagIDs,
- TeamIDs: arc.TeamIDs,
- Years: arc.Years,
- }
- if data, err = s.arcRelationChanges(c, arg, _typeAdd); err != nil {
- return
- }
- arcRelation.UpAddGids = append(arcRelation.UpAddGids, data.UpAddGids...)
- arcRelation.UpDelGids = append(arcRelation.UpDelGids, data.UpDelGids...)
- arcRelation.AddGids = append(arcRelation.AddGids, data.AddGids...)
- arcRelation.UpAddMatchs = append(arcRelation.UpAddMatchs, data.UpAddMatchs...)
- arcRelation.UpDelMatchs = append(arcRelation.UpDelMatchs, data.UpDelMatchs...)
- arcRelation.AddMatchs = append(arcRelation.AddMatchs, data.AddMatchs...)
- arcRelation.UpAddTags = append(arcRelation.UpAddTags, data.UpAddTags...)
- arcRelation.UpDelTags = append(arcRelation.UpDelTags, data.UpDelTags...)
- arcRelation.AddTags = append(arcRelation.AddTags, data.AddTags...)
- arcRelation.UpAddTeams = append(arcRelation.UpAddTeams, data.UpAddTeams...)
- arcRelation.UpDelTeams = append(arcRelation.UpDelTeams, data.UpDelTeams...)
- arcRelation.AddTeams = append(arcRelation.AddTeams, data.AddTeams...)
- arcRelation.UpAddYears = append(arcRelation.UpAddYears, data.UpAddYears...)
- arcRelation.UpDelYears = append(arcRelation.UpDelYears, data.UpDelYears...)
- arcRelation.AddYears = append(arcRelation.AddYears, data.AddYears...)
- }
- }
- if err = upArcRelation(tx, arcRelation); err != nil {
- err = tx.Rollback().Error
- return
- }
- }
- err = tx.Commit().Error
- return
- }
- func (s *Service) arcRelationChanges(c context.Context, arg *model.ArcImportParam, typ string) (data *model.ArcRelation, err error) {
- data = new(model.ArcRelation)
- // add game map
- if len(arg.Gids) > 0 {
- var gidMaps []*model.GIDMap
- 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 {
- log.Error("arcRelationChanges GIDMap s.dao.DB.Model MatchMap(%+v) error(%v)", arg.Gids, err)
- return
- }
- if len(gidMaps) > 0 {
- gidMap := make(map[int64]*model.GIDMap, len(gidMaps))
- for _, v := range gidMaps {
- gidMap[v.Gid] = v
- }
- for _, gid := range arg.Gids {
- if gidItem, ok := gidMap[gid]; ok {
- if gidItem.IsDeleted == _deleted {
- data.UpAddGids = append(data.UpAddGids, gidItem.ID)
- }
- } else {
- data.AddGids = append(data.AddGids, &model.GIDMap{Type: model.TypeArc, Gid: gid, Oid: arg.Aid})
- }
- }
- } else {
- for _, gid := range arg.Gids {
- data.AddGids = append(data.AddGids, &model.GIDMap{Type: model.TypeArc, Gid: gid, Oid: arg.Aid})
- }
- }
- }
- // add match map
- if len(arg.MatchIDs) > 0 {
- var matchs []*model.MatchMap
- if err = s.dao.DB.Model(&model.MatchMap{}).Where("aid=?", arg.Aid).Where("mid IN (?)", arg.MatchIDs).Find(&matchs).Error; err != nil {
- log.Error("arcRelationChanges Arc s.dao.DB.Model MatchMap(%+v) error(%v)", arg.MatchIDs, err)
- return
- }
- if len(matchs) > 0 {
- matchMap := make(map[int64]*model.MatchMap, len(matchs))
- for _, v := range matchs {
- matchMap[v.Mid] = v
- }
- for _, mid := range arg.MatchIDs {
- if match, ok := matchMap[mid]; ok {
- if match.IsDeleted == _deleted {
- data.UpAddMatchs = append(data.UpAddMatchs, match.ID)
- }
- } else {
- data.AddMatchs = append(data.AddMatchs, &model.MatchMap{Mid: mid, Aid: arg.Aid})
- }
- }
- } else {
- for _, mid := range arg.MatchIDs {
- data.AddMatchs = append(data.AddMatchs, &model.MatchMap{Mid: mid, Aid: arg.Aid})
- }
- }
- }
- // add tags map
- if len(arg.TagIDs) > 0 {
- var tags []*model.TagMap
- if err = s.dao.DB.Model(&model.TagMap{}).Where("aid=?", arg.Aid).Where("tid IN (?)", arg.TagIDs).Find(&tags).Error; err != nil {
- log.Error("arcRelationChanges Arc s.dao.DB.Model TagMap(%+v) error(%v)", arg.TagIDs, err)
- return
- }
- if len(tags) > 0 {
- tagMap := make(map[int64]*model.TagMap, len(tags))
- for _, v := range tags {
- tagMap[v.Tid] = v
- }
- for _, tid := range arg.TagIDs {
- if tag, ok := tagMap[tid]; ok {
- if tag.IsDeleted == _deleted {
- data.UpAddTags = append(data.UpAddTags, tag.ID)
- }
- } else {
- data.AddTags = append(data.AddTags, &model.TagMap{Tid: tid, Aid: arg.Aid})
- }
- }
- } else {
- for _, tid := range arg.TagIDs {
- data.AddTags = append(data.AddTags, &model.TagMap{Tid: tid, Aid: arg.Aid})
- }
- }
- }
- // add teams map
- if len(arg.TeamIDs) > 0 {
- var teams []*model.TeamMap
- if err = s.dao.DB.Model(&model.TeamMap{}).Where("aid=?", arg.Aid).Where("tid IN (?)", arg.TeamIDs).Find(&teams).Error; err != nil {
- log.Error("arcRelationChanges Arc s.dao.DB.Model TeamMap(%+v) error(%v)", arg.TeamIDs, err)
- return
- }
- if len(teams) > 0 {
- teamMap := make(map[int64]*model.TeamMap, len(teams))
- for _, v := range teams {
- teamMap[v.Tid] = v
- }
- for _, teamID := range arg.TeamIDs {
- if team, ok := teamMap[teamID]; ok {
- if team.IsDeleted == _deleted {
- data.UpAddTeams = append(data.UpAddTeams, team.ID)
- }
- } else {
- data.AddTeams = append(data.AddTeams, &model.TeamMap{Tid: teamID, Aid: arg.Aid})
- }
- }
- } else {
- for _, teamID := range arg.TeamIDs {
- data.AddTeams = append(data.AddTeams, &model.TeamMap{Tid: teamID, Aid: arg.Aid})
- }
- }
- }
- // add year map
- if len(arg.Years) > 0 {
- var yearMaps []*model.YearMap
- if err = s.dao.DB.Model(&model.YearMap{}).Where("aid=?", arg.Aid).Where("year IN (?)", arg.Years).Find(&yearMaps).Error; err != nil {
- log.Error("arcRelationChanges Arc s.dao.DB.Model YearMap(%+v) error(%v)", arg.Years, err)
- return
- }
- if len(yearMaps) > 0 {
- yearMap := make(map[int64]*model.YearMap, len(yearMaps))
- for _, v := range yearMaps {
- yearMap[v.Year] = v
- }
- for _, yearID := range arg.Years {
- if year, ok := yearMap[yearID]; ok {
- if year.IsDeleted == _deleted {
- data.UpAddYears = append(data.UpAddYears, year.ID)
- }
- } else {
- data.AddYears = append(data.AddYears, &model.YearMap{Year: yearID, Aid: arg.Aid})
- }
- }
- } else {
- for _, yearID := range arg.Years {
- data.AddYears = append(data.AddYears, &model.YearMap{Year: yearID, Aid: arg.Aid})
- }
- }
- }
- if typ == _typeEdit {
- var (
- gidMaps []*model.GIDMap
- matchs []*model.MatchMap
- tags []*model.TagMap
- teams []*model.TeamMap
- years []*model.YearMap
- )
- // gid map
- 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 {
- log.Error("arcRelationChanges GIDMap s.dao.DB.Model MatchMap(%+v) error(%v)", arg.Gids, err)
- return
- }
- if len(gidMaps) > 0 {
- if len(arg.Gids) > 0 {
- gidMap := make(map[int64]int64, len(arg.Gids))
- for _, gid := range arg.Gids {
- gidMap[gid] = gid
- }
- for _, v := range gidMaps {
- if _, ok := gidMap[v.Gid]; !ok {
- data.UpDelGids = append(data.UpDelGids, v.ID)
- }
- }
- } else {
- for _, v := range gidMaps {
- data.UpDelGids = append(data.UpDelGids, v.ID)
- }
- }
- }
- // match
- if err = s.dao.DB.Model(&model.MatchMap{}).Where("aid=?", arg.Aid).Where("is_deleted=?", _notDeleted).Find(&matchs).Error; err != nil {
- log.Error("arcRelationChanges Arc s.dao.DB.Model MatchMap(%+v) error(%v)", arg.MatchIDs, err)
- return
- }
- if len(matchs) > 0 {
- if len(arg.MatchIDs) > 0 {
- matchIDMap := make(map[int64]int64, len(arg.MatchIDs))
- for _, id := range arg.MatchIDs {
- matchIDMap[id] = id
- }
- for _, v := range matchs {
- if _, ok := matchIDMap[v.Mid]; !ok {
- data.UpDelMatchs = append(data.UpDelMatchs, v.ID)
- }
- }
- } else {
- for _, v := range matchs {
- data.UpDelMatchs = append(data.UpDelMatchs, v.ID)
- }
- }
- }
- // tag
- if err = s.dao.DB.Model(&model.TagMap{}).Where("aid=?", arg.Aid).Where("is_deleted=?", _notDeleted).Find(&tags).Error; err != nil {
- log.Error("arcRelationChanges Arc s.dao.DB.Model TagMap(%+v) error(%v)", arg.TagIDs, err)
- return
- }
- if len(tags) > 0 {
- if len(arg.TagIDs) > 0 {
- tagIDMap := make(map[int64]int64, len(arg.TagIDs))
- for _, id := range arg.TagIDs {
- tagIDMap[id] = id
- }
- for _, v := range tags {
- if _, ok := tagIDMap[v.Tid]; !ok {
- data.UpDelTags = append(data.UpDelTags, v.ID)
- }
- }
- } else {
- for _, v := range tags {
- data.UpDelTags = append(data.UpDelTags, v.ID)
- }
- }
- }
- // team
- if err = s.dao.DB.Model(&model.TeamMap{}).Where("aid=?", arg.Aid).Where("is_deleted=?", _notDeleted).Find(&teams).Error; err != nil {
- log.Error("arcRelationChanges Arc s.dao.DB.Model MatchMap(%+v) error(%v)", arg.MatchIDs, err)
- return
- }
- if len(teams) > 0 {
- if len(arg.TeamIDs) > 0 {
- teamIDMap := make(map[int64]int64, len(arg.TeamIDs))
- for _, id := range arg.TeamIDs {
- teamIDMap[id] = id
- }
- for _, v := range teams {
- if _, ok := teamIDMap[v.Tid]; !ok {
- data.UpDelTeams = append(data.UpDelTeams, v.ID)
- }
- }
- } else {
- for _, v := range teams {
- data.UpDelTeams = append(data.UpDelTeams, v.ID)
- }
- }
- }
- // year
- if err = s.dao.DB.Model(&model.YearMap{}).Where("aid=?", arg.Aid).Where("is_deleted=?", _notDeleted).Find(&years).Error; err != nil {
- log.Error("arcRelationChanges Arc s.dao.DB.Model MatchMap(%+v) error(%v)", arg.MatchIDs, err)
- return
- }
- if len(years) > 0 {
- if len(arg.Years) > 0 {
- yearMap := make(map[int64]int64, len(arg.Years))
- for _, id := range arg.Years {
- yearMap[id] = id
- }
- for _, v := range years {
- if _, ok := yearMap[v.Year]; !ok {
- data.UpDelYears = append(data.UpDelYears, v.ID)
- }
- }
- } else {
- for _, v := range years {
- data.UpDelYears = append(data.UpDelYears, v.ID)
- }
- }
- }
- }
- return
- }
- // BatchDelArc batch del archive.
- func (s *Service) BatchDelArc(c context.Context, aids []int64) (err error) {
- tx := s.dao.DB.Begin()
- if err = tx.Error; err != nil {
- log.Error("s.dao.DB.Begin error(%v)", err)
- return
- }
- if err = tx.Model(&model.Arc{}).Where("aid IN (?)", aids).Update(map[string]int{"is_deleted": _deleted}).Error; err != nil {
- log.Error("BatchDelArc Arc s.dao.DB.Model Update(%+v) error(%v)", aids, err)
- err = tx.Rollback().Error
- return
- }
- if err = tx.Model(&model.GIDMap{}).Where("oid IN (?)", aids).Update(map[string]int{"is_deleted": _deleted}).Error; err != nil {
- log.Error("BatchDelArc GIDMap s.dao.DB.Model Update(%+v) error(%v)", aids, err)
- err = tx.Rollback().Error
- return
- }
- err = tx.Commit().Error
- return
- }
- func upArcRelation(tx *gorm.DB, data *model.ArcRelation) (err error) {
- if len(data.AddGids) > 0 {
- if err = tx.Model(&model.GIDMap{}).Exec(model.GidBatchAddSQL(data.AddGids)).Error; err != nil {
- log.Error("upArcRelation GIDMap tx.Model Exec(%+v) error(%v)", data.AddGids, err)
- return
- }
- }
- if len(data.UpAddGids) > 0 {
- if err = tx.Model(&model.GIDMap{}).Where("id IN (?)", data.UpAddGids).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
- log.Error("upArcRelation Tag tx.Model Updates(%+v) error(%v)", data.UpAddGids, err)
- return
- }
- }
- if len(data.UpDelGids) > 0 {
- if err = tx.Model(&model.GIDMap{}).Where("id IN (?)", data.UpDelGids).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
- log.Error("upArcRelation Tag tx.Model Updates(%+v) error(%v)", data.UpDelGids, err)
- return
- }
- }
- if len(data.AddMatchs) > 0 {
- if err = tx.Model(&model.MatchMap{}).Exec(model.BatchAddMachMapSQL(data.AddMatchs)).Error; err != nil {
- log.Error("upArcRelation Match tx.Model Exec(%+v) error(%v)", data.AddMatchs, err)
- return
- }
- }
- if len(data.UpAddMatchs) > 0 {
- if err = tx.Model(&model.MatchMap{}).Where("id IN (?)", data.UpAddMatchs).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
- log.Error("upArcRelation Match tx.Model Updates(%+v) error(%v)", data.UpAddMatchs, err)
- return
- }
- }
- if len(data.UpDelMatchs) > 0 {
- if err = tx.Model(&model.MatchMap{}).Where("id IN (?)", data.UpDelMatchs).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
- log.Error("upArcRelation Match tx.Model Updates(%+v) error(%v)", data.UpDelMatchs, err)
- return
- }
- }
- if len(data.AddTags) > 0 {
- if err = tx.Model(&model.TagMap{}).Exec(model.BatchAddTagMapSQL(data.AddTags)).Error; err != nil {
- log.Error("upArcRelation Tag tx.Model Exec(%+v) error(%v)", data.AddTags, err)
- return
- }
- }
- if len(data.UpAddTags) > 0 {
- if err = tx.Model(&model.TagMap{}).Where("id IN (?)", data.UpAddTags).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
- log.Error("upArcRelation Tag tx.Model Updates(%+v) error(%v)", data.UpAddTags, err)
- return
- }
- }
- if len(data.UpDelTags) > 0 {
- if err = tx.Model(&model.TagMap{}).Where("id IN (?)", data.UpDelTags).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
- log.Error("upArcRelation Tag tx.Model Updates(%+v) error(%v)", data.UpDelTags, err)
- return
- }
- }
- if len(data.AddTeams) > 0 {
- if err = tx.Model(&model.TeamMap{}).Exec(model.BatchAddTeamMapSQL(data.AddTeams)).Error; err != nil {
- log.Error("upArcRelation Team tx.Model Exec(%+v) error(%v)", data.AddTeams, err)
- return
- }
- }
- if len(data.UpAddTeams) > 0 {
- if err = tx.Model(&model.TeamMap{}).Where("id IN (?)", data.UpAddTeams).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
- log.Error("upArcRelation Team tx.Model Updates(%+v) error(%v)", data.UpAddTags, err)
- return
- }
- }
- if len(data.UpDelTeams) > 0 {
- if err = tx.Model(&model.TeamMap{}).Where("id IN (?)", data.UpDelTeams).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
- log.Error("upArcRelation Team tx.Model Updates(%+v) error(%v)", data.UpDelTags, err)
- return
- }
- }
- if len(data.AddYears) > 0 {
- if err = tx.Model(&model.YearMap{}).Exec(model.BatchAddYearMapSQL(data.AddYears)).Error; err != nil {
- log.Error("upArcRelation Year tx.Model Exec(%+v) error(%v)", data.AddYears, err)
- return
- }
- }
- if len(data.UpAddYears) > 0 {
- if err = tx.Model(&model.YearMap{}).Where("id IN (?)", data.UpAddYears).Updates(map[string]interface{}{"is_deleted": _notDeleted}).Error; err != nil {
- log.Error("upArcRelation Year tx.Model Updates(%+v) error(%v)", data.UpAddTags, err)
- return
- }
- }
- if len(data.UpDelYears) > 0 {
- if err = tx.Model(&model.YearMap{}).Where("id IN (?)", data.UpDelYears).Updates(map[string]interface{}{"is_deleted": _deleted}).Error; err != nil {
- log.Error("upArcRelation Year tx.Model Updates(%+v) error(%v)", data.UpDelTags, err)
- return
- }
- }
- return
- }
|