|
@@ -29,7 +29,7 @@ import (
|
|
|
var fixedHeaders = []string{
|
|
|
"公司商品编号", "商品中文名", "商品英文名", "分类", "图片",
|
|
|
"单位包材毛重(KG)", "单位包材体积(CBM)", "长度(MM)", "门幅/宽(MM)", "厚度/高(MM)",
|
|
|
- "备注", "首选供应商", "默认采购单价", "样品搜集人", "开发日期",
|
|
|
+ "备注", "首选供应商", "默认采购单价", "样品搜集人编号", "样品搜集人", "开发日期",
|
|
|
// "出口属性", "内销属性", "内购属性", "委外属性", 2025-4-29沟通去掉这几个属性
|
|
|
"报关助记符", "报关商品编码", "报关商品中文名", "录入人名称", "适合的市场",
|
|
|
"供应商编号", "种类分类", "种类分类名称", "基布", "基布名称",
|
|
@@ -46,10 +46,675 @@ func RegExcelRouter(router *GinRouter) {
|
|
|
router.POSTJWT("/zip/export", ZipExport)
|
|
|
}
|
|
|
|
|
|
+func ParseMatObject(row []string, headers []string, cates []*model.Category) (model.MatImage, error) {
|
|
|
+ imageMat := model.MatImage{}
|
|
|
+ // 构建基础数据
|
|
|
+ imageMat.CusNum = row[0]
|
|
|
+ imageMat.NameCN = row[1]
|
|
|
+ imageMat.NameEN = row[2]
|
|
|
+
|
|
|
+ //1.商品编号
|
|
|
+ CusNumIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "公司商品编号" {
|
|
|
+ CusNumIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if CusNumIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("公司商品编号列未找到")
|
|
|
+ }
|
|
|
+ imageMat.CusNum = row[CusNumIndex]
|
|
|
+
|
|
|
+ //2中文名称
|
|
|
+ NameCNIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "商品中文名" {
|
|
|
+ NameCNIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if NameCNIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("商品中文名列未找到")
|
|
|
+ }
|
|
|
+ imageMat.NameCN = row[NameCNIndex]
|
|
|
+
|
|
|
+ //3英文名称
|
|
|
+ NameENIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "商品英文名" {
|
|
|
+ NameENIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if NameENIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("商品英文名列未找到")
|
|
|
+ }
|
|
|
+ imageMat.NameEN = row[NameENIndex]
|
|
|
+
|
|
|
+ //4.商品分类
|
|
|
+ CategoryIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "分类" {
|
|
|
+ CategoryIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if CategoryIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("分类列未找到")
|
|
|
+ }
|
|
|
+ //imageMat.Categories = append(imageMat.Categories, row[CategoryIndex])
|
|
|
+ // 根据分类层级一的名字获取对应id,遍历一层获取对应数据
|
|
|
+ rootCate := &model.Category{}
|
|
|
+ for _, cate := range cates {
|
|
|
+ if cate.Name == "商品分类" {
|
|
|
+ for _, c := range cate.Children {
|
|
|
+ if c.Name == row[CategoryIndex] {
|
|
|
+ // 记录该分类为其他自动做准备
|
|
|
+ rootCate = c
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(rootCate.IdStr) <= 0 {
|
|
|
+ return imageMat, fmt.Errorf("%s的商品分类未找到定义", row[CategoryIndex])
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取rootCate下的二级分类
|
|
|
+ var getRootCate2 = func(rootCate *model.Category, pName string, name string, cusNum string) *model.Category {
|
|
|
+ for _, cate := range rootCate.Children {
|
|
|
+ if cate.Name == pName {
|
|
|
+ for _, c := range cate.Children {
|
|
|
+ if c.Name == name {
|
|
|
+ if len(cusNum) > 0 {
|
|
|
+ if c.CusNum == cusNum {
|
|
|
+ return c
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return c
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ //分类rootId
|
|
|
+ imageMat.Categories = append(imageMat.Categories, row3Cate.IdStr)
|
|
|
+
|
|
|
+ //5 单位包材毛重(KG)
|
|
|
+ PackageGrossWeightIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "单位包材毛重(KG)" {
|
|
|
+ PackageGrossWeightIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if PackageGrossWeightIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("单位包材毛重(KG)列未找到")
|
|
|
+ }
|
|
|
+ var str2float64 = func(s string) *float64 {
|
|
|
+ if len(s) == 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ f, err := strconv.ParseFloat(s, 64)
|
|
|
+ if err != nil {
|
|
|
+ return 0 // 或 math.NaN() 表示无效值
|
|
|
+ }
|
|
|
+ return f
|
|
|
+ }
|
|
|
+
|
|
|
+ row5 := str2float64(row[PackageGrossWeightIndex])
|
|
|
+ imageMat.PackageGrossWeight = row5
|
|
|
+
|
|
|
+ //6.单位包材体积(CBM)
|
|
|
+ PackageVolumeIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "单位包材体积(CBM)" {
|
|
|
+ PackageVolumeIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if PackageVolumeIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("单位包材体积(CBM)列未找到")
|
|
|
+ }
|
|
|
+ row6 := str2float64(row[PackageVolumeIndex])
|
|
|
+ imageMat.PackageVolume = row6
|
|
|
+
|
|
|
+ //7.长度(MM)
|
|
|
+ PhyHeightIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "长度(MM)" {
|
|
|
+ PhyHeightIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if PhyHeightIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("长度(MM)列未找到")
|
|
|
+ }
|
|
|
+ row7 := str2float64(row[PhyHeightIndex])
|
|
|
+ imageMat.PhyHeight = row7
|
|
|
+
|
|
|
+ //8.门幅/宽(MM)
|
|
|
+ PhyWidthIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "门幅/宽(MM)" {
|
|
|
+ PhyWidthIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if PhyWidthIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("门幅/宽(MM)列未找到")
|
|
|
+ }
|
|
|
+ row8 := str2float64(row[PhyWidthIndex])
|
|
|
+ imageMat.PhyWidth = row8
|
|
|
+
|
|
|
+ //9.厚度/高(MM)
|
|
|
+ ThicknessIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "厚度/高(MM)" {
|
|
|
+ ThicknessIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ThicknessIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("厚度/高(MM)列未找到")
|
|
|
+ }
|
|
|
+ row9 := str2float64(row[ThicknessIndex])
|
|
|
+ imageMat.Thickness = row9
|
|
|
+
|
|
|
+ //10.备注
|
|
|
+ RemarksIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "备注" {
|
|
|
+ RemarksIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if RemarksIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("备注列未找到")
|
|
|
+ }
|
|
|
+ imageMat.Remarks = row[RemarksIndex]
|
|
|
+
|
|
|
+ //11.首选供应商
|
|
|
+ MainSupplierIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "首选供应商" {
|
|
|
+ MainSupplierIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if MainSupplierIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("首选供应商列未找到")
|
|
|
+ }
|
|
|
+ row11Cate := &model.Category{}
|
|
|
+ for _, cate := range cates {
|
|
|
+ if cate.Name == "首选供应商" {
|
|
|
+ for _, c := range cate.Children {
|
|
|
+ if c.Name == row[MainSupplierIndex] {
|
|
|
+ // 记录该分类为其他自动做准备
|
|
|
+ row11Cate = c
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(row11Cate.IdStr) <= 0 {
|
|
|
+ // 记录日志跳过
|
|
|
+ result.Status = "失败"
|
|
|
+ result.ErrorMessage = "首选供应商未找到"
|
|
|
+ importResults = append(importResults, result)
|
|
|
+ return imageMat, fmt.Errorf("%s的首选供应商未找到定义", row[MainSupplierIndex])
|
|
|
+ }
|
|
|
+ //添加首先供应商
|
|
|
+ imageMat.Categories = append(imageMat.Categories, row11Cate.IdStr)
|
|
|
+
|
|
|
+ //12.默认采购单价
|
|
|
+ PriceIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "默认采购单价" {
|
|
|
+ PriceIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if PriceIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("默认采购单价列未找到")
|
|
|
+ }
|
|
|
+ row12 := str2float64(row[PriceIndex])
|
|
|
+ imageMat.Price = row12
|
|
|
+
|
|
|
+ //13.样品搜集人
|
|
|
+ FromIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "样品搜集人" {
|
|
|
+ FromIndex = i
|
|
|
+ if i+1 < len(headers) && headers[i+1] == "样品搜集人" {
|
|
|
+ FromIndex = i + 1
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if FromIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("样品搜集人列未找到")
|
|
|
+ }
|
|
|
+ staffName := row[FromIndex]
|
|
|
+ if len(staffName) > 0 {
|
|
|
+ staff := model.StaffUser{}
|
|
|
+ // 验证样品收集人是否预设
|
|
|
+ found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionStaffUser,
|
|
|
+ Query: repo.Map{"name": staffName},
|
|
|
+ }, &staff)
|
|
|
+ if found {
|
|
|
+ imageMat.From = staffName
|
|
|
+ } else {
|
|
|
+ return imageMat, fmt.Errorf("样品搜集人未配置")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //14.开发日期
|
|
|
+ DevTimeIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "开发日期" {
|
|
|
+ DevTimeIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if DevTimeIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("开发日期列未找到")
|
|
|
+ }
|
|
|
+ if len(row[DevTimeIndex]) > 0 {
|
|
|
+ layout := "2006/1/2"
|
|
|
+ // 解析字符串
|
|
|
+ devTime, _ := time.Parse(layout, row[DevTimeIndex])
|
|
|
+ imageMat.DevTime = &devTime
|
|
|
+ }
|
|
|
+ //15.出口属性
|
|
|
+ ExportPropertyIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "出口属性" {
|
|
|
+ ExportPropertyIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ExportPropertyIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("出口属性列未找到")
|
|
|
+ }
|
|
|
+ row15 := str2bool(row[ExportPropertyIndex])
|
|
|
+ imageMat.ExportProperty = row15
|
|
|
+
|
|
|
+ //16.内销属性
|
|
|
+ DomesticPropertyIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "内销属性" {
|
|
|
+ DomesticPropertyIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if DomesticPropertyIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("内销属性列未找到")
|
|
|
+ }
|
|
|
+ row16 := str2bool(row[DomesticPropertyIndex])
|
|
|
+ imageMat.DomesticProperty = row16
|
|
|
+
|
|
|
+ //17.内购属性
|
|
|
+ InpurchasePropertyIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "内购属性" {
|
|
|
+ InpurchasePropertyIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if InpurchasePropertyIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("内购属性列未找到")
|
|
|
+ }
|
|
|
+ row17 := str2bool(row[InpurchasePropertyIndex])
|
|
|
+ imageMat.InpurchaseProperty = row17
|
|
|
+
|
|
|
+ //18.委外属性
|
|
|
+ OutsourcedPropertyIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "委外属性" {
|
|
|
+ OutsourcedPropertyIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if OutsourcedPropertyIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("委外属性列未找到")
|
|
|
+ }
|
|
|
+ row18 := str2bool(row[OutsourcedPropertyIndex])
|
|
|
+ imageMat.OutsourcedProperty = row18
|
|
|
+
|
|
|
+ //19.报关助记符
|
|
|
+ MnemonicSignIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "报关助记符" {
|
|
|
+ MnemonicSignIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if MnemonicSignIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("报关助记符列未找到")
|
|
|
+ }
|
|
|
+
|
|
|
+ row15Cate := &model.Category{}
|
|
|
+ for _, cate := range cates {
|
|
|
+ if cate.Name == "报关助记符" {
|
|
|
+ for _, c := range cate.Children {
|
|
|
+ if c.Name == row[MnemonicSignIndex] {
|
|
|
+ row15Cate = c
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(row15Cate.IdStr) <= 0 {
|
|
|
+ return imageMat, fmt.Errorf("%s的报关助记符未找到定义", row[MnemonicSignIndex])
|
|
|
+ }
|
|
|
+ //添加报关助记符
|
|
|
+ imageMat.Categories = append(imageMat.Categories, row15Cate.IdStr)
|
|
|
+
|
|
|
+ //20.报关商品中文名
|
|
|
+ TaxNameCNIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "报关商品中文名" {
|
|
|
+ TaxNameCNIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if TaxNameCNIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("报关商品中文名列未找到")
|
|
|
+ }
|
|
|
+ imageMat.TaxNameCN = row[TaxNameCNIndex]
|
|
|
+
|
|
|
+ //21.录入人名称
|
|
|
+ RecordUserIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "录入人名称" {
|
|
|
+ RecordUserIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if RecordUserIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("录入人名称列未找到")
|
|
|
+ }
|
|
|
+ imageMat.RecordUser = row[RecordUserIndex]
|
|
|
+
|
|
|
+ //22.适合的市场
|
|
|
+ FitMarketIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "适合的市场" {
|
|
|
+ FitMarketIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if FitMarketIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("适合的市场列未找到")
|
|
|
+ }
|
|
|
+ imageMat.FitMarket = row[FitMarketIndex]
|
|
|
+
|
|
|
+ //23.种类分类
|
|
|
+ TypeCategoryIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "种类分类" {
|
|
|
+ TypeCategoryIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if TypeCategoryIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("种类分类列未找到")
|
|
|
+ }
|
|
|
+
|
|
|
+ //24.种类分类名称
|
|
|
+ TypeCategoryNameIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "种类分类名称" {
|
|
|
+ TypeCategoryNameIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if TypeCategoryNameIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("种类分类名称列未找到")
|
|
|
+ }
|
|
|
+
|
|
|
+ typeCate := row[TypeCategoryIndex]
|
|
|
+ typeCateName := row[TypeCategoryNameIndex]
|
|
|
+ typeCateObj := getRootCate2(rootCate, "种类分类", typeCate, typeCateName)
|
|
|
+ if typeCateObj == nil {
|
|
|
+ return imageMat, fmt.Errorf("%s / %s 的种类分类未找到定义", typeCate, typeCateName)
|
|
|
+ }
|
|
|
+ imageMat.Categories = append(imageMat.Categories, typeCateObj.IdStr)
|
|
|
+
|
|
|
+ //25.基布
|
|
|
+ BaseClothIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "基布" {
|
|
|
+ BaseClothIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if BaseClothIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("基布列未找到")
|
|
|
+ }
|
|
|
+
|
|
|
+ //26.基布名称
|
|
|
+ BaseClothNameIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "基布名称" {
|
|
|
+ BaseClothNameIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if BaseClothNameIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("基布名称列未找到")
|
|
|
+ }
|
|
|
+
|
|
|
+ baseCloth := row[BaseClothIndex]
|
|
|
+ baseClothName := row[BaseClothNameIndex]
|
|
|
+ baseClothObj := getRootCate2(rootCate, "基布", baseCloth, baseClothName)
|
|
|
+ if baseClothObj == nil {
|
|
|
+ return imageMat, fmt.Errorf("%s / %s 的基布未找到定义", baseCloth, baseClothName)
|
|
|
+ }
|
|
|
+ imageMat.Categories = append(imageMat.Categories, baseClothObj.IdStr)
|
|
|
+
|
|
|
+ //27.表面工艺
|
|
|
+ SurfaceProcessIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "表面工艺" {
|
|
|
+ SurfaceProcessIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if SurfaceProcessIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("表面工艺列未找到")
|
|
|
+ }
|
|
|
+
|
|
|
+ //28.表面工艺名称
|
|
|
+ SurfaceProcessNameIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "表面工艺名称" {
|
|
|
+ SurfaceProcessNameIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if SurfaceProcessNameIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("表面工艺名称列未找到")
|
|
|
+ }
|
|
|
+
|
|
|
+ surfaceProcess := row[SurfaceProcessIndex]
|
|
|
+ surfaceProcessName := row[SurfaceProcessNameIndex]
|
|
|
+ surfaceProcessObj := getRootCate2(rootCate, "表面工艺", surfaceProcess, surfaceProcessName)
|
|
|
+ if surfaceProcessObj == nil {
|
|
|
+ return imageMat, fmt.Errorf("%s / %s 的表面工艺未找到定义", surfaceProcess, surfaceProcessName)
|
|
|
+ }
|
|
|
+ imageMat.Categories = append(imageMat.Categories, surfaceProcessObj.IdStr)
|
|
|
+
|
|
|
+ //29.产品克重(KG)
|
|
|
+ ProductWeightIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "产品克重(KG)" {
|
|
|
+ ProductWeightIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ProductWeightIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("产品克重(KG)列未找到")
|
|
|
+ }
|
|
|
+ pw := str2float64(row[ProductWeightIndex])
|
|
|
+ imageMat.ProductWeight = pw
|
|
|
+
|
|
|
+ //30.运营周期
|
|
|
+ OperationCycleIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "运营周期" {
|
|
|
+ OperationCycleIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if OperationCycleIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("运营周期列未找到")
|
|
|
+ }
|
|
|
+ imageMat.OperationCycle = row[OperationCycleIndex]
|
|
|
+
|
|
|
+ //31.商品单位体积
|
|
|
+ ProductVolumeIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "商品单位体积" {
|
|
|
+ ProductVolumeIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ProductVolumeIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("商品单位体积列未找到")
|
|
|
+ }
|
|
|
+ pv := str2float64(row[ProductVolumeIndex])
|
|
|
+ imageMat.ProductVolume = pv
|
|
|
+
|
|
|
+ //32.商品分类代码
|
|
|
+ // ProductCategoryIndex := -1
|
|
|
+ // for i, header := range headers {
|
|
|
+ // if header == "商品分类代码" {
|
|
|
+ // ProductCategoryIndex = i
|
|
|
+ // break
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // if ProductCategoryIndex == -1 {
|
|
|
+ // return imageMat, fmt.Errorf("商品分类代码列未找到")
|
|
|
+ // }
|
|
|
+ // imageMat.ProductCategory = row[ProductCategoryIndex]
|
|
|
+
|
|
|
+ //33.产品系列
|
|
|
+ ProductSeriesIndex := -1
|
|
|
+ ProductSeriesCodeIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "产品系列" {
|
|
|
+ ProductSeriesIndex = i
|
|
|
+ if i+1 < len(headers) && headers[i+1] == "产品系列" {
|
|
|
+ ProductSeriesIndex = i + 1
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ProductSeriesIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("产品系列列未找到")
|
|
|
+ }
|
|
|
+ imageMat.ProductSeries = row[ProductSeriesIndex]
|
|
|
+
|
|
|
+ if ProductSeriesCodeIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("产品系列代码列未找到")
|
|
|
+ }
|
|
|
+ productSeriesCode := row[ProductSeriesCodeIndex]
|
|
|
+ productSeriesCodeName := row[ProductSeriesCodeIndex+1]
|
|
|
+ productSeriesCodeObj := getRootCate2(rootCate, "产品系列", productSeriesCode, productSeriesCodeName)
|
|
|
+ if productSeriesCodeObj == nil {
|
|
|
+ return imageMat, fmt.Errorf("%s / %s 的产品系列代码未找到定义", productSeriesCode, productSeriesCodeName)
|
|
|
+ }
|
|
|
+ imageMat.Categories = append(imageMat.Categories, productSeriesCodeObj.IdStr)
|
|
|
+
|
|
|
+ //35.产品用途
|
|
|
+ ProductUsageIndex := -1
|
|
|
+ ProductUsageNameIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "产品用途" {
|
|
|
+ ProductUsageIndex = i
|
|
|
+ if i+1 < len(headers) && headers[i+1] == "产品用途" {
|
|
|
+ ProductUsageNameIndex = i + 1
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ProductUsageIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("产品用途列未找到")
|
|
|
+ }
|
|
|
+ if ProductUsageNameIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("产品用途名称列未找到")
|
|
|
+ }
|
|
|
+
|
|
|
+ imageMat.ProductUsage = row[ProductUsageIndex]
|
|
|
+
|
|
|
+ ProductUsageCode := row[ProductUsageIndex]
|
|
|
+ ProductUsageCodeName := row[ProductUsageNameIndex]
|
|
|
+ ProductUsageObj := getRootCate2(rootCate, "产品用途", ProductUsageCode, ProductUsageCodeName)
|
|
|
+ if ProductUsageObj == nil {
|
|
|
+ return imageMat, fmt.Errorf("%s / %s 产品用途未找到定义", ProductUsageCode, ProductUsageCodeName)
|
|
|
+ }
|
|
|
+ imageMat.Categories = append(imageMat.Categories, ProductUsageObj.IdStr)
|
|
|
+
|
|
|
+ //36.原命名编号
|
|
|
+ OriginalNumberIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "原命名编号" {
|
|
|
+ OriginalNumberIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if OriginalNumberIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("原命名编号列未找到")
|
|
|
+ }
|
|
|
+ imageMat.OriginalNumber = row[OriginalNumberIndex]
|
|
|
+
|
|
|
+ //37.底布克重
|
|
|
+ BackingWeightIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "底布克重" {
|
|
|
+ BackingWeightIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if BackingWeightIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("底布克重列未找到")
|
|
|
+ }
|
|
|
+ bw := str2float64(row[BackingWeightIndex])
|
|
|
+ imageMat.BackingWeight = bw
|
|
|
+
|
|
|
+ //38.面布克重
|
|
|
+ SurfaceWeightIndex := -1
|
|
|
+ for i, header := range headers {
|
|
|
+ if header == "面布克重" {
|
|
|
+ SurfaceWeightIndex = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if SurfaceWeightIndex == -1 {
|
|
|
+ return imageMat, fmt.Errorf("面布克重列未找到")
|
|
|
+ }
|
|
|
+ sw := str2float64(row[SurfaceWeightIndex])
|
|
|
+ imageMat.SurfaceWeight = sw
|
|
|
+
|
|
|
+ return imageMat, nil
|
|
|
+}
|
|
|
+
|
|
|
func ExcelImportWithImages(c *gin.Context, apictx *ApiSession, file io.Reader, goodsDir, textureDir string) (interface{}, error) {
|
|
|
// 读取Excel文件
|
|
|
xlsx, err := excelize.OpenReader(file)
|
|
|
if err != nil {
|
|
|
+ fmt.Println(err.Error())
|
|
|
return nil, NewError("解析Excel文件失败")
|
|
|
}
|
|
|
defer func() {
|
|
@@ -94,6 +759,8 @@ func ExcelImportWithImages(c *gin.Context, apictx *ApiSession, file io.Reader, g
|
|
|
}
|
|
|
importResults := make([]ImportResult, 0, len(rows)-1)
|
|
|
|
|
|
+ headers := rows[0]
|
|
|
+
|
|
|
// 根据模板解析每列数据
|
|
|
for i, row := range rows {
|
|
|
// 跳过表头
|
|
@@ -107,249 +774,13 @@ func ExcelImportWithImages(c *gin.Context, apictx *ApiSession, file io.Reader, g
|
|
|
Status: "成功",
|
|
|
}
|
|
|
|
|
|
- imageMat := model.MatImage{}
|
|
|
- // 构建基础数据
|
|
|
- imageMat.CusNum = row[0]
|
|
|
- imageMat.NameCN = row[1]
|
|
|
- imageMat.NameEN = row[2]
|
|
|
- // 根据分类层级一的名字获取对应id,遍历一层获取对应数据
|
|
|
- row3Cate := &model.Category{}
|
|
|
- for _, cate := range cates {
|
|
|
- if cate.Name == "商品分类" {
|
|
|
- for _, c := range cate.Children {
|
|
|
- if c.Name == row[3] {
|
|
|
- // 记录该分类为其他自动做准备
|
|
|
- row3Cate = c
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if len(row3Cate.IdStr) <= 0 {
|
|
|
- // 记录日志跳过
|
|
|
- result.Status = "失败"
|
|
|
- result.ErrorMessage = "商品分类未找到"
|
|
|
- importResults = append(importResults, result)
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- // 获取rowCate下的二级分类
|
|
|
- var getRowCate2 = func(rowCate *model.Category, pName string, name string, cusNum string) *model.Category {
|
|
|
- for _, cate := range rowCate.Children {
|
|
|
- if cate.Name == pName {
|
|
|
- for _, c := range cate.Children {
|
|
|
- if c.Name == name {
|
|
|
- if len(cusNum) > 0 {
|
|
|
- if c.CusNum == cusNum {
|
|
|
- return c
|
|
|
- }
|
|
|
- return nil
|
|
|
- }
|
|
|
- return c
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- imageMat.Categories = append(imageMat.Categories, row3Cate.IdStr)
|
|
|
-
|
|
|
- // 跳过图片 后面统一处理
|
|
|
- var str2float64 = func(s string) float64 {
|
|
|
- f, err := strconv.ParseFloat(s, 64)
|
|
|
- if err != nil {
|
|
|
- return 0 // 或 math.NaN() 表示无效值
|
|
|
- }
|
|
|
- return f
|
|
|
- }
|
|
|
- row5 := str2float64(row[5])
|
|
|
- imageMat.PackageGrossWeight = &row5
|
|
|
- row6 := str2float64(row[6])
|
|
|
- imageMat.PackageVolume = &row6
|
|
|
- row7 := str2float64(row[7])
|
|
|
- imageMat.PhyHeight = &row7
|
|
|
- row8 := str2float64(row[8])
|
|
|
- imageMat.PhyWidth = &row8
|
|
|
- row9 := str2float64(row[9])
|
|
|
- imageMat.Thickness = &row9
|
|
|
- imageMat.Remarks = row[10]
|
|
|
- row11Cate := &model.Category{}
|
|
|
- for _, cate := range cates {
|
|
|
- if cate.Name == "首选供应商" {
|
|
|
- for _, c := range cate.Children {
|
|
|
- if c.Name == row[11] {
|
|
|
- // 记录该分类为其他自动做准备
|
|
|
- row11Cate = c
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if len(row11Cate.IdStr) <= 0 {
|
|
|
- // 记录日志跳过
|
|
|
- result.Status = "失败"
|
|
|
- result.ErrorMessage = "首选供应商未找到"
|
|
|
- importResults = append(importResults, result)
|
|
|
- continue
|
|
|
- }
|
|
|
- imageMat.Categories = append(imageMat.Categories, row11Cate.IdStr)
|
|
|
-
|
|
|
- var str2int = func(s string) int {
|
|
|
- f, err := strconv.Atoi(s)
|
|
|
- if err != nil {
|
|
|
- return 0 // 或 math.NaN() 表示无效值
|
|
|
- }
|
|
|
- return f
|
|
|
- }
|
|
|
- row12 := str2int(row[12])
|
|
|
- imageMat.Price = &row12
|
|
|
- staffName := row[14]
|
|
|
- if len(staffName) > 0 {
|
|
|
- staff := model.StaffUser{}
|
|
|
- // 验证样品收集人是否预设
|
|
|
- found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
- CollectName: repo.CollectionStaffUser,
|
|
|
- Query: repo.Map{"name": row[14]},
|
|
|
- }, &staff)
|
|
|
- if found {
|
|
|
- imageMat.From = staffName
|
|
|
- } else {
|
|
|
- // 记录日志
|
|
|
- result.Status = "失败"
|
|
|
- result.ErrorMessage = "样品收集人未预设"
|
|
|
- importResults = append(importResults, result)
|
|
|
- continue
|
|
|
- }
|
|
|
- }
|
|
|
- //开发日期
|
|
|
- if len(row[15]) > 0 {
|
|
|
- layout := "2006/1/2"
|
|
|
- // 解析字符串
|
|
|
- devTime, _ := time.Parse(layout, row[15])
|
|
|
- imageMat.DevTime = &devTime
|
|
|
- }
|
|
|
- // var str2bool = func(s string) *bool {
|
|
|
- // result := false
|
|
|
- // if s == "True" {
|
|
|
- // result = true
|
|
|
- // }
|
|
|
- // return &result
|
|
|
- // }
|
|
|
- // 出口属性
|
|
|
- imageMat.ExportProperty = BoolValue(true) //str2bool(row[16])
|
|
|
- // 内销属性
|
|
|
- imageMat.DomesticProperty = BoolValue(true) //str2bool(row[17])
|
|
|
- // 内购属性
|
|
|
- imageMat.InpurchaseProperty = BoolValue(true) //str2bool(row[18])
|
|
|
- // 委外属性
|
|
|
- imageMat.OutsourcedProperty = BoolValue(true) //str2bool(row[19])
|
|
|
-
|
|
|
- // 报关助记
|
|
|
- // 根据分类层级一的名字获取对应id,遍历一层获取对应数据
|
|
|
- row20Cate := &model.Category{}
|
|
|
- for _, cate := range cates {
|
|
|
- if cate.Name == "报关助记符" {
|
|
|
- for _, c := range cate.Children {
|
|
|
- if c.Name == row[20-4] {
|
|
|
- // 记录该分类为其他自动做准备
|
|
|
- row20Cate = c
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if len(row20Cate.IdStr) <= 0 {
|
|
|
- // 记录日志跳过
|
|
|
- result.Status = "失败"
|
|
|
- result.ErrorMessage = "报关助记符未找到"
|
|
|
- importResults = append(importResults, result)
|
|
|
- continue
|
|
|
- }
|
|
|
- imageMat.Categories = append(imageMat.Categories, row20Cate.IdStr)
|
|
|
- imageMat.TaxNameCN = row[22-4]
|
|
|
- // 录入人
|
|
|
- imageMat.UserId, _ = primitive.ObjectIDFromHex(apictx.User.ID)
|
|
|
- imageMat.FitMarket = row[24-4]
|
|
|
- // 供应商编号
|
|
|
- imageMat.SupplierID = row[25-4]
|
|
|
-
|
|
|
- // 种类分类
|
|
|
- row26Cate := getRowCate2(row3Cate, "种类分类", row[27-4], row[26-4])
|
|
|
- if row26Cate == nil {
|
|
|
- // 没有找到对应分类
|
|
|
- result.Status = "失败"
|
|
|
- result.ErrorMessage = "种类分类未找到"
|
|
|
- importResults = append(importResults, result)
|
|
|
- continue
|
|
|
- }
|
|
|
- imageMat.Categories = append(imageMat.Categories, row26Cate.IdStr)
|
|
|
-
|
|
|
- // 基布
|
|
|
- row28Cate := getRowCate2(row3Cate, "基布", row[29-4], row[28-4])
|
|
|
- if row28Cate == nil {
|
|
|
- // 没有找到对应分类
|
|
|
- result.Status = "失败"
|
|
|
- result.ErrorMessage = "基布未找到"
|
|
|
- importResults = append(importResults, result)
|
|
|
- continue
|
|
|
- }
|
|
|
- imageMat.Categories = append(imageMat.Categories, row28Cate.IdStr)
|
|
|
-
|
|
|
- // 表面工艺
|
|
|
- row30Cate := getRowCate2(row3Cate, "表面工艺", row[31-4], row[30-4])
|
|
|
- if row30Cate == nil {
|
|
|
- // 没有找到对应分类
|
|
|
- result.Status = "失败"
|
|
|
- result.ErrorMessage = "表面工艺未找到"
|
|
|
- importResults = append(importResults, result)
|
|
|
- continue
|
|
|
- }
|
|
|
- imageMat.Categories = append(imageMat.Categories, row30Cate.IdStr)
|
|
|
-
|
|
|
- // 产品克重
|
|
|
- pw := str2float64(row[32-4])
|
|
|
- imageMat.ProductWeight = &pw
|
|
|
- // 运营周期
|
|
|
- imageMat.OperationCycle = row[33-4]
|
|
|
- // 商品单位体积
|
|
|
- pv := str2float64(row[34-4])
|
|
|
- imageMat.ProductVolume = &pv
|
|
|
-
|
|
|
- // ??? 产品分类代码
|
|
|
-
|
|
|
- // 产品系列
|
|
|
- row36Cate := getRowCate2(row3Cate, "产品系列", row[37-4], row[36-4])
|
|
|
- if row36Cate == nil {
|
|
|
- // 没有找到对应分类
|
|
|
- result.Status = "失败"
|
|
|
- result.ErrorMessage = "产品系列未找到"
|
|
|
- importResults = append(importResults, result)
|
|
|
- continue
|
|
|
- }
|
|
|
- imageMat.Categories = append(imageMat.Categories, row36Cate.IdStr)
|
|
|
-
|
|
|
- // 产品用途
|
|
|
- row38Cate := getRowCate2(row3Cate, "产品用途", row[39-4], row[38-4])
|
|
|
- if row38Cate == nil {
|
|
|
- // 没有找到对应分类
|
|
|
+ imageMat, err := ParseMatObject(row, headers, cates)
|
|
|
+ if err != nil {
|
|
|
result.Status = "失败"
|
|
|
- result.ErrorMessage = "产品用途未找到"
|
|
|
+ result.ErrorMessage = err.Error()
|
|
|
importResults = append(importResults, result)
|
|
|
continue
|
|
|
}
|
|
|
- imageMat.Categories = append(imageMat.Categories, row38Cate.IdStr)
|
|
|
-
|
|
|
- // 样品编号
|
|
|
- imageMat.SampleNumber = row[40-4]
|
|
|
- // 留样册号
|
|
|
- imageMat.CatalogNumber = row[41-4]
|
|
|
- // 原命名编号
|
|
|
- imageMat.OriginalNumber = row[42-4]
|
|
|
- // 底布克重
|
|
|
- bw := str2float64(row[43-4])
|
|
|
- imageMat.BackingWeight = &bw
|
|
|
- // 面布克重
|
|
|
- sw := str2float64(row[44-4])
|
|
|
- imageMat.SurfaceWeight = &sw
|
|
|
|
|
|
imageName := row[0] // 不包含后缀 .jpg .png .jpeg
|
|
|
// 根据图片名称检查goods和texture中是否存在,图片可能是.jpg .png .jpeg后缀
|
|
@@ -422,8 +853,8 @@ func ExcelImportWithImages(c *gin.Context, apictx *ApiSession, file io.Reader, g
|
|
|
sheet := "Sheet1"
|
|
|
|
|
|
// 复制原始表头并添加状态列
|
|
|
- headers := append(rows[0], "导入状态", "失败原因", "数据库ID")
|
|
|
- for i, header := range headers {
|
|
|
+ headers2 := append(rows[0], "导入状态", "失败原因", "数据库ID")
|
|
|
+ for i, header := range headers2 {
|
|
|
colName := columnIndexToName(i)
|
|
|
resultExcel.SetCellValue(sheet, colName+"1", header)
|
|
|
}
|
|
@@ -460,7 +891,7 @@ func ExcelImportWithImages(c *gin.Context, apictx *ApiSession, file io.Reader, g
|
|
|
}
|
|
|
|
|
|
// 设置列宽
|
|
|
- for i := range headers {
|
|
|
+ for i := range headers2 {
|
|
|
colName := columnIndexToName(i)
|
|
|
resultExcel.SetColWidth(sheet, colName, colName, 15)
|
|
|
}
|