From d91f181819984ed68d928bec6e926da6566e7a3f Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 05 七月 2024 14:53:02 +0800
Subject: [PATCH] 当操作为出库或调拨时,操作详情明细返回产品相应位置的实时库存

---
 controllers/operation.go          |   29 ++++++++-
 models/location_product_amount.go |   10 +++
 docs/swagger.yaml                 |   14 ++++
 docs/docs.go                      |   26 +++++++-
 docs/swagger.json                 |   23 +++++++
 service/location_amount.go        |   24 ++++++++
 service/operation.go              |    2 
 7 files changed, 114 insertions(+), 14 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index f0afda4..5efb66b 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -287,12 +287,11 @@
 
 // GetOperationInfo
 // @Tags      鍏ュ簱/鍑哄簱
-// @Summary   鍏ュ簱/鍑哄簱鍒楄〃
+// @Summary   鎿嶄綔璇︽儏鎺ュ彛
 // @Produce   application/json
-//
-//	@Param		id	path		string			true	"id"
-//
-// @Success   200 {object} util.Response	"鎴愬姛"
+// @Param	  id path  string true "id"
+// @Param     Authorization	header string true "token"
+// @Success   200 {object} util.Response{data=models.Operation}	"鎴愬姛"
 // @Router    /api-wms/v1/operation/getOperationInfo/{id} [get]
 func (slf OperationController) GetOperationInfo(c *gin.Context) {
 	number := c.Param("id")
@@ -306,6 +305,26 @@
 		util.ResponseFormat(c, code.RequestParamError, err.Error())
 		return
 	}
+
+	if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing || operation.BaseOperationType == constvar.BaseOperationTypeInternal { //鏌ヨ瀹炴椂鍦ㄥ簱搴撳瓨
+		productIds := make([]string, 0)
+		locationIds := make([]int, 0)
+		for _, v := range operation.Details {
+			productIds = append(productIds, v.ProductId)
+			locationIds = append(locationIds, v.FromLocationID)
+		}
+		locationAmountMap, err := service.GetLocationAmounts(productIds, locationIds)
+		if err != nil {
+			util.ResponseFormat(c, code.RequestParamError, "鏌ヨ浜у搧搴撳瓨閿欒")
+			return
+		}
+		for _, v := range operation.Details {
+			if locationAmountMap[v.ProductId] != nil {
+				v.StockAmount = locationAmountMap[v.ProductId][v.FromLocationID]
+			}
+		}
+	}
+
 	util.ResponseFormat(c, code.Success, operation)
 }
 
diff --git a/docs/docs.go b/docs/docs.go
index da9fa40..aee23f5 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -1,5 +1,4 @@
-// Code generated by swaggo/swag. DO NOT EDIT.
-
+// Package docs Code generated by swaggo/swag. DO NOT EDIT
 package docs
 
 import "github.com/swaggo/swag"
@@ -2032,7 +2031,7 @@
                 "tags": [
                     "鍏ュ簱/鍑哄簱"
                 ],
-                "summary": "鍏ュ簱/鍑哄簱鍒楄〃",
+                "summary": "鎿嶄綔璇︽儏鎺ュ彛",
                 "parameters": [
                     {
                         "type": "string",
@@ -2040,13 +2039,32 @@
                         "name": "id",
                         "in": "path",
                         "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "token",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
                     }
                 ],
                 "responses": {
                     "200": {
                         "description": "鎴愬姛",
                         "schema": {
-                            "$ref": "#/definitions/util.Response"
+                            "allOf": [
+                                {
+                                    "$ref": "#/definitions/util.Response"
+                                },
+                                {
+                                    "type": "object",
+                                    "properties": {
+                                        "data": {
+                                            "$ref": "#/definitions/models.Operation"
+                                        }
+                                    }
+                                }
+                            ]
                         }
                     }
                 }
diff --git a/docs/swagger.json b/docs/swagger.json
index 6d4985c..76cdd10 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -2020,7 +2020,7 @@
                 "tags": [
                     "鍏ュ簱/鍑哄簱"
                 ],
