zhangzengfei
2025-02-08 8ed1e960d5b13822385ecb9fcbdd18807de701e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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"`         // 值的范围,eg: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
}