From 7be31486748d2d90f5037d63c0c0f055d343285b Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Sun, 23 Oct 2016 13:54:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=87=E5=87=86=20=20datainfo=20=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E4=B8=9A=E5=8A=A1=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/service/IPreDataInfoService.java | 2 +- .../service/impl/PreDataInfoServiceImpl.java | 108 ++++++++++++------ 2 files changed, 75 insertions(+), 35 deletions(-) diff --git a/src/com/platform/service/IPreDataInfoService.java b/src/com/platform/service/IPreDataInfoService.java index 1a09e1fe..aa7cacef 100644 --- a/src/com/platform/service/IPreDataInfoService.java +++ b/src/com/platform/service/IPreDataInfoService.java @@ -19,7 +19,7 @@ public interface IPreDataInfoService { * @return * @throws Exception */ - public Map importExcel(List paths)throws Exception; + public Map importExcel(List paths)throws Exception; /** 条件查询 * @param op diff --git a/src/com/platform/service/impl/PreDataInfoServiceImpl.java b/src/com/platform/service/impl/PreDataInfoServiceImpl.java index 47a67c88..51805322 100644 --- a/src/com/platform/service/impl/PreDataInfoServiceImpl.java +++ b/src/com/platform/service/impl/PreDataInfoServiceImpl.java @@ -11,13 +11,16 @@ import java.util.Map; import javax.annotation.Resource; +import org.apache.log4j.Logger; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.stereotype.Service; import com.platform.utils.BeanCopy; +import com.platform.utils.Configs; import com.platform.utils.Constant; import com.platform.utils.DateForm; +import com.platform.controller.ExcelController; import com.platform.dao.IRegionalismCodeDao; import com.platform.dao.ISystemCodeDao; import com.platform.dao.PreDataInfoDao; @@ -33,6 +36,8 @@ import com.platform.utils.excelUtils.ExcelOperation; @Service(value = "preDataInfoService") public class PreDataInfoServiceImpl implements IPreDataInfoService { + public static Logger log = Configs.DAILY_ROLLING_LOGGER.getLogger(PreDataInfoServiceImpl.class); + @Resource(name = "preDataInfoDao") private PreDataInfoDao preDataInfoDao; @@ -49,9 +54,10 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { } @Override - public Map importExcel(List paths) throws Exception { - Map map = new HashMap(); + public Map importExcel(List paths) throws Exception { + Map errmap = new HashMap(); List errArea = new ArrayList(); + List errFile = new ArrayList(); // 待增加的 List all2Insert = new ArrayList(); // 待修改的 @@ -65,14 +71,28 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { //excel读出的 所有新增系统操作的数据 List list4AddSystem = new ArrayList(); for (String filePath : paths) { + // file.temp 文件存在? 存在则删除 file,且读入 file.temp + //读取完后删除源文件(不管是否成功导入) + XSSFWorkbook workbook = null; + File tmpf = new File(filePath + ".temp"); File f = new File(filePath); - if (!f.exists()) { - continue; + // file.temp 文件存在 + if (tmpf.exists()) { + //删除 file + workbook = new XSSFWorkbook(tmpf); } - Date d = new Date(); - XSSFWorkbook workbook = null; + else { + if (!f.exists()) { + log.error(filePath + " 文件 不存在! "); + errFile.add(f.getName()); + continue; + } + else { + workbook = new XSSFWorkbook(f); + } + } + //Excel导入 try { - workbook = new XSSFWorkbook(f); //excel数据 --> java bean List subPreData = ExcelOperation.readExcel4Update(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, PreDataInfo.class); if (null != subPreData && subPreData.size() > 0) { @@ -91,10 +111,16 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { } } } catch (IllegalArgumentException - | IllegalAccessException | InstantiationException | InvalidFormatException | IOException e) { + | IllegalAccessException | InstantiationException e) { e.printStackTrace(); } - + } + + if (errFile.size() > 0) { + errmap.put("fileUnExist",errFile); + } + if (errArea.size() > 0) { + errmap.put("areaUnImport", errArea); } //根据 操作 类别 分类(修改, 新增行政区划,新增信息系统) if (all.size() > 0) { @@ -125,7 +151,10 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { for (PreDataInfo preDataInfo : list4AddSystem) { sysNames.add(preDataInfo.getSysName()); } - List listSystem = systemCodeDao.findByName(sysNames); + List listSystem = new ArrayList(); + if (sysNames.size() > 0) { + listSystem = systemCodeDao.findByName(sysNames); + } Map syscodeMap = new HashMap(); for (SystemEntity systemEntity : listSystem) { syscodeMap.put(systemEntity.getSystemName(), String.valueOf(systemEntity.getCode())); @@ -154,7 +183,10 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { sysNamesArea.add(preDataInfo.getSysName()); } //查询系统的code。 - List listSystem = systemCodeDao.findByName(sysNamesArea); + List listSystem = new ArrayList(); + if (sysNamesArea.size() > 0) { + listSystem = systemCodeDao.findByName(sysNamesArea); + } Map syscodeMap = new HashMap(); for (SystemEntity systemEntity : listSystem) { syscodeMap.put(systemEntity.getSystemName(), String.valueOf(systemEntity.getCode())); @@ -173,27 +205,32 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { } //在 pre_data_info表中 更新 操作为 (修改) 的系统 - List allUpdate = new ArrayList(); - // 查询系统 code - for (PreDataInfo preDataInfo : list4Update) { - sysNamesArea.add(preDataInfo.getSysName()); - } - //查询系统的code。 - List listSystem4Update = systemCodeDao.findByName(sysNamesArea); - for (SystemEntity systemEntity : listSystem4Update) { - syscodeMap.put(systemEntity.getSystemName(), String.valueOf(systemEntity.getCode())); - } - for (PreDataInfo preUpdate : list4Update) { - PreDataInfoFull UpdateFull = new PreDataInfoFull(); - BeanCopy.copyBean(preUpdate, UpdateFull); - UpdateFull.setSysCode(syscodeMap.get(UpdateFull.getSysName())); - UpdateFull.setUpdateTime(DateForm.date2StringBysecond(new Date())); - allUpdate.add(UpdateFull); - } - for (PreDataInfoFull preEntity : allUpdate) { - preDataInfoDao.update(preEntity); + if (list4Update.size() > 0) { + List allUpdate = new ArrayList(); + // 查询系统 code + for (PreDataInfo preDataInfo : list4Update) { + sysNamesArea.add(preDataInfo.getSysName()); + } + //查询系统的code。 + List listSystem4Update = new ArrayList(); + if (sysNamesArea.size() > 0) { + listSystem4Update = systemCodeDao.findByName(sysNamesArea); + } + for (SystemEntity systemEntity : listSystem4Update) { + syscodeMap.put(systemEntity.getSystemName(), String.valueOf(systemEntity.getCode())); + } + for (PreDataInfo preUpdate : list4Update) { + PreDataInfoFull UpdateFull = new PreDataInfoFull(); + BeanCopy.copyBean(preUpdate, UpdateFull); + UpdateFull.setSysCode(syscodeMap.get(UpdateFull.getSysName())); + UpdateFull.setUpdateTime(DateForm.date2StringBysecond(new Date())); + allUpdate.add(UpdateFull); + } + for (PreDataInfoFull preEntity : allUpdate) { + preDataInfoDao.update(preEntity); + } } - return map; + return errmap; } /** 给system_info增加新系统 @@ -221,7 +258,8 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { } ////数据库 不存在的 系统-(添加新系统) if (sysNames.size() > 0) { - insertNum = systemCodeDao.insertBatch(sysNames); + systemCodeDao.insertBatch(sysNames); + insertNum = sysNames.size(); } } return insertNum; @@ -254,9 +292,11 @@ public class PreDataInfoServiceImpl implements IPreDataInfoService { for ( String key : areaMap.keySet()) { insert2Regionalism.add(areaMap.get(key)); } - insertNum = regionalismCodeDao.insertBatch(insert2Regionalism); + if (insert2Regionalism.size() > 0) { + regionalismCodeDao.insertBatch(insert2Regionalism); + insertNum = insert2Regionalism.size(); + } } - return insertNum; }