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

---
 controllers/warehouse.go |   68 +++++++++++++++++++---------------
 1 files changed, 38 insertions(+), 30 deletions(-)

diff --git a/controllers/warehouse.go b/controllers/warehouse.go
index f9b54c9..087679e 100644
--- a/controllers/warehouse.go
+++ b/controllers/warehouse.go
@@ -12,7 +12,6 @@
 	"wms/extend/code"
 	"wms/extend/util"
 	"wms/models"
-	"wms/pkg/logx"
 	"wms/pkg/structx"
 	"wms/request"
 )
@@ -42,36 +41,46 @@
 		util.ResponseFormat(c, code.RequestParamError, err.Error())
 		return
 	}
-	//鍒涘缓瑙嗗浘
-	view := &models.Location{
-		Name:      params.Code,
-		JointName: params.Code,
-		Type:      constvar.LocationTypeView,
-	}
-	id, err := models.NewLocationSearch().CreateReturnId(view)
-	if err != nil {
-		util.ResponseFormat(c, code.SaveFail, "瑙嗗浘鍒涘缓澶辫触")
-		return
-	}
-	//鍒涘缓榛樿浣嶇疆
-	location := &models.Location{
-		Name:              "榛樿浣嶇疆",
-		JointName:         params.Code + "/榛樿浣嶇疆",
-		Type:              constvar.LocationTypeInternal,
-		ReplenishLocation: true,
-		ParentId:          id,
-	}
-	locationId, err := models.NewLocationSearch().CreateReturnId(location)
-	if err != nil {
-		util.ResponseFormat(c, code.SaveFail, "浣嶇疆鍒涘缓澶辫触")
-		return
-	}
-	params.LocationId = locationId
-	err = models.WithTransaction(func(tx *gorm.DB) error {
+
+	err := models.WithTransaction(func(tx *gorm.DB) error {
 		err := models.NewWarehouseSearch().SetOrm(tx).Create(&params)
 		if err != nil {
 			return err
 		}
+
+		//鍒涘缓瑙嗗浘
+		view := &models.Location{
+			Name:        params.Code,
+			JointName:   params.Code,
+			Type:        constvar.LocationTypeView,
+			WarehouseId: params.Id,
+		}
+		viewId, err := models.NewLocationSearch().CreateReturnId(view)
+		if err != nil {
+			return err
+		}
+
+		//鍒涘缓榛樿浣嶇疆
+		location := &models.Location{
+			Name:              params.Name,
+			JointName:         params.Name,
+			Type:              constvar.LocationTypeInternal,
+			ReplenishLocation: true,
+			ParentId:          viewId,
+			WarehouseId:       params.Id,
+		}
+		locationId, err := models.NewLocationSearch().SetOrm(tx).CreateReturnId(location)
+		if err != nil {
+			return err
+		}
+
+		//鏇存敼浠撳簱璁板綍閲岀殑浣嶇疆id
+		params.LocationId = locationId
+		err = models.NewWarehouseSearch().SetOrm(tx).SetID(params.Id).UpdateByMap(map[string]interface{}{"location_id": locationId})
+		if err != nil {
+			return err
+		}
+
 		//鍒涘缓涓変釜榛樿鎿嶄綔绫诲瀷
 		var types []*models.OperationType
 		inType := &models.OperationType{
@@ -95,9 +104,8 @@
 		err = models.NewOperationTypeSearch().SetOrm(tx).CreateBatch(types)
 		return err
 	})
+
 	if err != nil {
-		_ = models.NewLocationSearch().SetID(locationId).Delete()
-		logx.Errorf("warehouse create err: %v", err)
 		util.ResponseFormat(c, code.SaveFail, "鎻掑叆澶辫触")
 		return
 	}
@@ -238,7 +246,7 @@
 	for _, warehouse := range list {
 		for _, location := range locations {
 			if warehouse.LocationId == location.Id {
-				warehouse.WarehouseLocation = warehouse.Code + "/" + location.Name
+				warehouse.WarehouseLocation = location.Name
 				break
 			}
 		}

--
Gitblit v1.8.0