From 2716487d13f3521adeb25a48aa07a89f885d32c7 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期一, 09 十月 2023 09:36:08 +0800
Subject: [PATCH] 仓库功能修改

---
 controllers/location_product_amount.go |   38 +++++++++++++++++++++-----------------
 1 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/controllers/location_product_amount.go b/controllers/location_product_amount.go
index 053979e..8d44349 100644
--- a/controllers/location_product_amount.go
+++ b/controllers/location_product_amount.go
@@ -51,7 +51,8 @@
 					material.unit,
 					wms_location_product_amount.create_date,
 					wms_operation_details.amount as adjust_amount,
-					wms_operation.id as operation_id`).
+					wms_operation.id as operation_id,
+					wms_operation.status`).
 		InnerJoins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").
 		InnerJoins("inner join material on material.id=wms_location_product.product_id").
 		InnerJoins("inner join wms_location on wms_location.id=wms_location_product.location_id").
@@ -70,7 +71,7 @@
 	if params.Page*params.PageSize > 0 {
 		search.Orm = search.Orm.Offset((params.Page - 1) * params.PageSize).Limit(params.PageSize)
 	}
-	if err := search.Orm.Find(&records).Error; err != nil {
+	if err := search.Orm.Order("wms_operation.created_at desc").Find(&records).Error; err != nil {
 		util.ResponseFormat(c, code.RequestError, fmt.Errorf("find count err: %v", err))
 		return
 	}
@@ -123,6 +124,11 @@
 		util.ResponseFormat(c, code.RequestError, "涓嶅瓨鍦ㄨ涓婃灦瑙勫垯锛屾棤娉曡繘琛屽簱瀛樼洏鐐�")
 		return
 	}
+	existAmount, err := models.NewLocationProductAmountSearch().SetLocationProductId(locProduct.Id).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestError, err.Error())
+		return
+	}
 	location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeAdjust)).First()
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, err.Error())
@@ -130,11 +136,15 @@
 	}
 	//reqParams.AdjustAmount = reqParams.Amount.Sub(reqParams.DifferenceAmount)
 	reqParams.AdjustAmount = reqParams.DifferenceAmount.Sub(reqParams.Amount)
-	locAmount := models.LocationProductAmount{
-		LocationProductId: locProduct.Id,
-		Amount:            decimal.NewFromInt(0),
-		CreateDate:        time.Now().Format("2006-01-02 15:04:05"),
+	var locAmount models.LocationProductAmount
+	if existAmount.Id == 0 {
+		locAmount = models.LocationProductAmount{
+			LocationProductId: locProduct.Id,
+			Amount:            decimal.NewFromInt(0),
+			CreateDate:        time.Now().Format("2006-01-02 15:04:05"),
+		}
 	}
+
 	detail := &models.OperationDetails{
 		ProductId: reqParams.ProductId,
 		Amount:    reqParams.AdjustAmount,
@@ -150,20 +160,14 @@
 		ToLocationID:      reqParams.LocationId,
 	}
 
-	//if reqParams.AdjustAmount.GreaterThanOrEqual(decimal.NewFromInt(0)) {
-	//	operation.FromLocationID = Location.Id
-	//	operation.ToLocationID = reqParams.LocationId
-	//} else {
-	//	operation.FromLocationID = reqParams.LocationId
-	//	operation.ToLocationID = Location.Id
-	//}
-
 	if err := models.WithTransaction(func(tx *gorm.DB) error {
 		if err := models.NewOperationSearch().SetOrm(tx).Create(&operation); err != nil {
 			return err
 		}
-		if err := models.NewLocationProductAmountSearch().SetOrm(tx).Create(&locAmount); err != nil {
-			return err
+		if existAmount.Id == 0 {
+			if err := models.NewLocationProductAmountSearch().SetOrm(tx).Create(&locAmount); err != nil {
+				return err
+			}
 		}
 		return nil
 	}); err != nil {
@@ -200,7 +204,7 @@
 	util.ResponseFormat(c, code.Success, ruleList)
 }
 
-// Add
+// Update
 // @Tags      搴撳瓨鐩樼偣
 // @Summary   淇敼搴撳瓨鐩樼偣淇℃伅
 // @Produce   application/json

--
Gitblit v1.8.0