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 }