kenaito-dns/dao/resolve_record.go

201 lines
5.2 KiB
Go
Raw Normal View History

2024-11-07 23:54:55 +08:00
package dao
/*
* @Description 解析记录定义与操作
* @Author www.odboy.cn
* @Date 20241107
*/
import (
"fmt"
"kenaito-dns/domain"
"kenaito-dns/util"
"strings"
)
type ResolveRecord struct {
2024-11-09 21:27:39 +08:00
Id int `xorm:"pk not null integer 'id' autoincr" json:"id"`
Name string `xorm:"not null text 'name'" json:"name"`
RecordType string `xorm:"not null text 'record_type'" json:"recordType"`
Ttl int `xorm:"not null integer 'ttl'" json:"ttl"`
Value string `xorm:"not null text 'value'" json:"value"`
Version int `xorm:"not null integer 'version'" json:"version"`
2024-11-07 23:54:55 +08:00
}
2024-11-09 02:27:57 +08:00
func (ResolveRecord) TableName() string {
return "resolve_record"
}
2024-11-09 21:27:39 +08:00
func FindResolveRecordById(id int) *ResolveRecord {
2024-11-09 02:06:10 +08:00
var record ResolveRecord
_, err := Engine.Table("resolve_record").Where("`id` = ?", id).Get(&record)
2024-11-07 23:54:55 +08:00
if err != nil {
fmt.Println(err)
2024-11-09 21:27:39 +08:00
return nil
2024-11-07 23:54:55 +08:00
}
2024-11-09 21:27:39 +08:00
return &record
}
func FindOneResolveRecord(wrapper *ResolveRecord, version int) *ResolveRecord {
var record ResolveRecord
_, err := Engine.Table("resolve_record").Where("`name` = ? and `record_type` = ? and `value` = ? and `version` = ?",
wrapper.Name,
wrapper.RecordType,
wrapper.Value,
version,
).Get(&record)
if err != nil {
fmt.Println(err)
return nil
}
return &record
2024-11-07 23:54:55 +08:00
}
func FindResolveRecordByVersion(version int) []ResolveRecord {
var records []ResolveRecord
err := Engine.Table("resolve_record").Where("`version` = ?", version).Find(&records)
if err != nil {
fmt.Println(err)
}
return records
}
func FindResolveRecordByNameType(name string, recordType string) []ResolveRecord {
var records []ResolveRecord
2024-11-09 01:27:32 +08:00
err := Engine.Table("resolve_record").Where("`name` = ? and `record_type` = ? and `version` = ?", name, recordType, GetResolveVersion()).Find(&records)
2024-11-07 23:54:55 +08:00
if err != nil {
fmt.Println(err)
}
return records
}
func FindResolveRecordPage(pageNo int, pageSize int, args *domain.QueryPageArgs) []*ResolveRecord {
// 每页显示5条记录
if pageSize <= 5 {
pageSize = 5
}
// 要查询的页码
if pageNo <= 0 {
pageNo = 1
}
// 计算跳过的记录数
offset := (pageNo - 1) * pageSize
records := make([]*ResolveRecord, 0)
session := Engine.Table("resolve_record").Where("")
if args != nil {
if !util.IsBlank(args.Name) {
qs := "%" + strings.TrimSpace(args.Name) + "%"
session.And("`name` LIKE ?", qs)
}
if !util.IsBlank(args.Type) {
qs := strings.TrimSpace(args.Type)
session.And("`record_type` = ?", qs)
}
if !util.IsBlank(args.Value) {
qs := strings.TrimSpace(args.Value)
session.And("`value` = ?", qs)
}
}
2024-11-09 01:27:32 +08:00
session.And("`version` = ?", GetResolveVersion())
2024-11-07 23:54:55 +08:00
err := session.Limit(pageSize, offset).Find(&records)
if err != nil {
fmt.Println(err)
}
return records
}
func SaveResolveRecord(wrapper *ResolveRecord) (bool, error) {
_, err := Engine.Table("resolve_record").Insert(wrapper)
if err != nil {
fmt.Println(err)
return false, err
}
return true, nil
}
func BackupResolveRecord(record *ResolveRecord) (bool, error, int, int) {
var backupRecords []*ResolveRecord
2024-11-09 01:27:32 +08:00
oldVersion := GetResolveVersion()
newVersion := GetResolveVersion() + 1
2024-11-07 23:54:55 +08:00
oldRecords := FindResolveRecordByVersion(oldVersion)
for _, oldRecord := range oldRecords {
newRecord := new(ResolveRecord)
newRecord.Name = oldRecord.Name
newRecord.RecordType = oldRecord.RecordType
newRecord.Ttl = oldRecord.Ttl
newRecord.Value = oldRecord.Value
newRecord.Version = newVersion
backupRecords = append(backupRecords, newRecord)
}
record.Version = newVersion
if len(backupRecords) > 0 {
_, err := Engine.Table("resolve_record").Insert(backupRecords)
if err != nil {
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
}
return true, nil, oldVersion, newVersion
}
func RemoveResolveRecord(wrapper *ResolveRecord) (bool, error) {
_, err := Engine.Table("resolve_record").Delete(wrapper)
if err != nil {
fmt.Println(err)
return false, err
}
return true, nil
}
func IsResolveRecordExist(wrapper *ResolveRecord) bool {
2024-11-09 01:27:32 +08:00
wrapper.Version = GetResolveVersion()
2024-11-07 23:54:55 +08:00
count, err := Engine.Table("resolve_record").Count(wrapper)
if err != nil {
fmt.Println(err)
return false
}
return count > 0
}
func IsResolveRecordExistById(id int) bool {
wrapper := new(ResolveRecord)
wrapper.Id = id
return IsResolveRecordExist(wrapper)
}
func IsUpdResolveRecordExist(id int, wrapper *ResolveRecord) bool {
2024-11-09 21:27:39 +08:00
wrapper.Version = GetResolveVersion()
count, err := Engine.Table("resolve_record").Where("id != ?", id).Count(wrapper)
2024-11-07 23:54:55 +08:00
if err != nil {
fmt.Println(err)
return false
}
return count > 0
}
func ModifyResolveRecordById(id int, updateRecord *ResolveRecord) (bool, error) {
wrapper := new(ResolveRecord)
wrapper.Id = id
_, err := Engine.Table("resolve_record").Update(updateRecord, wrapper)
if err != nil {
fmt.Println(err)
return false, err
}
return true, nil
}
func FindResolveVersion() []int {
var records []int
err := Engine.Select("distinct version").Cols("version").Table("resolve_record").Find(&records)
if err != nil {
fmt.Println(err)
}
return records
}