mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-04-08 06:35:18 +08:00
优化代码
This commit is contained in:
155
internal/db/models/clients/client_browser_dao.go
Normal file
155
internal/db/models/clients/client_browser_dao.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ClientBrowserStateEnabled = 1 // 已启用
|
||||
ClientBrowserStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
func init() {
|
||||
dbs.OnReadyDone(func() {
|
||||
// 清理数据任务
|
||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
err := SharedClientBrowserDAO.Clean(nil, 7) // 只保留N天
|
||||
if err != nil {
|
||||
remotelogs.Error("SharedClientBrowserDAO", "clean expired data failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
type ClientBrowserDAO dbs.DAO
|
||||
|
||||
func NewClientBrowserDAO() *ClientBrowserDAO {
|
||||
return dbs.NewDAO(&ClientBrowserDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeClientBrowsers",
|
||||
Model: new(ClientBrowser),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*ClientBrowserDAO)
|
||||
}
|
||||
|
||||
var SharedClientBrowserDAO *ClientBrowserDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedClientBrowserDAO = NewClientBrowserDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableClientBrowser 启用条目
|
||||
func (this *ClientBrowserDAO) EnableClientBrowser(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", ClientBrowserStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableClientBrowser 禁用条目
|
||||
func (this *ClientBrowserDAO) DisableClientBrowser(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", ClientBrowserStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledClientBrowser 查找启用中的条目
|
||||
func (this *ClientBrowserDAO) FindEnabledClientBrowser(tx *dbs.Tx, id int64) (*ClientBrowser, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", ClientBrowserStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*ClientBrowser), err
|
||||
}
|
||||
|
||||
// FindClientBrowserName 根据主键查找名称
|
||||
func (this *ClientBrowserDAO) FindClientBrowserName(tx *dbs.Tx, id uint32) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateBrowserIfNotExists 创建浏览器信息
|
||||
func (this *ClientBrowserDAO) CreateBrowserIfNotExists(tx *dbs.Tx, browserName string) error {
|
||||
const maxlength = 50
|
||||
if len(browserName) > maxlength {
|
||||
browserName = browserName[:50]
|
||||
}
|
||||
|
||||
// 检查缓存
|
||||
var cacheKey = "clientBrowser:" + browserName
|
||||
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
|
||||
if cacheItem != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查是否已经存在
|
||||
// 不需要加状态条件
|
||||
browserId, err := this.Query(tx).
|
||||
Attr("name", browserName).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if browserId > 0 {
|
||||
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
|
||||
|
||||
return this.Query(tx).
|
||||
Pk(browserId).
|
||||
Set("createdDay", timeutil.Format("Ymd")).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// 如果不存在,则创建之
|
||||
var op = NewClientBrowserOperator()
|
||||
op.Name = browserName
|
||||
op.CreatedDay = timeutil.Format("Ymd")
|
||||
op.State = ClientBrowserStateEnabled
|
||||
browserId, err = this.SaveInt64(tx, op)
|
||||
if err != nil && models.CheckSQLErrCode(err, 1062 /** duplicate entry **/) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||
if browserId > 0 {
|
||||
ttlcache.SharedCache.Write(cacheKey, browserId, time.Now().Unix()+3600)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Clean 清理
|
||||
func (this *ClientBrowserDAO) Clean(tx *dbs.Tx, days int) error {
|
||||
if days <= 0 {
|
||||
days = 30
|
||||
}
|
||||
|
||||
return this.Query(tx).
|
||||
Lt("createdDay", timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))).
|
||||
DeleteQuickly()
|
||||
}
|
||||
33
internal/db/models/clients/client_browser_dao_test.go
Normal file
33
internal/db/models/clients/client_browser_dao_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientBrowserDAO_CreateBrowser(t *testing.T) {
|
||||
var dao = models.NewClientBrowserDAO()
|
||||
err := dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = dao.CreateBrowserIfNotExists(nil, "Hello")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientBrowserDAO_Clean(t *testing.T) {
|
||||
var dao = models.NewClientBrowserDAO()
|
||||
err := dao.Clean(nil, 30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
24
internal/db/models/clients/client_browser_model.go
Normal file
24
internal/db/models/clients/client_browser_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package clients
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// ClientBrowser 终端浏览器信息
|
||||
type ClientBrowser struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
Name string `field:"name"` // 浏览器名称
|
||||
Codes dbs.JSON `field:"codes"` // 代号
|
||||
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type ClientBrowserOperator struct {
|
||||
Id any // ID
|
||||
Name any // 浏览器名称
|
||||
Codes any // 代号
|
||||
CreatedDay any // 创建日期YYYYMMDD
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewClientBrowserOperator() *ClientBrowserOperator {
|
||||
return &ClientBrowserOperator{}
|
||||
}
|
||||
1
internal/db/models/clients/client_browser_model_ext.go
Normal file
1
internal/db/models/clients/client_browser_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package clients
|
||||
154
internal/db/models/clients/client_system_dao.go
Normal file
154
internal/db/models/clients/client_system_dao.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/ttlcache"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ClientSystemStateEnabled = 1 // 已启用
|
||||
ClientSystemStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
func init() {
|
||||
dbs.OnReadyDone(func() {
|
||||
// 清理数据任务
|
||||
var ticker = time.NewTicker(time.Duration(rands.Int(24, 48)) * time.Hour)
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
err := SharedClientSystemDAO.Clean(nil, 7) // 只保留N天
|
||||
if err != nil {
|
||||
remotelogs.Error("SharedClientSystemDAO", "clean expired data failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
type ClientSystemDAO dbs.DAO
|
||||
|
||||
func NewClientSystemDAO() *ClientSystemDAO {
|
||||
return dbs.NewDAO(&ClientSystemDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeClientSystems",
|
||||
Model: new(ClientSystem),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*ClientSystemDAO)
|
||||
}
|
||||
|
||||
var SharedClientSystemDAO *ClientSystemDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedClientSystemDAO = NewClientSystemDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableClientSystem 启用条目
|
||||
func (this *ClientSystemDAO) EnableClientSystem(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", ClientSystemStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableClientSystem 禁用条目
|
||||
func (this *ClientSystemDAO) DisableClientSystem(tx *dbs.Tx, id uint32) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", ClientSystemStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledClientSystem 查找启用中的条目
|
||||
func (this *ClientSystemDAO) FindEnabledClientSystem(tx *dbs.Tx, id int64) (*ClientSystem, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", ClientSystemStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*ClientSystem), err
|
||||
}
|
||||
|
||||
// FindClientSystemName 根据主键查找名称
|
||||
func (this *ClientSystemDAO) FindClientSystemName(tx *dbs.Tx, id uint32) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// CreateSystemIfNotExists 创建系统信息
|
||||
func (this *ClientSystemDAO) CreateSystemIfNotExists(tx *dbs.Tx, systemName string) error {
|
||||
const maxlength = 50
|
||||
if len(systemName) > maxlength {
|
||||
systemName = systemName[:50]
|
||||
}
|
||||
|
||||
// 检查缓存
|
||||
var cacheKey = "clientSystem:" + systemName
|
||||
var cacheItem = ttlcache.SharedCache.Read(cacheKey)
|
||||
if cacheItem != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查是否已经存在
|
||||
// 不需要加状态条件
|
||||
systemId, err := this.Query(tx).
|
||||
Attr("name", systemName).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if systemId > 0 {
|
||||
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
|
||||
|
||||
return this.Query(tx).
|
||||
Pk(systemId).
|
||||
Set("createdDay", timeutil.Format("Ymd")).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
var op = NewClientSystemOperator()
|
||||
op.Name = systemName
|
||||
op.CreatedDay = timeutil.Format("Ymd")
|
||||
op.State = ClientSystemStateEnabled
|
||||
systemId, err = this.SaveInt64(tx, op)
|
||||
if err != nil && models.CheckSQLErrCode(err, 1062 /** duplicate entry **/) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 加入缓存,但缓存时间不要过长,因为有别的操作在更新数据
|
||||
if systemId > 0 {
|
||||
ttlcache.SharedCache.Write(cacheKey, systemId, time.Now().Unix()+3600)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Clean 清理
|
||||
func (this *ClientSystemDAO) Clean(tx *dbs.Tx, days int) error {
|
||||
if days <= 0 {
|
||||
days = 30
|
||||
}
|
||||
|
||||
return this.Query(tx).
|
||||
Lt("createdDay", timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))).
|
||||
DeleteQuickly()
|
||||
}
|
||||
31
internal/db/models/clients/client_system_dao_test.go
Normal file
31
internal/db/models/clients/client_system_dao_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package clients_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientSystemDAO_CreateSystemIfNotExists(t *testing.T) {
|
||||
var dao = models.NewClientSystemDAO()
|
||||
{
|
||||
err := dao.CreateSystemIfNotExists(nil, "Mac OS X")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
{
|
||||
err := dao.CreateSystemIfNotExists(nil, "Mac OS X 2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientSystemDAO_Clean(t *testing.T) {
|
||||
var dao = models.NewClientSystemDAO()
|
||||
err := dao.Clean(nil, 30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
24
internal/db/models/clients/client_system_model.go
Normal file
24
internal/db/models/clients/client_system_model.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package clients
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// ClientSystem 终端操作系统信息
|
||||
type ClientSystem struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
Name string `field:"name"` // 系统名称
|
||||
Codes dbs.JSON `field:"codes"` // 代号
|
||||
CreatedDay string `field:"createdDay"` // 创建日期YYYYMMDD
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type ClientSystemOperator struct {
|
||||
Id any // ID
|
||||
Name any // 系统名称
|
||||
Codes any // 代号
|
||||
CreatedDay any // 创建日期YYYYMMDD
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewClientSystemOperator() *ClientSystemOperator {
|
||||
return &ClientSystemOperator{}
|
||||
}
|
||||
1
internal/db/models/clients/client_system_model_ext.go
Normal file
1
internal/db/models/clients/client_system_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package clients
|
||||
Reference in New Issue
Block a user