-                "summary": "鍏ュ簱/鍑哄簱鍒楄〃",
+                "summary": "鎿嶄綔璇︽儏鎺ュ彛",
                 "parameters": [
                     {
                         "type": "string",
@@ -2028,13 +2028,32 @@
                         "name": "id",
                         "in": "path",
                         "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "token",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
                     }
                 ],
                 "responses": {
                     "200": {
                         "description": "鎴愬姛",
                         "schema": {
-                            "$ref": "#/definitions/util.Response"
+                            "allOf": [
+                                {
+                                    "$ref": "#/definitions/util.Response"
+                                },
+                                {
+                                    "type": "object",
+                                    "properties": {
+                                        "data": {
+                                            "$ref": "#/definitions/models.Operation"
+                                        }
+                                    }
+                                }
+                            ]
                         }
                     }
                 }
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index ff40eef..336f0d7 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -3669,14 +3669,24 @@
         name: id
         required: true
         type: string
+      - description: token
+        in: header
+        name: Authorization
+        required: true
+        type: string
       produces:
       - application/json
       responses:
         "200":
           description: 鎴愬姛
           schema:
-            $ref: '#/definitions/util.Response'
-      summary: 鍏ュ簱/鍑哄簱鍒楄〃
+            allOf:
+            - $ref: '#/definitions/util.Response'
+            - properties:
+                data:
+                  $ref: '#/definitions/models.Operation'
+              type: object
+      summary: 鎿嶄綔璇︽儏鎺ュ彛
       tags:
       - 鍏ュ簱/鍑哄簱
   /api-wms/v1/operation/getPersonnelList:
diff --git a/models/location_product_amount.go b/models/location_product_amount.go
index 98abd13..6b82f7c 100644
--- a/models/location_product_amount.go
+++ b/models/location_product_amount.go
@@ -36,6 +36,7 @@
 		ProductIds  []string
 		Ids         []int
 		Query       string
+		Fields      string
 	}
 
 	LocationProductAmountWithOperation struct {
@@ -131,6 +132,11 @@
 	return slf
 }
 
+func (slf *LocationProductAmountSearch) SetFields(fields string) *LocationProductAmountSearch {
+	slf.Fields = fields
+	return slf
+}
+
 func (slf *LocationProductAmountSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&LocationProductAmount{})
 
@@ -181,6 +187,10 @@
 		db = db.Where("warehouse_id = ?", slf.WarehouseId)
 	}
 
+	if slf.Fields != "" {
+		db = db.Select(slf.Fields)
+	}
+
 	return db
 }
 
diff --git a/service/location_amount.go b/service/location_amount.go
new file mode 100644
index 0000000..b6c7b69
--- /dev/null
+++ b/service/location_amount.go
@@ -0,0 +1,24 @@
+package service
+
+import (
+	"github.com/shopspring/decimal"
+	"wms/models"
+)
+
+func GetLocationAmounts(productIds []string, locationIds []int) (locationAmountMap map[string]map[int]decimal.Decimal, err error) {
+	locationAmounts, err := models.NewLocationProductAmountSearch().
+		SetProductIds(productIds).SetLocationIds(locationIds).
+		SetFields("product_id, location_id, amount").
+		Find()
+	if err != nil {
+		return nil, err
+	}
+	locationAmountMap = make(map[string]map[int]decimal.Decimal)
+	for _, locationAmount := range locationAmounts {
+		if locationAmountMap[locationAmount.ProductId] == nil {
+			locationAmountMap[locationAmount.ProductId] = make(map[int]decimal.Decimal)
+		}
+		locationAmountMap[locationAmount.ProductId][locationAmount.LocationId] = locationAmount.Amount
+	}
+	return
+}
diff --git a/service/operation.go b/service/operation.go
index 0775865..733d84f 100644
--- a/service/operation.go
+++ b/service/operation.go
@@ -336,7 +336,7 @@
 			if locAmount.Amount.Equal(v.Amount) {
 				continue
 			}
-			if v.Amount.GreaterThan(locAmount.Amount) {
+			if v.Amount.GreaterThan(locAmount.Amount) { //todo 鏀规垚stockAmount
 				inputDetails = append(inputDetails, &models.OperationDetails{
 					ProductId:        v.ProductId,
 					Amount:           v.Amount.Sub(locAmount.Amount),

--
Gitblit v1.8.0