From 0b7036989cbbe1073e5338dc57b395c6d10ee0d1 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 20 六月 2024 10:01:42 +0800
Subject: [PATCH] 物料导入保存动态属性代码逻辑修正

---
 service/material.go |  119 ++++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 73 insertions(+), 46 deletions(-)

diff --git a/service/material.go b/service/material.go
index adabc1d..7513d30 100644
--- a/service/material.go
+++ b/service/material.go
@@ -45,7 +45,14 @@
 		return 0, errors.New("鏀规枃浠舵病鏈夋暟鎹唴瀹�")
 	}
 
-	inserts := rows[1:len(rows)]
+	inserts := rows[1:]
+	headers := rows[0]
+	attributesColumns := make([]int, 0)
+	for i, header := range headers {
+		if mapAttribute[header] != 0 {
+			attributesColumns = append(attributesColumns, i)
+		}
+	}
 
 	for index, insert := range inserts {
 		errMsg := ""
@@ -132,53 +139,73 @@
 		if len(insert) > 8 && insert[7] != "" {
 			material.Type = insert[7] //鍨嬪彿
 		}
-		if len(insert) > 9 && insert[8] != "" {
-			if mapAttribute[insert[8]] == 0 {
-				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒潗璐ㄥ睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
-				return 0, errors.New(errMsg)
-			}
-			//material.Quality = insert[8] //鏉愯川
-			attributeValue1 := new(models.AttributeValue)
-			attributeValue1.EntityID = material.ID
-			attributeValue1.AttributeID = mapAttribute[insert[8]]
-			attributeValue1.Value = insert[8]
-			attributeValueList = append(attributeValueList, attributeValue1)
+		{
+			/*
+				if len(insert) > 9 && insert[8] != "" {
+					if mapAttribute[insert[8]] == 0 {
+						errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒潗璐ㄥ睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
+						return 0, errors.New(errMsg)
+					}
+					//material.Quality = insert[8] //鏉愯川
+					attributeValue1 := new(models.AttributeValue)
+					attributeValue1.EntityID = material.ID
+					attributeValue1.AttributeID = mapAttribute[insert[8]]
+					attributeValue1.Value = insert[8]
+					attributeValueList = append(attributeValueList, attributeValue1)
+				}
+				if len(insert) > 10 && insert[9] != "" {
+					if mapAttribute[insert[9]] == 0 {
+						errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒搧鐗屽睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
+						return 0, errors.New(errMsg)
+					}
+					//material.Brand = insert[9]   //鍝佺墝
+					attributeValue2 := new(models.AttributeValue)
+					attributeValue2.EntityID = material.ID
+					attributeValue2.AttributeID = mapAttribute[insert[9]]
+					attributeValue2.Value = insert[9]
+					attributeValueList = append(attributeValueList, attributeValue2)
+				}
+				if len(insert) > 11 && insert[10] != "" {
+					if mapAttribute[insert[10]] == 0 {
+						errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒瓑绾у睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
+						return 0, errors.New(errMsg)
+					}
+					//material.Level = insert[10]  //绛夌骇
+					attributeValue3 := new(models.AttributeValue)
+					attributeValue3.EntityID = material.ID
+					attributeValue3.AttributeID = mapAttribute[insert[10]]
+					attributeValue3.Value = insert[10]
+					attributeValueList = append(attributeValueList, attributeValue3)
+				}
+				if len(insert) > 12 && insert[11] != "" {
+					if mapAttribute[insert[11]] == 0 {
+						errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒簞鍙e睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
+						return 0, errors.New(errMsg)
+					}
+					//material.From = insert[11]   //搴勫彛
+					attributeValue4 := new(models.AttributeValue)
+					attributeValue4.EntityID = material.ID
+					attributeValue4.AttributeID = mapAttribute[insert[11]]
+					attributeValue4.Value = insert[11]
+					attributeValueList = append(attributeValueList, attributeValue4)
+				}
+			*/
+
 		}
-		if len(insert) > 10 && insert[9] != "" {
-			if mapAttribute[insert[9]] == 0 {
-				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒搧鐗屽睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
-				return 0, errors.New(errMsg)
+
+		//淇濆瓨鍔ㄦ�佸睘鎬�
+		for attributesColumn := range attributesColumns {
+			if mapAttribute[insert[attributesColumn]] != 0 {
+				attributeValue2 := new(models.AttributeValue)
+				attributeValue2.EntityID = material.ID
+				attributeValue2.AttributeID = mapAttribute[insert[9]]
+				attributeValue2.Value = insert[9]
+				attributeValueList = append(attributeValueList, &models.AttributeValue{
+					EntityID:    material.ID,
+					AttributeID: mapAttribute[insert[attributesColumn]],
+					Value:       insert[attributesColumn],
+				})
 			}
-			//material.Brand = insert[9]   //鍝佺墝
-			attributeValue2 := new(models.AttributeValue)
-			attributeValue2.EntityID = material.ID
-			attributeValue2.AttributeID = mapAttribute[insert[9]]
-			attributeValue2.Value = insert[9]
-			attributeValueList = append(attributeValueList, attributeValue2)
-		}
-		if len(insert) > 11 && insert[10] != "" {
-			if mapAttribute[insert[10]] == 0 {
-				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒瓑绾у睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
-				return 0, errors.New(errMsg)
-			}
-			//material.Level = insert[10]  //绛夌骇
-			attributeValue3 := new(models.AttributeValue)
-			attributeValue3.EntityID = material.ID
-			attributeValue3.AttributeID = mapAttribute[insert[10]]
-			attributeValue3.Value = insert[10]
-			attributeValueList = append(attributeValueList, attributeValue3)
-		}
-		if len(insert) > 12 && insert[11] != "" {
-			if mapAttribute[insert[11]] == 0 {
-				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒簞鍙e睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�"
-				return 0, errors.New(errMsg)
-			}
-			//material.From = insert[11]   //搴勫彛
-			attributeValue4 := new(models.AttributeValue)
-			attributeValue4.EntityID = material.ID
-			attributeValue4.AttributeID = mapAttribute[insert[11]]
-			attributeValue4.Value = insert[11]
-			attributeValueList = append(attributeValueList, attributeValue4)
 		}
 
 	}

--
Gitblit v1.8.0