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

271 lines
8.3 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"
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 {
p, pv, u := dbquery.Published, dbquery.PublishedVersion, dbquery.User
where := p.
Select(
p.ID, p.Code.As("name"), p.Type, p.Category, p.Status,
pv.Note, pv.PublishAt, pv.Version, u.Name.As("publisher"),
).
LeftJoin(pv, p.DataID.EqCol(pv.ID)).
LeftJoin(u, pv.UserID.EqCol(u.ID)).
2023-12-01 15:09:40 +08:00
Order(p.Status.Desc(), pv.PublishAt.Desc())
2023-07-18 17:19:03 +08:00
if req.Name != "" {
2023-11-22 17:05:38 +08:00
where = where.Where(p.Code.Like(fmt.Sprintf("%%%s%%", req.Name)))
}
2023-11-22 17:05:38 +08:00
if req.Release {
where = where.Where(p.Status.Eq(1))
}
2023-11-22 17:05:38 +08:00
var records []*dto.PublishedDto
count, err := where.Debug().ScanByPage(&records, req.Offset(), req.Size)
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
2023-07-18 17:19:03 +08:00
}
2023-11-22 17:05:38 +08:00
return &dto.PageDto{Total: int(count), PageQueryDto: req.PageQueryDto, Records: records}
2023-07-18 17:19:03 +08:00
}
// 地图信息列表信息
func ListQueryPublished(req *dto.PublishedListReqDto) []*dto.PublishedDto {
2023-11-22 17:05:38 +08:00
p := dbquery.Published
where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category).Where(p.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
}
2023-11-22 17:05:38 +08:00
if req.Type != 0 {
where = where.Where(dbquery.Published.Type.Eq(req.Type))
}
2023-11-22 17:05:38 +08:00
if req.Category != "" {
where = where.Where(dbquery.Published.Category.Eq(req.Category))
}
2023-11-22 17:05:38 +08:00
var records []*dto.PublishedDto
err := where.Debug().Scan(&records)
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
2023-11-22 17:05:38 +08:00
return records
2023-07-18 17:19:03 +08:00
}
// 项目启动时查询发布地图信息
func ListAllPublished() []*dto.PublishedDto {
2023-11-22 17:05:38 +08:00
p, pv := dbquery.Published, dbquery.PublishedVersion
where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto).
LeftJoin(pv, p.DataID.EqCol(pv.ID)).
Where(p.Status.Eq(1), p.Type.Neq(trainDataType)).Order(pv.PublishAt)
var records []*dto.PublishedDto
err := where.Debug().Scan(&records)
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
2023-11-22 17:05:38 +08:00
return records
2023-08-01 17:16:16 +08:00
}
// 查询详细信息
func GetPublishedById(id int32) *dto.PublishedDto {
2023-11-22 17:05:38 +08:00
p, pv := dbquery.Published, dbquery.PublishedVersion
where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto).
LeftJoin(pv, p.DataID.EqCol(pv.ID)).
Where(p.ID.Eq(id), p.Status.Eq(1))
var record dto.PublishedDto
err := where.Debug().Scan(&record)
if err != nil {
panic(sys_error.New("查询发布地图信息失败", err))
}
2023-11-22 17:05:38 +08:00
return &record
2023-07-18 17:19:03 +08:00
}
// 草稿发布
func PublishFormDraft(req *dto.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(sys_error.New(fmt.Sprintf("草稿[%v]绘图数据信息为空", req.DraftId)))
}
2023-11-22 17:05:38 +08:00
publishData(&dto.PublishedDto{
Name: req.Name,
Proto: draft.Proto,
Type: draft.Type,
Category: draft.Category,
UserID: user.ID,
Note: req.Note,
}, req.Force)
}
// 发布数据
func publishData(publishData *dto.PublishedDto, force bool) {
publishMapMutex.Lock()
defer publishMapMutex.Unlock()
p, pv := dbquery.Published, dbquery.PublishedVersion
// 查询发布图数据
oldPs, err1 := p.Where(p.Code.Eq(publishData.Name)).Find()
if err1 != nil {
panic(sys_error.New("发布草稿数据失败", err1))
}
var pid, version int32 = 0, 1
if len(oldPs) > 0 { // 发布图数据不为空
pid = oldPs[0].ID
if oldPs[0].Category != publishData.Category || publishData.Type != oldPs[0].Type {
if !force { // 非强制覆盖
panic(sys_error.New("草稿数据与在线数据类型不一致"))
} else {
p.
Where(p.ID.Eq(pid)).
UpdateColumns(&model.Published{ID: pid, Type: publishData.Type, Category: publishData.Category})
}
}
vds, _ := pv.Select(pv.Version).Where(pv.PublishID.Eq(pid)).Order(pv.Version.Desc()).Find()
if len(vds) > 0 { // 版本号 + 1
version = version + vds[0].Version
}
}
// 存入新版本数据
2023-11-22 17:05:38 +08:00
pvd := &model.PublishedVersion{
Code: publishData.Name,
Proto: publishData.Proto,
UserID: publishData.UserID,
Note: publishData.Note,
Type: publishData.Type,
Category: publishData.Category,
PublishAt: time.Now(),
Version: version,
2023-11-22 17:05:38 +08:00
PublishID: pid,
2023-07-18 17:19:03 +08:00
}
2023-11-22 17:05:38 +08:00
err2 := pv.Create(pvd)
if err2 != nil {
panic(sys_error.New("发布草稿数据失败", err2))
}
// 更新发布数据
2023-11-22 17:05:38 +08:00
if pid == 0 {
pd := &model.Published{
Code: publishData.Name,
Type: publishData.Type,
Category: publishData.Category,
DataID: pvd.ID,
Status: 1,
}
// 保存发布信息
err4 := p.Create(pd)
if err4 != nil {
panic(sys_error.New("发布草稿数据失败", err4))
}
// 更新发布版本信息
pv.Where(pv.ID.Eq(pvd.ID)).UpdateColumn(pv.PublishID, pd.ID)
} else {
// 更新发布信息
p.Where(p.ID.Eq(pid)).UpdateColumn(p.DataID, pvd.ID)
}
2023-07-18 17:19:03 +08:00
}
2023-11-22 17:05:38 +08:00
// 删除发布数据
func DeletePublishedById(id int32) {
2023-11-22 17:05:38 +08:00
dbquery.Published.Where(dbquery.Published.ID.Eq(id)).Delete()
dbquery.PublishedVersion.Where(dbquery.PublishedVersion.PublishID.Eq(id)).Delete()
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)))
}
2023-11-22 17:05:38 +08:00
p, pv := dbquery.Published, dbquery.PublishedVersion
where := p.Select(p.Type, p.Category, pv.Proto).LeftJoin(pv, p.DataID.EqCol(pv.ID)).Where(p.ID.Eq(id), p.Status.Eq(1))
var record dto.PublishedDto
err := where.Debug().Scan(&record)
if err != nil {
2023-11-22 17:05:38 +08:00
panic(sys_error.New("查询发布地图信息失败", err))
}
err1 := dbquery.Drafting.Save(&model.Drafting{
Name: name,
2023-11-22 17:05:38 +08:00
Category: record.Category,
Proto: record.Proto,
CreatorID: user.ID,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
2023-11-22 17:05:38 +08:00
Type: record.Type,
})
if err1 != nil {
panic(sys_error.New("保存草稿出错", err1))
}
}
// 查询项目关联的详细数据
func QueryProjectPublished(id int32) []*model.Published {
// 获取项目关联的发布地图
2023-11-22 17:05:38 +08:00
dppl, p := dbquery.ProjectPublishLink, dbquery.Published
var publisheds []*model.Published
err := dppl.Select(p.ID, p.Code, p.Category, p.Type).
LeftJoin(p, p.ID.EqCol(dppl.Mid)).
Where(dppl.Pid.Eq(id), p.Status.Eq(1)).Debug().Order(p.Type, p.Code).Scan(&publisheds)
if err != nil {
panic(sys_error.New("发布草稿数据失败", err))
}
return publisheds
}
// 根据名称获取地图详细信息
func GetPublishedGiByName(param *dto.PublishedSingleQueryDto) *dto.PublishedDto {
2023-11-22 17:05:38 +08:00
p, pv := dbquery.Published, dbquery.PublishedVersion
where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto).
LeftJoin(pv, p.DataID.EqCol(pv.ID)).
Where(p.Code.Eq(param.Name), p.Status.Eq(1))
var record dto.PublishedDto
err := where.Debug().Scan(&record)
if err != nil {
2023-11-22 17:05:38 +08:00
panic(sys_error.New("查询发布地图信息失败", err))
}
2023-11-22 17:05:38 +08:00
return &record
}
// 修改发布数据状态
func ChangePublishStatus(id int32, release bool) {
var status int
if release {
status = 1
}
dbquery.Published.Debug().Where(dbquery.Published.ID.Eq(id)).UpdateColumn(dbquery.Published.Status, status)
if !release { // 下架时,删除关联关系
dbquery.ProjectPublishLink.Where(dbquery.ProjectPublishLink.Mid.In(id)).Delete()
}
}
// 修改发布Code
func ChangePublishCode(id int32, code string) {
p := dbquery.Published
count, err := p.Where(p.ID.Neq(id), p.Code.Eq(code)).Count()
if err != nil {
panic(sys_error.New("修改发布名称失败", err))
}
if count > 0 {
panic(sys_error.New("修改名称失败:名称已存在"))
}
p.Debug().Where(dbquery.Published.ID.Eq(id)).UpdateColumn(dbquery.Published.Code, code)
}
// 查询发布历史
func GetPublishHistory(id int32) []*dto.PublishHistoryDto {
u, pv := dbquery.User, dbquery.PublishedVersion
var records []*dto.PublishHistoryDto
err := pv.
Select(pv.ID, pv.PublishAt, pv.Note, pv.Version, u.Name.As("publisher")).
LeftJoin(u, u.ID.EqCol(pv.UserID)).
Where(pv.PublishID.Eq(id)).Order(pv.Version.Desc()).Scan(&records)
if err != nil {
panic(sys_error.New("查询发布历史信息失败", err))
}
2023-11-22 17:05:38 +08:00
return records
}