field.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package utils
  4. import (
  5. "adm-task/parser"
  6. "encoding/json"
  7. "fmt"
  8. "os"
  9. "sync"
  10. "time"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. )
  13. var settingsMutex *sync.RWMutex
  14. var filedMap map[string]string
  15. // 构建字段map
  16. func constructFieldMap() {
  17. fmt.Println("constructFieldMap:", time.Now().String())
  18. var fieldList []string
  19. err := database.DB().Raw("select field from t_adm_field_management").Scan(&fieldList).Error
  20. if err != nil {
  21. os.Exit(1)
  22. }
  23. settingsMutex.Lock()
  24. defer settingsMutex.Unlock()
  25. for _, v := range fieldList {
  26. filedMap[v] = ""
  27. }
  28. //fmt.Println(filedMap)
  29. }
  30. // 加载字段
  31. func FieldLoad() error {
  32. settingsMutex = &sync.RWMutex{}
  33. filedMap = make(map[string]string)
  34. constructFieldMap()
  35. go FiledTimer()
  36. return nil
  37. }
  38. func CheckField(content string) (bool, string) {
  39. settingsMutex.RLock()
  40. defer settingsMutex.RUnlock()
  41. checkFieldMap := make(map[string]interface{})
  42. err := json.Unmarshal([]byte(content), &checkFieldMap)
  43. if err != nil {
  44. return true, ""
  45. }
  46. for k, _ := range checkFieldMap {
  47. if _, ok := filedMap[k]; !ok {
  48. return false, k
  49. }
  50. }
  51. return true, ""
  52. }
  53. func FiledTimer() {
  54. for {
  55. // TODO 从配置文件中获取
  56. loopTime, _ := parser.Conf.FeildLoopTime.Int64()
  57. fmt.Println("loop time:", loopTime)
  58. if loopTime == 0 {
  59. loopTime = 5 * 60
  60. }
  61. time.Sleep(time.Duration(loopTime) * time.Second)
  62. constructFieldMap()
  63. }
  64. }