From 39d53cf7a2080b49224dec6c395fc4ed2463a424 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期三, 18 十二月 2024 18:23:20 +0800
Subject: [PATCH] 修改参数的获取逻辑

---
 models/gather_model.go |   63 +++++++---
 cron/cron.go           |   10 +
 db/task.go             |   54 +++++---
 db/db.go               |  104 +++++++++++++++++
 db/model_rule.go       |   86 ++++++++++++++
 5 files changed, 275 insertions(+), 42 deletions(-)

diff --git a/cron/cron.go b/cron/cron.go
index b77c86e..551c6f2 100644
--- a/cron/cron.go
+++ b/cron/cron.go
@@ -1,12 +1,14 @@
 package cron
 
 import (
+	"time"
+
 	"github.com/go-co-op/gocron"
+
 	"model-engine/models"
 	"model-engine/pkg/logger"
 	"model-engine/pkg/safe"
 	"model-engine/service"
-	"time"
 )
 
 var s *gocron.Scheduler
@@ -20,15 +22,17 @@
 	if err != nil {
 		panic(err)
 	}
+
 	for _, task := range tasks {
 		model, err := models.GetModel(task.ModelID)
 		if err != nil {
 			logger.Errorf("can not find model for id:%v", task.ModelID)
 			continue
 		}
-		task := task
+
+		t := task
 		safe.Go(func() {
-			if err := model.Init(task); err != nil {
+			if err := model.Init(t); err != nil {
 				return
 			}
 			if err := model.Run(); err != nil {
diff --git a/db/db.go b/db/db.go
index 54de63e..057191e 100644
--- a/db/db.go
+++ b/db/db.go
@@ -1,8 +1,11 @@
 package db
 
 import (
+	"time"
+
 	"github.com/elastic/go-elasticsearch/v6"
 	"gorm.io/gorm"
+
 	"model-engine/config"
 	"model-engine/db/es"
 	"model-engine/pkg/logger"
@@ -17,6 +20,9 @@
 	if err := es.InitClient([]string{"http://" + config.EsInfo.Ip + ":" + config.EsInfo.Port}); err != nil {
 		return err
 	}
+
+	InitDefaultData()
+
 	return nil
 }
 
@@ -50,3 +56,101 @@
 
 	return nil
 }
+
+// InitDefaultData 鍒濆鍖栨暟鎹�
+func InitDefaultData() error {
+	var models = []Model{
+		{
+			BaseModel: BaseModel{
+				ID:        ModelIdGather,
+				CreatedAt: time.Time{},
+				UpdatedAt: time.Time{},
+			},
+			Name:        "鐤戜技鑱氶泦",
+			Description: "閫氱敤鑱氶泦妯″瀷",
+			Version:     "v1.0.0",
+			Enabled:     false,
+		},
+	}
+
+	for i := range models {
+		GetDB().Save(&models[i])
+	}
+
+	var rules = []ModelRule{
+		{
+			Id:      "bfbdba7f-ee39-41fb-b188-b4c114a51eaa",
+			ModelId: ModelIdGather,
+			Scope:   "",
+			RuleArg: RuleArg{
+				Alias:    "gatherPersons",
+				Name:     "鑱氶泦浜烘暟",
+				Type:     "input",
+				Must:     true,
+				Unit:     "浜�",
+				Range:    "1,100",
+				Value:    "2",
+				ValType:  "int",
+				Operator: ">=",
+				Sort:     0,
+			},
+		},
+		{
+			Id:      "941bef84-ff7f-4460-b5dc-2ac6060304a4",
+			ModelId: ModelIdGather,
+			Scope:   "",
+			RuleArg: RuleArg{
+				Alias:    "appearInterval",
+				Name:     "鍑虹幇闂撮殧",
+				Type:     "input",
+				Must:     true,
+				Unit:     "绉�",
+				Range:    "1,7200",
+				Value:    "60",
+				ValType:  "int",
+				Operator: ">=",
+				Sort:     1,
+			},
+		},
+		{
+			Id:      "a9b50bae-2c40-40a1-9ebc-ac34850db964",
+			ModelId: ModelIdGather,
+			Scope:   "",
+			RuleArg: RuleArg{
+				Alias:    "threshold",
+				Name:     "鍑虹幇娆℃暟",
+				Type:     "input",
+				Must:     true,
+				Unit:     "娆�",
+				Range:    "1,60",
+				Value:    "1",
+				ValType:  "int",
+				Operator: ">=",
+				Sort:     2,
+			},
+		},
+		{
+			Id:      "aed7f95e-1ce6-4fa2-b1b3-aaf59ed86c50",
+			ModelId: ModelIdGather,
+			Scope:   "",
+			RuleArg: RuleArg{
+				Alias:    "daysWindow",
+				Name:     "鐩戞帶鏃堕棿",
+				Type:     "input",
+				Must:     true,
+				Unit:     "澶╁唴",
+				Range:    "1,7",
+				Value:    "1",
+				ValType:  "int",
+				Operator: "==",
+				Sort:     2,
+			},
+		},
+	}
+
+	for i := range rules {
+		GetDB().Save(&rules[i])
+	}
+
+	return nil
+}
diff --git a/db/model_rule.go b/db/model_rule.go
new file mode 100644
index 0000000..a6aab41
--- /dev/null
+++ b/db/model_rule.go
@@ -0,0 +1,86 @@
+package db
+
+import (
+	"fmt"
+
+	"gorm.io/gorm"
+)
+
+type (
+	ModelRule struct {
+		Id      string `gorm:"primary_key;column:id" json:"id"`
+		ModelId string `gorm:"column:model_id" json:"modelId"`
+		Scope   string `gorm:"column:scope" json:"scope"`
+		RuleArg
+	}
+
+	RuleArg struct {
+		Alias    string `gorm:"column:alias" json:"alias"`         // 鍙傛暟鐨勫埆鍚�
+		Name     string `gorm:"column:name" json:"name"`           // 鍙傛暟鍚嶇О
+		Type     string `gorm:"column:type" json:"type"`           // 鍙傛暟绫诲瀷: input, option, range, image
+		Must     bool   `gorm:"column:must" json:"must"`           // 鏄惁蹇呭~
+		Unit     string `gorm:"column:unit" json:"unit"`           // 鍗曚綅
+		Range    string `gorm:"column:range" json:"range"`         // 鍊肩殑鑼冨洿锛宔g锛�0,100琛ㄧず浠�0鍒�100
+		Value    string `gorm:"column:value" json:"value"`         // 鍙傛暟鍊�
+		ValType  string `gorm:"column:val_type" json:"valType"`    // 鍙傛暟鍊肩被鍨� int, string, bool
+		Operator string `gorm:"column:operator" json:"operator"`   // 杩愮畻绗�
+		Sort     int    `gorm:"column:sort;default:0" json:"sort"` // 鍙傛暟椤哄簭
+	}
+
+	ModelRuleSet struct {
+		Alias    string      `json:"alias"`    // 鍙傛暟鐨勫埆鍚�
+		Type     string      `json:"type"`     // 鍙傛暟绫诲瀷: input, option, range, image
+		Name     string      `json:"name"`     // 鍙傛暟鍚嶇О
+		Value    interface{} `json:"value"`    // 鍙傛暟鍊�
+		ValType  string      `json:"valType"`  // 鍊肩被鍨�
+		Operator string      `json:"operator"` // 杩愮畻绗�
+	}
+
+	ModelRuleSearch struct {
+		ModelRule
+		Orm *gorm.DB
+	}
+)
+
+func (e *ModelRule) TableName() string {
+	return "model_rule"
+}
+
+func NewModelRuleSearch() *ModelRuleSearch {
+	return &ModelRuleSearch{
+		Orm: GetDB(),
+	}
+}
+
+func (slf *ModelRuleSearch) SetOrm(tx *gorm.DB) *ModelRuleSearch {
+	slf.Orm = tx
+	return slf
+}
+
+func (slf *ModelRuleSearch) SetModelID(id string) *ModelRuleSearch {
+	slf.ModelId = id
+	return slf
+}
+
+func (slf *ModelRuleSearch) build() *gorm.DB {
+	var db = slf.Orm.Table(slf.TableName())
+
+	if slf.ModelId != "" {
+		db = db.Where("model_id = ?", slf.ModelId)
+	}
+
+	return db
+}
+
+func (slf *ModelRuleSearch) Find() ([]*ModelRule, error) {
+	var (
+		records = make([]*ModelRule, 0)
+		db      = slf.build()
+	)
+
+	if err := db.Find(&records).Error; err != nil {
+		return records, fmt.Errorf("find records err: %v", err)
+	}
+
+	return records, nil
+}
diff --git a/db/task.go b/db/task.go
index e7c6dc3..cc22131 100644
--- a/db/task.go
+++ b/db/task.go
@@ -1,48 +1,55 @@
 package db
 
 import (
+	"encoding/json"
 	"fmt"
-	"gorm.io/gorm"
 	"strings"
 	"time"
+
+	"gorm.io/gorm"
 )
 
 type (
 	ModelTask struct {
 		BaseModel
-		Name           string    `gorm:"type:varchar(255)" json:"name"`    //浠诲姟鍚嶇О
-		ModelID        string    `gorm:"type:varchar(255)" json:"modelID"` //妯″瀷ID
-		BeginTime      time.Time //宸ヤ綔鏃堕棿寮�濮�
-		EndTime        time.Time //鍏鏃堕棿缁撴潫
-		DomainUnitIds  []string  `gorm:"-" json:"domainUnitIds"` //鍖哄煙id鍒楄〃
-		DomainIds      string    `gorm:"type:varchar(1000)" json:"-"`
-		Building       string    `gorm:"type:varchar(255)" json:"building"`    //妤兼爧
-		Floor          string    `gorm:"type:varchar(255)" json:"floor"`       //妤煎眰
-		AlarmType      AlarmType `gorm:"type:varchar(255);" json:"alarmType"`  //棰勮鏂瑰紡
-		PersonType     string    `gorm:"type:varchar(255);" json:"personType"` //浜哄憳绫诲瀷
-		GatherPersons  int       `gorm:"type:int;" json:"gatherPersons"`       //鑱氶泦浜烘暟
-		AppearInterval int       `gorm:"type:int;" json:"appearInterval"`      //鍑虹幇闂撮殧锛屽崟浣嶄负绉�
-		DaysWindow     int       `gorm:"type:int;" json:"daysWindow" `         //杩戝嚑澶╁唴
-		Threshold      int       `gorm:"type:int;" json:"threshold" `          //杈惧嚑娆�
-		Enabled        bool      `json:"enabled"`                              //鏄惁寮�鍚�
+		Name           string         `gorm:"type:varchar(255)" json:"name"`    // 浠诲姟鍚嶇О
+		ModelID        string         `gorm:"type:varchar(255)" json:"modelID"` // 妯″瀷ID
+		BeginTime      time.Time      `json:"beginTime"`                        // 宸ヤ綔鏃堕棿寮�濮�
+		EndTime        time.Time      `json:"endTime"`                          // 宸ヤ綔鏃堕棿缁撴潫
+		DomainUnitIds  []string       `gorm:"-" json:"domainUnitIds"`           // 鍖哄煙id鍒楄〃
+		DomainIds      string         `gorm:"type:varchar(1000)" json:"-"`
+		Building       string         `gorm:"type:varchar(255)" json:"building"`      // 妤兼爧
+		Floor          string         `gorm:"type:varchar(255)" json:"floor"`         // 妤煎眰
+		AlarmType      AlarmType      `gorm:"type:varchar(255);" json:"alarmType"`    // 棰勮鏂瑰紡
+		PersonType     string         `gorm:"type:varchar(255);" json:"personType"`   // 浜哄憳绫诲瀷
+		PersonLabel    string         `gorm:"type:varchar(255);" json:"personLabel"`  // 浜哄憳鏍囩
+		IdentityType   string         `gorm:"type:varchar(255);" json:"identityType"` // 韬唤鏍囩, 闄岀敓浜�, 璁垮, 浣忔埛
+		AlarmLevel     string         `gorm:"type:varchar(255);" json:"alarmLevel"`   // 棰勮绾у埆
+		GatherPersons  int            `gorm:"type:int;" json:"gatherPersons"`         // 鑱氶泦浜烘暟
+		AppearInterval int            `gorm:"type:int;" json:"appearInterval"`        // 鍑虹幇闂撮殧锛屽崟浣嶄负绉�
+		DaysWindow     int            `gorm:"type:int;" json:"daysWindow" `           // 杩戝嚑澶╁唴
+		Threshold      int            `gorm:"type:int;" json:"threshold" `            // 杈惧嚑娆�
+		Enabled        bool           `json:"enabled"`                                // 鏄惁寮�鍚�
+		RuleSet        string         `gorm:"type:text" json:"-"`                     // 瀛樺偍妯″瀷浠诲姟璁剧疆鐨勮鍒欏弬鏁�
+		Rules          []ModelRuleSet `gorm:"-" json:"rules"`                         // 瑙勫垯閰嶇疆
 	}
 
 	ModelTaskSearch struct {
 		ModelTask
 		Orm       *gorm.DB
+		ModelIDs  []string
+		Unexpired bool
 		PageNum   int
 		PageSize  int
 		Keyword   string
-		ModelIDs  []string
-		Unexpired bool
 	}
 )
 
 type AlarmType string
 
 const (
-	AlarmTypeSave        AlarmType = "save"    //浠呬繚瀛�
-	AlarmTypeSendMessage AlarmType = "message" //鍙戞秷鎭�
+	AlarmTypeSave        AlarmType = "save"    // 浠呬繚瀛�
+	AlarmTypeSendMessage AlarmType = "message" // 鍙戞秷鎭�
 )
 
 func (slf *ModelTask) TableName() string {
@@ -78,6 +85,13 @@
 	if slf.DomainIds != "" {
 		slf.DomainUnitIds = strings.Split(slf.DomainIds, ",")
 	}
+	if slf.DomainIds != "" {
+		slf.DomainUnitIds = strings.Split(slf.DomainIds, ",")
+	}
+
+	if slf.RuleSet != "" {
+		_ = json.Unmarshal([]byte(slf.RuleSet), &slf.Rules)
+	}
 	return nil
 }
 
diff --git a/models/gather_model.go b/models/gather_model.go
index c384db2..b22f24c 100644
--- a/models/gather_model.go
+++ b/models/gather_model.go
@@ -6,28 +6,30 @@
 	"encoding/json"
 	"errors"
 	"fmt"
-	"github.com/elastic/go-elasticsearch/v6"
 	"log"
+	"strings"
+	"sync"
+	"time"
+
+	"github.com/elastic/go-elasticsearch/v6"
+
 	"model-engine/config"
 	"model-engine/db"
 	"model-engine/pkg/set"
 	"model-engine/service"
-	"strings"
-	"sync"
-	"time"
 )
 
 type GatherModel struct {
 	OrgIds         []interface{} `json:"-"`
 	AreaIds        []interface{} `json:"-"`
-	Building       string        `gorm:"type:varchar(255)" json:"building"`    //妤兼爧
-	Floor          string        `gorm:"type:varchar(255)" json:"floor"`       //妤煎眰
-	AlarmType      db.AlarmType  `gorm:"type:varchar(255);" json:"alarmType"`  //棰勮鏂瑰紡
-	PersonType     string        `gorm:"type:varchar(255);" json:"personType"` //浜哄憳绫诲瀷
-	GatherPersons  int           `gorm:"type:int;" json:"gatherPersons"`       //鑱氶泦浜烘暟
-	AppearInterval int           `gorm:"type:int;" json:"appearInterval"`      //鍑虹幇闂撮殧锛屽崟浣嶄负绉�
-	DaysWindow     int           `gorm:"type:int;" json:"daysWindow" `         //杩戝嚑澶╁唴
-	Threshold      int           `gorm:"type:int;" json:"threshold" `          //杈惧嚑娆�
+	Building       string        `gorm:"type:varchar(255)" json:"building"`    // 妤兼爧
+	Floor          string        `gorm:"type:varchar(255)" json:"floor"`       // 妤煎眰
+	AlarmType      db.AlarmType  `gorm:"type:varchar(255);" json:"alarmType"`  // 棰勮鏂瑰紡
+	PersonType     string        `gorm:"type:varchar(255);" json:"personType"` // 浜哄憳绫诲瀷
+	GatherPersons  int           `gorm:"type:int;" json:"gatherPersons"`       // 鑱氶泦浜烘暟
+	AppearInterval int           `gorm:"type:int;" json:"appearInterval"`      // 锛屽崟浣嶄负绉�
+	DaysWindow     int           `gorm:"type:int;" json:"daysWindow" `         // 杩戝嚑澶╁唴
+	Threshold      int           `gorm:"type:int;" json:"threshold" `          // 杈惧嚑娆�
 	Task           *db.ModelTask
 }
 
@@ -52,11 +54,34 @@
 	m.Floor = task.Floor
 	m.AlarmType = task.AlarmType
 	m.PersonType = task.PersonType
-	m.GatherPersons = task.GatherPersons
-	m.AppearInterval = task.AppearInterval
-	m.DaysWindow = task.DaysWindow
-	m.Threshold = task.Threshold
-	fmt.Println("GatherModel init finish ...")
+
+	for _, v := range task.Rules {
+		if v.Alias == "gatherPersons" {
+			if val, ok := v.Value.(float64); ok {
+				m.GatherPersons = int(val)
+			}
+		}
+
+		if v.Alias == "appearInterval" {
+			if val, ok := v.Value.(float64); ok {
+				m.AppearInterval = int(val)
+			}
+		}
+
+		if v.Alias == "daysWindow" {
+			if val, ok := v.Value.(float64); ok {
+				m.DaysWindow = int(val)
+			}
+		}
+
+		if v.Alias == "threshold" {
+			if val, ok := v.Value.(float64); ok {
+				m.Threshold = int(val)
+			}
+		}
+	}
+
+	fmt.Printf("GatherModel init finish ... rule:%+v\n", m)
 	return nil
 }
 
@@ -68,8 +93,8 @@
 	OrgId          string `json:"orgId"`
 	Building       string `json:"building"`
 	Floor          string `json:"floor"`
-	GatherPersons  int    `gorm:"type:int;" json:"gatherPersons"`  //鑱氶泦浜烘暟
-	AppearInterval int    `gorm:"type:int;" json:"appearInterval"` //鍑虹幇闂撮殧锛屽崟浣嶄负绉�
+	GatherPersons  int    `gorm:"type:int;" json:"gatherPersons"`  // 鑱氶泦浜烘暟
+	AppearInterval int    `gorm:"type:int;" json:"appearInterval"` // 鍑虹幇闂撮殧锛屽崟浣嶄负绉�
 }
 
 var (

--
Gitblit v1.8.0