From 73b6baf6af3d88cdcb0e2df7932a9bd96b0b85c5 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 01 七月 2024 22:32:34 +0800
Subject: [PATCH] 月度统计出入库按类型汇总报表定时任务和手动跑任务接口

---
 controllers/product_controller.go |  184 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 173 insertions(+), 11 deletions(-)

diff --git a/controllers/product_controller.go b/controllers/product_controller.go
index cb0e7fc..0a0bc3f 100644
--- a/controllers/product_controller.go
+++ b/controllers/product_controller.go
@@ -54,6 +54,13 @@
 		util.ResponseFormat(c, code.RequestParamError, "鍗曚綅涓嶈兘涓虹┖")
 		return
 	}
+	if params.BarCode != "" {
+		m, err := models.NewMaterialSearch().SetBarCode(params.BarCode).First()
+		if err == nil && m.ID != "" { //鏌ュ嚭鐗╂枡琛ㄦ槸鐗╂枡宸插瓨鍦�
+			util.ResponseFormat(c, code.RequestParamError, "鏉″舰鐮佸凡缁忚浣跨敤")
+			return
+		}
+	}
 	//params.ID = utils.GetUUID()
 
 	err := models.WithTransaction(func(tx *gorm.DB) error {
@@ -181,14 +188,46 @@
 			product.MaxInventoryRule = reorderRules[0].MaxInventory
 		}
 		product.ReorderRuleNum = rulesTotal
+		attributeValues, err := models.NewAttributeValueSearch().SetEntityID(product.ID).FindNotTotal()
+		aids := make([]uint, 0)
+		for _, v := range attributeValues {
+			aids = append(aids, v.AttributeID)
+		}
+		attributes, err := models.NewAttributeSearch().SetIDs(aids).FindNotTotal()
+		attributesMap := make(map[uint]*models.Attribute, len(attributes))
+		for _, v := range attributes {
+			attributesMap[v.ID] = v
+		}
+		if err != nil {
+			util.ResponseFormat(c, code.RequestParamError, "鏈煡鍔ㄦ�佸睘鎬�")
+		}
+		for _, v1 := range attributeValues {
+			attribute := attributesMap[v1.AttributeID]
+			if attribute == nil {
+				continue
+			}
+			//product.Attributes = append(product.Attributes,v1)
+			product.Attributes = append(product.Attributes, models.Attribute{
+				Model:        gorm.Model{ID: v1.ID, CreatedAt: v1.CreatedAt, UpdatedAt: v1.UpdatedAt, DeletedAt: v1.DeletedAt},
+				Name:         attribute.Name,
+				DataType:     attribute.DataType,
+				EntityType:   attribute.EntityType,
+				SelectValues: attribute.SelectValues,
+				SelectValue:  attribute.SelectValue,
+				//Value:        v1.Value,
+				Value: v1.Value,
+			})
+		}
+
 	}
 	util.ResponseFormatList(c, code.Success, products, int(total))
 }
 
 // GetProductDetails
 // @Tags      浜у搧
-// @Summary   鑾峰彇浜у搧璇︽儏
+// @Summary   閫氳繃浜у搧/鍟嗗搧/鐗╂枡 ID鑾峰彇浜у搧璇︽儏
 // @Produce   application/json
+// @Param     Authorization	header string true "token"
 // @Param		id	path		string			true	"id"  "鏌ヨ鍙傛暟"
 // @Success   200 {object} util.Response{data=models.Material}	"鎴愬姛"
 // @Router    /api-wms/v1/product/getProductDetails/{id} [get]
@@ -202,6 +241,96 @@
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
 		return
+	}
+	attributeValues, err := models.NewAttributeValueSearch().SetEntityID(material.ID).FindNotTotal()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
+		return
+	}
+	aids := make([]uint, 0)
+	for _, v := range attributeValues {
+		aids = append(aids, v.AttributeID)
+	}
+	attributes, err := models.NewAttributeSearch().SetIDs(aids).FindNotTotal()
+	attributesMap := make(map[uint]*models.Attribute, len(attributes))
+	for _, v := range attributes {
+		attributesMap[v.ID] = v
+	}
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏈煡鍔ㄦ�佸睘鎬�")
+	}
+	for _, v1 := range attributeValues {
+		attribute := attributesMap[v1.AttributeID]
+		if attribute == nil {
+			continue
+		}
+		//product.Attributes = append(product.Attributes,v1)
+		material.Attributes = append(material.Attributes, models.Attribute{
+			Model:        gorm.Model{ID: v1.ID, CreatedAt: v1.CreatedAt, UpdatedAt: v1.UpdatedAt, DeletedAt: v1.DeletedAt},
+			Name:         attribute.Name,
+			DataType:     attribute.DataType,
+			EntityType:   attribute.EntityType,
+			SelectValues: attribute.SelectValues,
+			SelectValue:  attribute.SelectValue,
+			//Value:        v1.Value,
+			Value: v1.Value,
+		})
+	}
+	util.ResponseFormat(c, code.Success, material)
+}
+
+// GetProductDetailsByBarCode
+// @Tags      浜у搧
+// @Summary   閫氳繃浜у搧/鍟嗗搧/鐗╂枡 鏉″舰鐮� 鑾峰彇浜у搧璇︽儏
+// @Produce   application/json
+// @Param     Authorization	header string true "token"
+// @Param		barCode	path		string			true	"barCode"  "鏌ヨ鍙傛暟"
+// @Success   200 {object} util.Response{data=models.Material}	"鎴愬姛"
+// @Router    /api-wms/v1/product/getProductDetailsByBarCode/{barCode} [get]
+func (slf ProductController) GetProductDetailsByBarCode(c *gin.Context) {
+	barCode := c.Param("barCode")
+	if barCode == "" {
+		util.ResponseFormat(c, code.RequestParamError, "鏃犳晥鏉″舰鐮�")
+		return
+	}
+	material, err := models.NewMaterialSearch().SetBarCode(barCode).SetPreload(true).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
+		return
+	}
+	attributeValues, err := models.NewAttributeValueSearch().SetEntityID(material.ID).FindNotTotal()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ユ壘澶辫触")
+		return
+	}
+	aids := make([]uint, 0)
+	for _, v := range attributeValues {
+		aids = append(aids, v.AttributeID)
+	}
+	attributes, err := models.NewAttributeSearch().SetIDs(aids).FindNotTotal()
+	attributesMap := make(map[uint]*models.Attribute, len(attributes))
+	for _, v := range attributes {
+		attributesMap[v.ID] = v
+	}
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏈煡鍔ㄦ�佸睘鎬�")
+	}
+	for _, v1 := range attributeValues {
+		attribute := attributesMap[v1.AttributeID]
+		if attribute == nil {
+			continue
+		}
+		//product.Attributes = append(product.Attributes,v1)
+		material.Attributes = append(material.Attributes, models.Attribute{
+			Model:        gorm.Model{ID: v1.ID, CreatedAt: v1.CreatedAt, UpdatedAt: v1.UpdatedAt, DeletedAt: v1.DeletedAt},
+			Name:         attribute.Name,
+			DataType:     attribute.DataType,
+			EntityType:   attribute.EntityType,
+			SelectValues: attribute.SelectValues,
+			SelectValue:  attribute.SelectValue,
+			//Value:        v1.Value,
+			Value: v1.Value,
+		})
 	}
 	util.ResponseFormat(c, code.Success, material)
 }
