【增加发布保存为草稿,发布时添加发布描述】

This commit is contained in:
weizhihong 2023-08-04 11:54:19 +08:00
parent e4dd831c5d
commit 39a394be75
11 changed files with 308 additions and 23 deletions

View File

@ -1,6 +1,9 @@
package api
import (
"net/http"
"strconv"
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
@ -9,8 +12,6 @@ import (
"joylink.club/bj-rtsts-server/dto/publishedGi"
"joylink.club/bj-rtsts-server/middleware"
"joylink.club/bj-rtsts-server/service"
"net/http"
"strconv"
)
func InitPublishedGiRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
@ -20,6 +21,7 @@ func InitPublishedGiRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddl
authed.GET("/:id", getPublishedGiById)
authed.POST("/publish", publishFromDraft)
authed.DELETE("/:id", deletePublishedGiById)
authed.POST("/saveAsDrafting/:id", saveAsDraftingFromPublish)
}
// 分页查询发布的图形数据
@ -160,3 +162,34 @@ func deletePublishedGiById(c *gin.Context) {
zap.S().Debug("id查询发布的图形数据", id)
service.DeletePublishedGiById(id)
}
// id 从发布数据拉取信息到草稿
//
// @Summary 从发布数据拉取信息到草稿
//
// @Security JwtAuth
//
// @Description 从发布数据拉取信息到草稿
// @Tags 发布的图形数据Api
// @Accept json
// @Produce json
// @Param id path int true "id"
// @Param PublishReqDto query publishedGi.PublishReqDto true "要保存的名称"
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/publishedGi/{id} [post]
func saveAsDraftingFromPublish(c *gin.Context) {
user, _ := c.Get(middleware.IdentityKey)
idStr := c.Param("id")
zap.S().Debugf("用户【%v】拉取发布图形数据【%s】", user, idStr)
id, err := strconv.Atoi(idStr)
if err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
req := publishedGi.PublishReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(dto.ErrorDto{Code: dto.ArgumentParseError, Message: err.Error()})
}
service.SaveAsDraftingFromPublish(int32(id), user.(*model.User), req.Name)
}

@ -1 +1 @@
Subproject commit f0da7c7c5c5a9fc8781d0d612a84e3a97f8c0f81
Subproject commit 83338e447623fca033c49005a537670e15f40b2a

View File

