zhangqian
2024-07-01 73b6baf6af3d88cdcb0e2df7932a9bd96b0b85c5
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,62 +139,82 @@
      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] + ",未能识别庄口属性,请先添加该属性"
                  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)
      }
      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] + ",未能识别庄口属性,请先添加该属性"
            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)
      //保存动态属性
      for _, attributesColumn := range attributesColumns {
         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().CreateBatch(materialList); err != nil {
      if err := models.NewMaterialSearch().SetOrm(db).CreateBatch(materialList); err != nil {
         return err
      }
      if err := models.NewAttributeValueSearch().CreateBatch(attributeValueList); err != nil {
         return err
      if len(attributeValueList) != 0 {
         if err := models.NewAttributeValueSearch().SetOrm(db).CreateBatch(attributeValueList); err != nil {
            return err
         }
      }
      return nil
   })