From 40132704b8b5078ae73ce0eb9c6313a6d4467cb3 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Wed, 13 Dec 2023 11:23:16 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8F=91=E5=B8=83=E5=9C=B0=E5=9B=BE?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=9B=9E=E9=80=80=E5=8A=9F=E8=83=BD=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/publishedGi.go | 34 +++++++++++++++++++++++++++++++++- dto/published.go | 6 ++++++ service/publishedGi.go | 30 ++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/api/publishedGi.go b/api/publishedGi.go index 4fc5861..362bdca 100644 --- a/api/publishedGi.go +++ b/api/publishedGi.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "log/slog" "net/http" "strconv" @@ -27,6 +28,7 @@ func InitPublishedGiRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddl authed.POST("/release", releasePublishedGiById) authed.POST("/rename", renamePublishedGiById) authed.GET("/:id/history", historyPublishedGiById) + authed.POST("/fallbackVersion", fallbackCurrentVersion) } // 分页查询发布的图形数据 @@ -268,7 +270,7 @@ func renamePublishedGiById(c *gin.Context) { // @Tags 发布的图形数据Api // @Accept json // @Produce json -// @Param id path int true "id" +// @Param PublishFallBackDto path int true "id" // @Success 200 {object} dto.PublishHistoryDto // @Failure 401 {object} dto.ErrorDto // @Failure 500 {object} dto.ErrorDto @@ -282,3 +284,33 @@ func historyPublishedGiById(c *gin.Context) { mid := int32(id) c.JSON(http.StatusOK, service.GetPublishHistory(mid)) } + +// 发布地图回退到历史版本 +// +// @Summary 发布地图回退到历史版本 +// +// @Security JwtAuth +// +// @Description 发布地图回退到历史版本 +// @Tags 发布的图形数据Api +// @Accept json +// @Produce json +// @Param id path query dto.PublishFallBackDto true "查询参数" +// @Success 200 {object} true +// @Failure 401 {object} dto.ErrorDto +// @Failure 500 {object} dto.ErrorDto +// @Router /api/v1/publishedGi/fallbackVersion [post] +func fallbackCurrentVersion(c *gin.Context) { + param := &dto.PublishFallBackDto{} + if err := c.ShouldBind(param); err != nil { + panic(sys_error.New("操作失败,查询参数格式错误", err)) + } + isChange, err := service.FallBackPublishHistory(param.MapId, param.VersionId) + if err != nil { + panic(sys_error.New(fmt.Sprintf("操作失败,%s", err.Error()), err)) + } + if isChange { + memory.DeleteMapVerifyStructure(param.MapId) // 移除内存中的发布信息 + } + c.JSON(http.StatusOK, isChange) +} diff --git a/dto/published.go b/dto/published.go index 64bf367..c01d99a 100644 --- a/dto/published.go +++ b/dto/published.go @@ -54,4 +54,10 @@ type PublishHistoryDto struct { PublishAt JsonTime `json:"publishAt" time_format:"2006-01-02 15:04:05"` Version int32 `json:"version" form:"version"` Note string `json:"note" form:"note"` + Current bool `json:"current" form:"current"` +} + +type PublishFallBackDto struct { + MapId int32 `json:"mapId" form:"mapId" binding:"required"` + VersionId int32 `json:"versionId" form:"versionId" binding:"required"` } diff --git a/service/publishedGi.go b/service/publishedGi.go index 9a7b552..5ba7a57 100644 --- a/service/publishedGi.go +++ b/service/publishedGi.go @@ -257,10 +257,14 @@ func ChangePublishCode(id int32, code string) { // 查询发布历史 func GetPublishHistory(id int32) []*dto.PublishHistoryDto { - u, pv := dbquery.User, dbquery.PublishedVersion + u, p, pv := dbquery.User, dbquery.Published, dbquery.PublishedVersion + pd, err1 := p.Select(p.ID, p.DataID).Where(p.ID.Eq(id)).First() + if err1 != nil { + panic(sys_error.New(fmt.Sprintf("地图【%d】不存在", id))) + } var records []*dto.PublishHistoryDto err := pv. - Select(pv.ID, pv.PublishAt, pv.Note, pv.Version, u.Name.As("publisher")). + Select(pv.ID, pv.PublishAt, pv.Note, pv.Version, u.Name.As("publisher"), pv.ID.Eq(pd.DataID).As("current")). LeftJoin(u, u.ID.EqCol(pv.UserID)). Where(pv.PublishID.Eq(id)).Order(pv.Version.Desc()).Scan(&records) if err != nil { @@ -268,3 +272,25 @@ func GetPublishHistory(id int32) []*dto.PublishHistoryDto { } return records } + +// 回退发布版本 +func FallBackPublishHistory(mapId, versionId int32) (bool, error) { + p, pv := dbquery.Published, dbquery.PublishedVersion + pd, err := p.Select(p.ID, p.DataID).Where(p.ID.Eq(mapId)).First() + if err != nil { + return false, fmt.Errorf("地图【%d】不存在", mapId) + } + pvd, err := pv.Select(pv.ID, pv.PublishID).Where(pv.ID.Eq(versionId)).First() + if err != nil { + return false, fmt.Errorf("地图【%d】版本【%d】数据不存在", mapId, versionId) + } + if pvd.PublishID != pd.ID { + return false, fmt.Errorf("地图【%d】版本【%d】数据不匹配", mapId, versionId) + } + if pd.DataID == pvd.ID { + return false, nil + } + // 更新版本数据 + p.Where(p.ID.Eq(mapId)).UpdateColumn(p.DataID, versionId) + return true, nil +}