From cfb6fbce3687230ccb4704dbc0c87fd411b39af1 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期三, 20 九月 2023 17:37:22 +0800
Subject: [PATCH] 仓库字段添加与功能修改

---
 controllers/warehouse.go |  125 +++++++++++++++++++++++++++++++----------
 1 files changed, 94 insertions(+), 31 deletions(-)

diff --git a/controllers/warehouse.go b/controllers/warehouse.go
index f4a297a..5dc9857 100644
--- a/controllers/warehouse.go
+++ b/controllers/warehouse.go
@@ -6,6 +6,8 @@
 	"github.com/gin-gonic/gin"
 	"github.com/spf13/cast"
 	"gorm.io/gorm"
+	"strconv"
+	"wms/constvar"
 	"wms/extend/code"
 	"wms/extend/util"
 	"wms/models"
@@ -39,6 +41,19 @@
 		util.ResponseFormat(c, code.RequestParamError, err.Error())
 		return
 	}
+	//鍒涘缓榛樿浣嶇疆
+	location := &models.Location{
+		Name:              "榛樿浣嶇疆",
+		ParentId:          params.Code,
+		Type:              constvar.LocationTypeInternal,
+		ReplenishLocation: true,
+	}
+	locationId, err := models.NewLocationSearch().CreateReturnId(location)
+	if err != nil {
+		util.ResponseFormat(c, code.SaveFail, "浣嶇疆鍒涘缓澶辫触")
+		return
+	}
+	params.LocationId = locationId
 	if err := models.NewWarehouseSearch().Create(&params); err != nil {
 		logx.Errorf("warehouse create err: %v", err)
 		util.ResponseFormat(c, code.SaveFail, "鎻掑叆澶辫触")
@@ -48,39 +63,24 @@
 	util.ResponseFormat(c, code.Success, "娣诲姞鎴愬姛")
 }
 
-// Update
+// UpdateWarehouse
 // @Tags      浠撳簱
 // @Summary   缂栬緫浠撳簱
 // @Produce   application/json
-// @Param     object  body request.UpdateWarehouse true  "浠撳簱淇℃伅"
-// @Param     id  path string true  "浠撳簱id"
+// @Param     object  body  models.Warehouse true  "浠撳簱淇℃伅"
 // @Success   200 {object} util.Response "鎴愬姛"
-// @Router    /api-wms/v1/warehouse/warehouse/{id} [put]
-func (slf WarehouseController) Update(c *gin.Context) {
-	id := cast.ToUint(c.Param("id"))
-	if id == 0 {
-		util.ResponseFormat(c, code.RequestParamError, "绌虹殑璁板綍id")
-		return
-	}
-	var (
-		reqParams request.UpdateWarehouse
-		params    models.Warehouse
-	)
-	if err := c.BindJSON(&reqParams); err != nil {
+// @Router    /api-wms/v1/warehouse/updateWarehouse [post]
+func (slf WarehouseController) UpdateWarehouse(c *gin.Context) {
+	var params models.Warehouse
+	if err := c.BindJSON(&params); err != nil {
 		util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("鍙傛暟瑙f瀽澶辫触: %v"+err.Error()))
 		return
 	}
-	if err := structx.AssignTo(reqParams, &params); err != nil {
-		util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("鏁版嵁杞崲閿欒: %v", err.Error()))
-		return
-	}
-	params.ID = id
 	if err := slf.ParamsCheck(params); err != nil {
 		util.ResponseFormat(c, code.RequestParamError, err.Error())
 		return
 	}
-
-	err := models.NewWarehouseSearch().SetID(params.ID).Update(&params)
+	err := models.NewWarehouseSearch().SetID(params.Id).Update(&params)
 
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "淇敼澶辫触")
@@ -92,19 +92,31 @@
 
 func (slf WarehouseController) ParamsCheck(params models.Warehouse) (err error) {
 	var oldRecord *models.Warehouse
-	if params.ID != 0 {
-		oldRecord, err = models.NewWarehouseSearch().SetID(params.ID).First()
+	if params.Id != 0 {
+		oldRecord, err = models.NewWarehouseSearch().SetID(params.Id).First()
 		if err == gorm.ErrRecordNotFound {
 			return errors.New("璁板綍涓嶅瓨鍦�")
 		}
 	}
-	if oldRecord == nil || params.Code != oldRecord.Code {
-		_, err = models.NewWarehouseSearch().SetCode(params.Code).First()
-		if err != gorm.ErrRecordNotFound {
+	//鏇存柊浣嶇疆淇℃伅
+	if oldRecord != nil && params.Code != oldRecord.Code {
+		m := make(map[string]interface{})
+		m["parent_id"] = params.Code
+		err := models.NewLocationSearch().SetID(oldRecord.LocationId).UpdateByMap(m)
+		if err != nil {
+			return errors.New("鏇存柊浣嶇疆淇℃伅澶辫触")
+		}
+	}
+	if oldRecord == nil {
+		record, err := models.NewWarehouseSearch().SetCode(params.Code).First()
+		if record != nil && record.Code == params.Code {
+			fmt.Println(err)
 			return errors.New("浠撳簱缂栧彿閲嶅")
 		}
 	}
-
+	if params.Code == "" {
+		return errors.New("缂╁啓涓嶈兘涓虹┖")
+	}
 	return nil
 }
 
@@ -126,6 +138,24 @@
 		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
 		return
 	}
+	//鑾峰彇浣嶇疆淇℃伅
+	codes := make([]string, 0)
+	for _, warehouse := range list {
+		codes = append(codes, warehouse.Code)
+	}
+	locations, err := models.NewLocationSearch().SetParents(codes).FindNotTotal()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "浣嶇疆淇℃伅鏌ユ壘澶辫触")
+		return
+	}
+	for _, warehouse := range list {
+		for _, location := range locations {
+			if warehouse.LocationId == location.Id {
+				warehouse.WarehouseLocation = warehouse.Code + "/" + location.Name
+				break
+			}
+		}
+	}
 
 	util.ResponseFormatList(c, code.Success, list, cast.ToInt(total))
 }
