2023-07-18 17:19:03 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-20 14:27:13 +08:00
|
|
|
"sync"
|
2023-07-31 18:20:00 +08:00
|
|
|
"time"
|
|
|
|
|
2023-11-17 16:22:22 +08:00
|
|
|
"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"
|
2023-11-17 16:22:22 +08:00
|
|
|
"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
|
|
|
|
|
2023-11-17 16:22:22 +08:00
|
|
|
// 查找发布地图分页信息
|
|
|
|
func PageQueryPublished(req *dto.PagePublishedReqDto) *dto.PageDto {
|
|
|
|
dp := dbquery.Published
|
2023-08-25 17:49:09 +08:00
|
|
|
where := dp.Where(dp.Status.Eq(1))
|
2023-07-18 17:19:03 +08:00
|
|
|
if req.Name != "" {
|
2023-11-17 16:22:22 +08:00
|
|
|
where = where.Where(dp.Code.Like(fmt.Sprintf("%%%s%%", req.Name)))
|
2023-08-01 14:47:04 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
result, count, err := where.Debug().FindByPage(req.Offset(), req.Size)
|
2023-08-01 14:47:04 +08:00
|
|
|
if err != nil {
|
2023-11-17 16:22:22 +08:00
|
|
|
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,
|
2023-11-17 16:22:22 +08:00
|
|
|
Records: dto.ConvertFromPublisheds(result, protoDatas),
|
2023-08-01 14:47:04 +08:00
|
|
|
}
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:22:22 +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
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
result, err := where.Debug().Find()
|
|
|
|
if err != nil {
|
|
|
|
panic(sys_error.New("查询发布地图信息失败", err))
|
2023-10-13 09:55:56 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
var dataIds []int32 // 数据列表
|
|
|
|
for _, r := range result {
|
|
|
|
dataIds = append(dataIds, r.DataID)
|
2023-10-13 11:19:39 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
pv := dbquery.PublishedVersion
|
|
|
|
protoDatas, err := pv.Select(pv.ID, pv.Proto, pv.Note, pv.PublishAt).Where(pv.ID.In(dataIds...)).Find()
|
2023-08-01 14:47:04 +08:00
|
|
|
if err != nil {
|
2023-11-17 16:22:22 +08:00
|
|
|
panic(sys_error.New("查询发布地图信息失败", err))
|
2023-08-01 14:47:04 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
return dto.ConvertFromPublisheds(result, protoDatas)
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:22:22 +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
|
|
|
}
|
|
|
|
|
2023-11-17 16:22:22 +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()
|
2023-10-23 09:45:04 +08:00
|
|
|
if err != nil {
|
2023-11-17 16:22:22 +08:00
|
|
|
panic(sys_error.New("查询发布地图信息失败", err))
|
2023-10-23 09:45:04 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
return dto.ConvertFromPublished(data, protoData)
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:22:22 +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 {
|
2023-11-17 16:22:22 +08:00
|
|
|
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,
|
2023-08-04 11:54:19 +08:00
|
|
|
UserID: user.ID,
|
|
|
|
PublishAt: time.Now(),
|
|
|
|
Note: req.Note,
|
2023-11-17 16:22:22 +08:00
|
|
|
Version: version,
|
|
|
|
})
|
2023-07-31 10:15:02 +08:00
|
|
|
if err != nil {
|
2023-11-17 16:22:22 +08:00
|
|
|
panic(sys_error.New("发布草稿数据失败", err))
|
2023-07-18 17:19:03 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +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))
|
2023-08-01 17:08:45 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
// 更新发布数据
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-17 16:22:22 +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
|
|
|
}
|
2023-08-04 11:54:19 +08:00
|
|
|
|
2023-11-17 16:22:22 +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 { // 处理重名情况
|
2023-11-17 16:22:22 +08:00
|
|
|
panic(sys_error.New(fmt.Sprintf("草稿【%s】已存在", name)))
|
2023-08-04 11:54:19 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
published, err := dbquery.Published.Where(dbquery.Published.ID.Eq(id)).Debug().First()
|
2023-08-04 11:54:19 +08:00
|
|
|
if err != nil {
|
2023-11-17 16:22:22 +08:00
|
|
|
panic(sys_error.New("查询发布数据出错", err))
|
2023-08-04 11:54:19 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
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{
|
2023-08-04 11:54:19 +08:00
|
|
|
Name: name,
|
2023-11-17 16:22:22 +08:00
|
|
|
Category: published.Category,
|
|
|
|
Proto: versionData.Proto,
|
2023-08-04 11:54:19 +08:00
|
|
|
CreatorID: user.ID,
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
UpdateAt: time.Now(),
|
2023-11-17 16:22:22 +08:00
|
|
|
Type: published.Type,
|
|
|
|
})
|
2023-08-04 11:54:19 +08:00
|
|
|
if err1 != nil {
|
2023-11-17 16:22:22 +08:00
|
|
|
panic(sys_error.New("保存草稿出错", err1))
|
2023-08-04 11:54:19 +08:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 15:32:36 +08:00
|
|
|
|
2023-11-17 16:22:22 +08:00
|
|
|
// 查询项目关联的详细数据
|
|
|
|
func QueryProjectPublished(id int32) []*model.Published {
|
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-11-17 16:22:22 +08:00
|
|
|
dp := dbquery.Published
|
|
|
|
publisheds, _ := dp.
|
|
|
|
Select(dp.ID, dp.Code, dp.Category, dp.Type).
|
2023-10-27 15:45:43 +08:00
|
|
|
Where(dp.ID.In(mids...), dp.Status.Eq(1)).
|
2023-11-17 16:22:22 +08:00
|
|
|
Order(dp.Type, dp.Code).
|
2023-10-27 15:45:43 +08:00
|
|
|
Find()
|
2023-11-17 16:22:22 +08:00
|
|
|
return publisheds
|
2023-08-25 10:41:13 +08:00
|
|
|
}
|
2023-10-17 10:26:31 +08:00
|
|
|
|
2023-11-17 16:22:22 +08:00
|
|
|
// 根据名称获取地图详细信息
|
|
|
|
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()
|
2023-10-23 09:45:04 +08:00
|
|
|
if err != nil {
|
2023-11-17 16:22:22 +08:00
|
|
|
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
|
2023-10-23 09:45:04 +08:00
|
|
|
}
|
2023-11-17 16:22:22 +08:00
|
|
|
return dto.ConvertFromPublished(published, detailData)
|
2023-10-17 10:26:31 +08:00
|
|
|
}
|