2023-07-18 17:19:03 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-07-31 18:20:00 +08:00
|
|
|
"time"
|
|
|
|
|
2023-08-07 15:32:36 +08:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"joylink.club/bj-rtsts-server/ats/verify/protos/graphicData"
|
2023-08-01 17:08:45 +08:00
|
|
|
"joylink.club/bj-rtsts-server/ats/verify/simulation/wayside/memory"
|
2023-07-18 17:19:03 +08:00
|
|
|
"joylink.club/bj-rtsts-server/db/dbquery"
|
|
|
|
"joylink.club/bj-rtsts-server/db/model"
|
|
|
|
"joylink.club/bj-rtsts-server/dto"
|
2023-08-01 14:47:04 +08:00
|
|
|
"joylink.club/bj-rtsts-server/dto/publishedGi"
|
2023-07-18 17:19:03 +08:00
|
|
|
)
|
|
|
|
|
2023-08-01 14:47:04 +08:00
|
|
|
func PageQueryPublishedGi(req *publishedGi.PublishedGiReqDto) *dto.PageDto {
|
2023-08-25 17:49:09 +08:00
|
|
|
dp := dbquery.PublishedGi
|
|
|
|
where := dp.Where(dp.Status.Eq(1))
|
2023-07-18 17:19:03 +08:00
|
|
|
if req.Name != "" {
|
2023-08-25 17:49:09 +08:00
|
|
|
where = where.Where(dp.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
|
2023-08-01 14:47:04 +08:00
|
|
|
}
|
2023-09-05 09:38:12 +08:00
|
|
|
result, count, err := where.Omit(dbquery.PublishedGi.Proto).Debug().FindByPage(req.Offset(), req.Size)
|
2023-08-01 14:47:04 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
|
|
|
return &dto.PageDto{
|
|
|
|
Total: int(count),
|
|
|
|
PageQueryDto: req.PageQueryDto,
|
2023-08-01 14:47:04 +08:00
|
|
|
Records: publishedGi.ConvertFromSlice(result),
|
|
|
|
}
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
|
|
|
|
2023-08-28 09:56:34 +08:00
|
|
|
func ListQueryPublishedGi(req *publishedGi.PublishedGiListReqDto) []*publishedGi.PublishedGiDto {
|
2023-08-31 16:16:18 +08:00
|
|
|
where := dbquery.PublishedGi.Omit(dbquery.PublishedGi.Proto).Where(dbquery.PublishedGi.Status.Eq(1))
|
2023-07-18 17:19:03 +08:00
|
|
|
if req.Name != "" {
|
2023-08-04 11:54:19 +08:00
|
|
|
where = where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
2023-08-01 14:47:04 +08:00
|
|
|
find, err := where.Debug().Find()
|
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
|
|
|
}
|
|
|
|
return publishedGi.ConvertFromSlice(find)
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
|
|
|
|
2023-08-01 17:16:16 +08:00
|
|
|
func ListAllPublishedGi() ([]*model.PublishedGi, error) {
|
2023-08-04 11:54:19 +08:00
|
|
|
return dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Status.Eq(1)).Find()
|
2023-08-01 17:16:16 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 17:19:03 +08:00
|
|
|
func GetPublishedGiById(id int) (*model.PublishedGi, error) {
|
|
|
|
return dbquery.PublishedGi.Where(dbquery.PublishedGi.ID.Eq(int32(id))).Debug().First()
|
|
|
|
}
|
|
|
|
|
2023-08-01 14:47:04 +08:00
|
|
|
func PublishFormDraft(req *publishedGi.PublishReqDto, user *model.User) {
|
2023-07-18 17:19:03 +08:00
|
|
|
draft := QueryDrafting(req.DraftId)
|
|
|
|
if draft.Proto == nil || len(draft.Proto) == 0 {
|
|
|
|
panic(fmt.Sprintf("草稿[%v]绘图数据信息为空", req.DraftId))
|
|
|
|
}
|
2023-08-04 11:54:19 +08:00
|
|
|
//需要删除的同名数据
|
2023-08-01 17:08:45 +08:00
|
|
|
oldData, _ := dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Name.Eq(req.Name)).Find()
|
2023-09-13 11:23:52 +08:00
|
|
|
oldDataLen := len(oldData)
|
|
|
|
mids := make([]int32, oldDataLen)
|
|
|
|
if oldDataLen > 0 {
|
2023-08-04 11:54:19 +08:00
|
|
|
// 逻辑删除
|
|
|
|
dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Name.Eq(req.Name)).UpdateColumn(dbquery.PublishedGi.Status, 0)
|
2023-09-13 11:23:52 +08:00
|
|
|
for i, v := range oldData {
|
|
|
|
mids[i] = v.ID
|
2023-08-01 17:08:45 +08:00
|
|
|
memory.DeleteMapVerifyStructure(v.ID) // 移除内存中的发布信息
|
|
|
|
}
|
|
|
|
}
|
2023-08-04 11:54:19 +08:00
|
|
|
entity := model.PublishedGi{
|
|
|
|
Name: req.Name,
|
2023-08-07 15:32:36 +08:00
|
|
|
Proto: handlerPublishedGiLinkData(draft.Proto, false),
|
2023-08-04 11:54:19 +08:00
|
|
|
UserID: user.ID,
|
|
|
|
PublishAt: time.Now(),
|
|
|
|
Category: draft.Category,
|
|
|
|
Note: req.Note,
|
2023-09-18 15:19:07 +08:00
|
|
|
Type: draft.Type,
|
2023-08-04 11:54:19 +08:00
|
|
|
Status: 1,
|
|
|
|
}
|
2023-07-31 10:15:02 +08:00
|
|
|
//插入新数据
|
|
|
|
err := dbquery.PublishedGi.Debug().Create(&entity)
|
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: fmt.Sprintf("数据创建失败:\n%s", err.Error())})
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
2023-08-04 11:54:19 +08:00
|
|
|
newData, _ := dbquery.PublishedGi.
|
|
|
|
Where(dbquery.PublishedGi.Name.Eq(req.Name), dbquery.PublishedGi.Status.Eq(1)).
|
|
|
|
Order(dbquery.PublishedGi.PublishAt.Desc()).First()
|
2023-09-13 11:23:52 +08:00
|
|
|
// 对项目关联的地图进行修改
|
|
|
|
if oldDataLen > 0 {
|
|
|
|
dbquery.ProjectPublishLink.Where(dbquery.ProjectPublishLink.Mid.In(mids...)).UpdateColumn(dbquery.ProjectPublishLink.Mid, newData.ID)
|
|
|
|
}
|
2023-08-01 17:08:45 +08:00
|
|
|
// 地图信息更新到缓存
|
|
|
|
if newData != nil {
|
|
|
|
memory.PublishMapVerifyStructure(newData)
|
|
|
|
}
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeletePublishedGiById(id int) {
|
2023-09-13 11:29:58 +08:00
|
|
|
mid := int32(id)
|
|
|
|
dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.ID.Eq(mid)).UpdateColumn(dbquery.PublishedGi.Status, 0)
|
2023-08-01 17:08:45 +08:00
|
|
|
memory.DeleteMapVerifyStructure(int32(id)) // 移除内存中的发布信息
|
2023-09-13 11:29:58 +08:00
|
|
|
dbquery.ProjectPublishLink.Where(dbquery.ProjectPublishLink.Mid.In(mid)).Delete()
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
2023-08-04 11:54:19 +08:00
|
|
|
|
|
|
|
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,
|
2023-08-07 15:32:36 +08:00
|
|
|
Proto: handlerPublishedGiLinkData(publishedGi.Proto, true),
|
2023-08-04 11:54:19 +08:00
|
|
|
CreatorID: user.ID,
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
UpdateAt: time.Now(),
|
2023-09-18 15:19:07 +08:00
|
|
|
Type: publishedGi.Type,
|
2023-08-04 11:54:19 +08:00
|
|
|
}
|
|
|
|
err1 := dbquery.Drafting.Save(drafting)
|
|
|
|
if err1 != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()})
|
|
|
|
}
|
|
|
|
}
|
2023-08-07 15:32:36 +08:00
|
|
|
|
2023-08-31 16:16:18 +08:00
|
|
|
func QueryProjectPublishedGi(id int32) []*model.PublishedGi {
|
2023-08-25 10:41:13 +08:00
|
|
|
// 获取项目关联的发布地图
|
|
|
|
dppl := dbquery.ProjectPublishLink
|
|
|
|
links, _ := dppl.Select(dppl.Mid).Where(dppl.Pid.Eq(id)).Find()
|
2023-08-31 16:16:18 +08:00
|
|
|
if len(links) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
mids := make([]int32, len(links))
|
|
|
|
for i, m := range links {
|
|
|
|
mids[i] = m.Mid
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-08-31 16:16:18 +08:00
|
|
|
dp := dbquery.PublishedGi
|
2023-09-18 15:19:07 +08:00
|
|
|
publishedGis, _ := dp.Select(dp.ID, dp.Name, dp.Category, dp.Type).Where(dp.ID.In(mids...), dp.Status.Eq(1)).Find()
|
2023-08-31 16:16:18 +08:00
|
|
|
return publishedGis
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
|
|
|
|
2023-08-07 15:32:36 +08:00
|
|
|
// 操作地图数据中的link数据
|
|
|
|
// del 代表是否删除地图中的link数据
|
|
|
|
func handlerPublishedGiLinkData(data []byte, del bool) []byte {
|
|
|
|
gd := &graphicData.RtssGraphicStorage{}
|
|
|
|
proto.Unmarshal(data, gd)
|
|
|
|
if del {
|
|
|
|
gd.CalculateLink = []*graphicData.CalculateLink{}
|
|
|
|
} else {
|
|
|
|
gd.CalculateLink = memory.BuildCalculateLinkData(gd)
|
|
|
|
}
|
|
|
|
rd, err := proto.Marshal(gd)
|
|
|
|
if err != nil {
|
|
|
|
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err.Error()})
|
|
|
|
}
|
|
|
|
return rd
|
|
|
|
}
|