You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.3 KiB
90 lines
3.3 KiB
package models
|
|
|
|
import (
|
|
"errors"
|
|
"ims/util/db/dts"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Warehouse 仓库
|
|
type Warehouse struct {
|
|
Id uint `json:"id" gorm:"primarykey"` // 仓库ID
|
|
Name string `json:"name" gorm:"index:,unique,composite:uni_name_with_and_company"` // 仓库名称
|
|
Code string `json:"code"` // 仓库编码
|
|
TypeID uint `json:"type_id"` // 仓库类型编号
|
|
Address *Address `json:"address,omitempty" gorm:"polymorphic:Owner;polymorphicValue:customer"` // 仓库地址
|
|
Capacity int `json:"capacity"` // 仓库容量/立方
|
|
ManagerID dts.NullUint `json:"manager_id"` // 仓库主管编号
|
|
Status SimpleStatus `json:"status"` // 仓库状态
|
|
|
|
CreatedBy uint `json:"created_by"` // 创建者(员工编号)
|
|
CreatedAt time.Time `json:"created_at"` // 创建时间
|
|
UpdatedBy dts.NullUint `json:"updated_by"` // 创建者(员工编号)
|
|
UpdatedAt dts.NullTime `json:"updated_at" gorm:"autoUpdateTime:false"` // 上次操作时间
|
|
DeletedBy dts.NullUint `json:"deleted_by"` // 删除者(员工编号)
|
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"` // 删除数据时间
|
|
|
|
Creator *Employee `json:"creator,omitempty" gorm:"foreignKey:CreatedBy"` // 创建数据的员工
|
|
Updater *Employee `json:"updater,omitempty" gorm:"foreignKey:UpdatedBy"` // 上次更新数据的员工
|
|
Deleter *Employee `json:"deleter,omitempty" gorm:"foreignKey:DeletedBy"` // 删除数据的员工
|
|
|
|
Type *Tag `json:"type" gorm:"foreignKey:TypeID"` // 仓库类型
|
|
Manager *Employee `json:"manager" gorm:"foreignKey:ManagerID"` // 仓库主管
|
|
}
|
|
|
|
func (w *Warehouse) testType(tx *gorm.DB) error {
|
|
var typ Tag
|
|
err := tx.Unscoped().Where("id", w.TypeID).First(&typ).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errors.New("仓库分类不存在")
|
|
}
|
|
return err
|
|
}
|
|
if typ.Type != TagWarehouseType {
|
|
return errors.New("不是有效的仓库分类")
|
|
}
|
|
if typ.DeletedAt.Valid {
|
|
return errors.New("仓库分类已删除")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *Warehouse) testManager(tx *gorm.DB) error {
|
|
if !w.ManagerID.Valid {
|
|
return nil
|
|
}
|
|
var manager Employee
|
|
err := tx.Unscoped().First(&manager, "id", w.ManagerID.Uint).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errors.New("员工不存在")
|
|
}
|
|
return err
|
|
}
|
|
// TODO 验证是不是公司的员工
|
|
if manager.DeletedAt.Valid {
|
|
return errors.New("员工已删除")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *Warehouse) test(tx *gorm.DB) error {
|
|
err := w.testType(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return w.testManager(tx)
|
|
}
|
|
|
|
// BeforeCreate 实现 callbacks.BeforeCreateInterface 接口
|
|
func (w *Warehouse) BeforeCreate(tx *gorm.DB) error {
|
|
return w.test(tx)
|
|
}
|
|
|
|
// BeforeUpdate 实现 callback.BeforeUpdateInterface 接口
|
|
func (w *Warehouse) BeforeUpdate(tx *gorm.DB) error {
|
|
return w.test(tx)
|
|
}
|
|
|