gva-pms/server/service/system/monitor.go

79 lines
1.6 KiB
Go

package system
import (
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
"sync"
)
type MonitorService struct {
}
var monitorService = new(MonitorService)
var autoCodeService = new(AutoCodeService)
var (
MonitorCache = make(map[string]map[string]bool) // 缓存结构: table -> field -> exists
CacheMutex sync.RWMutex
)
// 初始化监控配置
func (s *MonitorService) InitMonitor() (err error) {
// var configs []system.MonitorConfig
// err = global.GVA_DB.Find(&configs).Error
// if err != nil {
// return
// }
configs := []system.MonitorConfig{
{
BusinessDB: "Local",
Database: "gvapp",
Table: "sample_order",
Columns: []string{"customer_code", "demand_quantity"},
IsEnabled: true,
},
{
BusinessDB: "Local",
Database: "gvapp",
Table: "customer",
Columns: []string{"code", "name"},
IsEnabled: true,
},
}
s.refreshCache(configs)
return nil
}
func (s *MonitorService) refreshCache(configs []system.MonitorConfig) {
newCache := make(map[string]map[string]bool)
for _, c := range configs {
if !c.IsEnabled {
continue
}
if newCache[c.Table] == nil {
newCache[c.Table] = make(map[string]bool)
}
for _, col := range c.Columns {
if col != "" {
newCache[c.Table][col] = true
}
}
}
CacheMutex.Lock()
MonitorCache = newCache
CacheMutex.Unlock()
}
// IsFieldMonitored 检查字段是否被监控
func (s *MonitorService) IsFieldMonitored(table, field string) bool {
CacheMutex.RLock()
defer CacheMutex.RUnlock()
fields, ok := MonitorCache[table]
if !ok {
return false
}
return fields[field]
}