package models
|
|
import (
|
"encoding/json"
|
"fmt"
|
"gorm.io/gorm"
|
"wms/pkg/mysqlx"
|
)
|
|
type (
|
// 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字符串 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 {
|
Attribute
|
Order string
|
PageNum int
|
PageSize int
|
Orm *gorm.DB
|
Keyword string
|
IDs []uint
|
}
|
)
|
|
type DataType int
|
|
const (
|
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 {
|
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) 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
|
}
|
|
func (slf *AttributeSearch) build() *gorm.DB {
|
var db = slf.Orm.Table(slf.TableName())
|
|
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
|
}
|
|
// 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
|
}
|