@@ -230,6 +359,13 @@
 	if params.Unit == "" {
 		util.ResponseFormat(c, code.RequestParamError, "鍗曚綅涓嶈兘涓虹┖")
 		return
+	}
+	if params.BarCode != "" {
+		m, err := models.NewMaterialSearch().SetBarCode(params.BarCode).First()
+		if err == nil && m.ID != params.ID { //鏌ュ嚭鐗╂枡涓擨D涓嶄竴鏍�,
+			util.ResponseFormat(c, code.RequestParamError, "鏉″舰鐮佸凡缁忚浣跨敤")
+			return
+		}
 	}
 	err := models.NewMaterialSearch().SetID(params.ID).Save(&params)
 	if err != nil {
@@ -268,25 +404,25 @@
 			}
 		}
 
-		attributeValueSearch := models.NewAttributeValueSearch()
+		/*attributeValueSearch := models.NewAttributeValueSearch()
 		//鍒犻櫎浠庢柊鎻掑叆
 		if err := attributeValueSearch.SetOrm(tx).SetEntityID(params.ID).Delete(); err != nil {
 			return err
-		}
-		avs := make([]*models.AttributeValue, 0)
+		}*/
+		//avs := make([]*models.AttributeValue, 0)
 		for _, v := range params.Attributes {
 			av := models.AttributeValue{
-				Model:       gorm.Model{},
+				Model:       gorm.Model{ID: v.ID},
 				EntityID:    params.ID,
 				AttributeID: v.ID,
 				Value:       v.Value,
 			}
-			avs = append(avs, &av)
+			//avs = append(avs, &av)
+			if err := models.NewAttributeValueSearch().SetOrm(tx).Save(&av); err != nil {
+				return err
+			}
+		}
 
-		}
-		if err := models.NewAttributeValueSearch().SetOrm(tx).CreateBatch(avs); err != nil {
-			return err
-		}
 		return nil
 	})
 	if err != nil {
@@ -299,7 +435,7 @@
 
 // DeleteProduct
 // @Tags      浜у搧
-// @Summary   鍒犻櫎浜у搧
+// @Summary   閫氳繃浜у搧/鍟嗗搧/鐗╂枡 ID鍒犻櫎浜у搧
 // @Produce   application/json
 // @Param		id	path		string			true	"id"  "鏌ヨ鍙傛暟"
 // @Success   200 {object} util.Response "鎴愬姛"
@@ -311,6 +447,27 @@
 		return
 	}
 	err := models.NewMaterialSearch().SetID(id).Delete()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鍒犻櫎澶辫触")
+		return
+	}
+	util.ResponseFormat(c, code.Success, "鍒犻櫎鎴愬姛")
+}
+
+// DeleteProductByBarCode
+// @Tags      浜у搧
+// @Summary    閫氳繃浜у搧/鍟嗗搧/鐗╂枡 鏉″舰鐮佸垹闄や骇鍝�
+// @Produce   application/json
+// @Param		barCode	path		string			true	"barCode"  "鏌ヨ鍙傛暟"
+// @Success   200 {object} util.Response "鎴愬姛"
+// @Router    /api-wms/v1/product/deleteProductByBarCode/{barCode} [delete]
+func (slf ProductController) DeleteProductByBarCode(c *gin.Context) {
+	barCode := c.Param("barCode")
+	if barCode == "" {
+		util.ResponseFormat(c, code.RequestParamError, "鏃犳晥id")
+		return
+	}
+	err := models.NewMaterialSearch().SetBarCode(barCode).Delete()
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鍒犻櫎澶辫触")
 		return
@@ -881,6 +1038,11 @@
 //
 //	@Tags		鐗╂枡绠$悊
 //	@Summary	瀵煎叆鐗╂枡
+//
+// @Accept multipart/form-data
+// @Param file formData file true "file"
+// @Param     Authorization	header string true "token"
+//
 //	@Produce	application/xlsx
 //	@Success	200		{object}	util.Response 	"鎴愬姛"
 //	@Router		/api-wms/v1/product/inputProduct [post]

--
Gitblit v1.8.0