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 |  177 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 152 insertions(+), 25 deletions(-)

diff --git a/controllers/location_product_amount.go b/controllers/location_product_amount.go
index 5e0b383..8d44349 100644
--- a/controllers/location_product_amount.go
+++ b/controllers/location_product_amount.go
@@ -12,6 +12,7 @@
 	"wms/extend/util"
 	"wms/models"
 	"wms/request"
+	"wms/response"
 )
 
 type LocationProductAmountController struct {
@@ -41,6 +42,7 @@
 	search.Orm = search.Orm.
 		Table("wms_location_product_amount").
 		Select(`
+					wms_location_product_amount.id as location_product_amount_id,
 					wms_location.id as location_id,
 					wms_location.name as location_name,
 					material.id as product_id,
@@ -48,14 +50,14 @@
 					wms_location_product_amount.amount,
 					material.unit,
 					wms_location_product_amount.create_date,
-					wms_operation_details.amount as adjust_amount`).
+					wms_operation_details.amount as adjust_amount,
+					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").
-		InnerJoins("inner join wms_operation_details on wms_operation_details.product_id=wms_location_product.product_id").
-		InnerJoins("inner join material on material.id=wms_operation_details.product_id").
-		InnerJoins("inner join wms_operation on wms_operation.id=wms_operation_details.operation_id").
-		Where("wms_operation.base_operation_type=?", constvar.BaseOperationTypeAdjust)
-
+		Joins("left join wms_operation_details on wms_operation_details.product_id=wms_location_product.product_id").
+		Joins("left join wms_operation on wms_operation.id=wms_operation_details.operation_id and wms_operation.base_operation_type=? and wms_operation.status=?", constvar.BaseOperationTypeAdjust, constvar.OperationStatus_Ready)
 	var (
 		records = make([]*models.LocationProductAmountWithOperation, 0)
 		total   int64
@@ -69,12 +71,14 @@
 	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
 	}
 	for k, v := range records {
-		records[k].DifferenceAmount = v.Amount.Sub(v.AdjustAmount)
+		if v.OperationId != 0 {
+			records[k].DifferenceAmount = v.Amount.Add(v.AdjustAmount)
+		}
 	}
 	util.ResponseFormatListWithPage(c, code.Success, records, int(total), params.Page, params.PageSize)
 }
@@ -120,17 +124,27 @@
 		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())
 		return
 	}
