From 33cdf7278ec1f90c88dde1d34aac1870681a2d24 Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Thu, 27 Nov 2025 18:32:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E6=93=8D=E4=BD=9C=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs.go | 23 ++++++++++++++++++++++- docs/swagger.json | 23 ++++++++++++++++++++++- docs/swagger.yaml | 12 ++++++++++++ internal/app/dto/inventory_converter.go | 11 +++++++---- internal/app/dto/inventory_dto.go | 17 ++++++++++------- internal/app/service/inventory_service.go | 2 +- 6 files changed, 74 insertions(+), 14 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index d08f2c4..97adb7f 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -7346,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" @@ -9211,7 +9219,8 @@ const docTemplate = `{ "type": "object", "required": [ "change_amount", - "raw_material_id" + "raw_material_id", + "source_type" ], "properties": { "change_amount": { @@ -9226,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" + } + ] } } }, diff --git a/docs/swagger.json b/docs/swagger.json index 7687226..450e877 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -7338,6 +7338,14 @@ "dto.CurrentStockResponse": { "type": "object", "properties": { + "last_operation_source_type": { + "description": "上次库存变动的来源类型", + "allOf": [ + { + "$ref": "#/definitions/models.StockLogSourceType" + } + ] + }, "last_updated": { "description": "最后更新时间", "type": "string" @@ -9203,7 +9211,8 @@ "type": "object", "required": [ "change_amount", - "raw_material_id" + "raw_material_id", + "source_type" ], "properties": { "change_amount": { @@ -9218,6 +9227,18 @@ "description": "备注", "type": "string", "maxLength": 255 + }, + "source_id": { + "description": "来源ID, 例如: 配方ID, 采购单ID等", + "type": "integer" + }, + "source_type": { + "description": "库存变动来源类型", + "allOf": [ + { + "$ref": "#/definitions/models.StockLogSourceType" + } + ] } } }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 50b7304..08f9ea2 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -470,6 +470,10 @@ definitions: type: object dto.CurrentStockResponse: properties: + last_operation_source_type: + allOf: + - $ref: '#/definitions/models.StockLogSourceType' + description: 上次库存变动的来源类型 last_updated: description: 最后更新时间 type: string @@ -1723,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: diff --git a/internal/app/dto/inventory_converter.go b/internal/app/dto/inventory_converter.go index 91ad693..02d4256 100644 --- a/internal/app/dto/inventory_converter.go +++ b/internal/app/dto/inventory_converter.go @@ -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, } } diff --git a/internal/app/dto/inventory_dto.go b/internal/app/dto/inventory_dto.go index a695a91..5844099 100644 --- a/internal/app/dto/inventory_dto.go +++ b/internal/app/dto/inventory_dto.go @@ -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 定义了获取当前库存列表的请求参数 diff --git a/internal/app/service/inventory_service.go b/internal/app/service/inventory_service.go index 93226c2..0acef7c 100644 --- a/internal/app/service/inventory_service.go +++ b/internal/app/service/inventory_service.go @@ -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