@ -32,6 +32,9 @@ func newPublishedGi(db *gorm.DB, opts ...gen.DOOption) publishedGi {
_publishedGi.Proto = field.NewBytes(tableName, "proto")
_publishedGi.UserID = field.NewInt32(tableName, "user_id")
_publishedGi.PublishAt = field.NewTime(tableName, "publish_at")
_publishedGi.Category = field.NewInt32(tableName, "category")
_publishedGi.Note = field.NewString(tableName, "note")
_publishedGi.Status = field.NewInt32(tableName, "status")
_publishedGi.fillFieldMap()
@ -47,6 +50,9 @@ type publishedGi struct {
Proto field.Bytes // 图形界面数据
UserID field.Int32 // 发布用户id
PublishAt field.Time // 发布时间
Category field.Int32 // 厂家信息
Note field.String // 发布描述
Status field.Int32 // 显示状态
fieldMap map[string]field.Expr
}
@ -68,6 +74,9 @@ func (p *publishedGi) updateTableName(table string) *publishedGi {
p.Proto = field.NewBytes(table, "proto")
p.UserID = field.NewInt32(table, "user_id")
p.PublishAt = field.NewTime(table, "publish_at")
p.Category = field.NewInt32(table, "category")
p.Note = field.NewString(table, "note")
p.Status = field.NewInt32(table, "status")
p.fillFieldMap()
@ -84,12 +93,15 @@ func (p *publishedGi) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (p *publishedGi) fillFieldMap() {
p.fieldMap = make(map[string]field.Expr, 5)
p.fieldMap = make(map[string]field.Expr, 8)
p.fieldMap["id"] = p.ID
p.fieldMap["name"] = p.Name
p.fieldMap["proto"] = p.Proto
p.fieldMap["user_id"] = p.UserID
p.fieldMap["publish_at"] = p.PublishAt
p.fieldMap["category"] = p.Category
p.fieldMap["note"] = p.Note
p.fieldMap["status"] = p.Status
}
func (p publishedGi) clone(db *gorm.DB) publishedGi {

View File

@ -17,6 +17,9 @@ type PublishedGi struct {
Proto []byte `gorm:"column:proto;not null;comment:图形界面数据" json:"proto"` // 图形界面数据
UserID int32 `gorm:"column:user_id;not null;comment:发布用户id" json:"user_id"` // 发布用户id
PublishAt time.Time `gorm:"column:publish_at;not null;comment:发布时间" json:"publish_at"` // 发布时间
Category int32 `gorm:"column:category;comment:厂家信息" json:"category"` // 厂家信息
Note string `gorm:"column:note;comment:发布描述" json:"note"` // 发布描述
Status int32 `gorm:"column:status;comment:显示状态" json:"status"` // 显示状态
}
// TableName PublishedGi's table name

View File

@ -945,6 +945,11 @@ const docTemplate = `{
"description": "发布后的名称",
"name": "name",
"in": "query"
},
{
"type": "string",
"name": "note",
"in": "query"
}
],
"responses": {
@ -1014,6 +1019,67 @@ const docTemplate = `{
}
}
},
"post": {
"security": [
{
"JwtAuth": []
}
],
"description": "从发布数据拉取信息到草稿",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"发布的图形数据Api"
],
"summary": "从发布数据拉取信息到草稿",
"parameters": [
{
"type": "integer",
"description": "id",
"name": "id",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "草稿数据的id",
"name": "draftingId",
"in": "query"
},
{
"type": "string",
"description": "发布后的名称",
"name": "name",
"in": "query"
},
{
"type": "string",
"name": "note",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/dto.ErrorDto"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/dto.ErrorDto"
}
}
}
},
"delete": {
"security": [
{
@ -1888,6 +1954,10 @@ const docTemplate = `{
"model.PublishedGi": {
"type": "object",
"properties": {
"category": {
"description": "厂家信息",
"type": "integer"
},
"id": {
"description": "id",
"type": "integer"
@ -1896,6 +1966,10 @@ const docTemplate = `{
"description": "发布图形界面名称",
"type": "string"
},
"note": {
"description": "发布描述",
"type": "string"
},
"proto": {
"description": "图形界面数据",
"type": "array",
@ -1907,6 +1981,10 @@ const docTemplate = `{
"description": "发布时间",
"type": "string"
},
"status": {
"description": "显示状态",
"type": "integer"
},
"user_id": {
"description": "发布用户id",
"type": "integer"

View File

@ -938,6 +938,11 @@
"description": "发布后的名称",
"name": "name",
"in": "query"
},
{
"type": "string",
"name": "note",
"in": "query"
}
],
"responses": {
@ -1007,6 +1012,67 @@
}
}
},
"post": {
"security": [
{
"JwtAuth": []
}
],
"description": "从发布数据拉取信息到草稿",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"发布的图形数据Api"
],
"summary": "从发布数据拉取信息到草稿",
"parameters": [
{
"type": "integer",
"description": "id",
"name": "id",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "草稿数据的id",
"name": "draftingId",
"in": "query"
},
{
"type": "string",
"description": "发布后的名称",
"name": "name",
"in": "query"
},
{
"type": "string",
"name": "note",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/dto.ErrorDto"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/dto.ErrorDto"
}
}
}
},
"delete": {
"security": [
{
@ -1881,6 +1947,10 @@
"model.PublishedGi": {
"type": "object",
"properties": {
"category": {
"description": "厂家信息",
"type": "integer"
},
"id": {
"description": "id",
"type": "integer"
@ -1889,6 +1959,10 @@
"description": "发布图形界面名称",
"type": "string"
},
"note": {
"description": "发布描述",
"type": "string"
},
"proto": {
"description": "图形界面数据",
"type": "array",
@ -1900,6 +1974,10 @@
"description": "发布时间",
"type": "string"
},
"status": {
"description": "显示状态",
"type": "integer"
},
"user_id": {
"description": "发布用户id",
"type": "integer"

View File

@ -191,12 +191,18 @@ definitions:
type: object
model.PublishedGi:
properties:
category:
description: 厂家信息
type: integer
id:
description: id
type: integer
name:
description: 发布图形界面名称
type: string
note:
description: 发布描述
type: string
proto:
description: 图形界面数据
items:
@ -205,6 +211,9 @@ definitions:
publish_at:
description: 发布时间
type: string
status:
description: 显示状态
type: integer
user_id:
description: 发布用户id
type: integer
@ -760,6 +769,45 @@ paths:
summary: id查询发布的图形数据
tags:
- 发布的图形数据Api
post:
consumes:
- application/json
description: 从发布数据拉取信息到草稿
parameters:
- description: id
in: path
name: id
required: true
type: integer
- description: 草稿数据的id
in: query
name: draftingId
type: integer
- description: 发布后的名称
in: query
name: name
type: string
- in: query
name: note
type: string
produces:
- application/json
responses:
"200":
description: OK
"401":
description: Unauthorized
schema:
$ref: '#/definitions/dto.ErrorDto'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/dto.ErrorDto'
security:
- JwtAuth: []
summary: 从发布数据拉取信息到草稿
tags:
- 发布的图形数据Api
/api/v1/publishedGi/list:
get:
consumes:
@ -863,6 +911,9 @@ paths:
in: query
name: name
type: string
- in: query
name: note
type: string
produces:
- application/json
responses:

View File

@ -16,8 +16,8 @@ type PublishReqDto struct {
//发布后的名称
Name string `json:"name" form:"name"`
//草稿数据的id
DraftId int32 `json:"draftingId" form:"draftingId"`
DraftId int32 `json:"draftingId" form:"draftingId"`
Note string `json:"note" form:"note"`
//Time dto.JsonTime `json:"time" form:"time"`
////是否覆盖同名数据
//Overwrite bool `json:"overwrite" form:"overwrite"`

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
swaggerFiles "github.com/swaggo/files" // swagger embed files
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
"joylink.club/bj-rtsts-server/api"
@ -31,7 +32,7 @@ func main() {
api.InitDraftingRouter(router, authMiddleware)
api.InitPublishedGiRouter(router, authMiddleware)
api.InitSimulationRouter(router, authMiddleware)
api.InitCategoryRouter(router, authMiddleware)
docs.SwaggerInfo.Title = "CBTC测试系统API"
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

View File

@ -52,7 +52,7 @@ func CreateCategory(dto *dto.CategoryDto) (*model.Category, error) {
if err != nil {
panic(err)
}
return dbquery.Category.Where(dbquery.Category.Name.Eq(dto.Name)).Order(dbquery.Drafting.CreatedAt).Debug().First()
return dbquery.Category.Where(dbquery.Category.Name.Eq(dto.Name)).Order(dbquery.Category.CreatedAt).Debug().First()
}
func QueryCategory(id int32) *model.Category {

View File

@ -12,9 +12,9 @@ import (
)
func PageQueryPublishedGi(req *publishedGi.PublishedGiReqDto) *dto.PageDto {
where := dbquery.PublishedGi.Where()
where := dbquery.PublishedGi.Where(dbquery.PublishedGi.Status.Eq(1))
if req.Name != "" {
where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
where = where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
}
result, count, err := where.Debug().FindByPage(req.Offset(), req.Size)
if err != nil {
@ -28,9 +28,9 @@ func PageQueryPublishedGi(req *publishedGi.PublishedGiReqDto) *dto.PageDto {
}
func ListQueryPublishedGi(req *publishedGi.PublishedGiReqDto) []*publishedGi.PublishedGiDto {
where := dbquery.PublishedGi.Where()
where := dbquery.PublishedGi.Where(dbquery.PublishedGi.Status.Eq(1))
if req.Name != "" {
where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
where = where.Where(dbquery.PublishedGi.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
}
find, err := where.Debug().Find()
if err != nil {
@ -40,7 +40,7 @@ func ListQueryPublishedGi(req *publishedGi.PublishedGiReqDto) []*publishedGi.Pub
}
func ListAllPublishedGi() ([]*model.PublishedGi, error) {
return dbquery.PublishedGi.Debug().Find()
return dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.Status.Eq(1)).Find()
}
func GetPublishedGiById(id int) (*model.PublishedGi, error) {
@ -52,26 +52,32 @@ func PublishFormDraft(req *publishedGi.PublishReqDto, user *model.User) {
if draft.Proto == nil || len(draft.Proto) == 0 {
panic(fmt.Sprintf("草稿[%v]绘图数据信息为空", req.DraftId))
}
//需要删除的同名数据
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)).UpdateColumn(dbquery.PublishedGi.Status, 0)
for _, v := range oldData {
memory.DeleteMapVerifyStructure(v.ID) // 移除内存中的发布信息
}
}
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) // 移除内存中的发布信息
}
Category: draft.Category,
Note: req.Note,
Status: 1,
}
//插入新数据
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()
newData, _ := dbquery.PublishedGi.
Where(dbquery.PublishedGi.Name.Eq(req.Name), dbquery.PublishedGi.Status.Eq(1)).
Order(dbquery.PublishedGi.PublishAt.Desc()).First()
// 地图信息更新到缓存
if newData != nil {
memory.PublishMapVerifyStructure(newData)
@ -79,6 +85,29 @@ func PublishFormDraft(req *publishedGi.PublishReqDto, user *model.User) {
}
func DeletePublishedGiById(id int) {
_, _ = dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.ID.Eq(int32(id))).Delete()
dbquery.PublishedGi.Debug().Where(dbquery.PublishedGi.ID.Eq(int32(id))).UpdateColumn(dbquery.PublishedGi.Status, 0)
memory.DeleteMapVerifyStructure(int32(id)) // 移除内存中的发布信息
}
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,
Proto: publishedGi.Proto,
CreatorID: user.ID,
CreatedAt: time.Now(),
UpdateAt: time.Now(),
}
err1 := dbquery.Drafting.Save(drafting)
if err1 != nil {
panic(dto.ErrorDto{Code: dto.DataOperationError, Message: err1.Error()})
}
}