package com.platform.service.impl; import java.io.File; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.springframework.stereotype.Service; import com.base.Custom4exception; import com.base.CustomException; import com.platform.dao.DataInfoDao; import com.platform.dao.GatherOracleDao; import com.platform.dao.PreDataInfoDao; import com.platform.entities.CheckoutEntity; import com.platform.entities.DataInfoEntity; import com.platform.entities.GatherOracleInfo; import com.platform.entities.PreDataInfo; import com.platform.oracle.OracleConnector; import com.platform.service.DataInfoService; import com.platform.service.ICheckoutService; import com.platform.service.thread.ThreadCheckoutStandardOracle; import com.platform.service.thread.ThreadExtractStandardSqlServer; import com.platform.utils.BeanCopy; import com.platform.utils.CacheOracleCheckoutEntity; import com.platform.utils.Configs; import com.platform.utils.Constant; import com.platform.utils.DateForm; import com.platform.utils.FileOperateHelper; /** 校验业务类 * @author chen * */ @Service(value = "checkoutService") public class CheckoutServiceImpl implements ICheckoutService { /** * 日志 */ public static Logger log = Logger.getLogger(CheckoutServiceImpl.class); /** * 数据管理业务层 */ @Resource(name = "dataInfoService") private DataInfoService dataInfoService; /** * 系统清单数据 */ @Resource(name = "preDataInfoDao") private PreDataInfoDao preDataInfoDao; /** * 数据管理 */ @Resource(name = "dataInfoDao") private DataInfoDao dataInfoDao; /** * oracle数据库管理 */ @Resource(name = "gatherOracleDao") private GatherOracleDao gatherOracleDao; @Override public List findAll() throws Exception { List checks = new ArrayList(); List result = preDataInfoDao.findAllCollect(); DataInfoEntity data = new DataInfoEntity(); Calendar c2 = Calendar.getInstance(); // 时间设置为 半年前的时间 c2.set(Calendar.MONTH, getMonBeforeHalfYear(c2.get(Calendar.MONTH))); String time = DateForm.date2StringByDay(c2.getTime()); data.setCollectingTime(time); // data.setCollectingTime(collectingTime); Map dataMap = new HashMap(); List datas = dataInfoDao.findByParam(data ); for (DataInfoEntity dataInfoEntity : datas) { String key = dataInfoEntity.getRegionalismCode().toLowerCase()+"_"+ dataInfoEntity.getSystemCode(); if (dataMap.keySet().contains(key)) { if(dataInfoEntity.getDataVersion() > dataMap.get(key).getDataVersion()) dataMap.put(key, dataInfoEntity); } else{ dataMap.put(key, dataInfoEntity); } } for (PreDataInfo pre : result) { String key = pre.getAreaCode().toLowerCase()+"_"+ pre.getSysCode(); DataInfoEntity dataInfo = dataMap.get(key); CheckoutEntity ck = new CheckoutEntity(); BeanCopy.copyBean(pre, ck); if (null != dataInfo) { String pay = dataInfo.getPayResult(); String exec = dataInfo.getExecResult(); ck.setPayResult(isY(pay)); ck.setExecResult(isY(exec)); ck.setCheckResult(isY(pay,exec)); } else { ck.setPayResult(isY(null)); ck.setExecResult(isY(null)); ck.setCheckResult(isY(null)); ck.setCollection("否"); } checks.add(ck); } //排序 按 checkResult 排序 Collections.sort(checks); return checks; } @Override public List findByCity(String city) throws Exception { CheckoutEntity cksql = new CheckoutEntity(); Calendar c2 = Calendar.getInstance(); // 时间设置为 半年前的时间 c2.set(Calendar.MONTH, getMonBeforeHalfYear(c2.get(Calendar.MONTH))); String time = DateForm.date2StringByDay(c2.getTime()); cksql.setCollectingTime(time); cksql.setCityName(city); Map resul = new HashMap(); Map nodata = new HashMap(); List list = preDataInfoDao.findByCity(cksql); int length = list.size(); // 对所有数据分析 for (int i = 0; i < length; i++) { CheckoutEntity preDataInfo = list.get(i); preDataInfo.setId(i); preDataInfo.setCollUpdate(isY(preDataInfo.getCollUpdate())); if (preDataInfo.getDataVersion() < 1) { preDataInfo.setCollection("否"); } if (null != preDataInfo.getCollectingTime()) { if (c2.getTime().before(DateForm.string2DateByDay(preDataInfo.getCollectingTime()))) { resul.put(preDataInfo.getAreaCode()+"_"+preDataInfo.getSysCode()+"_"+preDataInfo.getDataId(), preDataInfo); } else { preDataInfo.setDataId(0); preDataInfo.setPath(null); preDataInfo.setDataVersion(0); preDataInfo.setExecResult(null); preDataInfo.setExecResultLast(null); preDataInfo.setPayResult(null); preDataInfo.setPayResultLast(null); preDataInfo.setCheckoutFlag(null); preDataInfo.setCollection("否"); //data_id 已经为 0 (数据不存在时) nodata.put(preDataInfo.getAreaCode()+"_"+preDataInfo.getSysCode(), preDataInfo); } } else{ nodata.put(preDataInfo.getAreaCode()+"_"+preDataInfo.getSysCode(), preDataInfo); } } List keyList = new ArrayList(); //记录 不存在数据 在 nodata的 key值 for (String nodatakey : nodata.keySet()) { keyList.add(nodatakey); } List resultList = new ArrayList(); for (String key : resul.keySet()) { //如果有 校验失败的结果则 为否 CheckoutEntity tmp = resul.get(key); if(!Constant.CHECKOUT_STATUS_ZERO.equals(tmp.getPayResultLast()) && !Constant.CHECKOUT_STATUS_ZERO.equals(tmp.getExecResultLast())){ if (Constant.CHECKOUT_STATUS_FOUR.equals(tmp.getPayResultLast()) || Constant.CHECKOUT_STATUS_FOUR.equals(tmp.getExecResultLast())) { tmp.setCheckResult(Constant.ORACLE_CHECK_REULT_ONE); } else if(!Constant.CHECKOUT_STATUS_TWO.equals(tmp.getPayResultLast()) && !Constant.CHECKOUT_STATUS_TWO.equals(tmp.getExecResultLast())){ tmp.setCheckResult(Constant.ORACLE_CHECK_REULT_ZERO); } } resultList.add(resul.get(key)); for (String nodatakey : keyList) { if (key.contains(nodatakey)) { nodata.remove(nodatakey); } } } for (String key : nodata.keySet()) { resultList.add(nodata.get(key)); } return resultList; } @Override public Map findDetail(CheckoutEntity ck) throws Exception { Map map = new HashMap(); PreDataInfo data = new CheckoutEntity(); String areaCode = ck.getAreaCode(); int sysCode = ck.getSysCode(); data.setAreaCode(areaCode); data.setSysCode(sysCode); PreDataInfo data1 = preDataInfoDao.findStandardByCode(data); data1.reSetWorkRange(new ArrayList()); map.put("data1", data1); data.setAreaCode(areaCode); data.setSysCode(sysCode); PreDataInfo data2 = preDataInfoDao.findLastByCode(data); // 对比字段 if (data1.getFunctionDetails().equals(data2.getFunctionDetails())) { data2.setFunctionDetails(""); } if (data1.getAreaLevel().equals(data2.getAreaLevel())) { data2.setAreaLevel(""); } if (data1.getBeginUseTime().equals(data2.getBeginUseTime())) { data2.setBeginUseTime(""); } if (data1.getDataBaseType().equals(data2.getDataBaseType())) { data2.setDataBaseType(""); } if (data1.getDataBaseVersion().equals(data2.getDataBaseVersion())) { data2.setDataBaseVersion(""); } if (data1.getDeveloperFullName().equals(data2.getDeveloperFullName())) { data2.setDeveloperFullName(""); } if (data1.getDepartmentManager().equals(data2.getDepartmentManager())) { data2.setDepartmentManager(""); } if (data1.getManagerContacts().equals(data2.getManagerContacts())) { data2.setManagerContacts(""); } if (data1.getSysVersion().equals(data2.getSysVersion())) { data2.setSysVersion(""); } if(!checkWorkRange(data1, data2)){ data2.reSetWorkRange(null); } map.put("data2", data2); return map; } @Override public List checkAll(List list) throws Exception { List oracleList = new ArrayList(); List sqlServerList = new ArrayList(); for (CheckoutEntity checkoutEntity : list) { if (checkoutEntity.getDataId() == 0) { continue; } if ("SQL SERVER".equals(checkoutEntity.getDataBaseType())) { sqlServerList.add(checkoutEntity); } else if ("ORACLE".equals(checkoutEntity.getDataBaseType())) { oracleList.add(checkoutEntity); } log.info(checkoutEntity.toString()); } // sql Server校验 for (CheckoutEntity checkoutEntity : sqlServerList) { if (checkoutEntity.getDataId() == 0) { continue; } DataInfoEntity data = new DataInfoEntity(); this.checkSqlServerOne(checkoutEntity); data.setId(checkoutEntity.getDataId()); data.setPayResultLast(checkoutEntity.getPayResultLast()); data.setExecResultLast(checkoutEntity.getExecResultLast()); data.setCheckoutFlag(checkoutEntity.getCheckoutFlag()); dataInfoDao.update(data); } //oracle 校验 for (CheckoutEntity checkoutEntity : oracleList) { if (checkoutEntity.getDataId() == 0) { continue; } DataInfoEntity data = new DataInfoEntity(); // 状态改为 正则校验 Constant.CHECKOUTFLAG_TWO this.checkOracleOne(checkoutEntity); data.setId(checkoutEntity.getDataId()); data.setPayResultLast(checkoutEntity.getPayResultLast()); data.setExecResultLast(checkoutEntity.getExecResultLast()); data.setCheckoutFlag(checkoutEntity.getCheckoutFlag()); dataInfoDao.update(data); String kuberTaskName = checkoutEntity.getAreaCode().toLowerCase()+"-" + checkoutEntity.getSysCode() + "-" + checkoutEntity.getDataVersion(); if(!CacheOracleCheckoutEntity.getCheckKeys().contains(kuberTaskName)){ CacheOracleCheckoutEntity.putCheck(kuberTaskName, checkoutEntity); } } //TODO 如果 CacheOracleCheckoutEntity.getCheckKeys() 有数据则启动 kuber客户端请求,尝试链接oracle,并查询标准表是否存在 if ( CacheOracleCheckoutEntity.getCheckKeys().size() > 0) { new ThreadCheckoutStandardOracle(dataInfoDao).start(); } List result = new ArrayList(); if (list.size() > 0) { result = findByCity(list.get(0).getCityName()); } // Map map = new HashMap(); // for (CheckoutEntity checkoutEntity : list) { // //校验当前的数据: // checkOne(checkoutEntity); // map.put(checkoutEntity.getAreaCode()+ "_" +checkoutEntity.getSysCode()+ "_" +checkoutEntity.getDataVersion(),checkoutEntity); // } // for (int i = 0; i < result.size(); i++) { // CheckoutEntity checkoutEntity = result.get(i); // String area_sys = checkoutEntity.getAreaCode()+ "_" +checkoutEntity.getSysCode()+ "_" +checkoutEntity.getDataVersion(); // if (map.containsKey(area_sys)) { // result.set(i, map.get(area_sys)); // } // } return result; } @Override public List deleteAll(List list) throws Exception { List dataIds = new ArrayList(); for (CheckoutEntity checkoutEntity : list) { int dataInfoId = checkoutEntity.getDataId(); if (dataInfoId > 0) { dataIds.add(dataInfoId); } } if (dataIds.size() > 0) { dataInfoService.deleteData(dataIds); // 根据 dataIds 删除 } List result = new ArrayList(); if (list.size() > 0) { result = findByCity(list.get(0).getCityName()); } return result; } @Override public int updateStandardInfo(PreDataInfo ck) throws Exception { ck.setProper(); String areaCode = ck.getAreaCode(); int sysCode = ck.getSysCode(); //对比字段 -- 采集描述的 更新字段是否 改为不更新 PreDataInfo data = new CheckoutEntity(); data.setAreaCode(areaCode); data.setSysCode(sysCode); PreDataInfo data2 = preDataInfoDao.findLastByCode(data); data2.setCollUpdate("n"); //对比 : if (checkField(ck, data2) && checkWorkRange(ck, data2)) { preDataInfoDao.updateCollection(data2); } int r = preDataInfoDao.update(ck); return r; } @Override public List extractSqlServer(List list) throws Exception { //待抽取标准表 的 SqlServer服务的 数据列表: List sqlServer = new ArrayList(); for (CheckoutEntity checkoutEntity : list) { if ("SQL SERVER".equals(checkoutEntity.getDataBaseType()) && checkoutEntity.getDataId() > 0) { sqlServer.add(checkoutEntity); } } //TODO 修改sqlServer 列表对应的 数据--- payResultLast 、execResultLast-> 正在抽取 // 待抽取的集合 List Extractlist = new ArrayList(); // TODO 抽取 --- 线程 if (sqlServer.size() > 0) { for (CheckoutEntity checkoutEntity : sqlServer) { boolean isTract = false; //校验结果:成功或失 0:未校验,1:不需校验,2:正在校验,3:校验成功,4:校验失败,5:待抽取,6:正在抽取,7:抽取完成 if(Constant.CHECKOUT_STATUS_THREE.equals(checkoutEntity.getPayResultLast())){ isTract = true; checkoutEntity.setPayResultLast(Constant.CHECKOUT_STATUS_FIVE); } if(Constant.CHECKOUT_STATUS_THREE.equals(checkoutEntity.getExecResultLast())){ isTract = true; checkoutEntity.setExecResultLast(Constant.CHECKOUT_STATUS_FIVE); } if (isTract) { Extractlist.add(checkoutEntity); } } // 汇总库的信息 GatherOracleInfo oracleInfo = null; List oracleConnects = gatherOracleDao.selectAllOracle(); for (GatherOracleInfo info : oracleConnects) { //抽取标准表的汇总库 if (Constant.ORACLE_EXTRACT_TYPE_ONE.equals(info.getType())) { oracleInfo = info; break; } } if (null != oracleInfo) { // 如果能连接 if(OracleConnector.canConnect("jdbc:oracle:thin:@" + oracleInfo.getIp() + ":" + oracleInfo.getPort() + ":" + oracleInfo.getDatabaseName(), oracleInfo.getUser(), oracleInfo.getPassword())){ for (CheckoutEntity check : Extractlist) { DataInfoEntity data = new DataInfoEntity(); data.setId(check.getDataId()); data.setPayResultLast(check.getPayResultLast()); data.setExecResultLast(check.getExecResultLast()); data.setCheckoutFlag(check.getCheckoutFlag()); try { dataInfoDao.update(data); } catch (Exception e) { log.error(e); } } //抽取 new ThreadExtractStandardSqlServer(oracleInfo, Extractlist, dataInfoDao).start(); } else { //连接不上标准表汇总库错误 throw new CustomException(Custom4exception.CHECKOUT_EXTRACT_EXCEPT, null); } } } List result = new ArrayList(); //TODO findByCity if (list.size() > 0) { result = findByCity(list.get(0).getCityName()); } return result; } /** 前 Configs.dataBefore 个月 * @param num * @return */ private int getMonBeforeHalfYear(int num){ num -= Configs.dataBefore; if (num <= 0) { num = num + 12; } return num; } private String isY(String str1, String str2) { String r = "否"; if (null !=str2 && null !=str1 && str1.equals(str2) && "y".equals(str1.toLowerCase())) { r = "是"; } return r; } private String isY(String str1) { if (null != str1 && "y".equals(str1.toLowerCase())) { return "是"; } else { return "否"; } } /** true 系统所属范围没变化,false:系统范围改变 * @param data1 * @param data2 * @return */ private boolean checkWorkRange(PreDataInfo data1, PreDataInfo data2) { if (!data1.getDepartmentBudgetManage().equals(data2.getDepartmentBudgetManage())) { return false; } if (!data1.getBudgetQuotaManage().equals(data2.getBudgetQuotaManage())) { return false; } if (!data1.getBudgetExecManage().equals(data2.getBudgetExecManage())) { return false; } if (!data1.getTotalBudgetAccount().equals(data2.getTotalBudgetAccount())) { return false; } if (!data1.getFinalAccountManage().equals(data2.getFinalAccountManage())) { return false; } if (!data1.getReportFormManage().equals(data2.getReportFormManage())) { return false; } if (!data1.getNonTaxManage().equals(data2.getNonTaxManage())) { return false; } if (!data1.getWageSystem().equals(data2.getWageSystem())) { return false; } if (!data1.getAnalysisOfFinancialEconomicProsperity().equals(data2.getAnalysisOfFinancialEconomicProsperity())) { return false; } if (!data1.getGovernmentDebtManage().equals(data2.getGovernmentDebtManage())) { return false; } return true; } /** 判断 data1 和 data2是否有差别 * @param data1 * @param data2 * @return */ private boolean checkField(PreDataInfo data1, PreDataInfo data2) { if (!data1.getFunctionDetails().equals(data2.getFunctionDetails())) { return false; } if (!data1.getAreaLevel().equals(data2.getAreaLevel())) { return false; } if (!data1.getBeginUseTime().equals(data2.getBeginUseTime())) { return false; } if (!data1.getDataBaseType().equals(data2.getDataBaseType())) { return false; } if (!data1.getDataBaseVersion().equals(data2.getDataBaseVersion())) { return false; } if (!data1.getDeveloperFullName().equals(data2.getDeveloperFullName())) { return false; } if (!data1.getDepartmentManager().equals(data2.getDepartmentManager())) { return false; } if (!data1.getManagerContacts().equals(data2.getManagerContacts())) { return false; } if (!data1.getSysVersion().equals(data2.getSysVersion())) { return false; } return true; } /** 校验sqlServer单条记录 * @param ck * @return */ private CheckoutEntity checkSqlServerOne(CheckoutEntity ck) { //TODO 校验: sqlserver数据-查看脚本在不在? 支付:standard_pay_地区_系统码.sql ?,支付:standard_indicate_地区_系统码.sql ? //TODO sqlserver校验结果记录进数据库, String pay = ck.getPayResult(); String exec = ck.getExecResult(); ck.setCheckResult("是"); if ("y".equals(pay) || "Y".equals(pay)) { String payFilePath = FileOperateHelper.addLastSeparator(ck.getPath())+Constant.standard_pay + ck.getAreaCode().toLowerCase()+"_"+ck.getSysCode()+".sql"; File f = new File(payFilePath); if(f.exists()){ ck.setPayResultLast(Constant.CHECKOUT_STATUS_THREE); } else { ck.setPayResultLast(Constant.CHECKOUT_STATUS_FOUR); } } else { //校验结果:成功或失败 0:未校验,1:不需校验,2:正在校验,3:校验成功,4:校验失败,5:待抽取,6:正在抽取,7:抽取完成 ck.setPayResultLast(Constant.CHECKOUT_STATUS_ONE); } if ("y".equals(exec) || "Y".equals(exec)) { String execFilePath = FileOperateHelper.addLastSeparator(ck.getPath())+Constant.standard_indicate + ck.getAreaCode().toLowerCase()+"_"+ck.getSysCode()+".sql"; File f = new File(execFilePath); if (f.exists()) { ck.setExecResultLast(Constant.CHECKOUT_STATUS_THREE); } else { ck.setExecResultLast(Constant.CHECKOUT_STATUS_FOUR); } } else { //校验结果:成功或失 0:未校验,1:不需校验,2:正在校验,3:校验成功,4:校验失败,5:待抽取,6:正在抽取,7:抽取完成 ck.setExecResultLast(Constant.CHECKOUT_STATUS_ONE); } // 1:已校验-- ck.setCheckoutFlag(Constant.CHECKOUTFLAG_ONE); return ck; } /** 校验ORACLE 单条记录 * @param ck * @return */ private CheckoutEntity checkOracleOne(CheckoutEntity ck) { //校验: Oracle数据-更改为正在校验 // Oracle校验结果记录进数据库, String pay = ck.getPayResult(); String exec = ck.getExecResult(); if (Constant.CHECKOUT_STATUS_ZERO.equals(ck.getPayResultLast())) { //校验结果:成功或失败 0:未校验,1:不需校验,2:正在校验,3:校验成功,4:校验失败,5:待抽取,6:正在抽取,7:抽取完成 if ("y".equals(pay) || "Y".equals(pay)) { ck.setPayResultLast(Constant.CHECKOUT_STATUS_TWO); } else { ck.setPayResultLast(Constant.CHECKOUT_STATUS_ONE); } } if (Constant.CHECKOUT_STATUS_ZERO.equals(ck.getExecResultLast())){ if ("y".equals(exec) || "Y".equals(exec)) { ck.setExecResultLast(Constant.CHECKOUT_STATUS_TWO); } else { //校验结果:成功或失 0:未校验,1:不需校验,2:正在校验,3:校验成功,4:校验失败,5:待抽取,6:正在抽取,7:抽取完成 ck.setExecResultLast(Constant.CHECKOUT_STATUS_ONE); } } // 2:正在校验-- ck.setCheckoutFlag(Constant.CHECKOUTFLAG_TWO); return ck; } }