From 029c8611556aa3b77cdae78376842e8531521c9f Mon Sep 17 00:00:00 2001
From: lishihai <dslsh@dscom>
Date: 星期四, 20 六月 2024 22:24:01 +0800
Subject: [PATCH] 产品->动态属性增改查

---
 controllers/product_controller.go |   49 ++++++++++++++++++++----
 controllers/attribute.go          |    8 ++--
 models/attribute.go               |   10 ++++-
 request/attribute.go              |    6 +-
 4 files changed, 55 insertions(+), 18 deletions(-)

diff --git a/controllers/attribute.go b/controllers/attribute.go
index a2cc273..079fb27 100644
--- a/controllers/attribute.go
+++ b/controllers/attribute.go
@@ -144,14 +144,14 @@
 
 // ListAttribute
 // @Tags      灞炴�у�煎拰瀵硅薄
-// @Summary   鏌ヨ灞炴�у�煎拰瀵硅薄
+// @Summary   娣诲姞灞炴�у�煎拰瀵硅薄
 // @Produce   application/json
-// object  body  request.GetAttributeList true  "鏌ヨ鍙傛暟"
+// @Param     object  body  request.GetAttributeList true  "灞炴�у�煎拰瀵硅薄淇℃伅"
 // @Success   200 {object} util.Response "鎴愬姛"
 // @Router    /api-wms/v1/attribute/list [post]
 func (slf AttributeController) ListAttribute(c *gin.Context) {
-	var params request.GetAttributeList
-	if err := c.ShouldBindQuery(&params); err != nil {
+	var params request.AttributeList
+	if err := c.ShouldBind(&params); err != nil {
 		util.ResponseFormat(c, code.RequestParamError, err.Error())
 		return
 	}
diff --git a/controllers/product_controller.go b/controllers/product_controller.go
index cb0e7fc..dea98e2 100644
--- a/controllers/product_controller.go
+++ b/controllers/product_controller.go
@@ -181,6 +181,37 @@
 			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))
 }
@@ -268,25 +299,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 {
diff --git a/models/attribute.go b/models/attribute.go
index 92ca831..8f1a368 100644
--- a/models/attribute.go
+++ b/models/attribute.go
@@ -26,6 +26,7 @@
 		PageSize int
 		Orm      *gorm.DB
 		Keyword  string
+		IDs      []uint
 	}
 )
 
@@ -104,7 +105,10 @@
 	slf.ID = id
 	return slf
 }
-
+func (slf *AttributeSearch) SetIDs(ids []uint) *AttributeSearch {
+	slf.IDs = ids
+	return slf
+}
 func (slf *AttributeSearch) SetName(name string) *AttributeSearch {
 	slf.Name = name
 	return slf
@@ -120,7 +124,9 @@
 	if slf.ID != 0 {
 		db = db.Where("id = ?", slf.ID)
 	}
-
+	if len(slf.IDs) != 0 {
+		db = db.Where("id in ?", slf.IDs)
+	}
 	if slf.Order != "" {
 		db = db.Order(slf.Order)
 	}
diff --git a/request/attribute.go b/request/attribute.go
index df8d05a..0b72fbc 100644
--- a/request/attribute.go
+++ b/request/attribute.go
@@ -2,10 +2,10 @@
 
 import "wms/models"
 
-type GetAttributeList struct {
+type AttributeList struct {
 	PageInfo
-	Keyword    string            `json:"keyword"`
-	EntityType models.EntityType `json:"entityType"`
+	Keyword    string            `json:"keyword" form:"keyword"`
+	EntityType models.EntityType `json:"entityType" form:"entityType"`
 }
 
 type AddAttribute struct {

--
Gitblit v1.8.0