From a8914a16b23e93f6bfd12bcfd5cbe8b24cf7eb84 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期日, 07 四月 2024 21:49:01 +0800
Subject: [PATCH] 发货支持仓库传参

---
 controllers/location_product.go |  107 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 82 insertions(+), 25 deletions(-)

diff --git a/controllers/location_product.go b/controllers/location_product.go
index ca86491..6089724 100644
--- a/controllers/location_product.go
+++ b/controllers/location_product.go
@@ -1,10 +1,11 @@
 package controllers
 
 import (
+	"errors"
 	"github.com/gin-gonic/gin"
-	"github.com/shopspring/decimal"
+	"gorm.io/gorm"
 	"strconv"
-	"time"
+	"wms/constvar"
 	"wms/extend/code"
 	"wms/extend/util"
 	"wms/models"
@@ -42,26 +43,45 @@
 		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浣嶇疆")
 		return
 	}
-	if params.ProductId == "" {
-		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧")
+
+	if params.RuleType != constvar.RuleType_Product && params.RuleType != constvar.RuleType_ProductCategory {
+		util.ResponseFormat(c, code.RequestParamError, "ruleType寮傚父")
 		return
 	}
-	//if params.ProductCategoryID == 0 {
-	//	util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧绫诲埆")
-	//	return
-	//}
+
+	if params.RuleType == constvar.RuleType_Product {
+		if params.ProductId == "" {
+			util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧")
+			return
+		}
+		if _, err := models.NewLocationProductSearch().SetProductId(params.ProductId).SetLocationId(params.LocationId).SetAreaId(params.AreaId).First(); err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+		} else {
+			util.ResponseFormat(c, code.RequestParamError, "宸插瓨鍦ㄨ涓婂瑙勫垯锛岃鍕块噸澶嶆坊鍔�")
+			return
+		}
+	}
+
+	if params.RuleType == constvar.RuleType_ProductCategory {
+		if params.ProductCategoryID == 0 {
+			util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧绫诲埆")
+			return
+		}
+		if _, err := models.NewLocationProductSearch().SetProductCategoryId(params.ProductCategoryID).SetLocationId(params.LocationId).SetAreaId(params.AreaId).First(); err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+		} else {
+			util.ResponseFormat(c, code.RequestParamError, "宸插瓨鍦ㄨ涓婂瑙勫垯锛岃鍕块噸澶嶆坊鍔�")
+			return
+		}
+	}
 
 	if err := models.NewLocationProductSearch().Create(&params); err != nil {
-		logx.Errorf("Operation create err: %v", err)
-		util.ResponseFormat(c, code.SaveFail, "娣诲姞澶辫触锛�"+err.Error())
-		return
-	}
-	locationProductAmount := &models.LocationProductAmount{
-		LocationProductId: params.Id,
-		Amount:            decimal.NewFromFloat(0),
-		CreateDate:        time.Now().Format("2006-01-02 15:04:05"),
-	}
-	if err := models.NewLocationProductAmountSearch().Create(locationProductAmount); err != nil {
 		logx.Errorf("Operation create err: %v", err)
 		util.ResponseFormat(c, code.SaveFail, "娣诲姞澶辫触锛�"+err.Error())
 		return
@@ -77,7 +97,7 @@
 // @Success   200 {object} util.Response	"鎴愬姛"
 // @Router    /api-wms/v1/locationProduct/list [post]
 func (slf LocationProductController) List(c *gin.Context) {
-	var params request.PageInfo
+	var params request.QueryLocationProductConditon
 	if err := c.BindJSON(&params); err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�:"+err.Error())
 		return
@@ -89,6 +109,14 @@
 
 	search := models.NewLocationProductSearch()
 	search.SetPage(params.Page, params.PageSize)
+
+	if params.ProductId != "" {
+		search = search.SetProductId(params.ProductId)
+	}
+
+	if params.ProductCategoryId != 0 {
+		search = search.SetProductCategoryId(params.ProductCategoryId)
+	}
 	list, total, err := search.SetPreload(true).SetOrder("created_at desc").FindByPage()
 	if err != nil {
 		util.ResponseFormat(c, code.RequestError, "鏌ユ壘澶辫触:"+err.Error())
@@ -128,14 +156,44 @@
 		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浣嶇疆")
 		return
 	}
-	if params.ProductId == "" {
-		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧")
+
+	if params.RuleType != constvar.RuleType_Product && params.RuleType != constvar.RuleType_ProductCategory {
+		util.ResponseFormat(c, code.RequestParamError, "ruleType寮傚父")
 		return
 	}
-	if params.ProductCategoryID == 0 {
-		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧绫诲埆")
-		return
+
+	if params.RuleType == constvar.RuleType_Product {
+		if params.ProductId == "" {
+			util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧")
+			return
+		}
+		if rule, err := models.NewLocationProductSearch().SetProductId(params.ProductId).SetLocationId(params.LocationId).SetAreaId(params.AreaId).First(); err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+		} else if params.Id != rule.Id {
+			util.ResponseFormat(c, code.RequestParamError, "宸插瓨鍦ㄨ涓婂瑙勫垯锛岃鍕块噸澶嶆坊鍔�")
+			return
+		}
 	}
+
+	if params.RuleType == constvar.RuleType_ProductCategory {
+		if params.ProductCategoryID == 0 {
+			util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧绫诲埆")
+			return
+		}
+		if rule, err := models.NewLocationProductSearch().SetProductCategoryId(params.ProductCategoryID).SetLocationId(params.LocationId).SetAreaId(params.AreaId).First(); err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+		} else if params.Id != rule.Id {
+			util.ResponseFormat(c, code.RequestParamError, "宸插瓨鍦ㄨ涓婂瑙勫垯锛岃鍕块噸澶嶆坊鍔�")
+			return
+		}
+	}
+
 	if err := models.NewLocationProductSearch().SetID(params.Id).Update(&params); err != nil {
 		logx.Errorf("LocationProduct update err: %v", err)
 		util.ResponseFormat(c, code.SaveFail, "淇敼澶辫触锛�"+err.Error())
@@ -163,7 +221,6 @@
 		util.ResponseFormat(c, code.RequestParamError, "id涓�0")
 		return
 	}
-	//TODO:姝ゅ鍙兘闇�瑕佸鍔犻檺鍒讹紝濡傛灉璇ヤ笂鏋惰鍒欏鏋滃凡缁忎骇鐢熶簡搴撳瓨鏁伴噺锛屽垹闄や細閫犳垚搴撳瓨鏌ヤ笉鍒扮殑褰卞搷
 	if err := models.NewLocationProductSearch().SetID(id).Delete(); err != nil {
 		logx.Errorf("LocationProduct delete err: %v", err)
 		util.ResponseFormat(c, code.SaveFail, "鍒犻櫎澶辫触锛�"+err.Error())

--
Gitblit v1.8.0