package service import ( "fmt" "sync" "time" "joylink.club/bj-rtsts-server/ats/verify/simulation/wayside/memory" "joylink.club/bj-rtsts-server/db/dbquery" "joylink.club/bj-rtsts-server/db/model" "joylink.club/bj-rtsts-server/dto" "joylink.club/bj-rtsts-server/dto/publishedGi" ) var publishMapMutex sync.Mutex func PageQueryPublishedGi(req *publishedGi.PublishedGiReqDto) *dto.PageDto { dp := dbquery.PublishedGi where := dp.Where(dp.Status.Eq(1)) if req.Name != "" { where = where.Where(dp.Name.Like(fmt.Sprintf("%%%s%%", req.Name))) } result, count, err := where.Omit(dbquery.PublishedGi.Proto).Debug().FindByPage(req.Offset(), req.Size) if err != nil { panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()}) } return &dto.PageDto{ Total: int(count), PageQueryDto: req.PageQueryDto, Records: publishedGi.ConvertFromSlice(result), } } func ListQueryPublishedGi(req *publishedGi.PublishedGiListReqDto) []*publishedGi.PublishedGiDto { where := dbquery.PublishedGi.Omit(dbquery.PublishedGi.Proto).Where(dbquery.PublishedGi.Status.Eq(1)) if req.Name != "" { where = where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name))) } if req.Type != 0 { where = where.Where(dbquery.PublishedGi.Type.Eq(req.Type)) } if req.Category != "" { where = where.Where(dbquery.PublishedGi.Category.Eq(req.Category)) } find, err := where.Debug().Find() if err != nil { panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()}) } return publishedGi.ConvertFromSlice(find) } func ListAllPublishedGi() ([]*model.PublishedGi, error) { return dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Status.Eq(1)).Find() } func GetPublishedGiById(id int) (*model.PublishedGi, error) { return dbquery.PublishedGi.Where(dbquery.PublishedGi.ID.Eq(int32(id))).Debug().First() } func PublishFormDraft(req *publishedGi.PublishReqDto, user *model.User) { publishMapMutex.Lock() defer publishMapMutex.Unlock() draft := QueryDrafting(req.DraftId) if draft.Proto == nil || len(draft.Proto) == 0 { panic(fmt.Sprintf("草稿[%v]绘图数据信息为空", req.DraftId)) } //需要删除的同名数据 oldData, _ := dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Name.Eq(req.Name)).Find() oldDataLen := len(oldData) mids := make([]int32, oldDataLen) if oldDataLen > 0 { // 逻辑删除 dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Name.Eq(req.Name)).UpdateColumn(dbquery.PublishedGi.Status, 0) for i, v := range oldData { mids[i] = v.ID memory.DeleteMapVerifyStructure(v.ID) // 移除内存中的发布信息 } } entity := model.PublishedGi{ Name: req.Name, Proto: draft.Proto, UserID: user.ID, PublishAt: time.Now(), Category: draft.Category, Note: req.Note, Type: draft.Type, Status: 1, } //插入新数据 err := dbquery.PublishedGi.Debug().Create(&entity) if err != nil { panic(dto.ErrorDto{Code: dto.DataOperationError, Message: fmt.Sprintf("数据创建失败:\n%s", err.Error())}) } newData, _ := dbquery.PublishedGi. Where(dbquery.PublishedGi.Name.Eq(req.Name), dbquery.PublishedGi.Status.Eq(1)). Order(dbquery.PublishedGi.PublishAt.Desc()).First() // 对项目关联的地图进行修改 if oldDataLen > 0 { dbquery.ProjectPublishLink.Where(dbquery.ProjectPublishLink.Mid.In(mids...)).UpdateColumn(dbquery.ProjectPublishLink.Mid, newData.ID) } // 地图信息更新到缓存 if newData != nil { memory.PublishMapVerifyStructure(newData) } } func DeletePublishedGiById(id int) { mid := int32(id) dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.ID.Eq(mid)).UpdateColumn(dbquery.PublishedGi.Status, 0) memory.DeleteMapVerifyStructure(int32(id)) // 移除内存中的发布信息 dbquery.ProjectPublishLink.Where(dbquery.ProjectPublishLink.Mid.In(mid)).Delete() } func SaveAsDraftingFromPublish(id int32, user *model.User, name string) { num, _ := dbquery.Drafting.Where(dbquery.Drafting.Name.Eq(name)).Count() if num > 0 { // 处理重名情况 panic(dto.ErrorDto{Code: dto.DataOperationError, Message: fmt.Sprintf("草稿【%s】已存在", name)}) } publishedGi, err := dbquery.PublishedGi.Where(dbquery.PublishedGi.ID.Eq(id)).Debug().First() if err != nil { panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()}) } drafting := &model.Drafting{ Name: name, Category: publishedGi.Category, Proto: publishedGi.Proto, CreatorID: user.ID, CreatedAt: time.Now(), UpdateAt: time.Now(), Type: publishedGi.Type, } err1 := dbquery.Drafting.Save(drafting) if err1 != nil { panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()}) } } func QueryProjectPublishedGi(id int32) []*model.PublishedGi { // 获取项目关联的发布地图 dppl := dbquery.ProjectPublishLink links, _ := dppl.Select(dppl.Mid).Where(dppl.Pid.Eq(id)).Find() if len(links) == 0 { return nil } mids := make([]int32, len(links)) for i, m := range links { mids[i] = m.Mid } dp := dbquery.PublishedGi publishedGis, _ := dp.Select(dp.ID, dp.Name, dp.Category, dp.Type).Where(dp.ID.In(mids...), dp.Status.Eq(1)).Order(dp.Name).Find() return publishedGis } func GetPublishedGiByName(param *publishedGi.PublishedGiSingleQueryDto) (*model.PublishedGi, error) { where := dbquery.PublishedGi. Where(dbquery.PublishedGi.Name.Eq(param.Name)). Where(dbquery.PublishedGi.Status.Eq(1)) if !param.Detail { where = where.Omit(dbquery.PublishedGi.Proto) } return where.Debug().First() }