From 79aef24a785581d95343ccd99e9cb362b2bc9c97 Mon Sep 17 00:00:00 2001
From: yinbentan <yinbentan@live.com>
Date: 星期六, 06 七月 2024 21:31:59 +0800
Subject: [PATCH] 调整库存商品位置错误

---
 controllers/operation.go |  140 ++++++++++++++++++++++++----------------------
 1 files changed, 74 insertions(+), 66 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index fb1285d..2f10d27 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -9,8 +9,6 @@
 	"github.com/shopspring/decimal"
 	"github.com/xuri/excelize/v2"
 	"gorm.io/gorm"
-	"io/ioutil"
-	"sort"
 	"strconv"
 	"strings"
 	"time"
@@ -95,7 +93,7 @@
 		params.WarehouseId = operationType.WarehouseId
 	}
 
-	if params.InventoryDealerType == 0 {
+	if params.DealerType == "" {
 		CheckInventoryDealerType(&params)
 	}
 
@@ -150,56 +148,50 @@
 
 func CheckInventoryDealerType(params *models.Operation) {
 	var dictType constvar.MiniDictType
-	dictName := "鍏跺畠"
+	dictName := "鍏朵粬"
 	switch params.BaseOperationType {
 	case constvar.BaseOperationTypeIncoming:
 		dictType = constvar.StorageType
-		//dictName = "鍏跺畠鍏ュ簱"
+		//dictName = dictName + "鍏ュ簱"
 	case constvar.BaseOperationTypeOutgoing:
 		dictType = constvar.StockoutType
-		//dictName = "鍏跺畠鍑哄簱"
+		//dictName = dictName + "鍑哄簱"
 	case constvar.BaseOperationTypeInternal:
 		dictType = constvar.TransferType
-		//dictName = "鍏跺畠璋冩嫧"
+		//dictName = dictName + "璋冩嫧"
 	case constvar.BaseOperationTypeDisuse:
 		dictType = constvar.DisuseType
-		//dictName = "鍏跺畠鎶ュ簾"
+		//dictName = dictName + "鎶ュ簾"
 	case constvar.BaseOperationTypeAdjust:
 		dictType = constvar.TakeStockType
-		//dictName = "鍏跺畠鐩樼偣"
+		//dictName = dictName + "鐩樼偣"
 	default:
 		return
 	}
-	miniDict, _ := models.NewMiniDictSearch().SetType(dictType).SetName(dictName).First()
-	if miniDict != nil {
-		params.InventoryDealerType = int(miniDict.ID)
-	} else {
-		dict := &models.MiniDict{
+	miniDict, err := models.NewMiniDictSearch().SetType(dictType).SetName(dictName).First()
+	if err == gorm.ErrRecordNotFound {
+		miniDict = &models.MiniDict{
 			Type: dictType,
 			Name: dictName,
 		}
-		models.NewMiniDictSearch().Create(dict)
-		miniDict, err := models.NewMiniDictSearch().SetType(dictType).SetName(dictName).First()
-		if err != nil {
-			return
-		}
-		params.InventoryDealerType = int(miniDict.ID)
+		models.NewMiniDictSearch().Create(miniDict)
 	}
+	params.DealerType = miniDict.Name
 }
 
+// CheckDetailsRepeat 鍑哄簱銆佸叆搴撱�佺洏鐐规秹鍙婁骇鍝侀噸澶嶆�ф鏌ワ紙浠ヤ骇鍝佺紪鍙枫�佸嚭搴撳簱鎴跨紪鍙枫�佸叆搴撳簱鎴跨紪鍙凤級
+// 瀛樺湪閲嶅鍊� true锛� 涓嶅瓨鍦ㄨ繑鍥� false
 func CheckDetailsRepeat(details []*models.OperationDetails) bool {
-	var detailIDs []string
-	var tempID string
-	for _, v := range details {
-		detailIDs = append(detailIDs, v.ProductId)
+	if len(details) == 0 {
+		return false
 	}
-	sort.Strings(detailIDs)
-	for _, v := range detailIDs {
-		if v != tempID {
-			tempID = v
-		} else {
-			return true
-		}
+	var detailKeyMap = make(map[string]bool)
+	for _, v := range details {
+		key := fmt.Sprintf("%v%v%v", v.ProductId, v.FromLocationID, v.ToLocationID)
+		detailKeyMap[key] = true
+	}
+	if len(detailKeyMap) != len(details) { // 闀垮害鐩稿悓锛屼笉瀛樺湪閲嶅鍊硷紱闀垮害涓嶅悓瀛樺湪瀛樺湪涓嶅悓
+		return true
 	}
 	return false
 }
@@ -260,12 +252,8 @@
 
 	if params.OperationTypeId == 0 && int(params.BaseOperationType) == 0 {
 		return errors.New("鏈瘑鍒璁板綍绫诲瀷1")
-	}
-
-	if params.BaseOperationType != 0 {
-		if params.WarehouseId == 0 {
-			return errors.New("鏈瘑鍒璁板綍绫诲瀷2")
-		}
+	} else if params.OperationTypeId == 0 && params.BaseOperationType != 0 && params.WarehouseId == 0 {
+		return errors.New("鏈瘑鍒璁板綍绫诲瀷2")
 	}
 
 	if params.OperationDate == "" {
@@ -298,12 +286,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")
@@ -317,6 +304,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)
 }
 
@@ -363,7 +370,7 @@
 		util.ResponseFormat(c, code.RequestError, "鏌ユ壘澶辫触:"+err.Error())
 		return
 	}
-	//userList, err := service.GetUserList()
+	//userList, err := user.GetUserList()
 	//userMap := make(map[string]string)
 	//if err == nil {
 	//	for _, user := range userList {
@@ -371,12 +378,16 @@
 	//	}
 	//}
 	userInfo := middleware.GetUserInfo(c)
+	if userInfo == nil {
+		util.ResponseFormat(c, code.SetStatusError, "鐧诲綍鐢ㄦ埛淇℃伅涓虹┖锛岃鐧诲綍鍚庨噸璇曪紒")
+		return
+	}
 	for _, v := range list {
+		v.CreatedBy = userInfo.NickName
 		//if err == nil {
 		if userInfo != nil {
 			//	v.CreatedBy = userMap[v.CreatedBy]
 			//	v.CheckedBy = userMap[v.CheckedBy]
-			v.CreatedBy = userInfo.NickName
 			v.CheckedBy = userInfo.NickName
 		}
 		for _, v1 := range v.Details {
@@ -847,22 +858,19 @@
 				return
 			}
 
-			if warehouse.FileTemplateCategoryIn == constvar.FileTemplateCategory_JialianInput1 {
-				fileUrl, err = JialianOperation2(warehouse.FileTemplateCategoryIn, operation)
-				if err != nil {
-					util.ResponseFormat(c, code.RequestParamError, err.Error())
-					return
-				}
-
-			} else if warehouse.FileTemplateCategoryIn == constvar.FileWarehouseCategory_JialianInput2 {
+			if warehouse.FileTemplateCategoryIn == constvar.FileWarehouseCategory_JialianInput2 {
 				fileUrl, err = JialianOperation1(warehouse.FileTemplateCategoryIn, operation)
 				if err != nil {
 					util.ResponseFormat(c, code.RequestParamError, err.Error())
 					return
 				}
-
+			} else {
+				fileUrl, err = JialianOperation2(constvar.FileTemplateCategory_JialianInput1, operation)
+				if err != nil {
+					util.ResponseFormat(c, code.RequestParamError, err.Error())
+					return
+				}
 			}
-
 		} else if companyName == "geruimi" {
 			fileUrl, err = ExportInputSelfmade(constvar.FileTemplateCategory_Selfmade, operation)
 			if err != nil {
@@ -887,15 +895,15 @@
 				return
 			}
 
-			if warehouse.FileTemplateCategoryOut == constvar.FileTemplateCategory_JialianOutput1 {
-				fileUrl, err = JialianOperation2(warehouse.FileTemplateCategoryOut, operation)
+			if warehouse.FileTemplateCategoryOut == constvar.FileWarehouseCategory_JialianOutput2 {
+				fileUrl, err = JialianOperation1(warehouse.FileTemplateCategoryOut, operation)
 				if err != nil {
 					util.ResponseFormat(c, code.RequestParamError, err.Error())
 					return
 				}
 
-			} else if warehouse.FileTemplateCategoryOut == constvar.FileWarehouseCategory_JialianOutput2 {
-				fileUrl, err = JialianOperation1(warehouse.FileTemplateCategoryOut, operation)
+			} else {
+				fileUrl, err = JialianOperation2(constvar.FileTemplateCategory_JialianOutput1, operation)
 				if err != nil {
 					util.ResponseFormat(c, code.RequestParamError, err.Error())
 					return
@@ -935,8 +943,6 @@
 
 	// 璇诲彇妯℃澘鍐呭
 	f, err := excelize.OpenReader(readerCloser)
-	//fl, err := os.Open("E:\\瀹剁汉鍑哄叆搴撳崟.xlsx")
-	//f, err := excelize.OpenReader(fl)
 	if err != nil {
 		return "", errors.New("璇诲彇excel妯$増澶辫触锛�" + err.Error())
 	}
@@ -988,11 +994,13 @@
 	totalAmount := decimal.NewFromInt(0) // 鎬婚噾棰�
 	totalPrice := decimal.NewFromInt(0)  // 鎬讳环
 	rowIndex := 6
+	str := ""
 	for i, v := range operation.Details {
 		//璁剧疆琛ㄥ崟鏈�澶�6鏉℃暟鎹�
 		if i > 6 {
 			break
 		}
+		str += v.Remark
 		f.SetCellValue(sheet, "B"+strconv.Itoa(rowIndex), v.Product.Name)                                                          // 浜у搧鍚嶇О
 		f.SetCellValue(sheet, "C"+strconv.Itoa(rowIndex), v.Product.Specs)                                                         //瑙勬牸
 		f.SetCellValue(sheet, "D"+strconv.Itoa(rowIndex), attributeValueMap[fmt.Sprintf("%d%s", attributeMap["棰滆壊"], v.ProductId)]) //棰滆壊
@@ -1052,7 +1060,8 @@
 	}
 
 	// 澶囨敞 绗�13琛�
-	f.SetCellValue(sheet, "B13", "澶囨敞锛�"+operation.Remark)
+	//f.SetCellValue(sheet, "B13", "澶囨敞锛�"+operation.Comment)
+	f.SetCellValue(sheet, "B13", "澶囨敞锛�"+str)
 	// 绗�14琛�
 	f.SetCellValue(sheet, "C14", operation.Manager)    // 瀹℃牳
 	f.SetCellValue(sheet, "F14", operation.Accountant) // 淇濈
@@ -1063,9 +1072,7 @@
 	}
 
 	fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), uuid.NewV4().String()+".xlsx", buf.Bytes())
-	ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644)
 	if err != nil {
-		//ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644)
 		logx.Errorf("file upload err: %v", err)
 		return "", err
 	}
@@ -1102,11 +1109,13 @@
 	totalAmount := decimal.NewFromInt(0) // 鎬婚噾棰�
 	totalPrice := decimal.NewFromInt(0)  // 鎬讳环
 	rowIndex := 5
+	str := ""
 	for i, v := range operation.Details {
 		//璁剧疆琛ㄥ崟鏈�澶�9鏉℃暟鎹�
 		if i > 9 {
 			break
 		}
+		str += v.Remark
 		f.SetCellValue(sheet, "A"+strconv.Itoa(rowIndex), v.Product.Name)    // 鍝佸悕
 		f.SetCellValue(sheet, "B"+strconv.Itoa(rowIndex), v.Product.Type)    // 鍨嬪彿
 		f.SetCellValue(sheet, "C"+strconv.Itoa(rowIndex), v.Product.Unit)    // 鍗曚綅
@@ -1170,7 +1179,8 @@
 		}
 	}
 
-	f.SetCellValue(sheet, "Q5", operation.Remark) // 澶囨敞
+	//f.SetCellValue(sheet, "Q5", operation.Comment) // 澶囨敞
+	f.SetCellValue(sheet, "Q5", str) // 澶囨敞
 
 	// 绗�14琛�
 	f.SetCellValue(sheet, "B15", operation.Manager)    // 涓荤
@@ -1183,7 +1193,6 @@
 	}
 
 	fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), uuid.NewV4().String()+".xlsx", buf.Bytes())
-	ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644)
 	if err != nil {
 		logx.Errorf("file upload err: %v", err)
 		return "", err
@@ -1267,7 +1276,6 @@
 		logx.Errorf("file upload err: %v", err)
 		return "", err
 	}
-
 	return fileUrl, nil
 }
 

--
Gitblit v1.8.0