zhangqian
2024-07-04 c950586b718ae6fc198bedf424609a4ac94cb5d1
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 {
@@ -45,11 +53,18 @@
      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 := ""
      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)
      }
@@ -75,125 +90,124 @@
      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) > 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)
         material.Specs = insert[8] //规格
      }
      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)
         material.Type = insert[9] //型号
      }
      materialList = append(materialList, material)
      //保存动态属性
      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 {
         err = models.NewAttributeValueSearch().SetOrm(db).CreateBatch(attributeValueList)
         if err != nil { //批量插入失败(可能已经存在值),分开一条一条插入重复的不插入
            for _, avl := range attributeValueList {
               first, err2 := models.NewAttributeValueSearch().SetAttributeID(avl.AttributeID).SetEntityID(avl.EntityID).First()
               if err2 == nil && first != nil { //表示已经存在动态属性
                  continue
               }
               err2 = models.NewAttributeValueSearch().SetOrm(db).Create(avl)
               if err2 != nil {
                  return err
               }
            }
         }
      }
      return nil
   })
   if err != nil {
      return 0, errors.New("导入失败")
      return 0, errors.New("导入失败:" + err.Error())
   }
   return len(inserts), err
}