From 4a9bc4b7c84985047c7ebe0b991e8c8364bb56a2 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期五, 03 十一月 2023 10:58:25 +0800
Subject: [PATCH] 重订货规则修改

---
 models/location.go                     |    1 
 models/location_product_amount.go      |   15 +-
 request/reorder_rule_request.go        |   15 +-
 controllers/reorder_rule_controller.go |   94 ++++++++++++------
 docs/swagger.yaml                      |   28 +++++
 controllers/location.go                |   28 +++++
 docs/docs.go                           |   47 +++++++++
 docs/swagger.json                      |   47 +++++++++
 router/router.go                       |    1 
 9 files changed, 231 insertions(+), 45 deletions(-)

diff --git a/controllers/location.go b/controllers/location.go
index 5bfec45..2ef2a95 100644
--- a/controllers/location.go
+++ b/controllers/location.go
@@ -76,6 +76,34 @@
 	util.ResponseFormatList(c, code.Success, list, int(total))
 }
 
+// GetLocationTreeList
+// @Tags      浣嶇疆
+// @Summary   鑾峰彇浣嶇疆鍒楄〃鏍�
+// @Produce   application/json
+// @Success   200 {object} util.ResponseList{data=[]models.Location}	"鎴愬姛"
+// @Router    /api-wms/v1/location/getLocationTreeList [get]
+func (slf LocationController) GetLocationTreeList(c *gin.Context) {
+	all, err := models.NewLocationSearch().SetType(3).FindAll()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
+		return
+	}
+	var tree []*models.Location
+	m := make(map[int]*models.Location)
+	for _, location := range all {
+		m[location.Id] = location
+	}
+	for _, location := range all {
+		if location.ParentId == 0 {
+			tree = append(tree, location)
+		} else {
+			m[location.ParentId].Children = append(m[location.ParentId].Children, location)
+		}
+	}
+
+	util.ResponseFormat(c, code.Success, tree)
+}
+
 // GetLocationDetails
 // @Tags      浣嶇疆
 // @Summary   鑾峰彇浣嶇疆璇︽儏
diff --git a/controllers/reorder_rule_controller.go b/controllers/reorder_rule_controller.go
index 4add8d6..0c04e43 100644
--- a/controllers/reorder_rule_controller.go
+++ b/controllers/reorder_rule_controller.go
@@ -65,7 +65,7 @@
 	if params.PageInfo.Check() {
 		search.SetPage(params.Page, params.PageSize)
 	}
-	rules, total, err := search.SetPreload(true).SetKeyword(params.KeyWord).Find()
+	rules, total, err := search.SetPreload(true).SetKeyword(params.KeyWord).SetLocationId(params.LocationId).SetProductId(params.ProductId).Find()
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ閲嶈璐ц鍒欏垪琛ㄥけ璐�")
 		return
@@ -76,29 +76,44 @@
 		productIds = append(productIds, rule.ProductId)
 		locationIds = append(locationIds, rule.LocationId)
 	}
+	if params.LocationId != 0 {
+		locationIds = []int{params.LocationId}
+	}
+	if params.ProductId != "" {
+		productIds = []string{params.ProductId}
+	}
 	//鍦ㄥ簱
-	var status = []constvar.OperationStatus{constvar.OperationStatus_Finish, constvar.OperationStatus_Ready}
+	amounts, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ鍦ㄥ簱鏁伴噺澶辫触")
+		return
+	}
+	for _, rule := range rules {
+		for _, amount := range amounts {
+			if rule.ProductId == amount.ProductId && rule.LocationId == amount.LocationId {
+				rule.Amount = rule.Amount.Add(amount.Amount)
+			}
+		}
+	}
+
+	//棰勬祴
+	//鍏ュ簱灏辩华
+	var status = []constvar.OperationStatus{constvar.OperationStatus_Ready}
 	var operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal}
 	amount, err := GetProductAmount(productIds, locationIds, nil, status, operationType)
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ閲嶈璐ц鍒欏垪琛ㄥけ璐�")
 		return
 	}
