2024-11-07 23:54:55 +08:00
package dao
/ *
* @ Description 解析记录定义与操作
* @ Author www . odboy . cn
* @ Date 20241107
* /
import (
"fmt"
2024-11-11 20:19:57 +08:00
"kenaito-dns/config"
2024-11-07 23:54:55 +08:00
"kenaito-dns/domain"
"kenaito-dns/util"
"strings"
2024-11-11 20:19:57 +08:00
"time"
2024-11-07 23:54:55 +08:00
)
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-11 20:19:57 +08:00
CreateTime string ` xorm:"not null text 'create_time'" json:"createTime" `
UpdateTime string ` xorm:"not null text 'update_time'" json:"updateTime" `
Enabled int ` xorm:"not null integer 'enabled'" json:"enabled" `
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
}
2024-11-11 20:19:57 +08:00
func FindResolveRecordByVersion ( version int , isAll bool ) [ ] ResolveRecord {
2024-11-07 23:54:55 +08:00
var records [ ] ResolveRecord
2024-11-11 20:19:57 +08:00
session := Engine . Table ( "resolve_record" )
session . Where ( "`version` = ?" , version )
if ! isAll {
session . Where ( "`enabled` = ?" , 1 )
}
err := session . Find ( & records )
2024-11-07 23:54:55 +08:00
if err != nil {
fmt . Println ( err )
}
return records
}
func FindResolveRecordByNameType ( name string , recordType string ) [ ] ResolveRecord {
var records [ ] ResolveRecord
2024-11-12 20:34:02 +08:00
err := Engine . Table ( "resolve_record" ) . Where ( "`name` = ? and `record_type` = ? and `version` = ? and `enabled` = ?" , name , recordType , GetResolveVersion ( ) , 1 ) . 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
}
2024-11-11 20:19:57 +08:00
func CountResolveRecordPage ( pageNo int , pageSize int , args * domain . QueryPageArgs ) int {
// 每页显示5条记录
if pageSize <= 5 {
pageSize = 5
}
// 要查询的页码
if pageNo <= 0 {
pageNo = 1
}
// 计算跳过的记录数
offset := ( pageNo - 1 ) * pageSize
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 )
}
}
session . And ( "`version` = ?" , GetResolveVersion ( ) )
count , err := session . Limit ( pageSize , offset ) . Count ( )
if err != nil {
fmt . Println ( err )
}
return int ( count )
}
2024-11-07 23:54:55 +08:00
func SaveResolveRecord ( wrapper * ResolveRecord ) ( bool , error ) {
2024-11-11 20:19:57 +08:00
wrapper . CreateTime = time . Now ( ) . Format ( config . DataTimeFormat )
wrapper . UpdateTime = time . Now ( ) . Format ( config . DataTimeFormat )
wrapper . Enabled = 1
2024-11-07 23:54:55 +08:00
_ , 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-11 20:19:57 +08:00
oldRecords := FindResolveRecordByVersion ( oldVersion , true )
2024-11-07 23:54:55 +08:00
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
2024-11-11 20:19:57 +08:00
newRecord . CreateTime = oldRecord . CreateTime
newRecord . UpdateTime = oldRecord . UpdateTime
newRecord . Enabled = oldRecord . Enabled
2024-11-07 23:54:55 +08:00
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
}
}
2024-11-11 21:01:32 +08:00
// 新增版本记录
var resolveVersion ResolveVersion
resolveVersion . Version = newVersion
_ , err2 := SaveResolveVersion ( & resolveVersion )
if err2 != nil {
return false , err2 , 0 , 0
2024-11-07 23:54:55 +08:00
}
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 ) {
2024-11-11 20:19:57 +08:00
updateRecord . UpdateTime = time . Now ( ) . Format ( config . DataTimeFormat )
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 SwitchResolveRecord ( id int , enabled int ) ( bool , error ) {
var updateRecord ResolveRecord
updateRecord . UpdateTime = time . Now ( ) . Format ( config . DataTimeFormat )
updateRecord . Enabled = enabled
2024-11-07 23:54:55 +08:00
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
}