zhangqian
2024-06-13 97d6acaf340b19d66244967b00dd2fdff410e034
attribute value add dateType and dateType enums
1个文件已修改
43 ■■■■■ 已修改文件
models/attribute.go 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/attribute.go
@@ -1,6 +1,7 @@
package models
import (
    "encoding/json"
    "fmt"
    "gorm.io/gorm"
    "wms/pkg/mysqlx"
@@ -10,9 +11,12 @@
    // 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 {
@@ -27,13 +31,44 @@
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) 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 {
    return &AttributeSearch{Orm: mysqlx.GetDB()}
}