package models
|
|
import (
|
"encoding/json"
|
"fmt"
|
"github.com/shopspring/decimal"
|
"gorm.io/gorm"
|
"wms/constvar"
|
"wms/pkg/mysqlx"
|
)
|
|
type (
|
// OperationDetails 库存操作明细表
|
OperationDetails struct {
|
WmsModel
|
Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
|
OperationID int `json:"operationId" gorm:"index;type:int;not null;comment:操作记录id"` //操作记录id
|
BaseOperationType constvar.BaseOperationType `json:"baseOperationType" gorm:"type:tinyint;not null;comment:基础作业类型"` //基础作业类型
|
ProductId string `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"` //产品id
|
//ProductName string `json:"productName" gorm:"type:varchar(255);not null;comment:产品名称"` //产品名称
|
Amount decimal.Decimal `json:"amount" gorm:"type:decimal(30,10);not null;comment:数量"` //数量
|
StockAmount decimal.Decimal `json:"stockAmount" gorm:"type:decimal(30,10);comment:库存数量"` //库存数量,盘点时用
|
//Unit string `json:"unit" gorm:"type:varchar(31);comment:单位"` //单位
|
Product Material `json:"product" gorm:"foreignKey:ProductId;references:ID"` // 物料信息
|
|
FromLocationID int `json:"fromLocationId" gorm:"type:int;not null;comment:源位置id"` //源位置id
|
FromLocation Location `json:"fromLocation" gorm:"foreignKey:FromLocationID;references:Id"` //源位置
|
ToLocationID int `json:"toLocationId" gorm:"type:int;not null;comment:目标位置id"` //目标位置id
|
ToLocation Location `json:"toLocation" gorm:"foreignKey:ToLocationID;references:Id"` //目标位置
|
TotalGrossWeight decimal.Decimal `json:"totalGrossWeight" gorm:"type:decimal(20,3);comment:总毛重"` // 总毛重
|
TotalNetWeight decimal.Decimal `json:"totalNetWeight" gorm:"type:decimal(20,3);comment:总净重"` // 总净重
|
AuxiliaryAmount decimal.Decimal `json:"auxiliaryAmount" gorm:"type:decimal(20,3);comment:辅助数量"` // 辅助数量
|
AuxiliaryUnit string `json:"auxiliaryUnit" gorm:"type:varchar(191);comment:辅助单位"` // 辅助单位
|
Remark string `json:"remark" gorm:"type:varchar(1024);comment:备注"` // 备注
|
IsInternalOutput bool `json:"isInternalOutput" gorm:"type:tinyint(1);comment:是否调拨产生的出库"` //是否调拨产生的出库
|
DealerType string `json:"dealerType" gorm:"type:varchar(255);comment:出入库类型"` //出入库类型
|
|
Cost decimal.Decimal `json:"cost" gorm:"type:decimal(20,4);comment:成本单价"` //成本单价
|
SalePrice decimal.Decimal `json:"salePrice" gorm:"type:decimal(20,4);comment:销售单价"` //销售单价
|
// 嘉联仓储添加 SilkMarket、SilkMarketClose
|
SilkMarket string `json:"silkMarket" gorm:"type:varchar(255);comment:庄口"` // 庄口
|
SilkMarketClose string `json:"silkMarketClose" gorm:"type:varchar(10);comment:庄口关闭"` // 庄口关闭
|
|
MoreUnitList []UnitItems `json:"moreUnitList" gorm:"-"`
|
MoreUnitValue string `json:"-" gorm:"type:varchar(255);comment:多单位值"`
|
}
|
|
OperationDetailsSearch struct {
|
OperationDetails
|
Order string
|
PageNum int
|
PageSize int
|
Keyword string
|
Orm *gorm.DB
|
Preload bool
|
OperationIDs []int
|
Fields string
|
ProductIds []string
|
}
|
)
|
|
func (slf *OperationDetails) TableName() string {
|
return "wms_operation_details"
|
}
|
|
func NewOperationDetailsSearch() *OperationDetailsSearch {
|
return &OperationDetailsSearch{Orm: mysqlx.GetDB()}
|
}
|
|
func (slf *OperationDetails) AfterFind(tx *gorm.DB) (err error) {
|
if slf.MoreUnitValue != "" {
|
var arr []UnitItems
|
err := json.Unmarshal([]byte(slf.MoreUnitValue), &arr)
|
if err != nil {
|
return err
|
}
|
slf.MoreUnitList = arr
|
}
|
|
return
|
}
|
|
func (slf *OperationDetails) BeforeCreate(tx *gorm.DB) (err error) {
|
if len(slf.MoreUnitList) != 0 {
|
items := make([]UnitItems, 0)
|
for k, item := range slf.MoreUnitList {
|
if item.Unit != "" && !item.Amount.IsZero() {
|
items = append(items, slf.MoreUnitList[k])
|
}
|
}
|
|
str, err := json.Marshal(items)
|
if err != nil {
|
return err
|
}
|
slf.MoreUnitValue = string(str)
|
}
|
return
|
}
|
|
func (slf *OperationDetailsSearch) SetOrm(tx *gorm.DB) *OperationDetailsSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetPage(page, size int) *OperationDetailsSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetOrder(order string) *OperationDetailsSearch {
|
slf.Order = order
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetID(ID int) *OperationDetailsSearch {
|
slf.Id = ID
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetKeyword(keyword string) *OperationDetailsSearch {
|
slf.Keyword = keyword
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetPreload(preload bool) *OperationDetailsSearch {
|
slf.Preload = preload
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetOperationId(operationId int) *OperationDetailsSearch {
|
slf.OperationID = operationId
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetOperationIds(operationIds []int) *OperationDetailsSearch {
|
slf.OperationIDs = operationIds
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetFields(fields string) *OperationDetailsSearch {
|
slf.Fields = fields
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetProductId(productId string) *OperationDetailsSearch {
|
slf.ProductId = productId
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetProductIds(productIds []string) *OperationDetailsSearch {
|
slf.ProductIds = productIds
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) SetBaseOperationType(baseOperationType constvar.BaseOperationType) *OperationDetailsSearch {
|
slf.BaseOperationType = baseOperationType
|
return slf
|
}
|
|
func (slf *OperationDetailsSearch) build() *gorm.DB {
|
var db = slf.Orm.Model(&OperationDetails{})
|
|
if slf.Id != 0 {
|
db = db.Where("id = ?", slf.Id)
|
}
|
|
if slf.Order != "" {
|
db = db.Order(slf.Order)
|
}
|
|
if slf.Keyword != "" {
|
db = db.Where("product_name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
|
}
|
|
if slf.OperationID != 0 {
|
db = db.Where("operation_id = ?", slf.OperationID)
|
}
|
if slf.ProductId != "" {
|
db = db.Where("product_id = ?", slf.ProductId)
|
}
|
if slf.Preload {
|
db = db.Preload("Product")
|
}
|
|
if len(slf.OperationIDs) > 0 {
|
db = db.Where("operation_id in ?", slf.OperationIDs)
|
}
|
|
if slf.Fields != "" {
|
db = db.Select(slf.Fields)
|
}
|
|
if slf.BaseOperationType != 0 {
|
db = db.Where("base_operation_type = ?", slf.BaseOperationType)
|
}
|
|
if len(slf.ProductIds) > 0 {
|
db = db.Where("product_id in ?", slf.ProductIds)
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *OperationDetailsSearch) Create(record *OperationDetails) error {
|
var db = slf.build()
|
|
if err := db.Create(record).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
// CreateBatch 批量插入
|
func (slf *OperationDetailsSearch) CreateBatch(records []*OperationDetails) 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 *OperationDetailsSearch) Update(record *OperationDetails) 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 *OperationDetailsSearch) 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 *OperationDetailsSearch) 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 *OperationDetailsSearch) Delete() error {
|
var db = slf.build()
|
return db.Unscoped().Delete(&OperationDetails{}).Error
|
}
|
|
func (slf *OperationDetailsSearch) First() (*OperationDetails, error) {
|
var (
|
record = new(OperationDetails)
|
db = slf.build()
|
)
|
|
if err := db.First(record).Error; err != nil {
|
return record, err
|
}
|
|
return record, nil
|
}
|
|
func (slf *OperationDetailsSearch) Find() ([]*OperationDetails, int64, error) {
|
var (
|
records = make([]*OperationDetails, 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 *OperationDetailsSearch) FindNotTotal() ([]*OperationDetails, error) {
|
var (
|
records = make([]*OperationDetails, 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 *OperationDetailsSearch) FindByQuery(query string, args []interface{}) ([]*OperationDetails, int64, error) {
|
var (
|
records = make([]*OperationDetails, 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 *OperationDetailsSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*OperationDetails, error) {
|
var (
|
records = make([]*OperationDetails, 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
|
}
|
|
func (slf *OperationDetailsSearch) FindAll() ([]*OperationDetails, error) {
|
var (
|
records = make([]*OperationDetails, 0)
|
db = slf.build()
|
)
|
|
if err := db.Find(&records).Error; err != nil {
|
return records, fmt.Errorf("find records err: %v", err)
|
}
|
|
return records, nil
|
}
|
|
func (slf *OperationDetailsSearch) GroupSum(groupField string, sumField string) ([]*GroupSum, error) {
|
var (
|
db = slf.build()
|
result = make([]*GroupSum, 0)
|
)
|
if err := db.Select("sum(" + sumField + ") as sum, " + groupField + " as class").Group(groupField).Scan(&result).Error; err != nil {
|
return nil, fmt.Errorf("select group err: %v", err)
|
}
|
return result, nil
|
}
|
|
type GroupByDealerTypeWarehouse struct {
|
DealerType string
|
ProductID string
|
Sum decimal.Decimal
|
}
|
|
func (slf *OperationDetailsSearch) GroupMultiSumAmount() ([]*GroupByDealerTypeWarehouse, error) {
|
var (
|
db = slf.build()
|
result = make([]*GroupByDealerTypeWarehouse, 0)
|
)
|
if err := db.Select("sum(amount) as sum, dealer_type, product_id").Group("product_id, dealer_type").Scan(&result).Error; err != nil {
|
return nil, fmt.Errorf("select group err: %v", err)
|
}
|
return result, nil
|
}
|