-	mp := make(map[int]decimal.Decimal)
 	for _, rule := range rules {
 		for _, productAmount := range amount {
-			if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.ToLocationId &&
-				productAmount.Status == constvar.OperationStatus_Finish {
-				rule.Amount = rule.Amount.Add(productAmount.Amount)
-			}
-			if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.ToLocationId &&
-				productAmount.Status == constvar.OperationStatus_Ready {
-				mp[rule.Id] = mp[rule.Id].Add(productAmount.Amount)
+			if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.ToLocationId {
+				rule.Prediction = rule.Prediction.Add(productAmount.Amount)
 			}
 		}
+		rule.Prediction = rule.Amount.Add(rule.Prediction)
 	}
-	//棰勬祴
-	status = []constvar.OperationStatus{constvar.OperationStatus_Finish, constvar.OperationStatus_Ready}
+	//鍑哄簱灏辩华
 	operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
 	amount, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
 	if err != nil {
@@ -108,13 +123,22 @@
 	for _, rule := range rules {
 		for _, productAmount := range amount {
 			if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.FromLocationId {
-				rule.Prediction = rule.Prediction.Add(productAmount.Amount)
+				rule.Prediction = rule.Prediction.Sub(productAmount.Amount)
 			}
 		}
-		rule.Prediction = rule.Amount.Add(mp[rule.Id]).Sub(rule.Prediction)
+	}
+	var result []*models.ReorderRule
+	if params.Type == "" {
+		result = rules
+	} else {
+		for _, rule := range rules {
+			if rule.MinInventory.GreaterThan(rule.Prediction) {
+				result = append(result, rule)
+			}
+		}
 	}
 
-	util.ResponseFormatList(c, code.Success, rules, int(total))
+	util.ResponseFormatList(c, code.Success, result, int(total))
 }
 
 // 璁$畻鍦ㄥ簱涓庨娴嬫暟閲�
@@ -124,7 +148,7 @@
 	search := models.NewOperationDetailsSearch()
 	search.Orm = search.Orm.Model(&models.OperationDetails{}).
 		Select("wms_operation_details.product_id, wms_operation_details.amount, wms_operation.to_location_id as to_location_id, " +
-			"wms_operation.from_location_id as from_location_id, wms_operation.status").
+			"wms_operation.from_location_id as from_location_id, wms_operation.base_operation_type").
 		Joins("left join wms_operation on wms_operation_details.operation_id = wms_operation.id")
 	if len(productIds) > 0 {
 		search.Orm.Where("wms_operation_details.product_id in (?)", productIds)
@@ -163,30 +187,24 @@
 	productIds = append(productIds, params.ProductId)
 	locationIds = append(locationIds, params.LocationId)
 	amount := decimal.NewFromInt(0)
-	p := decimal.NewFromInt(0)
 	prediction := decimal.NewFromInt(0)
 	//鍦ㄥ簱
-	var status = []constvar.OperationStatus{constvar.OperationStatus_Finish, constvar.OperationStatus_Ready}
-	var operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal}
-	list, err := GetProductAmount(productIds, locationIds, nil, status, operationType)
+	find, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find()
 	if err != nil {
-		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ閲嶈璐ц鍒欏垪琛ㄥけ璐�")
+		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ鍦ㄥ簱鏁伴噺澶辫触")
 		return
 	}
-	for _, productAmount := range list {
-		if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.ToLocationId &&
-			productAmount.Status == constvar.OperationStatus_Finish {
+	for _, productAmount := range find {
+		if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.LocationId {
 			amount = amount.Add(productAmount.Amount)
 		}
-		if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.ToLocationId &&
-			productAmount.Status == constvar.OperationStatus_Ready {
-			p = p.Add(productAmount.Amount)
-		}
 	}
+
 	//棰勬祴
-	status = []constvar.OperationStatus{constvar.OperationStatus_Finish, constvar.OperationStatus_Ready}
-	operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
-	list, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
+	//鍏ュ簱灏辩华
+	var status = []constvar.OperationStatus{constvar.OperationStatus_Ready}
+	var operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal}
+	list, err := GetProductAmount(productIds, locationIds, nil, status, operationType)
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ閲嶈璐ц鍒欏垪琛ㄥけ璐�")
 		return
@@ -196,7 +214,19 @@
 			prediction = prediction.Add(productAmount.Amount)
 		}
 	}
-	prediction = amount.Add(p).Sub(prediction)
+	prediction = amount.Add(prediction)
+	//鍑哄簱灏辩华
+	operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
+	list, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ閲嶈璐ц鍒欏垪琛ㄥけ璐�")
+		return
+	}
+	for _, productAmount := range list {
+		if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.FromLocationId {
+			prediction = prediction.Sub(productAmount.Amount)
+		}
+	}
 	m := make(map[string]int64)
 	m["amount"] = amount.IntPart()
 	m["prediction"] = prediction.IntPart()
diff --git a/docs/docs.go b/docs/docs.go
index 4e9ca24..575933f 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -572,6 +572,40 @@
                 }
             }
         },
