package com.example.upsecuritydata.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.example.upsecuritydata.cache.CodeCache; import com.example.upsecuritydata.entity.NbmsEnprefix; import com.example.upsecuritydata.entity.NbmsEntApply; import com.example.upsecuritydata.service.Method; import com.example.upsecuritydata.service.NbmsEnprefixService; import com.example.upsecuritydata.service.NbmsEntApplyServcie; import com.example.upsecuritydata.util.Constant; import com.example.upsecuritydata.util.Encrypt; import com.example.upsecuritydata.util.JschUtil; import com.jcraft.jsch.*; import com.xxl.job.core.context.XxlJobHelper; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.io.*; import java.util.*; import java.util.zip.GZIPInputStream; /** * @author laifeng * @since 2022/11/27 */ @Service @AllArgsConstructor public class MethodImpl implements Method { private final NbmsEntApplyServcie nbmsEntApplyServcie; private final NbmsEnprefixService enprefixService; @Override public void upMethod(String readFile, String upSrc, String tableName, String operationName) throws Exception { File file = new File(readFile); BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(file))); StringBuilder sb = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } br.close(); // 将字符串压缩成zip文件并获得字节数据 byte[] bytes = Encrypt.zipToBytes(Constant.zipPath, sb.toString()); // upload是压缩加密后的内容 byte[] upload = Encrypt.encryptAlgorithm("1", bytes, "1234567890abcDEF", "1234567890abcDEF"); // 生成文件名 String dateStr = Long.toString(System.currentTimeMillis()); // 生成13位时间戳 String fileName = tableName + dateStr + operationName; String src = Constant.localPackage + fileName; // 写入文件 FileOutputStream stream = new FileOutputStream(src); stream.write(upload); XxlJobHelper.log("写入成功:" + fileName); // 关闭输出流 stream.close(); // 远程连接服务器 // SSHRemoteCall.getInstance().sshRemoteCallLogin(Constant.ipAddress, Constant.userName, Constant.password); JschUtil jschUtil = new JschUtil(Constant.ipAddress, 22102, Constant.userName, Constant.password); jschUtil.connect(); // 确保目录一定存在 // SSHRemoteCall.getInstance().directory(upSrc); jschUtil.directory(upSrc); // 上传文件到指定目录 // SSHRemoteCall.getInstance().uploadFile("/data1/qingniao/" + upSrc + "/" + fileName, src); // TODO: 2023/3/1 上传路径这里需要改 jschUtil.upload("/data1/zhilianxintong/" + upSrc + "/" + fileName, src); } /** * 生成三、 标识解析企业主体信息上报文件 * * @param type 0 代表注册 1 代表修改 * @return */ @Override public Boolean creatIdentificationUpFile(Integer type, String yyyyMMdd, String yyyyMMddToday) throws IOException { String companyCode; //标识码企业前缀 String companyName; // 标识解析节点经营主体名称 String level; // 标识解析系统级别 String accessProvince; // 接入省份 String accessCity; // 接入地市 String registerTime; // 注册时间 String categoryA; // 经营主体行业门类 String categoryB; // 经营主体行业大类 String companyType; // 公司性质 String serviceProfession; //节点服务行业 List list = null; // 新增操作 if (type == 0) { list = nbmsEntApplyServcie.lambdaQuery().eq(NbmsEntApply::getFirstAuditStatus, 2) .eq(NbmsEntApply::getApplyType, 1) .eq(NbmsEntApply::getApplyStep, 4) .ge(NbmsEntApply::getUpdateDate, yyyyMMdd) .lt(NbmsEntApply::getUpdateDate, yyyyMMddToday) .list(); } // 修改操作 if (type == 1) { list = nbmsEntApplyServcie.lambdaQuery().eq(NbmsEntApply::getFirstAuditStatus, 2) .eq(NbmsEntApply::getApplyType, 2) .eq(NbmsEntApply::getApplyType, 3) .eq(NbmsEntApply::getApplyStep, 4) .ge(NbmsEntApply::getUpdateDate, yyyyMMdd) .lt(NbmsEntApply::getUpdateDate, yyyyMMddToday) .list(); } // 全量操作 if (type == 2) { list = nbmsEntApplyServcie.lambdaQuery().eq(NbmsEntApply::getFirstAuditStatus, 2) .eq(NbmsEntApply::getApplyType, 1) .eq(NbmsEntApply::getApplyStep, 4) .list(); } // 创建输出流 FileOutputStream stream = new FileOutputStream(Constant.localNotEncryFileNamePath); if (CollUtil.isNotEmpty(list)) { for (NbmsEntApply nbmsEnapply : list) { companyCode = nbmsEnapply.getEntPrefix(); companyName = nbmsEnapply.getOrgName(); List split = StrUtil.split(companyCode, "."); if ("000000".equals(split.get(2))) { level = "4"; } else { level = "5"; } accessProvince = nbmsEnapply.getOrgAddrProvince(); accessProvince = CodeCache.codeMap.get(accessProvince); accessCity = nbmsEnapply.getOrgAddrCity(); accessCity = CodeCache.codeMap.get(accessCity); registerTime = DateUtil.format(nbmsEnapply.getUpdateDate(), "yyyy-MM-dd HH:mm:ss"); categoryA = nbmsEnapply.getIndustrySpecific(); categoryA = CodeCache.codeMap.get(categoryA); categoryB = nbmsEnapply.getIndustryCategory(); categoryB = CodeCache.codeMap.get(categoryB); companyType = judge(nbmsEnapply.getOrgNature()); serviceProfession = categoryB; String template = companyCode + "|" + companyName + "|" + level + "|" + accessProvince + "|" + accessCity + "|" + registerTime + "|" + categoryA + "|" + categoryB + "|" + companyType + "|" + serviceProfession + "\n"; byte[] bytes = template.getBytes(); stream.write(bytes); } stream.flush(); stream.close(); return true; } return false; } /** * 四、 标识解析企业主体信息上报 * * @param type 0 代表注册 1 代表修改 */ @Override public Boolean creatForUpFile(Integer type, String yyyyMMdd, String yyyyMMddToday) throws IOException { String companyName; // 标识解析节点经营主体名称 String startIP = "112.6.110.61"; String endIP = "112.6.110.61"; String position = "山东省济宁市济宁宁化大数据中心"; List list = null; if (type == 0) { list = enprefixService.query() .select("DISTINCT(ent_name)") .eq("proxy_state", 4) .ge("update_date", yyyyMMdd) .lt("update_date", yyyyMMddToday) .list(); } if (CollUtil.isEmpty(list)) { return false; } // 创建输出流 FileOutputStream stream = new FileOutputStream(Constant.localNotEncryFileNamePath); for (NbmsEnprefix enprefix : list) { companyName = enprefix.getEntName(); String template = companyName + "|" + startIP + "|" + endIP + "|" + position + "\n"; byte[] bytes = template.getBytes(); stream.write(bytes); } stream.flush(); stream.close(); return true; } /** * 五、 标识注册企业主体信息上报 * * @param type * @param yyyyMMdd * @param yyyyMMddToday * @return */ @Override public Boolean creatFiveUpFile(Integer type, String yyyyMMdd, String yyyyMMddToday) throws IOException { String companyCode; //标识码企业前缀 String companyName; // 标识解析节点经营主体名称 String level; // 标识解析系统级别 String accessProvince; // 接入省份 String accessCity; // 接入地市 String registerTime; // 注册时间 String categoryA; // 经营主体行业门类 String categoryB; // 经营主体行业大类 String companyType; // 公司性质 String serviceProfession; //节点服务行业 List list = null; // 新增操作 if (type == 0) { list = nbmsEntApplyServcie.lambdaQuery().eq(NbmsEntApply::getFirstAuditStatus, 2) .eq(NbmsEntApply::getApplyType, 1) .eq(NbmsEntApply::getApplyStep, 4) .ge(NbmsEntApply::getUpdateDate, yyyyMMdd) .lt(NbmsEntApply::getUpdateDate, yyyyMMddToday) .list(); } // 修改操作 if (type == 1) { list = nbmsEntApplyServcie.lambdaQuery().eq(NbmsEntApply::getFirstAuditStatus, 2) .eq(NbmsEntApply::getApplyType, 2) .eq(NbmsEntApply::getApplyType, 3) .eq(NbmsEntApply::getApplyStep, 4) .ge(NbmsEntApply::getUpdateDate, yyyyMMdd) .lt(NbmsEntApply::getUpdateDate, yyyyMMddToday) .list(); } // 全量操作 if (type == 2) { list = nbmsEntApplyServcie.lambdaQuery().eq(NbmsEntApply::getFirstAuditStatus, 2) .eq(NbmsEntApply::getApplyType, 1) .eq(NbmsEntApply::getApplyStep, 4) .list(); } // 创建输出流 FileOutputStream stream = new FileOutputStream(Constant.localNotEncryFileNamePath); if (CollUtil.isNotEmpty(list)) { for (NbmsEntApply nbmsEnapply : list) { companyCode = nbmsEnapply.getEntPrefix(); companyName = nbmsEnapply.getOrgName(); // level = "4"; accessProvince = nbmsEnapply.getOrgAddrProvince(); accessProvince = CodeCache.codeMap.get(accessProvince); accessCity = nbmsEnapply.getOrgAddrCity(); accessCity = CodeCache.codeMap.get(accessCity); registerTime = DateUtil.format(nbmsEnapply.getUpdateDate(), "yyyy-MM-dd HH:mm:ss"); categoryA = nbmsEnapply.getIndustrySpecific(); categoryA = CodeCache.codeMap.get(categoryA); categoryB = nbmsEnapply.getIndustryCategory(); categoryB = CodeCache.codeMap.get(categoryB); companyType = judge(nbmsEnapply.getOrgNature()); serviceProfession = categoryB; String template = companyCode + "|" + companyName + "|" + accessProvince + "|" + accessCity + "|" + registerTime + "|" + categoryA + "|" + categoryB + "|" + companyType + "|" + serviceProfession + "\n"; byte[] bytes = template.getBytes(); stream.write(bytes); } stream.flush(); stream.close(); return true; } return false; } @Override public void creatSixUpFileAndUp(String yesterday) throws Exception { JSch jsch = new JSch(); Session session = jsch.getSession(Constant.user, Constant.host, 22); session.setPassword(Constant.password1); session.setConfig("StrictHostKeyChecking", "no"); session.connect(2000000); ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(2000000); // 连接远程服务器,日志存放地 String yesterdayStr = DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"); String directoryPath = Constant.sixRemoteLogPackage + yesterdayStr; List allFile = listFiles(directoryPath, sftp); if (CollUtil.isEmpty(allFile)) { // System.out.println("表4:昨日无日志文件夹!"); XxlJobHelper.log("表4:昨日无日志文件夹!"); return; } for (String filePath : allFile) { // 读取远程服务器上的日志文件 gz压缩格式 GZIPInputStream gzipInputStream = new GZIPInputStream(sftp.get(directoryPath + "/" + filePath)); Reader decoder = new InputStreamReader(gzipInputStream, "UTF-8"); BufferedReader reader = new BufferedReader(decoder); // 创建输出流 FileOutputStream outputStream = new FileOutputStream(Constant.localNotEncryFileNamePath); String tempString; //一次读一行,读入null时文件结束 while ((tempString = reader.readLine()) != null) { // 获得请求时间 JSONObject jsonObject = JSON.parseObject(tempString); String operatorTime = jsonObject.getString("operatorTime"); Date date1 = DateUtil.parse(operatorTime, "yyyyMMddHHmmss"); String requestTime = DateUtil.format(date1, "yyyy-MM-dd HH:mm:ss"); // 获得响应时间 String responseTime = requestTime; // 获得源IP String sourceIP = jsonObject.getString("userIp"); // 端口 String port = "80"; // 获得目的IP String destinationIP = jsonObject.getString("idisIp"); // 方法 String method = "HTTPM_GET"; // 企业标识码 String companyCode = jsonObject.getString("prefix"); // 标识编码 String ideCode = jsonObject.getString("handleName"); // // 组装字符串 tempString = requestTime + "|" + responseTime + "|" + sourceIP + "|" + port + "|" + destinationIP + "|" + port + "|" + method + "|" + companyCode + "|" + ideCode + "|" + "|" + "|" + "|" + "http://code.qnaiot.com" + "|" + "http://code.qnaiot.com" + "|" + "\n"; byte[] bytes = tempString.getBytes(); outputStream.write(bytes); } outputStream.flush(); outputStream.close(); decoder.close(); gzipInputStream.close(); // 上传 String fileName = Constant.localNotEncryFileNamePath; // 要上传的目录 String upSrc = "/004/" + yesterday; // 表名(文件名的一部分) String tableName = "ZLXT-basic_registered_log-"; String operationName = ".bin"; this.upMethod(fileName, upSrc, tableName, operationName); } sftp.disconnect(); // session.disconnect(); } @Override public void creatSevenUpFileAndUp(String yesterday) throws Exception { JSch jsch = new JSch(); Session session = jsch.getSession(Constant.user, Constant.host, 22); session.setPassword(Constant.password1); session.setConfig("StrictHostKeyChecking", "no"); session.connect(200000); ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp"); sftp.connect(200000); // 连接远程服务器,日志存放地 String yesterdayStr = DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"); String directoryPath = Constant.sixRemoteLogPackage + yesterdayStr; List allFile = listFiles(directoryPath, sftp); if (CollUtil.isEmpty(allFile)) { XxlJobHelper.log("表4:昨日无日志文件夹!"); return; } for (String filePath : allFile) { // 读取远程服务器上的日志文件 gz压缩格式 GZIPInputStream gzipInputStream = new GZIPInputStream(sftp.get(directoryPath + "/" + filePath)); Reader decoder = new InputStreamReader(gzipInputStream, "UTF-8"); BufferedReader reader = new BufferedReader(decoder); // 创建输出流 FileOutputStream outputStream = new FileOutputStream(Constant.localNotEncryFileNamePath); String tempString; //一次读一行,读入null时文件结束 while ((tempString = reader.readLine()) != null) { // 获得请求时间 JSONObject jsonObject = JSON.parseObject(tempString); String operatorTime = jsonObject.getString("operatorTime"); Date date1 = DateUtil.parse(operatorTime, "yyyyMMddHHmmss"); String requestTime = DateUtil.format(date1, "yyyy-MM-dd HH:mm:ss"); // 获得响应时间 String responseTime = requestTime; // 获得源IP String sourceIP = jsonObject.getString("userIp"); // 端口 String port = "80"; // 获得目的IP String destinationIP = jsonObject.getString("idisIp"); // 方法 String method = "HTTPM_GET"; // 企业标识码 String companyCode = jsonObject.getString("prefix"); // 标识编码 String ideCode = jsonObject.getString("handleName"); // // 组装字符串 tempString = requestTime + "|" + responseTime + "|" + sourceIP + "|" + port + "|" + destinationIP + "|" + port + "|" + method + "|" + companyCode + "|" + ideCode + "|" + "|" + "|" + "|" + "http://code.qnaiot.com" + "|" + "http://code.qnaiot.com" + "|" + "\n"; byte[] bytes = tempString.getBytes(); outputStream.write(bytes); } outputStream.flush(); outputStream.close(); decoder.close(); gzipInputStream.close(); // 上传 String fileName = Constant.localNotEncryFileNamePath; // 要上传的目录 String upSrc = "/005/" + yesterday; // 表名(文件名的一部分) String tableName = "ZLXT-basic_identification_log-"; String operationName = ".bin"; this.upMethod(fileName, upSrc, tableName, operationName); } sftp.disconnect(); } @Override public void deletUpFile(String localPackage) { File directory = new File(localPackage); for (File file : Objects.requireNonNull(directory.listFiles())) { if (!file.isDirectory()) { file.delete(); } } } // private void deleteFile(String src) { // // System.out.println(src); // File file = new File( src); // boolean delete = file.delete(); // System.out.println(delete); // } private String judge(int companyType) { //单位性质,国有控股、民营控股、外商控股、事业单位、民营非盈利 if (companyType == 1) { return "国有控股"; } if (companyType == 2) { return "民营控股"; } if (companyType == 3) { return "外商控股"; } if (companyType == 4) { return "事业单位"; } if (companyType == 5) { return "民营非盈利"; } return null; } /** * 列出目录下的文件 * * @param directory 要列出的目录 * @return * @throws SftpException * @throws JSchException */ private List listFiles(String directory, ChannelSftp channelSftp) { // 显示目录信息 Vector ls = null; try { ls = channelSftp.ls(directory); } catch (SftpException e) { return null; } Iterator iterator = ls.iterator(); try { ArrayList list = new ArrayList<>(); while (iterator.hasNext()) { ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next(); //文件名称 String fileName = file.getFilename(); //移除上级目录和根目录:"." ".." if (".".equals(fileName) || "..".equals(fileName)) { continue; } list.add(fileName); } return list; } catch (Exception e) { e.printStackTrace(); } return null; } }