From af55de2fd1677bd528ce0be6ceba8b5ee80b46ed Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 13 六月 2024 20:01:25 +0800 Subject: [PATCH] add attribute model and attribute value model --- models/attribute.go | 235 +++++++++++++++++++++++++++++ models/material.go | 1 models/attribute_value.go | 220 +++++++++++++++++++++++++++ 3 files changed, 456 insertions(+), 0 deletions(-) diff --git a/models/attribute.go b/models/attribute.go new file mode 100644 index 0000000..cd342fa --- /dev/null +++ b/models/attribute.go @@ -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 +} diff --git a/models/attribute_value.go b/models/attribute_value.go new file mode 100644 index 0000000..3b8ad53 --- /dev/null +++ b/models/attribute_value.go @@ -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 +} diff --git a/models/material.go b/models/material.go index 4c6eaa2..031fc32 100644 --- a/models/material.go +++ b/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:"-"` -- Gitblit v1.8.0