@@ -138,16 +168,49 @@
 // @Success   200 {object} util.Response "鎴愬姛"
 // @Router    /api-wms/v1/warehouse/warehouse/{id} [delete]
 func (slf WarehouseController) Delete(c *gin.Context) {
-	id := cast.ToUint(c.Param("id"))
+	id, _ := strconv.Atoi(c.Param("id"))
 	if id == 0 {
 		util.ResponseFormat(c, code.RequestParamError, "绌虹殑璁板綍id")
 		return
 	}
 
-	err := models.NewWarehouseSearch().SetID(id).Delete()
+	//鍒犻櫎浣嶇疆淇℃伅
+	first, err := models.NewWarehouseSearch().SetID(id).First()
 	if err != nil {
-		util.ResponseFormat(c, code.RequestParamError, "鍒犻櫎澶辫触")
+		util.ResponseFormat(c, code.RequestParamError, "鑾峰彇浠撳簱淇℃伅澶辫触")
+		return
+	}
+	err = models.NewLocationSearch().SetID(first.LocationId).Delete()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍒犻櫎浣嶇疆淇℃伅澶辫触")
+		return
+	}
+	err = models.NewWarehouseSearch().SetID(id).Delete()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍒犻櫎浠撳簱淇℃伅澶辫触")
 		return
 	}
 	util.ResponseFormat(c, code.UpdateSuccess, "鍒犻櫎鎴愬姛")
 }
+
+// GetWarehouseDetails
+// @Tags      浠撳簱
+// @Summary   鑾峰彇浠撳簱璇︽儏
+// @Produce   application/json
+// @Param     id  path string true  "浠撳簱id"
+// @Success   200 {object} util.Response{data=models.Warehouse}	"鎴愬姛"
+// @Router    /api-wms/v1/warehouse/getWarehouseDetails/{id} [get]
+func (slf WarehouseController) GetWarehouseDetails(c *gin.Context) {
+	id, _ := strconv.Atoi(c.Param("id"))
+	if id == 0 {
+		util.ResponseFormat(c, code.RequestParamError, "绌虹殑璁板綍id")
+		return
+	}
+
+	first, err := models.NewWarehouseSearch().SetID(id).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鑾峰彇浠撳簱淇℃伅澶辫触")
+		return
+	}
+	util.ResponseFormat(c, code.UpdateSuccess, first)
+}

--
Gitblit v1.8.0