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 +}