From 4a9bc4b7c84985047c7ebe0b991e8c8364bb56a2 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期五, 03 十一月 2023 10:58:25 +0800
Subject: [PATCH] 重订货规则修改

---
 controllers/location_product.go |   97 ++++++++++++++++++++++++++++++++++++------------
 1 files changed, 73 insertions(+), 24 deletions(-)

diff --git a/controllers/location_product.go b/controllers/location_product.go
index ca86491..5955d2c 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
@@ -128,14 +148,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 +213,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