diff --git a/controller/resolve_record.go b/controller/resolve_record.go index e2d75a7..26b9e80 100644 --- a/controller/resolve_record.go +++ b/controller/resolve_record.go @@ -234,13 +234,21 @@ func InitRestFunc(r *gin.Engine) { }) return }) - // 查询变更历史记录 - r.POST("/queryVersionList", func(c *gin.Context) { - records := dao.FindResolveVersion() + // 分页查询变更历史记录 + r.POST("/queryVersionPage", func(c *gin.Context) { + 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{ "code": 0, - "message": "查询变更历史记录列表成功", + "message": "分页查询变更历史记录成功", "data": records, + "count": count, }) return }) @@ -257,19 +265,19 @@ func InitRestFunc(r *gin.Engine) { c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("版本号 %d 不存在, 回滚失败", jsonObj.Version)}) return } - executeResult, err := dao.ModifyResolveVersion(jsonObj.Version) - if !executeResult { - c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("回滚失败, %v", err)}) - return - } - cache.ReloadCache() - body := make(map[string]interface{}) - body["currentVersion"] = jsonObj.Version - c.JSON(http.StatusOK, gin.H{ - "code": 0, - "message": "回滚成功", - "data": body, - }) + //executeResult, err := dao.ModifyResolveVersion(jsonObj.Version) + //if !executeResult { + // c.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("回滚失败, %v", err)}) + // return + //} + //cache.ReloadCache() + //body := make(map[string]interface{}) + //body["currentVersion"] = jsonObj.Version + //c.JSON(http.StatusOK, gin.H{ + // "code": 0, + // "message": "回滚成功", + // "data": body, + //}) return }) } diff --git a/dao/resolve_config.go b/dao/resolve_config.go deleted file mode 100644 index 3725ec8..0000000 --- a/dao/resolve_config.go +++ /dev/null @@ -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 -} diff --git a/dao/resolve_record.go b/dao/resolve_record.go index 3e78a1e..f20285e 100644 --- a/dao/resolve_record.go +++ b/dao/resolve_record.go @@ -182,13 +182,12 @@ func BackupResolveRecord(record *ResolveRecord) (bool, error, int, int) { return false, err, 0, 0 } } - updRecord := new(ResolveVersion) - updRecord.CurrentVersion = newVersion - condition := new(ResolveVersion) - condition.Id = 1 - _, err := Engine.Table("resolve_config").Update(updRecord, condition) - if err != nil { - return false, err, 0, 0 + // 新增版本记录 + var resolveVersion ResolveVersion + resolveVersion.Version = newVersion + _, err2 := SaveResolveVersion(&resolveVersion) + if err2 != nil { + return false, err2, 0, 0 } return true, nil, oldVersion, newVersion } diff --git a/dao/resolve_version.go b/dao/resolve_version.go new file mode 100644 index 0000000..13b4c12 --- /dev/null +++ b/dao/resolve_version.go @@ -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) +} diff --git a/dns.sqlite3 b/dns.sqlite3 index 88e4cde..a11f71a 100644 Binary files a/dns.sqlite3 and b/dns.sqlite3 differ