获取如图所示的Excel数据
1.设计思路
1.1. 得到每一个单元格的值
List rowArrayList = new ArrayList;
List<ArrayList> allArrayList = new ArrayList<ArrayList>;
遍历Excel表格的所有行,第0行用来判断一共有多少列。将所有行存放到allArrayList集合中。
遍历Excel表格除0行外的所有行,得到每行的每一个单元格。
得到的单元格可能是合并的单元格。若要得到合并单元格的值,首先得到所有合并的单元格进行遍历,判断此单元格是否在合并单元格,然后得到该合并单元格的数据。
依此类推,得到每一个单元格的数据。
每一行生成一个rowArrayList集合,所有行组成一个allArrayList集合。
1.2. 对allArrayList集合进行遍历存储
Map<String,String> mapExcel = new HashMap<String,String>;
对allArrayList集合进行遍历。
遍历每个rowArrayList的偶数元素得到指标编号。
将得到的指标编号到mapExcel集合中看看能不能找到对应的指标名称,若是找到对应的指标名称,说明在mapExcel中已经存在且已经存到数据库中了,就不进行存储了。若是找不到就是要存储的数据。存储到mapExcel集合以及数据库中。
存储的时候也是要分是否为第0列的数据,若是第0列,parentId为-1。若不是第0列,parentId为父节点主键Id。
2. 代码实现
2.1.将前端上传的Excel文件拷贝到服务器上
这是controller层,主要是将前端传来的文件复制到服务器上,并且将界面返回到Excel上传模板的模态框上。
2.1.1. 得到上传文件的文件名,后边拼接时间跟“.xlsx”;
String tempFileName = uploadExcel.getOriginalFilename()+DateBuilder.getTimeFormatToSecCompact()+".xlsx";
2.1.2. 得到上传文件的路径;
String realPath = req.getSession().getServletContext().getRealPath("/");
2.1.3. 编辑要存放到服务器上的Excel文件夹的路径;
2.1.3.1. 判断Excel文件夹是否存在,不存在就新建Excel文件夹;
String excelFolderPath = realPath + "/excelfolder"; // 存放Excel模板的文件夹
File excelFolder = new File(excelFolderPath);
// 判断excelFolder文件夹是否存在,如果不存在就新建文件
if(!excelFolder.exists()) {
excelFolder.mkdirs();
}
2.1.4. 得到服务器上文件的全路径+文件名;
String finalFullFileName = realPath + "/excelfolder/"+tempFileName;
2.1.5. 将文件复制到服务器的响应的文件夹下
File srcFile = new File(finalFullFileName);
is = uploadExcel.getInputStream();
FileCopyUtils.copy(FileCopyUtils.copyToByteArray(is), srcFile);
2.1.6. 整体代码如下:
/**
* excel模板上传解析
* @param uploadExcel 上传的Excel文件
* @param req
*/
@RequestMapping(value = "/uploadTemplate", method = RequestMethod.POST)
public ModelAndView uploadTemplate(@RequestParam("uploadExcel") MultipartFile uploadExcel,HttpServletRequest req) {
//首先进行文件拷贝
Map<String, Object> map = new HashMap<>();
InputStream is = null;
try {
//打开模板
//获取到模板的相对路径
String tempFileName = uploadExcel.getOriginalFilename()+DateBuilder.getTimeFormatToSecCompact()+".xlsx";
String realPath = req.getSession().getServletContext().getRealPath("/");
String excelFolderPath = realPath + "/excelfolder"; // 存放Excel模板的文件夹
String finalFullFileName = realPath + "/excelfolder/"+tempFileName;
File excelFolder = new File(excelFolderPath);
// 判断excelFolder文件夹是否存在,如果不存在就新建文件
if(!excelFolder.exists()) {
excelFolder.mkdirs();
}
File srcFile = new File(finalFullFileName);
is = uploadExcel.getInputStream();
FileCopyUtils.copy(FileCopyUtils.copyToByteArray(is), srcFile);
map = iotEvaluationIndicatorTemplateService.parseExcel(finalFullFileName,indicatorClassIdForExcel);
map.put("indicatorClassId",indicatorClassIdForExcel);
} catch (FileNotFoundException e) {
log.error("文件未找到!",e);
map.put("messageDicHerbal", "数据上传失败!文件未找到!"+e.getMessage());
} catch (IOException e) {
log.error("",e);
map.put("messageDicHerbal", "数据上传失败!IO读写错误!"+e.getMessage());
} catch (Exception e) {
log.error("",e);
map.put("messageDicHerbal", "数据上传失败!未知异常"+e.getMessage());
} finally{
try{
is.close();
}catch(IOException e){
}
}
return new ModelAndView(TO_EXCEL_MODEL_PAGE, map);
}
2.2. 对服务器上的文件进行遍历
主要是为了生成allArrayList<rowArrayList<String,String>>集合。
跳转到service层进行事务的处理,参数为服务器上文件的根路径。
2.2.1. 对合并单元格的值的处理的工具类
package com.common.tool.excel;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
//获得Excel单元格数据的工具类
public class PoiMergedCell {
/**
* 判断单元格是否为合并单元格,是的话则将单元格的值返回
* @param cell 需要判断的单元格
* @param sheet sheet 需要判断的工作表
* @return
*/
public String getCellValue(Cell cell,Sheet sheet)
throws Exception{
int firstC = 0;
int lastC = 0;
int firstR = 0;
int lastR = 0;
String cellValue = null;
List<CellRangeAddress> craList = sheet.getMergedRegions();
// 判断此单元格是否为合并单元格
if(isMergedRegion(sheet,cell)) {
// 如果是合并单元格,就将合并的单元格进行遍历,得到此单元格的值
for(CellRangeAddress ca:craList){
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC = ca.getFirstColumn();
lastC = ca.getLastColumn();
firstR = ca.getFirstRow();
lastR = ca.getLastRow();
if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR){
if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC){
Row fRow = sheet.getRow(firstR);
Cell fCell = fRow.getCell(firstC);
cellValue = getCellValueBasic(fCell);
break;
}
}
}
}else {
// 如果不是合并单元格,就直接取此单元格的值
cellValue = getCellValueBasic(cell);
}
return cellValue;
}
/**
* 获取单元格的值
* @param cell
* @return
*/
public String getCellValueBasic(Cell cell){
if(cell == null) return "";
if(cell.getCellTypeEnum() == CellType.STRING){
return cell.getStringCellValue();
}else if(cell.getCellTypeEnum() == CellType.BOOLEAN){
return String.valueOf(cell.getBooleanCellValue());
}else if(cell.getCellTypeEnum() == CellType.FORMULA){
return cell.getCellFormula() ;
}else if(cell.getCellTypeEnum() == CellType.NUMERIC){
return String.valueOf(cell.getNumericCellValue());
}
return "";
}
/**
* 判断此单元格是否为合并单元格
* @param sheet 工作表
* @param cell 单元格
* @return
*/
public boolean isMergedRegion(Sheet sheet,Cell cell) {
// 得到合并单元格的数量
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
// 得到此合并单元格的范围
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
if(cell.getRowIndex() >= firstRow && cell.getRowIndex() <= lastRow){
if(cell.getColumnIndex() >= firstColumn && cell.getColumnIndex() <= lastColumn){
return true;
}
}
}
return false;
}
}
2.2.2. 遍历Excel文件,生成allArrayList集合
此方法主要是用来对Excel文件进行遍历,并生成allArrayList<rowArrayList<String,String>>集合。其中会用到2.2.1的工具类。
创建工作簿
得到单元格
得到每行的值
得到每个单元格的值
/**
* 对Excel进行遍历,得到一个ArrayList<ArrayList<String>>的集合
*
* @param fileName Excel的路径
* @return
* @throws Exception
*/
public ArrayList<ArrayList<String>> traverseExcel(String fileName) throws Exception {
//生成获得Excel单元格数据的工具类对象
PoiMergedCell poiMergedCell = new PoiMergedCell();
Workbook wb = null;
File f = new File(fileName);
ArrayList<ArrayList<String>> allArrayList = new ArrayList<>(); //用来存放所有的数据
try {
FileInputStream fis = new FileInputStream(f);
// 生成工作簿
wb = WorkbookFactory.create(fis);
Sheet sheet = wb.getSheetAt(0); // 得到第一个工作表
int rowNum = sheet.getLastRowNum(); // 获取总共有多少行,然后从第一行开始读取内容
Row row = sheet.getRow(0); // 得到第一行,用来判断一共有多少列
int colNum = row.getLastCellNum();// 获取总共有多少列
ArrayList<String> rowArrayList; //用来存放每行的数据
for(int i = 1;i <= rowNum;i++) {
rowArrayList = new ArrayList<String>();
row = sheet.getRow(i);
for(int j = 0; j < colNum; j++) {
Cell cell = row.getCell(j);
// 判断cell的值是否为空
if(cell == null) {
break;
}
String value = poiMergedCell.getCellValue(cell, sheet);
if(value != null && !"".equals(value)) {
rowArrayList.add(value);
}else {
break;
}
}
allArrayList.add(rowArrayList);
}
} catch (EncryptedDocumentException e) {
logger.error("",e);
} catch (InvalidFormatException e) {
logger.error("",e);
} catch (IOException e) {
logger.error("",e);
} finally {
try {
wb.close();
}catch(Exception e) {
logger.error("",e);
}
}
return allArrayList;
}
}
2.3. 对得到的allArrayList集合进行分析存储
1、创建一个HashMap<String,Object> excelMap = new HashMap<>();集合。
2、调用自己编写的traverseExcel方法,对Excel文件进行遍历,生成allArrayList集合。
3、对allArrayList集合进行遍历,嵌套遍历rowArrayList集合,得到每个单元格的值。
4、判断excelMap集合中是否存在通过指标编号得到指标名称与下一个单元格相同值的键值对。如果有,说明此指标模板存储过;如果没有,说明没有存储过,先将此指标模板采访到excelMap集合中,然后存放到数据库中。
/**
* 对导入的Excel模板数据进行分析并存储
*
* @param fileName Excel文件的路径
* @param indicatorClassId 指标模板类的主键ID
*/
public HashMap<String,Object> parseExcel(String filePath,String indicatorClassId) {
String fileName = filePath; // Excel文件的根路径
Integer allCount = 0; // Excel中所有指标的个数
Integer insertCount = 0; // 新增数据库中指标的个数
Integer updateCount = 0; // 修改数据库中指标的个数
Integer successCount = 0; // 添加成功的指标的个数
Integer failCount = 0; // 添加失败的指标的个数
String errorName = ""; // 用来存放因名称问题而导入失败的字符串
HashMap<String,Object> mapCount = new HashMap<>(); // 创建一个mapCount集合用来存放数据的存储情况
try{
ArrayList<ArrayList<String>> allArrayList = traverseExcel(fileName); //遍历Excel并将结果存放到allArrayList集合中
Map<String,String> mapExcel = new HashMap<>(); // 创建一个mapExcel集合用来存放allArrayList集合中的数据
IotEvaluationIndicatorTemplate iotEvaluationIndicatorTemplate = null;
// 评估指标类对象
IotIndicatorClass iotIndicatorClass = iotIndicatorClassMapper.selectByPrimaryKey(indicatorClassId);
for(ArrayList<String> ral : allArrayList) {
// 如果每行的单元格数不是偶数列,说明Excel模板有错误
if(ral.size() % 2 != 0) {
mapCount.put("excelError", "每级指标由编码和名称两列组成");
return mapCount;
}
for(int j = 0; j < ral.size(); j+=2) {
String code = ral.get(j);
// 评估指标模板对象
iotEvaluationIndicatorTemplate = new IotEvaluationIndicatorTemplate();
// 判断以指标编号在mapExcel集合中获取的指标名称是否与下一个单元格的名称相同
// 若相同说明是同一个指标,不同则说明不是同一个指标
if(mapExcel.get(code) != ral.get(j+1)) {
allCount++;
// 没有的话就添加到mapExcel集合中
mapExcel.put(code, ral.get(j+1));
// 根据指标编号从数据库中取出相应的指标
IotEvaluationIndicatorTemplate beanByCode = getByCode(indicatorClassId,code);
// 判断此指标编号在数据库中是否存在
if(beanByCode != null) {
// 判断是否为第0列的单元格
if(j == 0) {
iotEvaluationIndicatorTemplate.setIeitId(getByCode(indicatorClassId,code).getIeitId()); // 将原来的主键封装到对象中
iotEvaluationIndicatorTemplate.setIotIndicatorClass(iotIndicatorClass); // 将评估指标分类对象封装到对象中
iotEvaluationIndicatorTemplate.setIeitName(ral.get(j+1));// 将指标名称封装到对象中
iotEvaluationIndicatorTemplate.setIeitCode(code); // 将指标编号封装到对象中
iotEvaluationIndicatorTemplate.setIeitParentId(getByCode(indicatorClassId,"root").getIeitId()); // 将父节点封装到对象中
iotEvaluationIndicatorTemplate.setIeitLevel(iotEvaluationIndicatorTemplateMapper.selectByPrimaryKey(iotEvaluationIndicatorTemplate.getIeitParentId()).getIeitLevel() + 1); // 级别
// 取出当级的最大序号
Integer maxCount = iotEvaluationIndicatorTemplateMapper.selectMaxOrder(iotEvaluationIndicatorTemplate.getIeitParentId());
if (maxCount == null) {
maxCount = 0;
}
int order = maxCount + 1;
iotEvaluationIndicatorTemplate.setIeitLevelOrder(order); // 级内排序
iotEvaluationIndicatorTemplate.setIeitFullOrder(iotEvaluationIndicatorTemplateMapper.selectByPrimaryKey(iotEvaluationIndicatorTemplate.getIeitParentId()).getIeitFullOrder() + "_" + StrUtil.zeroFill4(order+""));// 排序号全路径
iotEvaluationIndicatorTemplate.setIeitFullId(iotEvaluationIndicatorTemplate.getIeitParentId() + "_" + iotEvaluationIndicatorTemplate.getIeitId());// ID全称
iotEvaluationIndicatorTemplate.setIeitFullName(iotIndicatorClass.getIicName() + "_" + iotEvaluationIndicatorTemplate.getIeitName()); // 名称全称
// 判断是否有下级节点
List<IotEvaluationIndicatorTemplate> childList = iotEvaluationIndicatorTemplateMapper.selectListByParentId(iotEvaluationIndicatorTemplate.getIeitId());
// 判断j后边是否还有数据
if(j + 2 < (ral.size()-1) || (childList != null && childList.size() > 0)) {
// 还有数据,将是否为叶子节点设为0
iotEvaluationIndicatorTemplate.setIeitIsLeaf(0);
}else {
// 没有数据,将是否为叶子节点设为1
iotEvaluationIndicatorTemplate.setIeitIsLeaf(1);
}
iotEvaluationIndicatorTemplate.setIeitModifier(CurrentUserMsg.getUserName()); // 修改人
iotEvaluationIndicatorTemplate.setIeitModifyTime(DateBuilder.getTimeToSec()); // 修改时间
}else {
iotEvaluationIndicatorTemplate.setIeitId(getByCode(indicatorClassId,code).getIeitId()); // 将原来的主键封装到对象中
iotEvaluationIndicatorTemplate.setIotIndicatorClass(iotIndicatorClass); // 将评估指标分类对象封装到对象中
iotEvaluationIndicatorTemplate.setIeitName(ral.get(j+1));// 将指标名称封装到对象中
iotEvaluationIndicatorTemplate.setIeitCode(code); // 将指标编号封装到对象中
iotEvaluationIndicatorTemplate.setIeitParentId(getByCode(indicatorClassId,ral.get(j-2)).getIeitId()); // 得到上一级的主键作为本级的父节点
iotEvaluationIndicatorTemplate.setIeitLevel(iotEvaluationIndicatorTemplateMapper.selectByPrimaryKey(iotEvaluationIndicatorTemplate.getIeitParentId()).getIeitLevel() + 1); // 级别
// 取出当级的最大序号
Integer maxCount = iotEvaluationIndicatorTemplateMapper.selectMaxOrder(iotEvaluationIndicatorTemplate.getIeitParentId());
if (maxCount == null) {
maxCount = 0;
}
int order = maxCount + 1;
iotEvaluationIndicatorTemplate.setIeitLevelOrder(order); // 级内排序
iotEvaluationIndicatorTemplate.setIeitFullOrder(iotEvaluationIndicatorTemplateMapper.selectByPrimaryKey(iotEvaluationIndicatorTemplate.getIeitParentId()).getIeitFullOrder() + "_" + StrUtil.zeroFill4(order+""));// 排序号全路径
iotEvaluationIndicatorTemplate.setIeitFullId(getByCode(indicatorClassId,ral.get(j-2)).getIeitFullId() + "_" + iotEvaluationIndicatorTemplate.getIeitId());// ID全称
iotEvaluationIndicatorTemplate.setIeitFullName(getByCode(indicatorClassId,ral.get(j-2)).getIeitFullName() + "_" + iotEvaluationIndicatorTemplate.getIeitName()); // 名称全称
// 判断是否有下级节点
List<IotEvaluationIndicatorTemplate> childList = iotEvaluationIndicatorTemplateMapper.selectListByParentId(iotEvaluationIndicatorTemplate.getIeitId());
// 判断j后边是否还有数据
if(j + 2 < (ral.size()-1) || (childList != null && childList.size() > 0)) {
// 还有数据,将是否为叶子节点设为0
iotEvaluationIndicatorTemplate.setIeitIsLeaf(0);
}else {
// 没有数据,将是否为叶子节点设为1
iotEvaluationIndicatorTemplate.setIeitIsLeaf(1);
}
iotEvaluationIndicatorTemplate.setIeitModifier(CurrentUserMsg.getUserName()); // 修改人
iotEvaluationIndicatorTemplate.setIeitModifyTime(DateBuilder.getTimeToSec()); // 修改时间
}
int result = 0;
// 根据指标名称从数据库中取出相应的指标
IotEvaluationIndicatorTemplate beanByName = getByName(iotEvaluationIndicatorTemplate.getIeitParentId(),iotEvaluationIndicatorTemplate.getIeitName());
// 判断在同一个父节点下,是否存在名称相同的指标
// 如果没有就直接修改
// 如果有就用通过编号取出的指标与通过名称取出的指标进行比较
if(beanByName == null) {
result = iotEvaluationIndicatorTemplateMapper.updateByPrimaryKeySelective(iotEvaluationIndicatorTemplate);
}else{
// 通过编号取出的指标与通过名称取出的指标的名称与编号来判断是否为取出的同一个指标
// 若是同一个指标就进行更新
// 若不是同一个指标就说明是重名的
if(beanByName.getIeitCode() != null &&
beanByCode.getIeitName() != null &&
beanByName.getIeitName().equals(beanByCode.getIeitName()) &&
beanByName.getIeitCode().equals(beanByCode.getIeitCode())) {
result = iotEvaluationIndicatorTemplateMapper.updateByPrimaryKeySelective(iotEvaluationIndicatorTemplate);
}else {
errorName = errorName + iotEvaluationIndicatorTemplate.getIeitCode() + "-" + iotEvaluationIndicatorTemplate.getIeitName() + ",";
}
}
// 将封装的指标模板对象存储到数据库中,并判断是否成功,若成功就将成功和修改个数加1,失败就将失败个数加1
if(result == 1) {
updateCount++;
successCount++;
}else {
failCount++;
}
}else{
if(j == 0) {
iotEvaluationIndicatorTemplate.setIeitId(IDTools.getCommonId()); //随机生成一个32位的字符串封装到对象中
iotEvaluationIndicatorTemplate.setIotIndicatorClass(iotIndicatorClass); // 将评估指标分类对象封装到对象中
iotEvaluationIndicatorTemplate.setIeitName(ral.get(j+1));// 将指标名称封装到对象中
iotEvaluationIndicatorTemplate.setIeitCode(code); // 将指标编号封装到对象中
iotEvaluationIndicatorTemplate.setIeitParentId(getByCode(indicatorClassId,"root").getIeitId()); // 将父节点封装到对象中
iotEvaluationIndicatorTemplate.setIeitLevel(iotEvaluationIndicatorTemplateMapper.selectByPrimaryKey(iotEvaluationIndicatorTemplate.getIeitParentId()).getIeitLevel() + 1); // 级别
// 取出当级的最大序号
Integer maxCount = iotEvaluationIndicatorTemplateMapper.selectMaxOrder(iotEvaluationIndicatorTemplate.getIeitParentId());
if (maxCount == null) {
maxCount = 0;
}
int order = maxCount + 1;
iotEvaluationIndicatorTemplate.setIeitLevelOrder(order); // 级内排序
iotEvaluationIndicatorTemplate.setIeitFullOrder(iotEvaluationIndicatorTemplateMapper.selectByPrimaryKey(iotEvaluationIndicatorTemplate.getIeitParentId()).getIeitFullOrder() + "_" + StrUtil.zeroFill4(order+""));// 排序号全路径
iotEvaluationIndicatorTemplate.setIeitFullId(iotEvaluationIndicatorTemplate.getIeitParentId() + "_" + iotEvaluationIndicatorTemplate.getIeitId());// ID全称
iotEvaluationIndicatorTemplate.setIeitFullName(iotIndicatorClass.getIicName() + "_" + iotEvaluationIndicatorTemplate.getIeitName()); // 名称全称
// 判断j单元格后边是否还有数据
if(j + 2 < (ral.size()-1)) {
// 还有数据,将是否为叶子节点设为0
iotEvaluationIndicatorTemplate.setIeitIsLeaf(0);
}else {
// 没有数据,将是否为叶子节点设为1
iotEvaluationIndicatorTemplate.setIeitIsLeaf(1);
}
iotEvaluationIndicatorTemplate.setIeitCreator(CurrentUserMsg.getUserName()); // 创建人
iotEvaluationIndicatorTemplate.setIeitCreateDate(DateBuilder.getTimeToSec()); // 创建时间
}else {
iotEvaluationIndicatorTemplate.setIeitId(IDTools.getCommonId()); //随机生成一个32位的字符串封装到对象中
iotEvaluationIndicatorTemplate.setIotIndicatorClass(iotIndicatorClass); // 将评估指标分类对象封装到对象中
iotEvaluationIndicatorTemplate.setIeitName(ral.get(j+1));// 将指标名称封装到对象中
iotEvaluationIndicatorTemplate.setIeitCode(code); // 将指标编号封装到对象中
iotEvaluationIndicatorTemplate.setIeitParentId(getByCode(indicatorClassId,ral.get(j-2)).getIeitId()); // 得到上一级的主键作为本级的父节点
iotEvaluationIndicatorTemplate.setIeitLevel(iotEvaluationIndicatorTemplateMapper.selectByPrimaryKey(iotEvaluationIndicatorTemplate.getIeitParentId()).getIeitLevel() + 1); // 级别
// 取出当级的最大序号
Integer maxCount = iotEvaluationIndicatorTemplateMapper.selectMaxOrder(iotEvaluationIndicatorTemplate.getIeitParentId());
if (maxCount == null) {
maxCount = 0;
}
int order = maxCount + 1;
iotEvaluationIndicatorTemplate.setIeitLevelOrder(order); // 级内排序
iotEvaluationIndicatorTemplate.setIeitFullOrder(iotEvaluationIndicatorTemplateMapper.selectByPrimaryKey(iotEvaluationIndicatorTemplate.getIeitParentId()).getIeitFullOrder() + "_" + StrUtil.zeroFill4(order+""));// 排序号全路径
iotEvaluationIndicatorTemplate.setIeitFullId(getByCode(indicatorClassId,ral.get(j-2)).getIeitFullId() + "_" + iotEvaluationIndicatorTemplate.getIeitId());// ID全称
iotEvaluationIndicatorTemplate.setIeitFullName(getByCode(indicatorClassId,ral.get(j-2)).getIeitFullName() + "_" + iotEvaluationIndicatorTemplate.getIeitName()); // 名称全称
// 判断j后边是否还有数据
if(j + 2 < (ral.size()-1)) {
// 还有数据,将是否为叶子节点设为0
iotEvaluationIndicatorTemplate.setIeitIsLeaf(0);
}else {
// 没有数据,将是否为叶子节点设为1
iotEvaluationIndicatorTemplate.setIeitIsLeaf(1);
}
iotEvaluationIndicatorTemplate.setIeitCreator(CurrentUserMsg.getUserName()); // 创建人
iotEvaluationIndicatorTemplate.setIeitCreateDate(DateBuilder.getTimeToSec()); // 创建时间
}
int result = 0;
// 判断在同一个父节点下,是否存在名称相同的指标
if(getByName(iotEvaluationIndicatorTemplate.getIeitParentId(),iotEvaluationIndicatorTemplate.getIeitName()) == null) {
result = iotEvaluationIndicatorTemplateMapper.insertSelective(iotEvaluationIndicatorTemplate);
}else {
errorName = errorName + iotEvaluationIndicatorTemplate.getIeitCode() + "-" + iotEvaluationIndicatorTemplate.getIeitName() + ",";
}
// 将封装的指标模板对象存储到数据库中,并判断是否成功,若成功就将成功和新增个数加1,失败就将失败个数加1
if(result == 1) {
insertCount++;
successCount++;
}else {
failCount++;
}
}
}
}
}
// 去掉errorName最后边的","
if(errorName != null && !"".equals(errorName)){
errorName = errorName.substring(0, errorName.length()-1);
}
mapCount.put("allCount", allCount);
mapCount.put("insertCount", insertCount);
mapCount.put("updateCount", updateCount);
mapCount.put("successCount", successCount);
mapCount.put("failCount", failCount);
mapCount.put("errorName", errorName);
}catch (FileNotFoundException e) {
logger.error("文件未找到!",e);
} catch (IOException e) {
logger.error("",e);
} catch (Exception e) {
logger.error("",e);
}
return mapCount;
}