+        "/api-wms/v1/location/getLocationTreeList": {
+            "get": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "浣嶇疆"
+                ],
+                "summary": "鑾峰彇浣嶇疆鍒楄〃鏍�",
+                "responses": {
+                    "200": {
+                        "description": "鎴愬姛",
+                        "schema": {
+                            "allOf": [
+                                {
+                                    "$ref": "#/definitions/util.ResponseList"
+                                },
+                                {
+                                    "type": "object",
+                                    "properties": {
+                                        "data": {
+                                            "type": "array",
+                                            "items": {
+                                                "$ref": "#/definitions/models.Location"
+                                            }
+                                        }
+                                    }
+                                }
+                            ]
+                        }
+                    }
+                }
+            }
+        },
         "/api-wms/v1/location/updateLocation": {
             "post": {
                 "produces": [
@@ -2445,6 +2479,12 @@
         "models.Location": {
             "type": "object",
             "properties": {
+                "children": {
+                    "type": "array",
+                    "items": {
+                        "$ref": "#/definitions/models.Location"
+                    }
+                },
                 "companyId": {
                     "description": "鍏徃id",
                     "type": "integer"
@@ -3438,6 +3478,9 @@
                 "keyWord": {
                     "type": "string"
                 },
+                "locationId": {
+                    "type": "integer"
+                },
                 "page": {
                     "description": "椤电爜",
                     "type": "integer"
@@ -3445,6 +3488,10 @@
                 "pageSize": {
                     "description": "姣忛〉澶у皬",
                     "type": "integer"
+                },
+                "type": {
+                    "description": "绫诲瀷:bh=琛ヨ揣",
+                    "type": "string"
                 }
             }
         },
diff --git a/docs/swagger.json b/docs/swagger.json
index 953fc06..16eff99 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -560,6 +560,40 @@
                 }
             }
         },
+        "/api-wms/v1/location/getLocationTreeList": {
+            "get": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "浣嶇疆"
+                ],
+                "summary": "鑾峰彇浣嶇疆鍒楄〃鏍�",
+                "responses": {
+                    "200": {
+                        "description": "鎴愬姛",
+                        "schema": {
+                            "allOf": [
+                                {
+                                    "$ref": "#/definitions/util.ResponseList"
+                                },
+                                {
+                                    "type": "object",
+                                    "properties": {
+                                        "data": {
+                                            "type": "array",
+                                            "items": {
+                                                "$ref": "#/definitions/models.Location"
+                                            }
+                                        }
+                                    }
+                                }
+                            ]
+                        }
+                    }
+                }
+            }
+        },
         "/api-wms/v1/location/updateLocation": {
             "post": {
                 "produces": [
@@ -2433,6 +2467,12 @@
         "models.Location": {
             "type": "object",
             "properties": {
+                "children": {
+                    "type": "array",
+                    "items": {
+                        "$ref": "#/definitions/models.Location"
+                    }
+                },
                 "companyId": {
                     "description": "鍏徃id",
                     "type": "integer"
@@ -3426,6 +3466,9 @@
                 "keyWord": {
                     "type": "string"
                 },
+                "locationId": {
+                    "type": "integer"
+                },
                 "page": {
                     "description": "椤电爜",
                     "type": "integer"
@@ -3433,6 +3476,10 @@
                 "pageSize": {
                     "description": "姣忛〉澶у皬",
                     "type": "integer"
+                },
+                "type": {
+                    "description": "绫诲瀷:bh=琛ヨ揣",
+                    "type": "string"
                 }
             }
         },
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 5a13e9c..c318180 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -244,6 +244,10 @@
     type: object
   models.Location:
     properties:
+      children:
+        items:
+          $ref: '#/definitions/models.Location'
+        type: array
       companyId:
         description: 鍏徃id
         type: integer
@@ -937,12 +941,17 @@
     properties:
       keyWord:
         type: string
+      locationId:
+        type: integer
       page:
         description: 椤电爜
         type: integer
       pageSize:
         description: 姣忛〉澶у皬
         type: integer
+      type:
+        description: 绫诲瀷:bh=琛ヨ揣
+        type: string
     type: object
   request.GetRuleList:
     properties:
@@ -1701,6 +1710,25 @@
       summary: 鑾峰彇浣嶇疆鍒楄〃
       tags:
       - 浣嶇疆
+  /api-wms/v1/location/getLocationTreeList:
+    get:
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: 鎴愬姛
+          schema:
+            allOf:
+            - $ref: '#/definitions/util.ResponseList'
+            - properties:
+                data:
+                  items:
+                    $ref: '#/definitions/models.Location'
+                  type: array
+              type: object
+      summary: 鑾峰彇浣嶇疆鍒楄〃鏍�
+      tags:
+      - 浣嶇疆
   /api-wms/v1/location/updateLocation:
     post:
       parameters:
diff --git a/models/location.go b/models/location.go
index ef755e6..028e875 100644
--- a/models/location.go
+++ b/models/location.go
@@ -26,6 +26,7 @@
 		RecentlyCount        string                        `json:"recentlyCount" gorm:"type:varchar(255);comment:鏈�杩戠洏鐐�"`   //鏈�杩戠洏鐐�
 		NextCount            string                        `json:"nextCount" gorm:"type:varchar(255);comment:涓嬫鐩樼偣"`       //涓嬫鐩樼偣
 		JointName            string                        `json:"jointName" gorm:"type:varchar(255);comment:鎷兼帴鍚嶇О"`       //鎷兼帴鍚嶇О
+		Children             []*Location                   `json:"children" gorm:"-"`
 	}
 
 	LocationSearch struct {
diff --git a/models/location_product_amount.go b/models/location_product_amount.go
index 00c7b92..94cf597 100644
--- a/models/location_product_amount.go
+++ b/models/location_product_amount.go
@@ -34,6 +34,7 @@
 		Preload  bool
 		//LocationProductIds []int
 		LocationIds []int
+		ProductIds  []string
 	}
 
 	LocationProductAmountWithOperation struct {
@@ -102,10 +103,10 @@
 	return slf
 }
 
-//func (slf *LocationProductAmountSearch) SetLocationProductIds(ids []int) *LocationProductAmountSearch {
-//	slf.LocationProductIds = ids
-//	return slf
-//}
+func (slf *LocationProductAmountSearch) SetProductIds(ids []string) *LocationProductAmountSearch {
+	slf.ProductIds = ids
+	return slf
+}
 
 func (slf *LocationProductAmountSearch) SetLocationIds(ids []int) *LocationProductAmountSearch {
 	slf.LocationIds = ids
@@ -132,9 +133,9 @@
 	//if slf.LocationProductId != 0 {
 	//	db = db.Where("location_product_id=?", slf.LocationProductId)
 	//}
-	//if len(slf.LocationProductIds) > 0 {
-	//	db = db.Where("location_product_id in (?)", slf.LocationProductIds)
-	//}
+	if len(slf.ProductIds) > 0 {
+		db = db.Where("product_id in (?)", slf.ProductIds)
+	}
 
 	if len(slf.LocationIds) > 0 {
 		db = db.Where("location_id in (?)", slf.LocationIds)
diff --git a/request/reorder_rule_request.go b/request/reorder_rule_request.go
index bd33ec4..53e5c53 100644
--- a/request/reorder_rule_request.go
+++ b/request/reorder_rule_request.go
@@ -7,15 +7,18 @@
 
 type GetReorderRuleList struct {
 	PageInfo
-	KeyWord string `json:"keyWord"`
+	KeyWord    string `json:"keyWord"`
+	LocationId int    `json:"locationId"`
+	ProductId  string `json:"productId"`
+	Type       string `json:"type"` //绫诲瀷:bh=琛ヨ揣
 }
 
 type ProductAmount struct {
-	ProductId      string                   `json:"productId"`
-	ToLocationId   int                      `json:"toLocationId"`
-	FromLocationId int                      `json:"fromLocationId"`
-	Amount         decimal.Decimal          `json:"amount"`
-	Status         constvar.OperationStatus `json:"status"`
+	ProductId         string                     `json:"productId"`
+	ToLocationId      int                        `json:"toLocationId"`
+	FromLocationId    int                        `json:"fromLocationId"`
+	Amount            decimal.Decimal            `json:"amount"`
+	BaseOperationType constvar.BaseOperationType `json:"baseOperationType"`
 }
 
 type GetAmountAndPrediction struct {
diff --git a/router/router.go b/router/router.go
index a591cf7..7d527e8 100644
--- a/router/router.go
+++ b/router/router.go
@@ -61,6 +61,7 @@
 		locationAPI.POST("updateLocation", locationController.UpdateLocation)            //淇敼浣嶇疆
 		locationAPI.GET("getLocationDetails/:id", locationController.GetLocationDetails) //鑾峰彇浣嶇疆璇︽儏
 		locationAPI.DELETE("deleteLocation/:id", locationController.DeleteLocation)      //鍒犻櫎浣嶇疆
+		locationAPI.GET("getLocationTreeList", locationController.GetLocationTreeList)   //鑾峰彇浣嶇疆鍒楄〃鏍�
 	}
 
 	// 涓氬姟绫诲瀷

--
Gitblit v1.8.0