feat: 回滚

This commit is contained in:
骑着蜗牛追导弹 2024-11-11 21:01:32 +08:00
parent 287ce3636c
commit cb9c8d8ef3
5 changed files with 123 additions and 70 deletions

View File

@ -234,13 +234,21 @@ func InitRestFunc(r *gin.Engine) {
}) })
return return
}) })
// 查询变更历史记录 // 分页查询变更历史记录
r.POST("/queryVersionList", func(c *gin.Context) { r.POST("/queryVersionPage", func(c *gin.Context) {
records := dao.FindResolveVersion() var jsonObj domain.QueryPageArgs
err := c.ShouldBindJSON(&jsonObj)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("校验失败, %v", err)})
return
}
records := dao.FindResolveVersionPage(jsonObj.Page, jsonObj.PageSize)
count := dao.CountResolveVersionPage(jsonObj.Page, jsonObj.PageSize)
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"code": 0, "code": 0,
"message": "查询变更历史记录列表成功", "message": "分页查询变更历史记录成功",
"data": records, "data": records,
"count": count,
}) })
return return
}) })
@ -257,19 +265,19 @@ func InitRestFunc(r *gin.Engine) {
c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("版本号 %d 不存在, 回滚失败", jsonObj.Version)}) c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("版本号 %d 不存在, 回滚失败", jsonObj.Version)})
return return
} }
executeResult, err := dao.ModifyResolveVersion(jsonObj.Version) //executeResult, err := dao.ModifyResolveVersion(jsonObj.Version)
if !executeResult { //if !executeResult {
c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("回滚失败, %v", err)}) // c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("回滚失败, %v", err)})
return // return
} //}
cache.ReloadCache() //cache.ReloadCache()
body := make(map[string]interface{}) //body := make(map[string]interface{})
body["currentVersion"] = jsonObj.Version //body["currentVersion"] = jsonObj.Version
c.JSON(http.StatusOK, gin.H{ //c.JSON(http.StatusOK, gin.H{
"code": 0, // "code": 0,
"message": "回滚成功", // "message": "回滚成功",
"data": body, // "data": body,
}) //})
return return
}) })
} }

View File

@ -1,46 +0,0 @@
package dao
/*
* @Description 解析配置定义与操作
* @Author www.odboy.cn
* @Date 20241107
*/
import (
"fmt"
)
type ResolveVersion struct {
Id int `xorm:"pk not null integer 'id' autoincr" json:"id"`
CurrentVersion int `xorm:"not null integer 'curr_version'" json:"currentVersion"`
}
func (ResolveVersion) TableName() string {
return "resolve_version"
}
func GetResolveVersion() int {
var records []ResolveVersion
err := Engine.Table("resolve_config").Where("`id` = ?", 1).Find(&records)
if err != nil {
fmt.Println(err)
return 0
}
if len(records) == 0 {
return 0
}
return records[0].CurrentVersion
}
func ModifyResolveVersion(currentVersion int) (bool, error) {
wrapper := new(ResolveVersion)
wrapper.Id = 1
updateRecord := new(ResolveVersion)
updateRecord.CurrentVersion = currentVersion
_, err := Engine.Table("resolve_config").Update(updateRecord, wrapper)
if err != nil {
fmt.Println(err)
return false, err
}
return true, nil
}

View File

@ -182,13 +182,12 @@ func BackupResolveRecord(record *ResolveRecord) (bool, error, int, int) {
return false, err, 0, 0 return false, err, 0, 0
} }
} }
updRecord := new(ResolveVersion) // 新增版本记录
updRecord.CurrentVersion = newVersion var resolveVersion ResolveVersion
condition := new(ResolveVersion) resolveVersion.Version = newVersion
condition.Id = 1 _, err2 := SaveResolveVersion(&resolveVersion)
_, err := Engine.Table("resolve_config").Update(updRecord, condition) if err2 != nil {
if err != nil { return false, err2, 0, 0
return false, err, 0, 0
} }
return true, nil, oldVersion, newVersion return true, nil, oldVersion, newVersion
} }

92
dao/resolve_version.go Normal file
View File

@ -0,0 +1,92 @@
package dao
/*
* @Description 解析配置定义与操作
* @Author www.odboy.cn
* @Date 20241107
*/
import (
"fmt"
"kenaito-dns/config"
"time"
)
type ResolveVersion struct {
Id int `xorm:"pk not null integer 'id' autoincr" json:"id"`
Version int `xorm:"not null integer 'version'" json:"version"`
CreateTime string `xorm:"not null text 'create_time'" json:"createTime"`
IsRelease int `xorm:"not null integer 'is_release'" json:"isRelease"`
}
func (ResolveVersion) TableName() string {
return "resolve_version"
}
func GetResolveVersion() int {
var records []ResolveVersion
session := Engine.Table("resolve_version")
session.Desc("id")
session.And("is_release = ?", 1)
err := session.Find(&records)
if err != nil {
fmt.Println(err)
return 0
}
if len(records) == 0 {
return 0
}
return records[0].Version
}
func SaveResolveVersion(wrapper *ResolveVersion) (bool, error) {
// 全表更新为未发布
_, _ = Engine.Table("resolve_version").Update(ResolveVersion{IsRelease: 2}, ResolveVersion{})
// 新增发布记录
wrapper.CreateTime = time.Now().Format(config.DataTimeFormat)
wrapper.IsRelease = 1
_, err := Engine.Table("resolve_version").Insert(wrapper)
if err != nil {
fmt.Println(err)
return false, err
}
return true, nil
}
func FindResolveVersionPage(pageNo int, pageSize int) []*ResolveVersion {
// 每页显示5条记录
if pageSize <= 5 {
pageSize = 5
}
// 要查询的页码
if pageNo <= 0 {
pageNo = 1
}
// 计算跳过的记录数
offset := (pageNo - 1) * pageSize
records := make([]*ResolveVersion, 0)
session := Engine.Table("resolve_version")
session.Desc("id")
err := session.Limit(pageSize, offset).Find(&records)
if err != nil {
fmt.Println(err)
}
return records
}
func CountResolveVersionPage(pageNo int, pageSize int) int {
// 每页显示5条记录
if pageSize <= 5 {
pageSize = 5
}
// 要查询的页码
if pageNo <= 0 {
pageNo = 1
}
// 计算跳过的记录数
offset := (pageNo - 1) * pageSize
session := Engine.Table("resolve_version")
count, err := session.Limit(pageSize, offset).Count()
if err != nil {
fmt.Println(err)
}
return int(count)
}

Binary file not shown.