From 0c65eeb1f1ac4b3dd14727f41a25582fc3ee701e Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期四, 21 九月 2023 09:52:21 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/aps/WMS

---
 controllers/operation.go |   64 ++++++++++++++++++++++---------
 1 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index 06b3c19..97ba0fd 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -2,7 +2,9 @@
 
 import (
 	"errors"
+	"fmt"
 	"github.com/gin-gonic/gin"
+	"github.com/shopspring/decimal"
 	"github.com/spf13/cast"
 	"gorm.io/gorm"
 	"strconv"
@@ -110,7 +112,7 @@
 	//妫�鏌ユ槑缁嗛儴鍒�
 	for _, v := range params.Details {
 		if v.ProductId == "" {
-			return errors.New("productID涓�0")
+			return errors.New("productID涓虹┖")
 		}
 		if v.ProductName == "" {
 			return errors.New("浜у搧鍚嶇О寮傚父")
@@ -170,7 +172,7 @@
 // @Param     object  body request.UpdateOperation true  "鍏ュ簱淇℃伅"
 // @Param     id  path int true  "鍏ュ簱淇℃伅id"
 // @Success   200 {object} util.Response "鎴愬姛"
-// @Router    /api-wms/v1/operation/operation/{id} [put]
+// @Router    /api-wms/v1/operation/operation/{id} [post]
 func (slf OperationController) Update(c *gin.Context) {
 	id := cast.ToUint(c.Param("id"))
 	if id == 0 {
@@ -199,6 +201,11 @@
 		util.ResponseFormat(c, code.RequestParamError, err.Error())
 		return
 	}
+	fmt.Printf("%+v\n", *reqParams.Details[0])
+	fmt.Printf("%+v\n", *reqParams.Details[1])
+	fmt.Println("===============================================")
+	fmt.Printf("%+v\n", *params.Details[0])
+	fmt.Printf("%+v\n", *params.Details[1])
 	if err := models.WithTransaction(func(tx *gorm.DB) error {
 		if err := models.NewOperationDetailsSearch().SetOrm(tx).SetOperationId(params.Id).Delete(); err != nil {
 			return err
@@ -284,28 +291,47 @@
 		util.ResponseFormat(c, code.RequestError, "璇ュ嚭鍏ュ簱淇℃伅鏃犳硶瀹屾垚")
 		return
 	}
-	//operationType, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First()
-	//if err != nil {
-	//	util.ResponseFormat(c, code.RequestError, err.Error())
-	//	return
-	//}
-	//if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
-	//	if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeVendor)).First(); err != nil {
-	//		return err
-	//	} else {
-	//		params.FromLocationId = location.Id
-	//	}
-	//	if params.ToLocationId == 0 {
-	//		return errors.New("璇烽�夋嫨鐩爣浣嶇疆")
-	//	}
-	//}
+	operationType, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).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(id).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil {
 			return err
 		}
-		if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount + wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil {
-			return err
+		if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
+			if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount + wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil {
+				return err
+			}
+		} else if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing {
+			var listProdtId []string
+			var listProdt []*models.Material
+			mapProdt := make(map[string]decimal.Decimal)
+			listDetails, err := models.NewOperationDetailsSearch().SetOperationId(operation.Id).FindAll()
+			if err != nil {
+				return err
+			}
+			for _, v := range listDetails {
+				listProdtId = append(listProdtId, v.ProductId)
+				mapProdt[v.ProductId] = v.Quantity
+			}
+			if err := models.NewMaterialSearch().Orm.Where("id IN ?", listProdtId).Find(&listProdt).Error; err != nil {
+				return err
+			}
+			for _, v := range listProdt {
+				if value, ok := mapProdt[v.ID]; !ok {
+					return errors.New("浜у搧绉嶇被寮傚父")
+				} else {
+					if v.Amount.LessThan(value) {
+						return errors.New(fmt.Sprintf("浜у搧锛�%v,搴撳瓨锛�%v,鍑哄簱锛�%v,鏁伴噺涓嶅锛屾棤娉曞畬鎴愬嚭搴撴搷浣�", v.Name, v.Amount.String(), value.String()))
+					}
+				}
+			}
+			if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount - wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil {
+				return err
+			}
 		}
 		return nil
 	}); err != nil {

--
Gitblit v1.8.0