| | |
| | | package models |
| | | |
| | | import ( |
| | | "encoding/json" |
| | | "fmt" |
| | | "gorm.io/gorm" |
| | | "wms/pkg/mysqlx" |
| | |
| | | // Attribute 动态属性表 |
| | | Attribute struct { |
| | | gorm.Model |
| | | Name string `gorm:"type:varchar(100);not null;default:''" json:"name"` //属性名称 |
| | | DataType DataType `gorm:"type:tinyint;not null;default:0" json:"dataType"` //给谁用的 1 物料(产品) |
| | | Value string `json:"value" gorm:"-"` //从AttributeValue取到的value |
| | | Name string `gorm:"type:varchar(100);not null;default:''" json:"name"` //属性名称 |
| | | DataType DataType `gorm:"type:tinyint;not null;default:0" json:"dataType"` //值类型(1字符串 2 int 3 下拉框 ) |
| | | EntityType EntityType `gorm:"type:tinyint;not null;default:0" json:"entityType"` //给谁用的 1 物料(产品) |
| | | SelectValues []string `json:"selectValues" gorm:"-"` //dateType=3时用 |
| | | SelectValue string `json:"-" gorm:"type:text;"` |
| | | Value string `json:"value" gorm:"-"` //从AttributeValue取到的value |
| | | } |
| | | |
| | | AttributeSearch struct { |
| | |
| | | PageNum int |
| | | PageSize int |
| | | Orm *gorm.DB |
| | | Keyword string |
| | | IDs []uint |
| | | } |
| | | ) |
| | | |
| | | type DataType int |
| | | |
| | | const ( |
| | | DateTypeProduct = 1 |
| | | DataTypeString = 1 //手填字符串 |
| | | DataTypeInt = 2 //手填整型 |
| | | DataTypeSelect = 3 //下拉框 |
| | | ) |
| | | |
| | | type EntityType int |
| | | |
| | | const ( |
| | | EntityTypeProduct = 1 |
| | | ) |
| | | |
| | | func (slf *Attribute) TableName() string { |
| | | return "wms_attribute" |
| | | } |
| | | |
| | | func (slf *Attribute) BeforeCreate(tx *gorm.DB) (err error) { |
| | | if len(slf.SelectValues) != 0 { |
| | | bts, err := json.Marshal(slf.SelectValues) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | slf.SelectValue = string(bts) |
| | | } |
| | | return nil |
| | | } |
| | | |
| | | func (slf *Attribute) BeforeUpdate(tx *gorm.DB) (err error) { |
| | | if len(slf.SelectValues) != 0 { |
| | | bts, err := json.Marshal(slf.SelectValues) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | slf.SelectValue = string(bts) |
| | | } |
| | | return nil |
| | | } |
| | | |
| | | func (slf *Attribute) AfterFind(tx *gorm.DB) (err error) { |
| | | if slf.SelectValue != "" { |
| | | var list []string |
| | | err = json.Unmarshal([]byte(slf.SelectValue), &list) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | slf.SelectValues = list |
| | | } |
| | | return nil |
| | | } |
| | | |
| | | func NewAttributeSearch() *AttributeSearch { |
| | |
| | | slf.ID = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *AttributeSearch) SetIDs(ids []uint) *AttributeSearch { |
| | | slf.IDs = ids |
| | | return slf |
| | | } |
| | | func (slf *AttributeSearch) SetName(name string) *AttributeSearch { |
| | | slf.Name = name |
| | | return slf |
| | | } |
| | | func (slf *AttributeSearch) SetEntityType(entityType EntityType) *AttributeSearch { |
| | | slf.EntityType = entityType |
| | | return slf |
| | | } |
| | | |
| | |
| | | if slf.ID != 0 { |
| | | db = db.Where("id = ?", slf.ID) |
| | | } |
| | | |
| | | if len(slf.IDs) != 0 { |
| | | db = db.Where("id in ?", slf.IDs) |
| | | } |
| | | if slf.Order != "" { |
| | | db = db.Order(slf.Order) |
| | | } |
| | | if slf.EntityType != 0 { |
| | | db = db.Where("entity_type = ?", slf.EntityType) |
| | | } |
| | | |
| | | if slf.Name != "" { |
| | | db = db.Where("name = ?", slf.Name) |
| | | } |
| | | if slf.Keyword != "" { |
| | | db = db.Where("id like ? or data_type like ? ", fmt.Sprintf("%%%v%%", slf.Keyword), fmt.Sprintf("%%%v%%", slf.Keyword)) |
| | | } |
| | | |
| | | return db |
| | | } |