zhangqian
2024-06-13 97d6acaf340b19d66244967b00dd2fdff410e034
models/attribute.go
@@ -1,6 +1,7 @@
package models
import (
   "encoding/json"
   "fmt"
   "gorm.io/gorm"
   "wms/pkg/mysqlx"
@@ -11,7 +12,10 @@
   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 物料(产品)
      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
   }
@@ -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()}
}