package com.platform.service.impl; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import com.platform.dao.IRegionalismCodeDao; import com.platform.dao.ISystemCodeDao; import com.platform.dao.PreDataInfoDao; import com.platform.entities.PagerOptions; import com.platform.entities.PreDataInfo; import com.platform.entities.RegionalismEntity; import com.platform.entities.SystemEntity; import com.platform.service.IPreDataInfoService; import com.platform.utils.BeanCopy; import com.platform.utils.Configs; import com.platform.utils.Constant; import com.platform.utils.DateForm; 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; @Resource(name = "systemCodeDao") private ISystemCodeDao systemCodeDao; @Resource(name = "regionalismCodeDao") private IRegionalismCodeDao regionalismCodeDao; @Override public List findAll() throws Exception { List result = preDataInfoDao.findAll(); Map sqlFilePathsStandard = new HashMap(); sqlFilePathsStandard = getAllFile(sqlFilePathsStandard, Configs.SQL_SCRIPT_PATH_STANDARD); setScript(result, sqlFilePathsStandard); for (PreDataInfo preDataInfo : result) { preDataInfo.reSetWorkRange(new ArrayList()); preDataInfo.setSqlList(new ArrayList()); } return result; } @Override public List findByParam(PagerOptions op) { List result = preDataInfoDao.getLimitedDataInfoEntities(op); return result; } @Override public Map importExcel(List paths) throws Exception { Map errmap = new HashMap(); List errArea = new ArrayList(); List errFile = new ArrayList(); // 待增加的 List all2Insert = new ArrayList(); // 待修改的 List all2Update = new ArrayList(); //excel读出的 所有 List all = new ArrayList(); //excel读出的 所有修改操作的数据 List list4Update = new ArrayList(); //excel读出的 所有新增地区操作的数据 List list4AddArea = new ArrayList(); //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); // file.temp 文件存在 if (tmpf.exists()) { //删除 file workbook = new XSSFWorkbook(tmpf); } else { if (!f.exists()) { log.error(filePath + " 文件 不存在! "); errFile.add(f.getName()); continue; } else { workbook = new XSSFWorkbook(f); } } //Excel导入 try { PreDataInfo pre = new PreDataInfo(); //excel数据 --> java bean List subPreData = ExcelOperation.readExcel4Update(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, 0, pre, "id", "workRange","sysCode","updateTime","userTablespaceStatus", "checkoutIndicateStatus", "checkoutPayStatus", "userTablespacePath", "checkoutIndicatePath", "checkoutPayPath", "sqlList", "dataVersion", "collectingTime", "collUpdate"); if (null != subPreData && subPreData.size() > 0) { //是否是 同一个县区的 系统--是则 加入 boolean isSameArea = true; String areaCode = subPreData.get(0).getAreaCode(); for (PreDataInfo preDataInfo : subPreData) { if (!areaCode.equals(preDataInfo.getAreaCode())) { isSameArea = false; errArea.add(areaCode); break; } } if (isSameArea) { all.addAll(subPreData); } } } catch (IllegalArgumentException | IllegalAccessException | InstantiationException e) { e.printStackTrace(); } // finally{ // if (null != workbook) { // workbook.close(); // } // } } if (errFile.size() > 0) { errmap.put("fileUnExist",errFile); } if (errArea.size() > 0) { errmap.put("areaUnImport", errArea); } //根据 操作 类别 分类(修改, 新增行政区划,新增信息系统) if (all.size() > 0) { for (PreDataInfo preDataInfo : all) { switch (preDataInfo.getIsupdate()) { case Constant.preDataInfo_add_system: list4AddSystem.add(preDataInfo); break; case Constant.preDataInfo_add_area: list4AddArea.add(preDataInfo); break; case Constant.preDataInfo_update: list4Update.add(preDataInfo); break; default: break; } } } // 查系统 是否没有-(没有则在 pre_data_info添加新系统): if (list4AddSystem.size() > 0) { this.AddSystem(list4AddSystem); List sysNames = new ArrayList(); for (PreDataInfo preDataInfo : list4AddSystem) { sysNames.add(preDataInfo.getSysName()); } List listSystem = new ArrayList(); if (sysNames.size() > 0) { listSystem = systemCodeDao.findByName(sysNames); } Map syscodeMap = new HashMap(); for (SystemEntity systemEntity : listSystem) { syscodeMap.put(systemEntity.getSystemName(), systemEntity.getCode()); } for ( PreDataInfo systemEntity : list4AddSystem) { PreDataInfo systemFull = new PreDataInfo(); BeanCopy.copyBean(systemEntity, systemFull); systemFull.setSysCode(syscodeMap.get(systemFull.getSysName())); systemFull.setUpdateTime(DateForm.date2StringBysecond(new Date())); all2Insert.add(systemFull); } } //TODO 去掉 已经导入的系统。。(例如 excel反复导入) //在 pre_data_info表中 新增 操作为(新增信息系统)的系统 all2Insert = removeRepeat(all2Insert); if (all2Insert.size() > 0) { preDataInfoDao.insertBatch(all2Insert); } // 查 地区 是否没有-(没有则添加新地区): if (list4AddArea.size() > 0) { this.addArea(list4AddArea); } //在 pre_data_info表中 新增 操作为(新增行政区划)的系统 List allAreaInsert = new ArrayList(); List sysNamesArea = new ArrayList(); for (PreDataInfo preDataInfo : list4AddArea) { sysNamesArea.add(preDataInfo.getSysName()); } //查询系统的code。 List listSystem = new ArrayList(); if (sysNamesArea.size() > 0) { listSystem = systemCodeDao.findByName(sysNamesArea); } Map syscodeMap = new HashMap(); for (SystemEntity systemEntity : listSystem) { syscodeMap.put(systemEntity.getSystemName(), systemEntity.getCode()); } for (PreDataInfo preDataInfo : list4AddArea) { PreDataInfo areaFull = new PreDataInfo(); BeanCopy.copyBean(preDataInfo, areaFull); areaFull.setSysCode(syscodeMap.get(areaFull.getSysName())); areaFull.setUpdateTime(DateForm.date2StringBysecond(new Date())); allAreaInsert.add(areaFull); } //TODO 去掉 已经导入的系统。。(例如 excel反复导入) //在 pre_data_info表中 新增 操作为(新增行政区划)的系统 allAreaInsert = removeRepeat(allAreaInsert); if (allAreaInsert.size() > 0) { preDataInfoDao.insertBatch(allAreaInsert); } //在 pre_data_info表中 更新 操作为 (修改) 的系统 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(), systemEntity.getCode()); } for (PreDataInfo preUpdate : list4Update) { PreDataInfo UpdateFull = new PreDataInfo(); BeanCopy.copyBean(preUpdate, UpdateFull); UpdateFull.setSysCode(syscodeMap.get(UpdateFull.getSysName())); UpdateFull.setUpdateTime(DateForm.date2StringBysecond(new Date())); allUpdate.add(UpdateFull); } for (PreDataInfo preEntity : allUpdate) { preDataInfoDao.update(preEntity); } } return errmap; } @Override public Map exportExcel(String path) throws Exception { Map map = new HashMap(); List errList = new ArrayList(); List list = findAll(); // InputStreamReader in= new InputStreamReader(Resource.class.getResourceAsStream(templateFilePath), "UTF-8"); ClassPathResource res = new ClassPathResource(Constant.SYSTEM_INFO_EXCEL_TEMPLATE_FIEL_PATH); //此处 耗时太久:(原因:excel 2007 以后采用的方式不同造成的建议升级 到 poi 3.15以上) XSSFWorkbook workbook = null; XSSFWorkbook result = null; try { workbook = new XSSFWorkbook(res.getInputStream()); result = ExcelOperation.writeExcelTemplate(workbook, Constant.EXCEL_TEMPLATE_INIT_ROW, 0, 25, list, "id", "workRange","sysCode","updateTime","userTablespaceStatus", "checkoutIndicateStatus", "checkoutPayStatus", "userTablespacePath", "checkoutIndicatePath", "checkoutPayPath", "sqlList", "dataVersion", "collectingTime", "collUpdate"); } catch (IllegalArgumentException | IllegalAccessException | IOException e) { errList.add(e.getMessage()); } //不能进行close, // workbook.close(); FileOutputStream fileOut = null; try { File f = new File(path); f.createNewFile(); fileOut = new FileOutputStream(f); result.write(fileOut); } catch (IOException e) { errList.add(e.getMessage()); } finally{ if (null != fileOut) { fileOut.close(); } } if (errList.size() > 0) { map.put("err", errList); } return map; } /** 给system_info增加新系统 * @param list4AddSystem * @return * @throws Exception */ private int AddSystem(List list4AddSystem) throws Exception { int insertNum = 0; // 查系统 是否没有-(没有则添加新系统): List sysNames = new ArrayList(); for (PreDataInfo preDataInfo : list4AddSystem) { sysNames.add(preDataInfo.getSysName()); } // 判断-是否存在该系统 if (sysNames.size() > 0) { //数据库 已存在的 系统 List listSystem = systemCodeDao.findByName(sysNames); List existSysNames = new ArrayList(); if (null != listSystem) { for ( SystemEntity systemEntity : listSystem) { existSysNames.add(systemEntity.getSystemName()); } sysNames.removeAll(existSysNames); } ////数据库 不存在的 系统-(添加新系统) if (sysNames.size() > 0) { systemCodeDao.insertBatch(sysNames); insertNum = sysNames.size(); } } return insertNum; } /** 新增地区 * @param list4AddArea 待新增地区的列表 * @return * @throws Exception */ private int addArea(List list4AddArea)throws Exception { int insertNum = 0; List areacodeList = new ArrayList(); Map areaMap = new HashMap(); for (PreDataInfo preDataInfo : list4AddArea) { areacodeList.add(preDataInfo.getAreaCode()); RegionalismEntity region = new RegionalismEntity(); region.setCode(preDataInfo.getAreaCode()); region.setCityName(preDataInfo.getCityName()); region.setDistrictName(preDataInfo.getDistrictName()); areaMap.put(region.getCode(), region); } List existEntity = regionalismCodeDao.findExistRegionalism(areacodeList); for (RegionalismEntity regionalismEntity : existEntity) { areaMap.remove(regionalismEntity.getCode()); } //新增 地区 if (areaMap.size() > 0) { List insert2Regionalism = new ArrayList(); for ( String key : areaMap.keySet()) { insert2Regionalism.add(areaMap.get(key)); } if (insert2Regionalism.size() > 0) { regionalismCodeDao.insertBatch(insert2Regionalism); insertNum = insert2Regionalism.size(); } } return insertNum; } /** * 遍历文件 * * @param ps must not null * @param path * @return */ private Map getAllFile(Map ps, String path) { File f = new File(path); String name = f.getName(); // 如果是 sql文件 if (name.endsWith(".sql") || name.endsWith(".SQL")) ps.put(name.toLowerCase(), f.getAbsolutePath()); if (f.exists()) { String[] subpaths = f.list(); if (null != subpaths) { for (String tmppath : subpaths) { getAllFile(ps, f.getAbsolutePath() + "/" + tmppath); } } } return ps; } /** 设置脚本信息 * @param systemDataInfo * @param sqlFilePathsStandard * @return */ private List setScript(List systemDataInfo, Map sqlFilePathsStandard) { List fileEntitys = new ArrayList(); for (PreDataInfo preDataInfo : systemDataInfo) { // 归档表空间脚本的位置- String TablePathStandard = getFilePath("UserTablespace_", sqlFilePathsStandard, preDataInfo, ".sql"); if (null != TablePathStandard && !"".equals(TablePathStandard)) { preDataInfo.setUserTablespaceStatus("是"); preDataInfo.setUserTablespacePath("UserTablespace_"+preDataInfo.getAreaCode()+"_" + preDataInfo.getSysCode()+ ".sql"); } // 归档预算 脚本的位置- String indicatePathStandard = getFilePath("Checkout_Indicate_", sqlFilePathsStandard, preDataInfo, ".sql"); if (null != indicatePathStandard && !"".equals(indicatePathStandard)) { preDataInfo.setCheckoutIndicateStatus("是"); preDataInfo.setCheckoutIndicatePath("Checkout_Indicate_"+preDataInfo.getAreaCode()+"_" + preDataInfo.getSysCode()+ ".sql"); } // 归档表空间脚本的位置- String payPathStandard = getFilePath("Checkout_Pay_", sqlFilePathsStandard, preDataInfo, ".sql"); if (null != payPathStandard && !"".equals(payPathStandard)) { preDataInfo.setCheckoutIndicateStatus("是"); preDataInfo.setCheckoutIndicatePath("Checkout_Pay_"+preDataInfo.getAreaCode()+"_" + preDataInfo.getSysCode()+ ".sql"); } // 设置 状态 if (!"ORACLE".equals(preDataInfo.getDataBaseType().toUpperCase())) { preDataInfo.setUserTablespaceStatus("-"); } fileEntitys.add(preDataInfo); } return fileEntitys; } /** * 查找文件 * * @param prefix * @param map * @param myf * @param affix * @return */ private String getFilePath(String prefix, Map map, PreDataInfo pre, String affix) { StringBuffer sb = new StringBuffer(); sb.append(prefix).append(pre.getAreaCode()).append("_") .append(pre.getSysCode()).append(affix); String fileName = sb.toString().toLowerCase(); if (!map.containsKey(fileName)) { return null; } // 地区字母 小写 -- 文件属性小写 String pathStandard = map.get(fileName); return pathStandard; } /** 新增系统时,去掉系统已存在的 新增操作里的 * @param allAreaInsert * @throws Exception */ private List removeRepeat(List allAreaInsert) throws Exception { Map excelAdd = new HashMap(); List allResult = new ArrayList(); //系统已存在的 系统 List sysInDataBase = preDataInfoDao.findAll(); Map sysInDataBaseMap = new HashMap(); for (PreDataInfo preDataInfo : sysInDataBase) { sysInDataBaseMap.put(preDataInfo.getAreaCode()+"_"+preDataInfo.getSysName().trim(), null); } //本次新增的系统 for (PreDataInfo pre : allAreaInsert) { excelAdd.put(pre.getAreaCode()+"_"+pre.getSysName().trim(), pre); } for (String key : excelAdd.keySet()) { if (!sysInDataBaseMap.containsKey(key)) { allResult.add(excelAdd.get(key)); } } return allResult; } }