zhangqian
2024-06-13 af55de2fd1677bd528ce0be6ceba8b5ee80b46ed
add attribute model and attribute value model
2个文件已添加
1个文件已修改
456 ■■■■■ 已修改文件
models/attribute.go 235 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/attribute_value.go 220 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/material.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/attribute.go
New file
@@ -0,0 +1,235 @@
package models
import (
    "fmt"
    "gorm.io/gorm"
    "wms/pkg/mysqlx"
)
type (
    // Attribute 动态属性表
    Attribute struct {
        gorm.Model
        Name     string   `json:"name"`
        DataType DataType `json:"data_type"`
        Value    string   `json:"value" gorm:"-"`
    }
    AttributeSearch struct {
        Attribute
        Order    string
        PageNum  int
        PageSize int
        Orm      *gorm.DB
    }
)
type DataType int
const (
    DateTypeProduct = 1
)
func (slf *Attribute) TableName() string {
    return "wms_attribute"
}
func NewAttributeSearch() *AttributeSearch {
    return &AttributeSearch{Orm: mysqlx.GetDB()}
}
func (slf *AttributeSearch) SetOrm(tx *gorm.DB) *AttributeSearch {
    slf.Orm = tx
    return slf
}
func (slf *AttributeSearch) SetPage(page, size int) *AttributeSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *AttributeSearch) SetOrder(order string) *AttributeSearch {
    slf.Order = order
    return slf
}
func (slf *AttributeSearch) SetID(id uint) *AttributeSearch {
    slf.ID = id
    return slf
}
func (slf *AttributeSearch) SetName(name string) *AttributeSearch {
    slf.Name = name
    return slf
}
func (slf *AttributeSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    if slf.Name != "" {
        db = db.Where("name = ?", slf.Name)
    }
    return db
}
// Create 单条插入
func (slf *AttributeSearch) Create(record *Attribute) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
    return nil
}
// CreateBatch 批量插入
func (slf *AttributeSearch) CreateBatch(records []*Attribute) error {
    var db = slf.build()
    if err := db.Create(&records).Error; err != nil {
        return fmt.Errorf("create batch err: %v, records: %+v", err, records)
    }
    return nil
}
func (slf *AttributeSearch) Save(record *Attribute) error {
    var db = slf.build()
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *AttributeSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
    return nil
}
func (slf *AttributeSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
    return nil
}
func (slf *AttributeSearch) Delete() error {
    var db = slf.build()
    if err := db.Unscoped().Delete(&Attribute{}).Error; err != nil {
        return err
    }
    return nil
}
func (slf *AttributeSearch) First() (*Attribute, error) {
    var (
        record = new(Attribute)
        db     = slf.build()
    )
    if err := db.First(record).Error; err != nil {
        return record, err
    }
    return record, nil
}
func (slf *AttributeSearch) Find() ([]*Attribute, int64, error) {
    var (
        records = make([]*Attribute, 0)
        total   int64
        db      = slf.build()
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
    return records, total, nil
}
func (slf *AttributeSearch) FindNotTotal() ([]*Attribute, error) {
    var (
        records = make([]*Attribute, 0)
        db      = slf.build()
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
    return records, nil
}
// FindByQuery 指定条件查询.
func (slf *AttributeSearch) FindByQuery(query string, args []interface{}) ([]*Attribute, int64, error) {
    var (
        records = make([]*Attribute, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, total, nil
}
// FindByQueryNotTotal 指定条件查询&不查询总条数.
func (slf *AttributeSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Attribute, error) {
    var (
        records = make([]*Attribute, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, nil
}
models/attribute_value.go
New file
@@ -0,0 +1,220 @@
package models
import (
    "fmt"
    "gorm.io/gorm"
    "wms/pkg/mysqlx"
)
type (
    // AttributeValue 属性值和对象
    AttributeValue struct {
        gorm.Model
        EntityID    string `gorm:"primaryKey"`
        AttributeID uint   `gorm:"primaryKey"`
        Value       string `json:"value"`
    }
    AttributeValueSearch struct {
        AttributeValue
        Order    string
        PageNum  int
        PageSize int
        Orm      *gorm.DB
    }
)
func (slf *AttributeValue) TableName() string {
    return "wms_attribute_value"
}
func NewAttributeValueSearch() *AttributeValueSearch {
    return &AttributeValueSearch{Orm: mysqlx.GetDB()}
}
func (slf *AttributeValueSearch) SetOrm(tx *gorm.DB) *AttributeValueSearch {
    slf.Orm = tx
    return slf
}
func (slf *AttributeValueSearch) SetPage(page, size int) *AttributeValueSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *AttributeValueSearch) SetOrder(order string) *AttributeValueSearch {
    slf.Order = order
    return slf
}
func (slf *AttributeValueSearch) SetID(id uint) *AttributeValueSearch {
    slf.ID = id
    return slf
}
func (slf *AttributeValueSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    return db
}
// Create 单条插入
func (slf *AttributeValueSearch) Create(record *AttributeValue) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
    return nil
}
// CreateBatch 批量插入
func (slf *AttributeValueSearch) CreateBatch(records []*AttributeValue) error {
    var db = slf.build()
    if err := db.Create(&records).Error; err != nil {
        return fmt.Errorf("create batch err: %v, records: %+v", err, records)
    }
    return nil
}
func (slf *AttributeValueSearch) Save(record *AttributeValue) error {
    var db = slf.build()
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *AttributeValueSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
    return nil
}
func (slf *AttributeValueSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
    return nil
}
func (slf *AttributeValueSearch) Delete() error {
    var db = slf.build()
    if err := db.Unscoped().Delete(&AttributeValue{}).Error; err != nil {
        return err
    }
    return nil
}
func (slf *AttributeValueSearch) First() (*AttributeValue, error) {
    var (
        record = new(AttributeValue)
        db     = slf.build()
    )
    if err := db.First(record).Error; err != nil {
        return record, err
    }
    return record, nil
}
func (slf *AttributeValueSearch) Find() ([]*AttributeValue, int64, error) {
    var (
        records = make([]*AttributeValue, 0)
        total   int64
        db      = slf.build()
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
    return records, total, nil
}
func (slf *AttributeValueSearch) FindNotTotal() ([]*AttributeValue, error) {
    var (
        records = make([]*AttributeValue, 0)
        db      = slf.build()
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
    return records, nil
}
// FindByQuery 指定条件查询.
func (slf *AttributeValueSearch) FindByQuery(query string, args []interface{}) ([]*AttributeValue, int64, error) {
    var (
        records = make([]*AttributeValue, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, total, nil
}
// FindByQueryNotTotal 指定条件查询&不查询总条数.
func (slf *AttributeValueSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*AttributeValue, error) {
    var (
        records = make([]*AttributeValue, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, nil
}
models/material.go
@@ -86,6 +86,7 @@
        NetWeight               decimal.Decimal `json:"netWeight" gorm:"type:decimal(20,3);comment:净重"`
        GrossUnit               string          `json:"grossUnit" gorm:"type:varchar(255);comment:毛重单位"`
        NetUnit                 string          `json:"netUnit" gorm:"type:varchar(255);comment:净重单位"`
        Attributes              []Attribute     `json:"attributes" gorm:"-"` //动态属性
        //以下为不存库的字段
        AttachmentIDs    []uint          `json:"attachmentIDs" gorm:"-"`