|
|
@ -9,6 +9,8 @@ import ( |
|
|
|
"LAPP_ACURA_MOM_BACKEND/grmi" |
|
|
|
meta "LAPP_ACURA_MOM_BACKEND/meta/me" |
|
|
|
model "LAPP_ACURA_MOM_BACKEND/models/me" |
|
|
|
"github.com/360EntSecGroup-Skylar/excelize/v2" |
|
|
|
"strconv" |
|
|
|
) |
|
|
|
|
|
|
|
/****************************************************************************** |
|
|
@ -326,3 +328,132 @@ func (impl *ProductFamilyRelateServiceImplement) Update(user *global.User, entit |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// ImportFromExcel 从excel导入数据
|
|
|
|
func (impl *ProductFamilyRelateServiceImplement) ImportFromExcel(user *global.User, filepath string) error { |
|
|
|
grmi.Log(user, "/services/me/implments/ProductFamilyRelate.service.impl.go", "ImportFromExcel", "Copy复制Bom") |
|
|
|
|
|
|
|
engine := db.Eloquent.Master() |
|
|
|
session := engine.NewSession() |
|
|
|
defer session.Close() |
|
|
|
daoProject := dal.NewProjectDAO(session, user.PlantNr, user.UserId) |
|
|
|
daoProductFamily := dal.NewProductFamilyDAO(session, user.PlantNr, user.UserId) |
|
|
|
daoProduct := dal.NewProductDAO(session, user.PlantNr, user.UserId) |
|
|
|
daoProductFamilyRelate := dal.NewProductFamilyRelateDAO(session, user.PlantNr, user.UserId) |
|
|
|
file, err := excelize.OpenFile(filepath) |
|
|
|
if err != nil { |
|
|
|
return grmi.NewBusinessError("读取文件失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
rowsIterator, err := file.Rows("Sheet1") |
|
|
|
if err != nil { |
|
|
|
return grmi.NewBusinessError("读取Sheet1页失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
rowIndex := 0 |
|
|
|
projectMap := make(map[string]interface{}) |
|
|
|
productMap := make(map[string]string) |
|
|
|
productFamilyMap := make(map[string]interface{}) |
|
|
|
productFamilyRelateMap := make(map[string][]model.ProductFamilyRelate) |
|
|
|
for rowsIterator.Next() { |
|
|
|
rowIndex++ |
|
|
|
if rowIndex == 1 { |
|
|
|
_, err = rowsIterator.Columns() |
|
|
|
if err != nil { |
|
|
|
return grmi.NewBusinessError("去读excel行数据失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
continue |
|
|
|
} |
|
|
|
row, err := rowsIterator.Columns() |
|
|
|
if err != nil { |
|
|
|
return grmi.NewBusinessError("去读excel行数据失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
if len(row) != 4 { |
|
|
|
return grmi.NewBusinessError("excel数据不合规, 行数:" + strconv.Itoa(rowIndex)) |
|
|
|
} |
|
|
|
_, exist := projectMap[row[1]] |
|
|
|
if !exist { |
|
|
|
project, err := daoProject.SelectOne(row[1]) |
|
|
|
if err != nil { |
|
|
|
return grmi.NewBusinessError("查询车型项目数据失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
if project == nil { |
|
|
|
return grmi.NewBusinessError("车型项目不存在, 车型项目ID:" + row[1]) |
|
|
|
} |
|
|
|
projectMap[row[1]] = nil |
|
|
|
} |
|
|
|
desc, exist := productMap[row[2]] |
|
|
|
if !exist { |
|
|
|
product, err := daoProduct.SelectOne(row[2]) |
|
|
|
if err != nil { |
|
|
|
return grmi.NewBusinessError("查询产品总成数据失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
if product == nil { |
|
|
|
return grmi.NewBusinessError("产品总成不存在, 总成ID:" + row[2]) |
|
|
|
} |
|
|
|
productMap[row[2]] = product.Descr |
|
|
|
desc = product.Descr |
|
|
|
} |
|
|
|
_, exist = productFamilyMap[row[0]] |
|
|
|
if !exist { |
|
|
|
productFamily, err := daoProductFamily.SelectOne(row[0]) |
|
|
|
if err != nil { |
|
|
|
return grmi.NewBusinessError("查询派生数据失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
if productFamily == nil { |
|
|
|
return grmi.NewBusinessError("派生数据不存在, 派生ID:" + row[0]) |
|
|
|
} |
|
|
|
productFamilyMap[row[0]] = nil |
|
|
|
} |
|
|
|
count, err := strconv.Atoi(row[3]) |
|
|
|
if err != nil { |
|
|
|
return grmi.NewBusinessError("excel数据不合规, 行数:" + strconv.Itoa(rowIndex)) |
|
|
|
} |
|
|
|
relate := model.ProductFamilyRelate{ |
|
|
|
ProductFamilyId: row[0], |
|
|
|
ProjectId: row[1], |
|
|
|
ProductId: row[2], |
|
|
|
ProductDescr: desc, |
|
|
|
Count: count, |
|
|
|
} |
|
|
|
_, exist = productFamilyRelateMap[row[0]] |
|
|
|
if !exist { |
|
|
|
productFamilyRelateMap[row[0]] = make([]model.ProductFamilyRelate, 0) |
|
|
|
} |
|
|
|
productFamilyRelateMap[row[0]] = append(productFamilyRelateMap[row[0]], relate) |
|
|
|
} |
|
|
|
if err = session.Begin(); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
tempProductFamilyRelateLi := make([]model.ProductFamilyRelate, 0, 20) |
|
|
|
for productFamilyId, relateLi := range productFamilyRelateMap { |
|
|
|
originRelateLi, err := daoProductFamilyRelate.Select([]grmi.Predicate{meta.ProductFamilyRelate_ProductFamilyId.NewPredicate(grmi.Equal, productFamilyId)}, nil) |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return grmi.NewBusinessError("查询已存在关联关系失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
if len(originRelateLi) != 0 { |
|
|
|
_ = session.Rollback() |
|
|
|
return grmi.NewBusinessError("派生已存在关联关系, 派生ID:" + productFamilyId) |
|
|
|
} |
|
|
|
for index, relate := range relateLi { |
|
|
|
if index != 0 && index % 20 == 0 { |
|
|
|
err = daoProductFamilyRelate.Insert(&tempProductFamilyRelateLi) |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return grmi.NewBusinessError("写入关联关系失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
tempProductFamilyRelateLi = make([]model.ProductFamilyRelate, 0, 20) |
|
|
|
} |
|
|
|
relate.Pos = index + 1 |
|
|
|
tempProductFamilyRelateLi = append(tempProductFamilyRelateLi, relate) |
|
|
|
} |
|
|
|
if len(tempProductFamilyRelateLi) != 0 { |
|
|
|
err = daoProductFamilyRelate.Insert(&tempProductFamilyRelateLi) |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return grmi.NewBusinessError("写入关联关系失败, error:" + err.Error()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
_ = session.Commit() |
|
|
|
return nil |
|
|
|
} |