constvar/const.go | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
models/db.go | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
models/location.go | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
models/product.go | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
models/product_category.go | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
constvar/const.go
@@ -41,3 +41,89 @@ slf == WhetherTypeAlways || slf == ReservationNever } // ProductType 产品类型 type ProductType int const ( ProductTypeRaw = iota + 1 // 原材料 ProductTypeSemi // 半成品 ProductTypeFinished // 成品 ) type ProductStatus int const ( ProductStatusCreate ProductStatus = iota // 新建 ProductStatusActive // 启用 ProductStatusInactive = -1 // 停用 ) // PurchaseType 采购类型 type PurchaseType int const ( PurchaseTypeOutSource PurchaseType = iota + 1 // 采购 PurchaseTypeSelf // 自制 PurchaseTypeEntrust // 委外 ) func (t PurchaseType) Valid() bool { if t < PurchaseTypeOutSource || t > PurchaseTypeEntrust { return false } return true } // LocationType 位置类型 type LocationType int const ( LocationTypeVendor LocationType = iota + 1 // 供应商位置 LocationTypeView // 视图 LocationTypeInternal // 内部位置 LocationTypeCustomer // 客户位置 LocationTypeInventoryLoss // 库存损失 LocationTypeProduction // 生产 LocationTypeTransit // 中转位置 ) func (t LocationType) Valid() bool { return t >= LocationTypeVendor && t <= LocationTypeTransit } type ForceRemovalStrategy int const ( ForceRemovalStrategyFIFO ForceRemovalStrategy = iota + 1 ForceRemovalStrategyLIFO ForceRemovalStrategyClosestLocation ) func (t ForceRemovalStrategy) Valid() bool { return t >= ForceRemovalStrategyFIFO && t <= ForceRemovalStrategyClosestLocation } type CostingMethod int const ( CostingMethodStandardPrice CostingMethod = iota + 1 //标准价格 CostingMethodFIFO //先进先出 CostingMethodAverageCost // ) func (t CostingMethod) Valid() bool { return t >= CostingMethodStandardPrice && t <= CostingMethodAverageCost } type InventoryValuation int const ( InventoryValuationManual InventoryValuation = iota + 1 //手动 InventoryValuationAuto //自动 ) func (t InventoryValuation) Valid() bool { return t >= InventoryValuationManual && t <= InventoryValuationAuto } models/db.go
@@ -77,6 +77,13 @@ Warehouse{}, OperationType{}, Location{}, OperationType{}, Operation{}, OperationDetails{}, Scrap{}, MoveHistory{}, Product{}, ProductCategory{}, ) return err } models/location.go
@@ -3,6 +3,7 @@ import ( "fmt" "gorm.io/gorm" "wms/constvar" "wms/pkg/mysqlx" ) @@ -10,8 +11,16 @@ // Location 位置 Location struct { WmsModel Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` Name string `json:"name" gorm:"index;type:varchar(255);not null;comment:位置名称"` //位置名称 Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` Name string `json:"name" gorm:"index;type:varchar(255);not null;comment:位置名称"` //位置名称 ParentId int `json:"parentId" gorm:"type:int;not null"` //上级id CompanyId int `json:"companyId" gorm:"type:int;not null"` //公司id Company Company `json:"company" gorm:"foreignKey:CompanyId"` //公司 Type constvar.LocationType `json:"type" gorm:"type:tinyint;not null;comment:位置类型"` //位置类型 CountFrequency int `json:"countFrequency" gorm:"type:tinyint;not null;comment:盘点频率(天)"` //盘点频率(天) IsScrapLocation bool `json:"isScrapLocation" gorm:"type:tinyint;not null;comment:是否报废位置"` //是否报废位置 IsReturnLocation bool `json:"isReturnLocation" gorm:"type:tinyint;not null;comment:是否退货位置"` //是否退货位置 ReplenishLocation bool `json:"replenishLocation" gorm:"type:tinyint;not null;comment:是否补充位置"` //是否补充位置 } LocationSearch struct { models/product.go
New file @@ -0,0 +1,287 @@ package models import ( "fmt" "google.golang.org/genproto/googleapis/type/decimal" "gorm.io/gorm" "wms/constvar" "wms/pkg/mysqlx" ) type ( // Product 产品 Product struct { WmsModel Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` Name string `json:"name" gorm:"index;type:varchar(255);not null;comment:产品名称"` //产品名称 Type constvar.ProductType `gorm:"type:tinyint;comment:产品类型" json:"type"` //产品类型 CategoryId int `gorm:"type:int(11);comment:产品分类" json:"categoryId"` //产品分类id Category string `gorm:"type:int(11);comment:产品分类" json:"category"` //产品分类 Specs string `gorm:"type:varchar(191);comment:产品规格" json:"specs"` //产品规格 Model string `gorm:"type:varchar(191);comment:产品型号" json:"model"` //产品型号 //MinInventory decimal.Decimal `gorm:"type:decimal(20,2);comment:最小库存" json:"minInventory"` //最大库存 //MaxInventory decimal.Decimal `gorm:"type:decimal(20,2);comment:最大库存" json:"maxInventory"` //最小库存 //Amount decimal.Decimal `gorm:"type:decimal(20,2);comment:数量" json:"amount"` //LockAmount decimal.Decimal `gorm:"type:decimal(20,2);default:0;comment:锁定数量" json:"lockAmount"` Unit string `gorm:"type:varchar(100);comment:单位" json:"unit"` //单位 PurchaseUnit string `gorm:"type:varchar(100);comment:采购单位" json:"purchaseUnit"` //采购单位 Note string `gorm:"type:varchar(1024);comment:备注" json:"note"` Status constvar.ProductStatus `gorm:"type:int(11);comment:状态" json:"status"` Purchases []*PurchaseInfo `gorm:"-" json:"purchases"` //采购信息 PurchasesStr string `gorm:"column:purchase;type:varchar(4096);comment:购买信息" json:"-"` //PurchaseType constvar.PurchaseType `gorm:"type:int(11);comment:采购类型" json:"purchaseType"` ///采购类型 CanBePurchased bool `gorm:"type:int(11);not null;comment:是否可采购" json:"purchaseType"` //是否可采购 IsSale bool `gorm:"type:tinyint(1);comment:是否销售" json:"isSale"` //是否销售 SalePrice decimal.Decimal `gorm:"type:decimal(35,18);comment:销售单价" json:"salePrice"` //销售价格 CustomerTaxes decimal.Decimal `gorm:"type:decimal(20,2);not null;comment:客户税" json:"customerTaxes"` //客户税百分比 Cost decimal.Decimal `gorm:"type:decimal(20,2);not null;comment:成本" json:"cost"` //成本 OptionalProducts []int `gorm:"type:varchar(255);not null;comment:相似产品id" json:"optionalProducts"` //相识产品 Principal string `gorm:"type:varchar(255);not null;comment:负责人" json:"principal"` //负责人 Weight string `gorm:"type:decimal(20,2);not null;comment:重量" json:"weight"` //重量 Volume string `gorm:"type:decimal(20,2);not null;comment:体积" json:"volume"` //体积 InternalReference string `gorm:"type:varchar(255);not null;comment:内部参考" json:"internalReference"` //内部参考 Barcode string `gorm:"type:varchar(255);not null;comment:条码" json:"barcode"` //条码 Tags string `gorm:"type:varchar(255);not null;comment:产品标签" json:"tags"` //产品标签 InternalNotes string `gorm:"type:varchar(512);not null;comment:内部说明" json:"internalNotes"` //内部说明 } ProductSearch struct { Product Order string PageNum int PageSize int Keyword string Orm *gorm.DB Preload bool } PurchaseInfo struct { PurchasePrice decimal.Decimal `gorm:"type:decimal(35,18);comment:采购价格" json:"purchasePrice"` PurchaseAheadDay int `gorm:"type:int(11);comment:采购提前期(天)" json:"purchaseAheadDay"` ProduceAheadDay int `gorm:"type:int(11);comment:制造提前期(天)" json:"produceAheadDay"` MinPurchaseAmount decimal.Decimal `gorm:"type:decimal(35,18);comment:最小采购量" json:"minPurchaseAmount"` } ) func (slf *Product) TableName() string { return "product" } func (slf *Product) BeforeCreate(db *gorm.DB) error { return nil } func (slf *Product) AfterFind(db *gorm.DB) error { return nil } func NewProductSearch() *ProductSearch { return &ProductSearch{Orm: mysqlx.GetDB()} } func (slf *ProductSearch) SetOrm(tx *gorm.DB) *ProductSearch { slf.Orm = tx return slf } func (slf *ProductSearch) SetPage(page, size int) *ProductSearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *ProductSearch) SetOrder(order string) *ProductSearch { slf.Order = order return slf } func (slf *ProductSearch) SetID(id uint) *ProductSearch { slf.ID = id return slf } func (slf *ProductSearch) SetName(name string) *ProductSearch { slf.Name = name return slf } func (slf *ProductSearch) SetKeyword(keyword string) *ProductSearch { slf.Keyword = keyword return slf } func (slf *ProductSearch) SetPreload(preload bool) *ProductSearch { slf.Preload = preload return slf } func (slf *ProductSearch) build() *gorm.DB { var db = slf.Orm.Model(&Product{}) 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 *ProductSearch) Create(record *Product) error { var db = slf.build() if err := db.Create(record).Error; err != nil { return err } return nil } // CreateBatch 批量插入 func (slf *ProductSearch) CreateBatch(records []*Product) 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 *ProductSearch) Update(record *Product) 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 *ProductSearch) 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 *ProductSearch) 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 *ProductSearch) Delete() error { var db = slf.build() return db.Delete(&Product{}).Error } func (slf *ProductSearch) First() (*Product, error) { var ( record = new(Product) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } func (slf *ProductSearch) Find() ([]*Product, int64, error) { var ( records = make([]*Product, 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 *ProductSearch) FindNotTotal() ([]*Product, error) { var ( records = make([]*Product, 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 *ProductSearch) FindByQuery(query string, args []interface{}) ([]*Product, int64, error) { var ( records = make([]*Product, 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 *ProductSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Product, error) { var ( records = make([]*Product, 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/product_category.go
New file @@ -0,0 +1,246 @@ package models import ( "fmt" "gorm.io/gorm" "wms/constvar" "wms/pkg/mysqlx" ) type ( // ProductCategory 产品分类 ProductCategory struct { WmsModel Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` Name string `json:"name" gorm:"index;type:varchar(255);not null;comment:位置名称"` //位置名称 ParentId int `json:"parentId" gorm:"type:int;not null"` //上级id CompanyId int `json:"companyId" gorm:"type:int;not null"` //公司id Company Company `json:"company" gorm:"foreignKey:CompanyId"` //公司 ForceRemovalStrategy constvar.ForceRemovalStrategy `json:"forceRemovalStrategy" gorm:"type:tinyint;not null;comment:强制下架策略"` //强制下架策略 CostingMethod constvar.CostingMethod `json:"costingMethod" gorm:"type:tinyint;not null;comment:成本方法"` //成本方法 InventoryValuation constvar.InventoryValuation `json:"inventoryValuation" gorm:"type:tinyint;not null;comment:成本方法"` //库存计价 } ProductCategorySearch struct { ProductCategory Order string PageNum int PageSize int Keyword string Orm *gorm.DB Preload bool } ) func (slf *ProductCategory) TableName() string { return "ProductCategory" } func NewProductCategorySearch() *ProductCategorySearch { return &ProductCategorySearch{Orm: mysqlx.GetDB()} } func (slf *ProductCategorySearch) SetOrm(tx *gorm.DB) *ProductCategorySearch { slf.Orm = tx return slf } func (slf *ProductCategorySearch) SetPage(page, size int) *ProductCategorySearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *ProductCategorySearch) SetOrder(order string) *ProductCategorySearch { slf.Order = order return slf } func (slf *ProductCategorySearch) SetID(id uint) *ProductCategorySearch { slf.ID = id return slf } func (slf *ProductCategorySearch) SetName(name string) *ProductCategorySearch { slf.Name = name return slf } func (slf *ProductCategorySearch) SetKeyword(keyword string) *ProductCategorySearch { slf.Keyword = keyword return slf } func (slf *ProductCategorySearch) SetPreload(preload bool) *ProductCategorySearch { slf.Preload = preload return slf } func (slf *ProductCategorySearch) build() *gorm.DB { var db = slf.Orm.Model(&ProductCategory{}) 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 *ProductCategorySearch) Create(record *ProductCategory) error { var db = slf.build() if err := db.Create(record).Error; err != nil { return err } return nil } // CreateBatch 批量插入 func (slf *ProductCategorySearch) CreateBatch(records []*ProductCategory) 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 *ProductCategorySearch) Update(record *ProductCategory) 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 *ProductCategorySearch) 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 *ProductCategorySearch) 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 *ProductCategorySearch) Delete() error { var db = slf.build() return db.Delete(&ProductCategory{}).Error } func (slf *ProductCategorySearch) First() (*ProductCategory, error) { var ( record = new(ProductCategory) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } func (slf *ProductCategorySearch) Find() ([]*ProductCategory, int64, error) { var ( records = make([]*ProductCategory, 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 *ProductCategorySearch) FindNotTotal() ([]*ProductCategory, error) { var ( records = make([]*ProductCategory, 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 *ProductCategorySearch) FindByQuery(query string, args []interface{}) ([]*ProductCategory, int64, error) { var ( records = make([]*ProductCategory, 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 *ProductCategorySearch) FindByQueryNotTotal(query string, args []interface{}) ([]*ProductCategory, error) { var ( records = make([]*ProductCategory, 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 }