From f45e4cb8045fe60f96f467dc4ea1e2ae57445712 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 03 七月 2024 23:13:40 +0800
Subject: [PATCH] 默认位置parent_id为视图位置id

---
 service/material.go |  146 +++++++++++++++++++++---------------------------
 1 files changed, 64 insertions(+), 82 deletions(-)

diff --git a/service/material.go b/service/material.go
index 5ae63e5..c753e21 100644
--- a/service/material.go
+++ b/service/material.go
@@ -19,6 +19,7 @@
 	}
 	mapExistMaterial := make(map[string]bool)
 	mapAttribute := make(map[string]uint)
+	mapProductCategory := make(map[string]int)
 	for _, v := range existMaterials {
 		mapExistMaterial[v.ID] = true
 	}
@@ -26,8 +27,15 @@
 	if err != nil {
 		return 0, err
 	}
+	productCategory, err := models.NewProductCategorySearch().FindNotTotal()
+	if err != nil {
+		return 0, err
+	}
 	for _, v := range attributes {
 		mapAttribute[v.Name] = v.ID
+	}
+	for _, v := range productCategory {
+		mapProductCategory[v.Name] = v.Id
 	}
 	f, err := excelize.OpenReader(file)
 	if err != nil {
@@ -56,7 +64,7 @@
 
 	for index, insert := range inserts {
 		errMsg := ""
-		if len(insert) < 4 || insert[0] == "" || insert[1] == "" || insert[2] == "" || insert[3] == "" {
+		if len(insert) < 6 || insert[0] == "" || insert[1] == "" || insert[2] == "" || insert[3] == "" || insert[4] == "" || insert[5] == "" {
 			errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝娌℃湁濉啓蹇呭~椤归」"
 			return 0, errors.New(errMsg)
 		}
@@ -82,139 +90,113 @@
 		case string(constvar.MaterialModeOther):
 			material.Model = constvar.MaterialModeOther
 		default:
+			errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾棤娉曡瘑鍒墿鏂欑被鍨�"
+			return 0, errors.New(errMsg)
+		}
+		if mapProductCategory[strings.Trim(insert[3], " ")] == 0 {
 			errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾棤娉曡瘑鍒浜у搧绫诲埆"
 			return 0, errors.New(errMsg)
 		}
+		material.CategoryId = mapProductCategory[strings.Trim(insert[3], " ")]
+		purchaseTypeList := make([]int, 0)
+		purchaseTypes := strings.Split(strings.Trim(insert[4], " "), "/")
+		for _, v := range purchaseTypes {
+			switch strings.Trim(v, " ") {
+			case "閲囪喘":
+				purchaseTypeList = append(purchaseTypeList, int(constvar.PurchaseTypeOutSource))
+			case "鑷埗":
+				purchaseTypeList = append(purchaseTypeList, int(constvar.PurchaseTypeSelf))
+			case "濮斿":
+				purchaseTypeList = append(purchaseTypeList, int(constvar.PurchaseTypeEntrust))
+			default:
+				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾棤娉曡瘑鍒噰璐被鍨�"
+				return 0, errors.New(errMsg)
+			}
 
-		if len(insert) > 4 && insert[3] != "" {
-			//errMsg = "绗�" + strconv.Itoa(index+1) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭濉啓鍗曚綅"
-			//return 0, errors.New(errMsg)
-			material.Unit = insert[3]
 		}
+		material.PurchaseTypeList = purchaseTypeList
+		material.Unit = insert[5]
 
 		var moreUnit = true
-		if len(insert) > 5 && insert[4] != "" {
+		if len(insert) > 7 && insert[6] != "" {
 			material.MoreUnit = &moreUnit
 			var ut models.UnitItems
-			ut.Unit = insert[4]
-			if len(insert) < 13 || insert[12] == "" {
+			ut.Unit = insert[6]
+			if len(insert) < 14 || insert[13] == "" {
 				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛岃濉啓涓庤緟璁¢噺鍗曚綅1鐩稿叧鐨勫崟浣嶆崲绠楁瘮渚�1"
 				return 0, errors.New(errMsg)
 			}
-			ut.Amount = decimal.RequireFromString(insert[12])
-			if len(insert) < 14 || insert[13] == "" {
+			ut.Amount = decimal.RequireFromString(insert[13])
+			if len(insert) < 15 || insert[14] == "" {
 				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛岃濉啓涓庤緟璁¢噺鍗曚綅1鐩稿叧鐨勬槸鍚︽诞鍔ㄦ崲绠�1"
 				return 0, errors.New(errMsg)
 			}
-			if strings.Trim(insert[13], " ") == "鏄�" {
+			if strings.Trim(insert[14], " ") == "鏄�" {
 				ut.Floating = true
 			} else {
 				ut.Floating = false
 			}
 			material.MoreUnitList = append(material.MoreUnitList, ut)
 		}
-		if len(insert) > 6 && insert[5] != "" {
+		if len(insert) > 8 && insert[7] != "" {
 			material.MoreUnit = &moreUnit
 			var ut models.UnitItems
-			ut.Unit = insert[5]
-			if len(insert) < 15 || insert[14] == "" {
+			ut.Unit = insert[7]
+			if len(insert) < 16 || insert[15] == "" {
 				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛岃濉啓涓庤緟璁¢噺鍗曚綅2鐩稿叧鐨勫崟浣嶆崲绠楁瘮渚�2"
 				return 0, errors.New(errMsg)
 			}
-			ut.Amount = decimal.RequireFromString(insert[14])
-			if len(insert) < 16 || insert[15] == "" {
+			ut.Amount = decimal.RequireFromString(insert[15])
+			if len(insert) < 17 || insert[16] == "" {
 				errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛岃濉啓涓庤緟璁¢噺鍗曚綅2鐩稿叧鐨勬槸鍚︽诞鍔ㄦ崲绠�2"
 				return 0, errors.New(errMsg)
 			}
-			if strings.Trim(insert[15], " ") == "鏄�" {
+			if strings.Trim(insert[16], " ") == "鏄�" {
 				ut.Floating = true
 			} else {
 				ut.Floating = false
 			}
 			material.MoreUnitList = append(material.MoreUnitList, ut)
 		}
-		if len(insert) > 7 && insert[6] != "" {
-			material.Specs = insert[6] //瑙勬牸
+		if len(insert) > 9 && insert[8] != "" {
+			material.Specs = insert[8] //瑙勬牸
 		}
-		if len(insert) > 8 && insert[7] != "" {
-			material.Type = insert[7] //鍨嬪彿
+		if len(insert) > 10 && insert[9] != "" {
+			material.Type = insert[9] //鍨嬪彿
 		}
-		{
-			/*
-				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)
-				}
-			*/
 
-		}
+		materialList = append(materialList, material)
 
 		//淇濆瓨鍔ㄦ�佸睘鎬�
-		for attributesColumn := range attributesColumns {
-			attributeValueList = append(attributeValueList, &models.AttributeValue{
-				EntityID:    material.ID,
-				AttributeID: mapAttribute[insert[attributesColumn]],
-				Value:       insert[attributesColumn],
-			})
+		for _, attributesColumn := range attributesColumns {
+			if len(insert) > attributesColumn {
+				attributeValueList = append(attributeValueList, &models.AttributeValue{
+					EntityID: material.ID,
+					//AttributeID: mapAttribute[insert[attributesColumn]],
+					AttributeID: mapAttribute[headers[attributesColumn]],
+					Value:       insert[attributesColumn],
+				})
+			}
 		}
 
+	}
+	if len(materialList) == 0 {
+		return 0, errors.New("鐗╂枡鏁版嵁涓虹┖")
 	}
 	err = models.WithTransaction(func(db *gorm.DB) error {
 		if err := models.NewMaterialSearch().SetOrm(db).CreateBatch(materialList); err != nil {
 			return err
 		}
-		if err := models.NewAttributeValueSearch().SetOrm(db).CreateBatch(attributeValueList); err != nil {
-			return err
+		if len(attributeValueList) != 0 {
+			if err := models.NewAttributeValueSearch().SetOrm(db).CreateBatch(attributeValueList); err != nil {
+				return err
+			}
 		}
 		return nil
 	})
 
 	if err != nil {
-		return 0, errors.New("瀵煎叆澶辫触")
+		return 0, errors.New("瀵煎叆澶辫触:" + err.Error())
 	}
 	return len(inserts), err
 }

--
Gitblit v1.8.0