package models
|
|
import (
|
"fmt"
|
"github.com/shopspring/decimal"
|
"gorm.io/gorm"
|
"wms/pkg/mysqlx"
|
)
|
|
type (
|
// WarehouseMonthStats 按仓库进行月度统计
|
WarehouseMonthStats struct {
|
WmsModel
|
Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
|
WarehouseId int `json:"warehouseId" gorm:"type:int;not null;default:0"` //仓库ID
|
ProductId string `json:"productId" gorm:"type:varchar(255);not null;comment:产品id"` //产品id
|
ProductName string `json:"productName" gorm:"type:varchar(255);not null;comment:产品名称"` //产品名称
|
Unit string `json:"unit" gorm:"type:char(10);not null;comment:单位"` //单位
|
SalePrice decimal.Decimal `gorm:"type:decimal(35,18);comment:销售单价" json:"salePrice"` //销售单价
|
|
BeginAmount decimal.Decimal `json:"beginAmount" gorm:"type:decimal(30,10);not null;comment:数量"` //期初数量
|
EndAmount decimal.Decimal `json:"amount" gorm:"type:decimal(30,10);not null;comment:数量"` //期末结余数量
|
|
InputAmount decimal.Decimal `json:"inputAmount" gorm:"type:decimal(30,10);not null;comment:数量"` //入库数量
|
InputItems []*WarehouseStatsItems `json:"inputItems" gorm:"-"` //入库明细
|
Items []*WarehouseStatsItems `json:"-"`
|
OutputAmount decimal.Decimal `json:"outputAmount" gorm:"type:decimal(30,10);not null;comment:数量"` //出库数量
|
OutputItems []*WarehouseStatsItems `json:"outputItems" gorm:"-"` //出库明细
|
|
Date string `json:"date" gorm:"index;type:varchar(255); not null;default ''"` //日期 2024-04
|
}
|
|
WarehouseStatsItems struct {
|
WarehouseMonthStatsId int `json:"warehouseMonthStatsId"`
|
Type MonthStatsItemsType `json:"type" gorm:"type:tinyint;not null;default:1"`
|
Name string `json:"name" gorm:"type:varchar(255);not null;default:''"` //入库来源,出库去处
|
Amount decimal.Decimal `json:"amount" gorm:"type:decimal(30,10);not null;"` //数量
|
}
|
|
WarehouseMonthStatsSearch struct {
|
WarehouseMonthStats
|
Order string
|
PageNum int
|
PageSize int
|
Keyword string
|
Orm *gorm.DB
|
Preload bool
|
Fields string
|
}
|
)
|
|
type MonthStatsItemsType int
|
|
const (
|
MonthStatsItemsTypeInput MonthStatsItemsType = 1 //入库
|
MonthStatsItemsTypeOutput MonthStatsItemsType = 2 //出库
|
)
|
|
func (slf *WarehouseStatsItems) TableName() string {
|
return "wms_warehouse_month_stats_items"
|
}
|
|
func (slf *WarehouseMonthStats) TableName() string {
|
return "wms_warehouse_month_stats"
|
}
|
|
func (slf *WarehouseMonthStats) BeforeCreate(tx *gorm.DB) error {
|
if len(slf.InputItems) != 0 || len(slf.OutputItems) != 0 {
|
items := make([]*WarehouseStatsItems, 0, len(slf.InputItems)+len(slf.OutputItems))
|
for _, item := range slf.InputItems {
|
items = append(items, &WarehouseStatsItems{
|
Type: MonthStatsItemsTypeInput,
|
Name: item.Name,
|
Amount: item.Amount,
|
})
|
}
|
|
for _, item := range slf.OutputItems {
|
items = append(items, &WarehouseStatsItems{
|
Type: MonthStatsItemsTypeOutput,
|
Name: item.Name,
|
Amount: item.Amount,
|
})
|
}
|
|
slf.Items = items
|
}
|
|
return nil
|
}
|
|
func (slf *WarehouseMonthStats) AfterFind(tx *gorm.DB) error {
|
if len(slf.Items) != 0 {
|
inputItems := make([]*WarehouseStatsItems, 0)
|
outputItems := make([]*WarehouseStatsItems, 0)
|
for _, v := range slf.Items {
|
item := WarehouseStatsItems{
|
Type: v.Type,
|
Name: v.Name,
|
Amount: v.Amount,
|
}
|
if v.Type == MonthStatsItemsTypeInput {
|
inputItems = append(inputItems, &item)
|
} else {
|
outputItems = append(outputItems, &item)
|
}
|
}
|
}
|
return nil
|
}
|
|
func NewWarehouseMonthStatsSearch() *WarehouseMonthStatsSearch {
|
return &WarehouseMonthStatsSearch{Orm: mysqlx.GetDB()}
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetOrm(tx *gorm.DB) *WarehouseMonthStatsSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetPage(page, size int) *WarehouseMonthStatsSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetOrder(order string) *WarehouseMonthStatsSearch {
|
slf.Order = order
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetID(id int) *WarehouseMonthStatsSearch {
|
slf.Id = id
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetKeyword(keyword string) *WarehouseMonthStatsSearch {
|
slf.Keyword = keyword
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetPreload(preload bool) *WarehouseMonthStatsSearch {
|
slf.Preload = preload
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetDate(date string) *WarehouseMonthStatsSearch {
|
slf.Date = date
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetFields(fields string) *WarehouseMonthStatsSearch {
|
slf.Fields = fields
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) SetWarehouseId(id int) *WarehouseMonthStatsSearch {
|
slf.WarehouseId = id
|
return slf
|
}
|
|
func (slf *WarehouseMonthStatsSearch) Save(record *WarehouseMonthStats) 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 *WarehouseMonthStatsSearch) build() *gorm.DB {
|
var db = slf.Orm.Model(&WarehouseMonthStats{})
|
|
if slf.Id != 0 {
|
db = db.Where("id = ?", slf.Id)
|
}
|
|
if slf.Order != "" {
|
db = db.Order(slf.Order)
|
}
|
|
if slf.Keyword != "" {
|
kw := fmt.Sprintf("%%%v%%", slf.Keyword)
|
db = db.Where("product_id like ? or product_name like ?", kw, kw)
|
}
|
|
if slf.Date != "" {
|
db = db.Where("date = ?", slf.Date)
|
}
|
|
if slf.Fields != "" {
|
db = db.Select(slf.Fields)
|
}
|
|
if slf.WarehouseId != 0 {
|
db = db.Where("warehouse_id = ?", slf.WarehouseId)
|
}
|
|
if slf.Preload {
|
db = db.Preload("Items")
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *WarehouseMonthStatsSearch) Create(record *WarehouseMonthStats) error {
|
var db = slf.build()
|
|
if err := db.Create(record).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
// CreateBatch 批量插入
|
func (slf *WarehouseMonthStatsSearch) CreateBatch(records []*WarehouseMonthStats) 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 *WarehouseMonthStatsSearch) Update(record *WarehouseMonthStats) 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 *WarehouseMonthStatsSearch) 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 *WarehouseMonthStatsSearch) 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 *WarehouseMonthStatsSearch) Delete() error {
|
var db = slf.build()
|
return db.Delete(&WarehouseMonthStats{}).Error
|
}
|
|
func (slf *WarehouseMonthStatsSearch) First() (*WarehouseMonthStats, error) {
|
var (
|
record = new(WarehouseMonthStats)
|
db = slf.build()
|
)
|
|
if err := db.First(record).Error; err != nil {
|
return record, err
|
}
|
|
return record, nil
|
}
|
|
func (slf *WarehouseMonthStatsSearch) Find() ([]*WarehouseMonthStats, int64, error) {
|
var (
|
records = make([]*WarehouseMonthStats, 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 *WarehouseMonthStatsSearch) FindNotTotal() ([]*WarehouseMonthStats, error) {
|
var (
|
records = make([]*WarehouseMonthStats, 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 *WarehouseMonthStatsSearch) FindByQuery(query string, args []interface{}) ([]*WarehouseMonthStats, int64, error) {
|
var (
|
records = make([]*WarehouseMonthStats, 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 *WarehouseMonthStatsSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*WarehouseMonthStats, error) {
|
var (
|
records = make([]*WarehouseMonthStats, 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 WarehouseMonthStatsMap(records []*WarehouseMonthStats) (m map[string]*WarehouseMonthStats) {
|
m = make(map[string]*WarehouseMonthStats, len(records))
|
for _, record := range records {
|
m[record.ProductId] = record
|
}
|
return m
|
}
|
|
func WarehouseStatsItemMap(records []*WarehouseStatsItems) (m map[string]*WarehouseStatsItems) {
|
m = make(map[string]*WarehouseStatsItems, len(records))
|
for _, record := range records {
|
m[record.Name] = record
|
}
|
return m
|
}
|
|
func (slf *WarehouseMonthStatsSearch) Count() (int64, error) {
|
var (
|
total int64
|
db = slf.build()
|
)
|
err := db.Count(&total).Error
|
return total, err
|
}
|