-	reqParams.AdjustAmount = reqParams.Amount.Sub(reqParams.DifferenceAmount)
-	//reqParams.AdjustAmount = reqParams.DifferenceAmount.Sub(reqParams.Amount)
-	locAmount := models.LocationProductAmount{
-		LocationProductId: locProduct.Id,
-		Amount:            decimal.NewFromInt(0),
+	//reqParams.AdjustAmount = reqParams.Amount.Sub(reqParams.DifferenceAmount)
+	reqParams.AdjustAmount = reqParams.DifferenceAmount.Sub(reqParams.Amount)
+	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,
@@ -142,24 +156,18 @@
 		Comment:           "搴撳瓨鐩樼偣",
 		BaseOperationType: constvar.BaseOperationTypeAdjust,
 		Details:           []*models.OperationDetails{detail},
-		FromLocationID:    reqParams.LocationId,
-		ToLocationID:      location.Id,
+		FromLocationID:    location.Id,
+		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 {
@@ -169,3 +177,122 @@
 
 	util.ResponseFormat(c, code.Success, "娣诲姞鎴愬姛")
 }
+
+// GetRuleList
+// @Tags      搴撳瓨鐩樼偣
+// @Summary   鑾峰彇涓婃灦瑙勫垯
+// @Produce   application/json
+// @Param     object  body  request.GetRuleList true  "鏌ヨ淇℃伅"
+// @Success   200 {object} util.ResponseList{data=[]response.RuleList}	"鎴愬姛"
+// @Router    /api-wms/v1/locationProductAmount/getRuleList [post]
+func (slf LocationProductAmountController) GetRuleList(c *gin.Context) {
+	var params request.GetRuleList
+	if err := c.BindJSON(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		return
+	}
+	list, err := models.NewLocationProductSearch().SetProductId(params.ProductId).SetLocationId(params.LocationId).Find()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
+	}
+	var ruleList response.RuleList
+	for _, l := range list {
+		ruleList.LocationList = append(ruleList.LocationList, l.Location)
+		ruleList.ProductList = append(ruleList.ProductList, l.Product)
+	}
+	util.ResponseFormat(c, code.Success, ruleList)
+}
+
+// Update
+// @Tags      搴撳瓨鐩樼偣
+// @Summary   淇敼搴撳瓨鐩樼偣淇℃伅
+// @Produce   application/json
+// @Param     object  body  request.UpdateLocationProductAmount true  "鍏ュ簱/鍑哄簱淇℃伅"
+// @Success   200 {object} util.Response "鎴愬姛"
+// @Router    /api-wms/v1/locationProductAmount/update [post]
+func (slf LocationProductAmountController) Update(c *gin.Context) {
+	var reqParams request.UpdateLocationProductAmount
+	if err := c.BindJSON(&reqParams); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		return
+	}
+	reqParams.AdjustAmount = reqParams.DifferenceAmount.Sub(reqParams.Amount)
+	operation, err := models.NewOperationSearch().SetID(reqParams.OperationId).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏈壘鍒扮浉鍏虫暟鎹�")
+		return
+	}
+	if operation.Status != constvar.OperationStatus_Ready {
+		util.ResponseFormat(c, code.RequestParamError, "璇ヨ褰曞凡楠岃瘉閫氳繃锛屾棤娉曡繘琛屼慨鏀�")
+		return
+	}
+
+	if err := models.NewOperationDetailsSearch().SetOperationId(reqParams.OperationId).SetProductId(reqParams.ProductId).Update(&models.OperationDetails{Amount: reqParams.AdjustAmount}); err != nil {
+		util.ResponseFormat(c, code.RequestError, "淇敼澶辫触锛�"+err.Error())
+		return
+	}
+
+	util.ResponseFormat(c, code.Success, "淇敼鎴愬姛")
+}
+
+// finish
+//
+//	@Tags		搴撳瓨鐩樼偣
+//	@Summary	搴旂敤銆侀獙璇�
+//	@Produce	application/json
+//	@Param		object  body  request.FinishLocationProductAmount true  "鍏ュ弬"
+//	@Success	200	{object}	util.Response	"鎴愬姛"
+//	@Router		/api-wms/v1/locationProductAmount/finish [post]
+func (slf LocationProductAmountController) Finish(c *gin.Context) {
+	var reqParams request.FinishLocationProductAmount
+	if err := c.BindJSON(&reqParams); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		return
+	}
+	if reqParams.LocationProductAmountId == 0 {
+		util.ResponseFormat(c, code.RequestParamError, "locationProductAmountId涓�0")
+		return
+	}
+	if reqParams.OperationId == 0 {
+		util.ResponseFormat(c, code.RequestParamError, "operationId涓�0")
+		return
+	}
+	operationSearch := models.NewOperationSearch().SetPreload(true)
+	operationSearch.SetOrm(operationSearch.Orm.InnerJoins("inner join wms_operation_details on wms_operation_details.operation_id=wms_operation.id").Where("wms_operation_details.product_id=? and wms_operation.id=?", reqParams.ProductId, reqParams.OperationId))
+	operation, err := operationSearch.First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏈壘鍒扮浉鍏充俊鎭�:"+err.Error())
+		return
+	}
+	if operation.Status != constvar.OperationStatus_Ready {
+		util.ResponseFormat(c, code.RequestError, "璇ラ獙璇佹棤娉曞畬鎴�")
+		return
+	}
+	material, err := models.NewMaterialSearch().SetID(reqParams.ProductId).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestError, err.Error())
+		return
+	}
+	locProdtAmount, err := models.NewLocationProductAmountSearch().SetID(reqParams.LocationProductAmountId).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestError, err.Error())
+		return
+	}
+	if err := models.WithTransaction(func(tx *gorm.DB) error {
+		if err := models.NewOperationSearch().SetOrm(tx).SetID(reqParams.OperationId).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil {
+			return err
+		}
+		if err := models.NewMaterialSearch().SetOrm(tx).SetID(reqParams.ProductId).Update(&models.Material{Amount: material.Amount.Add(operation.Details[0].Amount)}); err != nil {
+			return err
+		}
+		if err := models.NewLocationProductAmountSearch().SetOrm(tx).SetID(reqParams.LocationProductAmountId).Update(&models.LocationProductAmount{Amount: locProdtAmount.Amount.Add(operation.Details[0].Amount)}); err != nil {
+			return err
+		}
+		return nil
+	}); err != nil {
+		util.ResponseFormat(c, code.RequestError, err.Error())
+		return
+	}
+	util.ResponseFormat(c, code.Success, "鎿嶄綔鎴愬姛")
+}

--
Gitblit v1.8.0