package models
|
|
import (
|
"fmt"
|
"gorm.io/gorm"
|
"strings"
|
"wms/pkg/mysqlx"
|
)
|
|
type (
|
// Warehouse 部门信息
|
Warehouse struct {
|
WmsModel
|
Name string `json:"name" gorm:"index;type:varchar(255);not null;comment:仓库名称"` //仓库名称
|
Active bool `json:"active" gorm:"type:tinyint(1);not null;comment:是否激活"` //是否启用,传true就行
|
Code string `json:"code" binding:"required,min=1,max=5" gorm:"index;type:varchar(255);not null;comment:仓库编码"` //仓库编码
|
PartnerID int `json:"partnerId" gorm:"type:int;not null;comment:合作伙伴id"` //合作伙伴id
|
BuyToResupply bool `json:"buyToResupply" gorm:"type:tinyint(1);not null;comment:是否购买补给"` //是否购买补给,已购买产品能够发送到此仓库
|
ResupplyWhIdsStr string `json:"-" gorm:"column:resupply_wh_ids;type:varchar(255);not null;comment:补给来源仓库ID"` //补给来源仓库ID
|
ResupplyWhIds []string `json:"resupplyWhIds" gorm:"-"` //补给来源仓库ID
|
ResupplyWh []*Warehouse `json:"resupplyWh" gorm:"-"` //补给来源仓库
|
}
|
|
WarehouseSearch struct {
|
Warehouse
|
Order string
|
PageNum int
|
PageSize int
|
Keyword string
|
Orm *gorm.DB
|
}
|
)
|
|
func (slf *Warehouse) TableName() string {
|
return "warehouse"
|
}
|
|
func (slf *Warehouse) BeforeCreate(tx *gorm.DB) (err error) {
|
slf.ResupplyWhIdsStr = strings.Join(slf.ResupplyWhIds, ",")
|
return nil
|
}
|
|
func NewWarehouseSearch() *WarehouseSearch {
|
return &WarehouseSearch{Orm: mysqlx.GetDB()}
|
}
|
|
func (slf *WarehouseSearch) SetOrm(tx *gorm.DB) *WarehouseSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *WarehouseSearch) SetPage(page, size int) *WarehouseSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *WarehouseSearch) SetOrder(order string) *WarehouseSearch {
|
slf.Order = order
|
return slf
|
}
|
|
func (slf *WarehouseSearch) SetID(id uint) *WarehouseSearch {
|
slf.ID = id
|
return slf
|
}
|
|
func (slf *WarehouseSearch) SetCode(code string) *WarehouseSearch {
|
slf.Code = code
|
return slf
|
}
|
|
func (slf *WarehouseSearch) SetName(name string) *WarehouseSearch {
|
slf.Name = name
|
return slf
|
}
|
|
func (slf *WarehouseSearch) SetKeyword(keyword string) *WarehouseSearch {
|
slf.Keyword = keyword
|
return slf
|
}
|
|
func (slf *WarehouseSearch) 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.Keyword != "" {
|
db = db.Where("name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
|
}
|
if slf.Name != "" {
|
db = db.Where("name = ?", slf.Name)
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *WarehouseSearch) Create(record *Warehouse) error {
|
var db = slf.build()
|
|
if err := db.Create(record).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
// CreateBatch 批量插入
|
func (slf *WarehouseSearch) CreateBatch(records []*Warehouse) 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 *WarehouseSearch) Update(record *Warehouse) error {
|
var db = slf.build()
|
|
if err := db.Omit("CreatedAt").Updates(record).Error; err != nil {
|
return fmt.Errorf("save err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
func (slf *WarehouseSearch) 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 *WarehouseSearch) 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 *WarehouseSearch) Delete() error {
|
var db = slf.build()
|
return db.Delete(&Warehouse{}).Error
|
}
|
|
func (slf *WarehouseSearch) First() (*Warehouse, error) {
|
var (
|
record = new(Warehouse)
|
db = slf.build()
|
)
|
|
if err := db.First(record).Error; err != nil {
|
return record, err
|
}
|
|
return record, nil
|
}
|
|
func (slf *WarehouseSearch) Find() ([]*Warehouse, int64, error) {
|
var (
|
records = make([]*Warehouse, 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 *WarehouseSearch) FindNotTotal() ([]*Warehouse, error) {
|
var (
|
records = make([]*Warehouse, 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 *WarehouseSearch) FindByQuery(query string, args []interface{}) ([]*Warehouse, int64, error) {
|
var (
|
records = make([]*Warehouse, 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 *WarehouseSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Warehouse, error) {
|
var (
|
records = make([]*Warehouse, 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
|
}
|