// Copyright 2019 autocareai.com. All rights reserved. // Use of this source code is governed by autocareai.com. package utils import ( "gadm-task/parser" "encoding/json" "fmt" "os" "sync" "time" "git.getensh.com/common/gopkgsv2/database" ) var settingsMutex *sync.RWMutex var filedMap map[string]string // 构建字段map func constructFieldMap() { fmt.Println("constructFieldMap:", time.Now().String()) var fieldList []string err := database.DB().Raw("select field from t_adm_field_management").Scan(&fieldList).Error if err != nil { os.Exit(1) } settingsMutex.Lock() defer settingsMutex.Unlock() for _, v := range fieldList { filedMap[v] = "" } //fmt.Println(filedMap) } // 加载字段 func FieldLoad() error { settingsMutex = &sync.RWMutex{} filedMap = make(map[string]string) constructFieldMap() go FiledTimer() return nil } func CheckField(content string) (bool, string) { settingsMutex.RLock() defer settingsMutex.RUnlock() checkFieldMap := make(map[string]interface{}) err := json.Unmarshal([]byte(content), &checkFieldMap) if err != nil { return true, "" } for k, _ := range checkFieldMap { if _, ok := filedMap[k]; !ok { return false, k } } return true, "" } func FiledTimer() { for { // TODO 从配置文件中获取 loopTime, _ := parser.Conf.FeildLoopTime.Int64() fmt.Println("loop time:", loopTime) if loopTime == 0 { loopTime = 5 * 60 } time.Sleep(time.Duration(loopTime) * time.Second) constructFieldMap() } }