rts-sim-testing-service/service/publishedGi.go
2023-08-01 17:08:45 +08:00

73 lines
2.3 KiB
Go

package service
import (
"fmt"
"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"
)
func PageQueryPublishedGi(req *dto.PublishedGiReqDto) (*dto.PageDto, error) {
w := dbquery.PublishedGi.Where()
if req.Name != "" {
w = w.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
}
result, count, err := w.Debug().FindByPage(req.Offset(), req.Size)
return &dto.PageDto{
Total: int(count),
PageQueryDto: req.PageQueryDto,
Records: result,
}, err
}
func ListQueryPublishedGi(req *dto.PublishedGiReqDto) ([]*model.PublishedGi, error) {
where := dbquery.PublishedGi.Where()
if req.Name != "" {
where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
}
return where.Debug().Find()
}
func GetPublishedGiById(id int) (*model.PublishedGi, error) {
return dbquery.PublishedGi.Where(dbquery.PublishedGi.ID.Eq(int32(id))).Debug().First()
}
func PublishFormDraft(req *dto.PublishReqDto, user *model.User) {
draft := QueryDrafting(req.DraftId)
if draft.Proto == nil || len(draft.Proto) == 0 {
panic(fmt.Sprintf("草稿[%v]绘图数据信息为空", req.DraftId))
}
entity := model.PublishedGi{
Name: req.Name,
Proto: draft.Proto,
UserID: user.ID,
PublishAt: time.Now(),
}
//删除同名数据
oldData, _ := dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Name.Eq(req.Name)).Find()
if oldData != nil && len(oldData) > 0 {
dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Name.Eq(req.Name)).Delete()
for _, v := range oldData {
memory.DeleteMapVerifyStructure(v.ID) // 移除内存中的发布信息
}
}
//插入新数据
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)).First()
// 地图信息更新到缓存
if newData != nil {
memory.PublishMapVerifyStructure(newData)
}
}
func DeletePublishedGiById(id int) {
_, _ = dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.ID.Eq(int32(id))).Delete()
memory.DeleteMapVerifyStructure(int32(id)) // 移除内存中的发布信息
}