From 23fefd6ab86b0f80d9fba8f3e95f7adabc29f066 Mon Sep 17 00:00:00 2001
From: "youHong.ai" <774495953@qq.com>
Date: Wed, 23 Nov 2022 18:52:38 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ecsv=E8=AF=BB=E5=8F=96?=
=?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C=E4=BA=BA=E5=91=98=E7=BB=84?=
=?UTF-8?q?=E7=BB=87=E7=BB=93=E6=9E=84=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E4=BB=8Ewesmat=E6=95=B4=E7=90=86=E5=88=B0hr=E7=B3=BB=E7=BB=9F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../youhong/ai/csv/ReadCsvUtil.java | 70 ++
.../youhong/ai/csv/entity/CsvTable.java | 128 +++
.../youhong/ai/sftp/SftpConnectUtil.java | 199 +++-
...ice.java => OrganizationAsyncService.java} | 2 +-
.../hrorganization/sftp/FetchDataUtil.java | 57 --
.../wesmat/SyncOrganizationForOtherAPI.java | 213 ++++
.../wesmat/config/SyncOrganizationConfig.java | 161 +++
.../wesmat/model/Department.java | 87 ++
.../hrorganization/wesmat/model/Employee.java | 276 ++++++
.../hrorganization/wesmat/model/Position.java | 132 +++
.../wesmat/result/ApiAsyncConfigResult.java | 121 +++
.../wesmat/result/GetOrganizationResult.java | 49 +
.../wesmat/result/ResultBean.java | 151 +++
.../wesmat/util/GetModelValue.java | 20 +
.../wesmat/util/SyncOrganizationUtils.java | 917 ++++++++++++++++++
src/main/resources/WEB-INF/log4j.xml | 9 +-
src/test/java/basetest/BaseTest.java | 29 +-
.../java/youhong/ai/pcn/TestOrganization.java | 24 +-
18 files changed, 2533 insertions(+), 112 deletions(-)
create mode 100644 src/main/java/ebu7common/youhong/ai/csv/ReadCsvUtil.java
create mode 100644 src/main/java/ebu7common/youhong/ai/csv/entity/CsvTable.java
rename src/main/java/weaver/youhong/ai/pcn/hrorganization/service/{OranizationAsyncService.java => OrganizationAsyncService.java} (81%)
delete mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/sftp/FetchDataUtil.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/SyncOrganizationForOtherAPI.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/config/SyncOrganizationConfig.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/model/Department.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/model/Employee.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/model/Position.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/result/ApiAsyncConfigResult.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/result/GetOrganizationResult.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/result/ResultBean.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/util/GetModelValue.java
create mode 100644 src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/util/SyncOrganizationUtils.java
diff --git a/src/main/java/ebu7common/youhong/ai/csv/ReadCsvUtil.java b/src/main/java/ebu7common/youhong/ai/csv/ReadCsvUtil.java
new file mode 100644
index 0000000..5567d6f
--- /dev/null
+++ b/src/main/java/ebu7common/youhong/ai/csv/ReadCsvUtil.java
@@ -0,0 +1,70 @@
+package ebu7common.youhong.ai.csv;
+
+import aiyh.utils.excention.CustomerException;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ *
读取csv文件
+ *
+ * create: 2022-11-23 17:19
+ *
+ * @author youHong.ai
+ */
+
+public class ReadCsvUtil {
+
+
+ /**
+ * 读取csv文件数据
+ *
+ * @param path csv文件路径
+ * @return csv文件shave
+ */
+ public static List> readCsvFromPath(String path) {
+ try {
+ return readScvFromInputStream(new FileInputStream(path));
+ } catch (FileNotFoundException e) {
+ throw new CustomerException("[读取CSV文件,插入数据时,读取文件异常]");
+ }
+ }
+
+
+ /**
+ * 读取csv文件
+ *
+ * @param inputStream 文件流
+ * @return csv文件
+ */
+ public static List> readScvFromInputStream(InputStream inputStream) {
+ List> tableInfo = new ArrayList<>();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
+ String[] fieldsArr = null;
+ String line = null;
+ try {
+ while ((line = reader.readLine()) != null) {
+ String str;
+ line += ",";
+ Pattern pCells = Pattern
+ .compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
+ Matcher mCells = pCells.matcher(line);
+ List cells = new ArrayList<>();
+ while (mCells.find()) {
+ str = mCells.group();
+ str = str.replaceAll(
+ "(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
+ str = str.replaceAll("(?sm)(\"(\"))", "$2");
+ cells.add(str);
+ }
+ tableInfo.add(cells);
+ }
+ } catch (IOException e) {
+ throw new CustomerException("无法读取文件数据!", e);
+ }
+ return tableInfo;
+ }
+}
diff --git a/src/main/java/ebu7common/youhong/ai/csv/entity/CsvTable.java b/src/main/java/ebu7common/youhong/ai/csv/entity/CsvTable.java
new file mode 100644
index 0000000..8e760f5
--- /dev/null
+++ b/src/main/java/ebu7common/youhong/ai/csv/entity/CsvTable.java
@@ -0,0 +1,128 @@
+package ebu7common.youhong.ai.csv.entity;
+
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * csv表信息
+ *
+ * create: 2022-11-23 17:19
+ *
+ * @author youHong.ai
+ */
+
+@Setter
+@Getter
+@ToString
+public class CsvTable {
+
+ private List csvRows = new ArrayList<>();
+
+ private CsvTableHeard csvTableHeard;
+
+ private int rowSize = 0;
+
+
+ public void setCsvTableHeard(CsvTableHeard csvTableHeard) {
+ this.csvTableHeard = csvTableHeard;
+ this.rowSize++;
+
+ }
+
+ public void addRow(CsvRow csvRow) {
+ csvRows.add(csvRow);
+ rowSize++;
+ }
+
+
+ /**
+ * csv列数据
+ *
+ * create: 2022-11-23 17:21
+ *
+ * @author youHong.ai
+ */
+
+ @Setter
+ @Getter
+ @ToString
+ static class CsvColumn {
+
+
+ }
+
+
+ /**
+ * csv表行数据
+ *
+ * create: 2022-11-23 17:21
+ *
+ * @author youHong.ai
+ */
+
+ @Setter
+ @Getter
+ @ToString
+ static class CsvRow {
+ private List csvColumns = new ArrayList<>();
+ private int rowSize = 0;
+
+ public void add(CsvColumn csvColumn) {
+ csvColumns.add(csvColumn);
+ rowSize++;
+ }
+
+
+ }
+
+
+ /**
+ * csv表头数据
+ *
+ * create: 2022-11-23 17:21
+ *
+ * @author youHong.ai
+ */
+ @ToString
+ static class CsvTableHeard {
+ private List heard;
+
+ private Map mapping;
+
+ private int heardSize = 0;
+
+ public void addHeard(String value) {
+ if (heard == null) {
+ this.heard = new ArrayList<>();
+ this.mapping = new HashMap<>();
+ }
+ mapping.put(value, heardSize++);
+ heard.add(value);
+ }
+
+ public int getHeardRow(String value) {
+ if (mapping != null) {
+ return mapping.get(value);
+ }
+ return -1;
+ }
+
+ public String getHeard(int columnIndex) {
+ if (heard != null) {
+ return this.heard.get(columnIndex);
+ }
+ return null;
+ }
+
+ }
+
+
+}
+
diff --git a/src/main/java/ebu7common/youhong/ai/sftp/SftpConnectUtil.java b/src/main/java/ebu7common/youhong/ai/sftp/SftpConnectUtil.java
index 3395123..4422314 100644
--- a/src/main/java/ebu7common/youhong/ai/sftp/SftpConnectUtil.java
+++ b/src/main/java/ebu7common/youhong/ai/sftp/SftpConnectUtil.java
@@ -1,15 +1,18 @@
package ebu7common.youhong.ai.sftp;
+import aiyh.utils.Util;
import aiyh.utils.excention.CustomerException;
import com.jcraft.jsch.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
+import weaver.general.GCONST;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Hashtable;
import java.util.Properties;
+import java.util.UUID;
import java.util.Vector;
/**
@@ -68,8 +71,16 @@ public class SftpConnectUtil extends ChannelSftp {
private Integer sftpTimeOut;
- public SftpConnectUtil(String userName, String prvKeyFilePath,String password, String sftpIp,
- Integer port, Integer sftpTimeOut) {
+ /**
+ * @param userName 用户姓名
+ * @param prvKeyFilePath 密钥文件地址
+ * @param password 密码 (密钥文件随便传递一个)
+ * @param sftpIp 链接ip地址
+ * @param port 端口
+ * @param sftpTimeOut 超时时间
+ * @author youHong.ai
+ */
+ public SftpConnectUtil(String userName, String prvKeyFilePath, String password, String sftpIp, Integer port, Integer sftpTimeOut) {
this.userName = userName;
this.prvKeyFilePath = prvKeyFilePath;
this.sftpIp = sftpIp;
@@ -86,8 +97,7 @@ public class SftpConnectUtil extends ChannelSftp {
* @author youHong.ai
*/
- private void login() {
-
+ private void login() {
JSch jSch = new JSch();
try {
if (prvKeyFilePath != null && !"".equals(prvKeyFilePath)) {
@@ -97,7 +107,7 @@ public class SftpConnectUtil extends ChannelSftp {
jSch.addIdentity(prvKeyFilePath);
}
} else {
- throw new RuntimeException("从sftp下载需要的ppk文件未找到");
+ throw new RuntimeException("从sftp下载需要的ppk/pem文件未找到");
}
if (port != null && port > 0) {
this.session = jSch.getSession(userName, sftpIp, port);
@@ -114,10 +124,153 @@ public class SftpConnectUtil extends ChannelSftp {
this.sftp = (ChannelSftp) channel;
this.success = true;
} catch (JSchException e) {
+ this.close();
throw new CustomerException("SFTP链接失败!", e);
}
}
+
+ /**
+ * 下载文件
+ *
+ * @param remoteFile 远程文件名(路径)
+ * @param localeFile 本地文件名
+ * @return 本地文件路径
+ * @throws IOException io异常
+ * @author youHong.ai
+ */
+ public String downFile(String remoteFile, String localeFile) throws IOException {
+ if (!isExist(remoteFile)) {
+ throw new FileNotFoundException("远程文件未找到!no search remote file");
+ }
+ try {
+ File file = new File(localeFile);
+ if (!file.exists()) {
+ boolean mkdirs = file.getParentFile().mkdirs();
+ if (!mkdirs) {
+ throw new CustomerException("无法创建目录(can not create dirs):[" + localeFile + "]");
+ }
+ } else {
+ String parent = file.getParent();
+ String name = file.getName();
+ if (null == parent) {
+ parent = GCONST.getSysFilePath();
+ }
+ if (!parent.endsWith(String.valueOf(File.separatorChar))) {
+ parent = parent + File.separatorChar;
+ }
+ localeFile = parent + System.currentTimeMillis() + UUID.randomUUID() + "_" + name;
+ }
+ this.get(remoteFile, Files.newOutputStream(Paths.get(localeFile)));
+ return localeFile;
+ } catch (SftpException e) {
+ throw new CustomerException("下载文件出错,down file error!");
+ }
+ }
+
+ /**
+ * 下载文件到系统文件目录中
+ *
+ * @param remoteFile 远程文件全路径
+ * @param localeFileName 下载后的文件名称,只需要名称
+ * @return 下载后的文件路径
+ * @throws IOException IO异常
+ */
+ public String downFileToSystemFilePath(String remoteFile, String localeFileName) throws IOException {
+ return this.downFile(remoteFile, GCONST.getSysFilePath() + localeFileName);
+ }
+
+
+ /**
+ * 下载文件到系统中,并生成docId
+ *
+ * @param remoteFile 远程文件路径
+ * @param catalogue doc目录id
+ * @param userId 文件创建者id
+ * @return docId
+ * @throws Exception 异常信息
+ */
+ public int downFileToDoc(String remoteFile, int catalogue, int userId) throws Exception {
+ if (!this.isExist(remoteFile)) {
+ throw new FileNotFoundException("远程文件未找到!no search remote file");
+ }
+ Vector ls = null;
+ try {
+ ls = this.ls(remoteFile);
+ } catch (SftpException e) {
+ throw new CustomerException("无法查询当前文件信息!can not find file info~", e);
+ }
+ if (ls.size() == 0) {
+ throw new CustomerException("无法查询当前文件信息,ls 大小为0!can not find file info~ls size is 0");
+ }
+ String fileName = null;
+// 查看远程文件文件名
+ for (int i = 0; i < ls.size(); i++) {
+ Object obj = ls.elementAt(i);
+ if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
+ LsEntry entry = (LsEntry) obj;
+ if (!entry.getAttrs().isDir()) {
+ fileName = entry.getFilename();
+ }
+ if (entry.getAttrs().isDir()) {
+ if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
+ fileName = entry.getFilename();
+ }
+ }
+ }
+ }
+ if (null == fileName) {
+ throw new CustomerException("无法自动获取文件名!");
+ }
+ InputStream inputStream;
+ try {
+ inputStream = this.get(remoteFile);
+ } catch (SftpException e) {
+ throw new CustomerException("下载文件是出错!down file error", e);
+ }
+ this.get(remoteFile);
+ int docId = Util.createDoc(fileName, catalogue, inputStream, userId);
+ return docId;
+ }
+
+ /**
+ * 下载文件流
+ *
+ * @param remoteFile 远程文件路径
+ * @return 文件流
+ * @throws FileNotFoundException 文件未找到
+ */
+ public InputStream down(String remoteFile) throws FileNotFoundException {
+ if (!this.isExist(remoteFile)) {
+ throw new FileNotFoundException("远程文件未找到!no search remote file");
+ }
+ try {
+ return this.get(remoteFile);
+ } catch (SftpException e) {
+ throw new CustomerException("下载文件是出错!down file error", e);
+ }
+ }
+
+ /**
+ * 判断文件是否存在
+ *
+ * @param remotePath 远程文件路径
+ * @return 是否存在
+ */
+ public boolean isExist(String remotePath) {
+ try {
+ this.ls(remotePath);
+ return true;
+ } catch (SftpException ignore) {
+ }
+ try {
+ this.lstat(remotePath);
+ return true;
+ } catch (SftpException e) {
+ return false;
+ }
+ }
+
/**
* 关闭连接 server
*
@@ -139,7 +292,7 @@ public class SftpConnectUtil extends ChannelSftp {
/**
* 关闭链接释放资源
*/
- public void close(){
+ public void close() {
this.login();
}
@@ -149,13 +302,13 @@ public class SftpConnectUtil extends ChannelSftp {
}
@Override
- public void setBulkRequests(int bulk_requests) throws JSchException {
- this.sftp.setBulkRequests(bulk_requests);
+ public int getBulkRequests() {
+ return this.sftp.getBulkRequests();
}
@Override
- public int getBulkRequests() {
- return this.sftp.getBulkRequests();
+ public void setBulkRequests(int bulk_requests) throws JSchException {
+ this.sftp.setBulkRequests(bulk_requests);
}
@Override
@@ -168,21 +321,11 @@ public class SftpConnectUtil extends ChannelSftp {
return this.sftp.isEOF();
}
- @Override
- public void setInputStream(InputStream in) {
- this.sftp.setInputStream(in);
- }
-
@Override
public void setInputStream(InputStream in, boolean dontclose) {
this.sftp.setInputStream(in, dontclose);
}
- @Override
- public void setOutputStream(OutputStream out) {
- this.sftp.setOutputStream(out);
- }
-
@Override
public void setOutputStream(OutputStream out, boolean dontclose) {
this.sftp.setOutputStream(out, dontclose);
@@ -203,6 +346,11 @@ public class SftpConnectUtil extends ChannelSftp {
return this.sftp.getInputStream();
}
+ @Override
+ public void setInputStream(InputStream in) {
+ this.sftp.setInputStream(in);
+ }
+
@Override
public InputStream getExtInputStream() throws IOException {
return this.sftp.getExtInputStream();
@@ -213,6 +361,11 @@ public class SftpConnectUtil extends ChannelSftp {
return this.sftp.getOutputStream();
}
+ @Override
+ public void setOutputStream(OutputStream out) {
+ this.sftp.setOutputStream(out);
+ }
+
@Override
public void quit() {
this.sftp.quit();
diff --git a/src/main/java/weaver/youhong/ai/pcn/hrorganization/service/OranizationAsyncService.java b/src/main/java/weaver/youhong/ai/pcn/hrorganization/service/OrganizationAsyncService.java
similarity index 81%
rename from src/main/java/weaver/youhong/ai/pcn/hrorganization/service/OranizationAsyncService.java
rename to src/main/java/weaver/youhong/ai/pcn/hrorganization/service/OrganizationAsyncService.java
index c7313df..2776e48 100644
--- a/src/main/java/weaver/youhong/ai/pcn/hrorganization/service/OranizationAsyncService.java
+++ b/src/main/java/weaver/youhong/ai/pcn/hrorganization/service/OrganizationAsyncService.java
@@ -8,7 +8,7 @@ package weaver.youhong.ai.pcn.hrorganization.service;
* @author youHong.ai
*/
-public class OranizationAsyncService {
+public class OrganizationAsyncService {
}
diff --git a/src/main/java/weaver/youhong/ai/pcn/hrorganization/sftp/FetchDataUtil.java b/src/main/java/weaver/youhong/ai/pcn/hrorganization/sftp/FetchDataUtil.java
deleted file mode 100644
index 713f78e..0000000
--- a/src/main/java/weaver/youhong/ai/pcn/hrorganization/sftp/FetchDataUtil.java
+++ /dev/null
@@ -1,57 +0,0 @@
-package weaver.youhong.ai.pcn.hrorganization.sftp;
-
-import ebu7common.youhong.ai.sftp.SftpConnectUtil;
-import com.jcraft.jsch.ChannelSftp;
-import com.jcraft.jsch.SftpException;
-
-import java.io.*;
-
-/**
- * 获取数据工具
- *
- * create: 2022-11-22 14:48
- *
- * HRIS_PositionExport20221119 职位文件名称
- * HRIS_EmployeeExport20210825 人员文件名称
- * HRIS_DepartmentExport20200503 部门文件名称
- *
- * @author youHong.ai
- */
-
-public class FetchDataUtil {
-
-
-
-
- public FetchDataUtil(){
-
- }
-
-
-
- public void downloadFile(SftpConnectUtil sftpConnectUtil,
- String fileName, String targetFile){
- try {
- // sftpConnectUtil.lcd(".");
- // Vector ls = sftpConnectUtil.ls(".");
- // for (Object l : ls) {
- // System.out.println(l);
- // }
- //
- File file = new File(targetFile);
- if (!file.exists()) {
- file.getParentFile().mkdirs();
- }
- sftpConnectUtil.get(fileName,new FileOutputStream(targetFile));
-
-
- } catch (SftpException e) {
- sftpConnectUtil.close();
- e.printStackTrace();
- // throw new CustomerException("下载文件出错,down file error!");
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
-
-}
\ No newline at end of file
diff --git a/src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/SyncOrganizationForOtherAPI.java b/src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/SyncOrganizationForOtherAPI.java
new file mode 100644
index 0000000..465d9df
--- /dev/null
+++ b/src/main/java/weaver/youhong/ai/pcn/hrorganization/wesmat/SyncOrganizationForOtherAPI.java
@@ -0,0 +1,213 @@
+package weaver.youhong.ai.pcn.hrorganization.wesmat;
+
+import aiyh.utils.Util;
+import aiyh.utils.zwl.common.ToolUtil;
+import com.weaver.esb.server.cache.ResourceComInfo;
+import weaver.hrm.company.DepartmentComInfo;
+import weaver.hrm.company.SubCompanyComInfo;
+import weaver.hrm.job.JobTitlesComInfo;
+import weaver.hrm.resource.HrmSynDAO;
+import weaver.integration.logging.Logger;
+import weaver.integration.logging.LoggerFactory;
+import weaver.interfaces.hrm.*;
+import weaver.matrix.MatrixUtil;
+import weaver.youhong.ai.pcn.hrorganization.wesmat.model.Department;
+import weaver.youhong.ai.pcn.hrorganization.wesmat.model.Employee;
+import weaver.youhong.ai.pcn.hrorganization.wesmat.model.Position;
+import weaver.youhong.ai.pcn.hrorganization.wesmat.result.GetOrganizationResult;
+import weaver.youhong.ai.pcn.hrorganization.wesmat.util.SyncOrganizationUtils;
+
+import java.util.*;
+
+
+/**
+ * @author EBU7-dev1-ayh
+ * @create 2021/7/20 0020 15:16
+ * 同步第三方系统人员组织结构
+ */
+
+
+public class SyncOrganizationForOtherAPI extends ToolUtil implements HrmSynService {
+ private final Logger log = LoggerFactory.getLogger(SyncOrganizationForOtherAPI.class);
+ private final GetOrganizationResult getOrganizationResult = new GetOrganizationResult();
+ private final SyncOrganizationUtils organizationUtils = new SyncOrganizationUtils();
+ String className = "SyncOrganizationForOtherAPI";
+ private HashMap synResult;
+
+ public SyncOrganizationForOtherAPI() {
+ this.writeDebuggerLog(className, "===========> create Object! <============");
+ this.synResult = new HashMap<>();
+ }
+
+ /**
+ * 同步到分公司(分部)
+ */
+ @Override
+ public String SynTimingToOASubCompany() {
+ this.writeDebuggerLog(className, "===========> synchronous company to OA system start <============");
+ this.synResult.put("1", null);
+ this.writeDebuggerLog(className, "===========> synchronous company to OA system end <============");
+ return null;
+ }
+
+ /**
+ * 同步到部门
+ */
+ @Override
+ public String SynTimingToOADepartment() {
+ this.writeDebuggerLog(className, "===========> synchronous department to OA system starts <============");
+ try {
+ List departmentList = getOrganizationResult.getDepartmentList();
+ Collections.sort(departmentList);
+ List