rts-sim-testing-service/service/publishedGi.go

209 lines
6.9 KiB
Go
Raw Normal View History

2023-07-18 17:19:03 +08:00
package service
import (
"fmt"
2023-10-20 14:27:13 +08:00
"sync"
"time"
"gorm.io/gorm/clause"
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"
"joylink.club/bj-rtsts-server/sys_error"
2023-07-18 17:19:03 +08:00
)
2023-10-20 14:27:13 +08:00
var publishMapMutex sync.Mutex
// 查找发布地图分页信息
func PageQueryPublished(req *dto.PagePublishedReqDto) *dto.PageDto {
dp := dbquery.Published
where := dp.Where(dp.Status.Eq(1))
2023-07-18 17:19:03 +08:00
if req.Name != "" {
where = where.Where(dp.Code.Like(fmt.Sprintf("%%%s%%", req.Name)))
}
result, count, err := where.Debug().FindByPage(req.Offset(), req.Size)
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
var dataIds []int32 // 数据列表
for _, r := range result {
dataIds = append(dataIds, r.DataID)
}
pv := dbquery.PublishedVersion
protoDatas, err := pv.Select(pv.ID, pv.Note, pv.PublishAt).Where(pv.ID.In(dataIds...)).Find()
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
2023-07-18 17:19:03 +08:00
}
return &dto.PageDto{
Total: int(count),
PageQueryDto: req.PageQueryDto,
Records: dto.ConvertFromPublisheds(result, protoDatas),
}
2023-07-18 17:19:03 +08:00
}
// 地图信息列表信息
func ListQueryPublished(req *dto.PublishedListReqDto) []*dto.PublishedDto {
where := dbquery.Published.Where(dbquery.Published.Status.Eq(1))
2023-07-18 17:19:03 +08:00
if req.Name != "" {
2023-11-21 13:33:41 +08:00
where = where.Where(dbquery.Published.Code.Like(fmt.Sprintf("%%%s%%", req.Name)))
2023-07-18 17:19:03 +08:00
}
result, err := where.Debug().Find()
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
var dataIds []int32 // 数据列表
for _, r := range result {
dataIds = append(dataIds, r.DataID)
}
pv := dbquery.PublishedVersion
protoDatas, err := pv.Select(pv.ID, pv.Proto, pv.Note, pv.PublishAt).Where(pv.ID.In(dataIds...)).Find()
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
return dto.ConvertFromPublisheds(result, protoDatas)
2023-07-18 17:19:03 +08:00
}
// 项目启动时查询发布地图信息
func ListAllPublished() []*dto.PublishedDto {
p := dbquery.Published
result, err := p.Select(p.ID, p.Code, p.Type, p.DataID).Where(dbquery.Published.Status.Eq(1)).Debug().Find()
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
var dataIds []int32 // 数据列表
for _, r := range result {
dataIds = append(dataIds, r.DataID)
}
pv := dbquery.PublishedVersion
protoDatas, err := pv.Select(pv.ID, pv.Proto, pv.Note, pv.PublishAt).Where(pv.ID.In(dataIds...)).Find()
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
return dto.ConvertFromPublisheds(result, protoDatas)
2023-08-01 17:16:16 +08:00
}
// 查询详细信息
func GetPublishedById(id int32) *dto.PublishedDto {
data, err := dbquery.Published.Where(dbquery.Published.ID.Eq(id)).Debug().First()
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
pv := dbquery.PublishedVersion
protoData, err := pv.Where(pv.ID.Eq(data.DataID)).Debug().First()
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
return dto.ConvertFromPublished(data, protoData)
2023-07-18 17:19:03 +08:00
}
// 草稿发布
func PublishFormDraft(req *dto.PublishReqDto, user *model.User) {
2023-10-20 14:27:13 +08:00
publishMapMutex.Lock()
defer publishMapMutex.Unlock()
2023-07-18 17:19:03 +08:00
draft := QueryDrafting(req.DraftId)
if draft.Proto == nil || len(draft.Proto) == 0 {
panic(sys_error.New(fmt.Sprintf("草稿[%v]绘图数据信息为空", req.DraftId)))
}
// 查询版本信息
vds, _ := dbquery.PublishedVersion.Select(dbquery.PublishedVersion.Version).
Where(dbquery.PublishedVersion.Code.Eq(req.Name)).Order(dbquery.PublishedVersion.Version.Desc()).Find()
var version int32 = 1
if len(vds) > 0 {
version = version + vds[len(vds)-1].Version
}
// 存入新版本数据
err := dbquery.PublishedVersion.Save(&model.PublishedVersion{
Code: req.Name,
2023-09-21 16:22:55 +08:00
Proto: draft.Proto,
UserID: user.ID,
PublishAt: time.Now(),
Note: req.Note,
Version: version,
})
if err != nil {
panic(sys_error.New("发布草稿数据失败", err))
2023-07-18 17:19:03 +08:00
}
versionData, err := dbquery.PublishedVersion.Select(dbquery.PublishedVersion.ID).
Where(dbquery.PublishedVersion.Code.Eq(req.Name), dbquery.PublishedVersion.Version.Eq(version)).First()
if err != nil {
panic(sys_error.New("发布草稿数据失败", err))
}
// 更新发布数据
dbquery.Published.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: dbquery.Published.Code.ColumnName().String()}},
DoUpdates: clause.AssignmentColumns([]string{dbquery.Published.DataID.ColumnName().String()}),
}).Create(&model.Published{Code: req.Name, Type: draft.Type, Category: draft.Category, DataID: versionData.ID, Status: 1})
2023-07-18 17:19:03 +08:00
}
// 删除发布图
func DeletePublishedById(id int32) {
dbquery.Published.Debug().Where(dbquery.Published.ID.Eq(id)).UpdateColumn(dbquery.Published.Status, 0)
dbquery.ProjectPublishLink.Where(dbquery.ProjectPublishLink.Mid.In(id)).Delete()
2023-07-18 17:19:03 +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(sys_error.New(fmt.Sprintf("草稿【%s】已存在", name)))
}
published, err := dbquery.Published.Where(dbquery.Published.ID.Eq(id)).Debug().First()
if err != nil {
panic(sys_error.New("查询发布数据出错", err))
}
versionData, err := dbquery.PublishedVersion.Where(dbquery.PublishedVersion.ID.Eq(published.DataID)).First()
if err != nil {
panic(sys_error.New("查询发布数据出错", err))
}
err1 := dbquery.Drafting.Save(&model.Drafting{
Name: name,
Category: published.Category,
Proto: versionData.Proto,
CreatorID: user.ID,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
Type: published.Type,
})
if err1 != nil {
panic(sys_error.New("保存草稿出错", err1))
}
}
// 查询项目关联的详细数据
func QueryProjectPublished(id int32) []*model.Published {
// 获取项目关联的发布地图
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
}
dp := dbquery.Published
publisheds, _ := dp.
Select(dp.ID, dp.Code, dp.Category, dp.Type).
Where(dp.ID.In(mids...), dp.Status.Eq(1)).
Order(dp.Type, dp.Code).
Find()
return publisheds
}
// 根据名称获取地图详细信息
func GetPublishedGiByName(param *dto.PublishedSingleQueryDto) *dto.PublishedDto {
published, err := dbquery.Published.Where(dbquery.Published.Code.Eq(param.Name), dbquery.Published.Status.Eq(1)).Debug().First()
if err != nil {
panic(sys_error.New("查询发布数据出错", err))
}
detailData := &model.PublishedVersion{}
if param.Detail {
versionData, err := dbquery.PublishedVersion.Where(dbquery.PublishedVersion.ID.Eq(published.DataID)).First()
if err != nil {
panic(sys_error.New("查询发布数据出错", err))
}
detailData = versionData
}
return dto.ConvertFromPublished(published, detailData)
}