From e5df488268e29b272932e6cc1d2b1e7034590ba0 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 12 六月 2024 11:40:45 +0800
Subject: [PATCH] weedfs包放到项目里

---
 controllers/location.go |  219 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 195 insertions(+), 24 deletions(-)

diff --git a/controllers/location.go b/controllers/location.go
index 128eb14..ecd21c0 100644
--- a/controllers/location.go
+++ b/controllers/location.go
@@ -3,6 +3,8 @@
 import (
 	"errors"
 	"github.com/gin-gonic/gin"
+	"strconv"
+	"strings"
 	"wms/extend/code"
 	"wms/extend/util"
 	"wms/models"
@@ -12,42 +14,211 @@
 type LocationController struct {
 }
 
-func (slf LocationController) ListLocationByType(c *gin.Context) {
-	var params request.LocationByType
+// AddLocation
+// @Tags      浣嶇疆
+// @Summary   娣诲姞浣嶇疆淇℃伅
+// @Produce   application/json
+// @Param     object  body  models.Location true  "浣嶇疆淇℃伅"
+// @Success   200 {object} util.Response "鎴愬姛"
+// @Router    /api-wms/v1/location/addLocation [post]
+func (slf LocationController) AddLocation(c *gin.Context) {
+	var params models.Location
 	if err := c.BindJSON(&params); err != nil {
-		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟缁戝畾澶辫触")
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�:"+err.Error())
 		return
 	}
-	if err := slf.CheckLocationByTypeParams(params); err != nil {
-		util.ResponseFormat(c, code.RequestParamError, err.Error())
-	}
-	search := models.NewLocationSearch()
-	search.SetType(params.Type)
-	if params.Keyword != "" {
-		search.SetKeyword(params.Keyword)
+	if err := slf.CheckLocation(params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err)
+		return
 	}
 	if params.ParentId != 0 {
-		search.SetParentId(params.ParentId)
+		//鏌ヨ涓婄骇鍚嶇О
+		first, err := models.NewLocationSearch().SetID(params.ParentId).First()
+		if err != nil {
+			util.ResponseFormat(c, code.RequestParamError, "鏌ヨ涓婄骇鍚嶇О澶辫触")
+			return
+		}
+		params.JointName = first.JointName + "/" + params.Name
+		if first.WarehouseId != 0 {
+			params.WarehouseId = first.Id
+		} else {
+			//鏍规嵁浠撳簱缂╁啓鏌ヨ浠撳簱
+			houseCode := strings.Split(first.JointName, "/")[0]
+			warehouse, err := models.NewWarehouseSearch().SetCode(houseCode).First()
+			if err != nil {
+				util.ResponseFormat(c, code.RequestParamError, err)
+				return
+			}
+			params.WarehouseId = warehouse.Id
+		}
+
+	} else {
+		params.JointName = params.Name
 	}
-	if params.CompanyId != 0 {
-		search.SetCompanyId(params.CompanyId)
-	}
-	res, err := search.SetOrder("created_at desc").FindAll()
+
+	err := models.NewLocationSearch().Create(&params)
 	if err != nil {
-		util.ResponseFormat(c, code.RequestError, err.Error())
+		util.ResponseFormat(c, code.RequestParamError, "鍒涘缓澶辫触")
+		return
 	}
-	util.ResponseFormat(c, code.Success, res)
+	util.ResponseFormat(c, code.Success, "鍒涘缓鎴愬姛")
 }
 
-func (slf LocationController) CheckLocationByTypeParams(params request.LocationByType) error {
+// GetLocationList
+// @Tags      浣嶇疆
+// @Summary   鑾峰彇浣嶇疆鍒楄〃
+// @Produce   application/json
+// @Param     object  body  request.GetLocationList true  "鏌ヨ鍙傛暟"
+// @Success   200 {object} util.ResponseList{data=[]models.Location}	"鎴愬姛"
+// @Router    /api-wms/v1/location/getLocationList [post]
+func (slf LocationController) GetLocationList(c *gin.Context) {
+	var params request.GetLocationList
+	if err := c.BindJSON(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		return
+	}
+	search := models.NewLocationSearch()
+	if params.PageInfo.Check() {
+		search.SetPage(params.Page, params.PageSize)
+	}
+	list, total, err := search.SetKeyword(params.KeyWord).SetType(params.Type).SetJointName(params.JointName).
+		SetIsScrapLocation(params.IsScrapLocation).SetOrder("created_at desc").Find()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
+		return
+	}
+
+	util.ResponseFormatList(c, code.Success, list, int(total))
+}
+
+// GetLocationTreeList
+// @Tags      浣嶇疆
+// @Summary   鑾峰彇浣嶇疆鍒楄〃鏍�
+// @Produce   application/json
+// @Success   200 {object} util.ResponseList{data=[]models.Location}	"鎴愬姛"
+// @Router    /api-wms/v1/location/getLocationTreeList [get]
+func (slf LocationController) GetLocationTreeList(c *gin.Context) {
+	all, err := models.NewLocationSearch().SetType(3).FindAll()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
+		return
+	}
+	var tree []*models.Location
+	m := make(map[int]*models.Location)
+	for _, location := range all {
+		m[location.Id] = location
+	}
+	for _, location := range all {
+		if location.ParentId == 0 {
+			tree = append(tree, location)
+		} else {
+			if _, ok := m[location.ParentId]; !ok {
+				tree = append(tree, location)
+				continue
+			}
+			if m[location.ParentId].Children == nil {
+				m[location.ParentId].Children = make([]*models.Location, 0)
+			}
+
+			m[location.ParentId].Children = append(m[location.ParentId].Children, location)
+		}
+	}
+
+	util.ResponseFormat(c, code.Success, tree)
+}
+
+// GetLocationDetails
+// @Tags      浣嶇疆
+// @Summary   鑾峰彇浣嶇疆璇︽儏
+// @Produce   application/json
+// @Param		id	path		string			true	"id"  "鏌ヨ鍙傛暟"
+// @Success   200 {object} util.Response{data=models.Location}	"鎴愬姛"
+// @Router    /api-wms/v1/location/getLocationDetails/{id} [get]
+func (slf LocationController) GetLocationDetails(c *gin.Context) {
+	id, _ := strconv.Atoi(c.Param("id"))
+	if id == 0 {
+		util.ResponseFormat(c, code.RequestParamError, "鏃犳晥id")
+		return
+	}
+	location, err := models.NewLocationSearch().SetID(id).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
+		return
+	}
+	util.ResponseFormat(c, code.Success, location)
+}
+
+// UpdateLocation
+// @Tags      浣嶇疆
+// @Summary   淇敼浣嶇疆
+// @Produce   application/json
+// @Param     object  body  models.Location true  "浜у搧淇℃伅"
+// @Success   200 {object} util.Response "鎴愬姛"
+// @Router    /api-wms/v1/location/updateLocation [post]
+func (slf LocationController) UpdateLocation(c *gin.Context) {
+	var params models.Location
+	if err := c.BindJSON(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		return
+	}
+	if err := slf.CheckLocation(params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err)
+		return
+	}
+	//index := strings.LastIndex(params.JointName, "/")
+	//if index > 0 {
+	//	jn := params.JointName[:index]
+	//	oldName := params.JointName[index+1:]
+	//	if oldName != params.Name {
+	//		params.JointName = jn + "/" + params.Name
+	//	}
+	//}
+	if params.ParentId != 0 {
+		//鏌ヨ涓婄骇鍚嶇О
+		first, err := models.NewLocationSearch().SetID(params.ParentId).First()
+		if err != nil {
+			util.ResponseFormat(c, code.RequestParamError, "鏌ヨ涓婄骇鍚嶇О澶辫触")
+			return
+		}
+		params.JointName = first.JointName + "/" + params.Name
+	} else {
+		params.JointName = strings.Split(params.JointName, "/")[0] + "/" + params.Name
+	}
+	err := models.NewLocationSearch().Update(&params)
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "浣嶇疆淇℃伅鏇存柊澶辫触")
+		return
+	}
+	util.ResponseFormat(c, code.Success, "鏇存柊鎴愬姛")
+}
+
+// DeleteLocation
+// @Tags      浣嶇疆
+// @Summary   鍒犻櫎浣嶇疆
+// @Produce   application/json
+// @Param		id	path		string			true	"id"  "鏌ヨ鍙傛暟"
+// @Success   200 {object} util.Response "鎴愬姛"
+// @Router    /api-wms/v1/location/deleteLocation/{id} [delete]
+func (slf LocationController) DeleteLocation(c *gin.Context) {
+	id, _ := strconv.Atoi(c.Param("id"))
+	if id == 0 {
+		util.ResponseFormat(c, code.RequestParamError, "鏃犳晥id")
+		return
+	}
+	err := models.NewLocationSearch().SetID(id).Delete()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍒犻櫎澶辫触")
+		return
+	}
+	util.ResponseFormat(c, code.Success, "鍒犻櫎鎴愬姛")
+}
+
+func (slf LocationController) CheckLocation(params models.Location) error {
+	if params.Name == "" {
+		return errors.New("鍚嶇О涓嶈兘涓虹┖")
+	}
 	if params.Type == 0 {
 		return errors.New("璇烽�夋嫨姝g‘鐨勪綅缃被鍨�")
-	}
-	if params.ParentId < 0 {
-		return errors.New("閿欒鍙傛暟ParentId")
-	}
-	if params.CompanyId < 0 {
-		return errors.New("閿欒鍙傛暟CompanyId")
 	}
 	return nil
 }

--
Gitblit v1.8.0