Compare commits

...

2 Commits

Author SHA1 Message Date
33cdf7278e 增加最后一次操作类型 2025-11-27 18:32:22 +08:00
3b12802900 实现按原料是否有库存筛选 2025-11-27 17:33:28 +08:00
8 changed files with 167 additions and 36 deletions

View File

@@ -3392,6 +3392,12 @@ const docTemplate = `{
],
"summary": "获取当前库存列表",
"parameters": [
{
"type": "boolean",
"description": "只查询有库存的原料",
"name": "has_stock",
"in": "query"
},
{
"type": "string",
"description": "排序字段, 例如 \"stock DESC\"",
@@ -3715,6 +3721,7 @@ const docTemplate = `{
},
{
"enum": [
7,
-1,
0,
1,
@@ -3724,12 +3731,12 @@ const docTemplate = `{
5,
-1,
5,
6,
7
6
],
"type": "integer",
"format": "int32",
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
@@ -3739,8 +3746,7 @@ const docTemplate = `{
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel",
"_numLevels"
"InvalidLevel"
],
"name": "level",
"in": "query"
@@ -7267,6 +7273,10 @@ const docTemplate = `{
"type": "string",
"maxLength": 255
},
"max_addition_ratio": {
"description": "最大添加比例",
"type": "number"
},
"name": {
"description": "原料名称",
"type": "string",
@@ -7336,6 +7346,14 @@ const docTemplate = `{
"dto.CurrentStockResponse": {
"type": "object",
"properties": {
"last_operation_source_type": {
"description": "上次库存变动的来源类型",
"allOf": [
{
"$ref": "#/definitions/models.StockLogSourceType"
}
]
},
"last_updated": {
"description": "最后更新时间",
"type": "string"
@@ -8824,6 +8842,10 @@ const docTemplate = `{
"id": {
"type": "integer"
},
"max_addition_ratio": {
"description": "最大添加比例",
"type": "number"
},
"name": {
"type": "string"
},
@@ -9197,7 +9219,8 @@ const docTemplate = `{
"type": "object",
"required": [
"change_amount",
"raw_material_id"
"raw_material_id",
"source_type"
],
"properties": {
"change_amount": {
@@ -9212,6 +9235,18 @@ const docTemplate = `{
"description": "备注",
"type": "string",
"maxLength": 255
},
"source_id": {
"description": "来源ID, 例如: 配方ID, 采购单ID等",
"type": "integer"
},
"source_type": {
"description": "库存变动来源类型",
"allOf": [
{
"$ref": "#/definitions/models.StockLogSourceType"
}
]
}
}
},
@@ -9864,6 +9899,10 @@ const docTemplate = `{
"type": "string",
"maxLength": 255
},
"max_addition_ratio": {
"description": "最大添加比例",
"type": "number"
},
"name": {
"description": "原料名称",
"type": "string",
@@ -10586,6 +10625,7 @@ const docTemplate = `{
"type": "integer",
"format": "int32",
"enum": [
7,
-1,
0,
1,
@@ -10595,10 +10635,10 @@ const docTemplate = `{
5,
-1,
5,
6,
7
6
],
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
@@ -10608,8 +10648,7 @@ const docTemplate = `{
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel",
"_numLevels"
"InvalidLevel"
]
}
},

View File

@@ -3384,6 +3384,12 @@
],
"summary": "获取当前库存列表",
"parameters": [
{
"type": "boolean",
"description": "只查询有库存的原料",
"name": "has_stock",
"in": "query"
},
{
"type": "string",
"description": "排序字段, 例如 \"stock DESC\"",
@@ -3707,6 +3713,7 @@
},
{
"enum": [
7,
-1,
0,
1,
@@ -3716,12 +3723,12 @@
5,
-1,
5,
6,
7
6
],
"type": "integer",
"format": "int32",
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
@@ -3731,8 +3738,7 @@
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel",
"_numLevels"
"InvalidLevel"
],
"name": "level",
"in": "query"
@@ -7259,6 +7265,10 @@
"type": "string",
"maxLength": 255
},
"max_addition_ratio": {
"description": "最大添加比例",
"type": "number"
},
"name": {
"description": "原料名称",
"type": "string",
@@ -7328,6 +7338,14 @@
"dto.CurrentStockResponse": {
"type": "object",
"properties": {
"last_operation_source_type": {
"description": "上次库存变动的来源类型",
"allOf": [
{
"$ref": "#/definitions/models.StockLogSourceType"
}
]
},
"last_updated": {
"description": "最后更新时间",
"type": "string"
@@ -8816,6 +8834,10 @@
"id": {
"type": "integer"
},
"max_addition_ratio": {
"description": "最大添加比例",
"type": "number"
},
"name": {
"type": "string"
},
@@ -9189,7 +9211,8 @@
"type": "object",
"required": [
"change_amount",
"raw_material_id"
"raw_material_id",
"source_type"
],
"properties": {
"change_amount": {
@@ -9204,6 +9227,18 @@
"description": "备注",
"type": "string",
"maxLength": 255
},
"source_id": {
"description": "来源ID, 例如: 配方ID, 采购单ID等",
"type": "integer"
},
"source_type": {
"description": "库存变动来源类型",
"allOf": [
{
"$ref": "#/definitions/models.StockLogSourceType"
}
]
}
}
},
@@ -9856,6 +9891,10 @@
"type": "string",
"maxLength": 255
},
"max_addition_ratio": {
"description": "最大添加比例",
"type": "number"
},
"name": {
"description": "原料名称",
"type": "string",
@@ -10578,6 +10617,7 @@
"type": "integer",
"format": "int32",
"enum": [
7,
-1,
0,
1,
@@ -10587,10 +10627,10 @@
5,
-1,
5,
6,
7
6
],
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
@@ -10600,8 +10640,7 @@
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel",
"_numLevels"
"InvalidLevel"
]
}
},

View File

@@ -416,6 +416,9 @@ definitions:
description: 描述
maxLength: 255
type: string
max_addition_ratio:
description: 最大添加比例
type: number
name:
description: 原料名称
maxLength: 100
@@ -467,6 +470,10 @@ definitions:
type: object
dto.CurrentStockResponse:
properties:
last_operation_source_type:
allOf:
- $ref: '#/definitions/models.StockLogSourceType'
description: 上次库存变动的来源类型
last_updated:
description: 最后更新时间
type: string
@@ -1446,6 +1453,9 @@ definitions:
type: string
id:
type: integer
max_addition_ratio:
description: 最大添加比例
type: number
name:
type: string
raw_material_nutrients:
@@ -1717,9 +1727,17 @@ definitions:
description: 备注
maxLength: 255
type: string
source_id:
description: '来源ID, 例如: 配方ID, 采购单ID等'
type: integer
source_type:
allOf:
- $ref: '#/definitions/models.StockLogSourceType'
description: 库存变动来源类型
required:
- change_amount
- raw_material_id
- source_type
type: object
dto.StockLogResponse:
properties:
@@ -2160,6 +2178,9 @@ definitions:
description: 描述
maxLength: 255
type: string
max_addition_ratio:
description: 最大添加比例
type: number
name:
description: 原料名称
maxLength: 100
@@ -2731,6 +2752,7 @@ definitions:
- PlanTypeFilterSystem
zapcore.Level:
enum:
- 7
- -1
- 0
- 1
@@ -2741,10 +2763,10 @@ definitions:
- -1
- 5
- 6
- 7
format: int32
type: integer
x-enum-varnames:
- _numLevels
- DebugLevel
- InfoLevel
- WarnLevel
@@ -2755,7 +2777,6 @@ definitions:
- _minLevel
- _maxLevel
- InvalidLevel
- _numLevels
info:
contact:
email: divano@example.com
@@ -4793,6 +4814,10 @@ paths:
get:
description: 获取所有原料的当前库存列表,支持分页和过滤。
parameters:
- description: 只查询有库存的原料
in: query
name: has_stock
type: boolean
- description: 排序字段, 例如 "stock DESC"
in: query
name: order_by
@@ -4985,6 +5010,7 @@ paths:
name: end_time
type: string
- enum:
- 7
- -1
- 0
- 1
@@ -4995,12 +5021,12 @@ paths:
- -1
- 5
- 6
- 7
format: int32
in: query
name: level
type: integer
x-enum-varnames:
- _numLevels
- DebugLevel
- InfoLevel
- WarnLevel
@@ -5011,7 +5037,6 @@ paths:
- _minLevel
- _maxLevel
- InvalidLevel
- _numLevels
- enum:
- 邮件
- 企业微信

View File

@@ -14,17 +14,20 @@ func ConvertCurrentStockToDTO(material *models.RawMaterial, latestLog *models.Ra
stock := float32(0)
lastUpdated := material.CreatedAt.Format(time.RFC3339) // 默认使用创建时间
var lastOperationSourceType models.StockLogSourceType
if latestLog != nil {
stock = latestLog.AfterQuantity
lastUpdated = latestLog.HappenedAt.Format(time.RFC3339)
lastOperationSourceType = latestLog.SourceType
}
return &CurrentStockResponse{
RawMaterialID: material.ID,
RawMaterialName: material.Name,
Stock: stock,
LastUpdated: lastUpdated,
RawMaterialID: material.ID,
RawMaterialName: material.Name,
Stock: stock,
LastUpdated: lastUpdated,
LastOperationSourceType: lastOperationSourceType,
}
}

View File

@@ -12,17 +12,20 @@ import (
// StockAdjustmentRequest 手动调整库存的请求体
type StockAdjustmentRequest struct {
RawMaterialID uint32 `json:"raw_material_id" validate:"required"` // 要调整的原料ID
ChangeAmount float32 `json:"change_amount" validate:"required,ne=0"` // 变动数量, 正数为入库, 负数为出库, 单位: g
Remarks string `json:"remarks" validate:"max=255"` // 备注
RawMaterialID uint32 `json:"raw_material_id" validate:"required"` // 要调整的原料ID
ChangeAmount float32 `json:"change_amount" validate:"required,ne=0"` // 变动数量, 正数为入库, 负数为出库, 单位: g
SourceType models.StockLogSourceType `json:"source_type" validate:"required"` // 库存变动来源类型
SourceID *uint32 `json:"source_id,omitempty"` // 来源ID, 例如: 配方ID, 采购单ID等
Remarks string `json:"remarks" validate:"max=255"` // 备注
}
// CurrentStockResponse 单个原料及其当前库存的响应体
type CurrentStockResponse struct {
RawMaterialID uint32 `json:"raw_material_id"` // 原料ID
RawMaterialName string `json:"raw_material_name"` // 原料名称
Stock float32 `json:"stock"` // 当前库存量, 单位: g
LastUpdated string `json:"last_updated"` // 最后更新时间
RawMaterialID uint32 `json:"raw_material_id"` // 原料ID
RawMaterialName string `json:"raw_material_name"` // 原料名称
Stock float32 `json:"stock"` // 当前库存量, 单位: g
LastUpdated string `json:"last_updated"` // 最后更新时间
LastOperationSourceType models.StockLogSourceType `json:"last_operation_source_type"` // 上次库存变动的来源类型
}
// ListCurrentStockRequest 定义了获取当前库存列表的请求参数
@@ -31,6 +34,7 @@ type ListCurrentStockRequest struct {
PageSize int `json:"page_size" query:"page_size"` // 每页数量
RawMaterialName *string `json:"raw_material_name" query:"raw_material_name"` // 按原料名称模糊查询
OrderBy string `json:"order_by" query:"order_by"` // 排序字段, 例如 "stock DESC"
HasStock *bool `json:"has_stock" query:"has_stock"` // 只查询有库存的原料
}
// ListCurrentStockResponse 是获取当前库存列表的响应结构

View File

@@ -47,7 +47,7 @@ func (s *inventoryServiceImpl) AdjustStock(ctx context.Context, req *dto.StockAd
serviceCtx := logs.AddFuncName(ctx, s.ctx, "AdjustStock")
// 调用领域服务执行核心业务逻辑
log, err := s.invSvc.AdjustStock(serviceCtx, req.RawMaterialID, req.ChangeAmount, models.StockLogSourceManual, nil, req.Remarks)
log, err := s.invSvc.AdjustStock(serviceCtx, req.RawMaterialID, req.ChangeAmount, req.SourceType, req.SourceID, req.Remarks)
if err != nil {
if errors.Is(err, inventory.ErrRawMaterialNotFound) {
return nil, ErrInventoryRawMaterialNotFound
@@ -75,8 +75,9 @@ func (s *inventoryServiceImpl) ListCurrentStock(ctx context.Context, req *dto.Li
// 1. 获取分页的原料列表
rawMatOpts := repository.RawMaterialListOptions{
Name: req.RawMaterialName,
OrderBy: req.OrderBy, // 注意:这里的排序可能需要调整,比如按原料名排序
Name: req.RawMaterialName,
OrderBy: req.OrderBy, // 注意:这里的排序可能需要调整,比如按原料名排序
HasStock: req.HasStock,
}
rawMaterials, total, err := s.rawMatRepo.ListRawMaterials(serviceCtx, rawMatOpts, req.Page, req.PageSize)
if err != nil {

View File

@@ -18,6 +18,7 @@ type RawMaterialListOptions struct {
NutrientName *string
MinReferencePrice *float32 // 参考价格最小值
MaxReferencePrice *float32 // 参考价格最大值
HasStock *bool // 是否只查询有库存的原料
OrderBy string
}
@@ -122,6 +123,22 @@ func (r *gormRawMaterialRepository) ListRawMaterials(ctx context.Context, opts R
db = db.Where("reference_price <= ?", *opts.MaxReferencePrice)
}
// 筛选有库存的原料
if opts.HasStock != nil && *opts.HasStock {
// 内部子查询:生成带有 rn 的结果集GORM 会自动为 models.RawMaterialStockLog 添加 deleted_at IS NULL
rankedLogsQuery := r.db.Model(&models.RawMaterialStockLog{}).
Select("raw_material_id, after_quantity, ROW_NUMBER() OVER(PARTITION BY raw_material_id ORDER BY happened_at DESC, id DESC) as rn")
// 外部子查询:从 ranked_logs 中筛选 rn=1 且 after_quantity > 0 的 raw_material_id
// GORM 会将 rankedLogsQuery 作为一个子查询嵌入到 FROM 子句中
latestStockLogSubQuery := r.db.Table("(?) as ranked_logs", rankedLogsQuery).
Select("raw_material_id").
Where("rn = 1 AND after_quantity > 0")
// 将这个子查询直接应用到主查询的 WHERE id IN (?) 条件中
db = db.Where("id IN (?)", latestStockLogSubQuery)
}
// 首先计算总数
if err := db.Count(&total).Error; err != nil {
return nil, 0, err

View File

@@ -145,6 +145,9 @@ internal/domain/task/task.go
internal/infra/config/config.go
internal/infra/database/postgres.go
internal/infra/database/seeder.go
internal/infra/database/seeder/nutrient_seeder.go
internal/infra/database/seeder/pig_nutrient_requirement_seeder.go
internal/infra/database/seeder/utils.go
internal/infra/database/storage.go
internal/infra/logs/context.go
internal/infra/logs/encoder.go