From f9ce7300c4ce1673d06a052e43cb462f8a69c13c Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期四, 02 十一月 2023 14:30:19 +0800
Subject: [PATCH] 优化重构2

---
 api/v1/test/product.go |   33 ++++++++++++++++++++++++++-------
 1 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/api/v1/test/product.go b/api/v1/test/product.go
index 46bf465..dfb82c9 100644
--- a/api/v1/test/product.go
+++ b/api/v1/test/product.go
@@ -26,17 +26,30 @@
 // @Security ApiKeyAuth
 // @accept application/json
 // @Produce application/json
-// @Param data body test.Product true "鍒涘缓Product"
+// @Param data body request.ProductCreate true "鍒涘缓Product"
 // @Success 200 {string} string "{"success":true,"data":{},"msg":"鑾峰彇鎴愬姛"}"
 // @Router /p/createProduct [post]
 func (pApi *ProductApi) CreateProduct(c *gin.Context) {
-	var p test.Product
+	var p testReq.ProductCreate
 	err := c.ShouldBindJSON(&p)
 	if err != nil {
 		response.FailWithMessage(err.Error(), c)
 		return
 	}
-	if err := pService.CreateProduct(&p); err != nil {
+	for _, t := range p.List {
+		var num int64
+		db := global.GVA_DB.Model(&test.Product{})
+		err := db.Where("number = ?", t.Number).Where("supplier_id = ?", t.SupplierId).Count(&num).Error
+		if err != nil {
+			response.FailWithMessage(err.Error(), c)
+			return
+		}
+		if num > 0 {
+			response.FailWithMessage("褰撳墠渚涘簲鍟嗗凡缁忔彁渚涙浜у搧", c)
+			return
+		}
+	}
+	if err := pService.CreateProduct(p.List); err != nil {
 		global.GVA_LOG.Error("鍒涘缓澶辫触!", zap.Error(err))
 		response.FailWithMessage("鍒涘缓澶辫触", c)
 	} else {
@@ -160,6 +173,10 @@
 		global.GVA_LOG.Error("鑾峰彇澶辫触!", zap.Error(err))
 		response.FailWithMessage("鑾峰彇澶辫触", c)
 	} else {
+		//娣诲姞渚涘簲鍟嗗悕绉�
+		for i := 0; i < len(list); i++ {
+			list[i].SupplierName = list[i].Supplier.Name
+		}
 		response.OkWithDetailed(response.PageResult{
 			List:     list,
 			Total:    total,
@@ -219,10 +236,12 @@
 		productList[k].Number = v.Number
 		productList[k].Name = v.Name
 		productList[k].Unit = v.Unit
-		productList[k].PurchasePrice = &v.SalePrice
-		//productList[k].MinimumStock = &v.SalePrice
-		//productList[k].MaximumStock = &v.SalePrice
-		//productList[k].Remark = &v.SalePrice
+		productList[k].PurchasePrice = v.SalePrice
+		min := int(v.MinInventory)
+		productList[k].MinimumStock = min
+		max := int(v.MaxInventory)
+		productList[k].MaximumStock = max
+		productList[k].Remark = v.Node
 		productList[k].ProductType = v.MaterialMode
 	}
 

--
Gitblit v1.8.0