package weaver.interfaces.hzzx.cronjob; import cn.hutool.http.HttpRequest; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.engine.hzzx.conn.DataUtil; import com.engine.hzzx.entity.Department; import com.engine.hzzx.entity.Employee; import com.engine.hzzx.entity.EmployeeTrans; import com.engine.hzzx.entity.Position; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import weaver.common.DateUtil; import weaver.conn.RecordSet; import weaver.general.Util; import weaver.interfaces.schedule.BaseCronJob; import java.util.*; /** * @author:dxfeng * @createTime: 2025/03/12 * @version: 1.0 */ public class SyncBeiSenInfoJob extends BaseCronJob { private static final String TOKEN_URL = "https://openapi.italent.cn/token"; private static final String GET_EMPLOYMENT_URL = "https://openapi.italent.cn/TenantBaseExternal/api/v5/Employee/GetByTimeWindow"; private static final String GET_DEPARTMENT_URL = "https://openapi.italent.cn/TenantBaseExternal/api/v5/Organization/GetByIds"; private static final String GET_POSITION_URL = "https://openapi.italent.cn/TenantBaseExternal/api/v5/Position/GetByOIds"; private static final String APP_KEY = "EE7D602509E04A59AE5DEFF0841F6A1C"; private static final String APP_SECRET = "F5229D9D83BF45DEBFA1EF8DAC13C0339D0074419CFE45FA8BAC2A0B9D170798"; private static final String EMPLOYEE_TABLE = "uf_ybygzqbssjtz"; private static final String INTERNSHIP_TABLE = "uf_sxszqbssjtz"; private static final String OPERATE_ID = "1"; RecordSet rs = new RecordSet(); @Override public void execute() { try { String accessToken = getToken(); // 获取近两天的数据 String startTime = DateUtil.getYesterday() + "T00:00:00"; String stopTime = DateUtil.getCurrentDate() + "T23:59:59"; // TODO 测试使用 startTime = "2025-03-01T00:00:00"; int[] empStatus = {1}; int[] employType = {0}; int[] internshipType = {2}; List employeeList = new ArrayList<>(); getPendingEmployment(startTime, stopTime, empStatus, employType, accessToken, "", employeeList); if (CollectionUtils.isNotEmpty(employeeList)) { // 遍历待入职信息,插入数据库 for (Employee employee : employeeList) { rs.writeLog("遍历待入职信息===" + employee.toString()); insertEmployee(employee); } } List internshipList = new ArrayList<>(); getPendingEmployment(startTime, stopTime, empStatus, internshipType, accessToken, "", internshipList); if (CollectionUtils.isNotEmpty(internshipList)) { // 遍历实习生信息,插入数据库 for (Employee employee : internshipList) { rs.writeLog("遍历实习生信息===" + employee.toString()); insertInternshipEmployee(employee); } } } catch (Exception e) { rs.writeLog("同步失败", e.getMessage()); } } /** * 获取待入职信息 * * @param startTime * @param stopTime * @param empStatus * @param accessToken * @param scrollId * @param employeeList * @return */ private String getPendingEmployment(String startTime, String stopTime, int[] empStatus, int[] employType, String accessToken, String scrollId, List employeeList) { JSONObject json = new JSONObject(); json.put("startTime", startTime); json.put("stopTime", stopTime); json.put("empStatus", empStatus); json.put("employType", employType); if (StringUtils.isNotBlank(scrollId)) { json.put("scrollId", scrollId); } rs.writeLog("GetByTimeWindow,入参===" + json.toString()); String response = HttpRequest.post(GET_EMPLOYMENT_URL) .header("Authorization", "Bearer " + accessToken) .body(json.toString()) .contentType("application/json") .execute() .body(); rs.writeLog("GetByTimeWindow,返参===" + response); JSONObject jsonObject = JSONObject.parseObject(response); String code = jsonObject.getString("code"); if (!"200".equals(code)) { throw new RuntimeException("GetByTimeWindow接口调用失败"); } scrollId = jsonObject.getString("scrollId"); JSONArray data = jsonObject.getJSONArray("data"); if (data == null || data.size() == 0) { rs.writeLog("GetByTimeWindow,无数据"); return ""; } for (int i = 0; i < data.size(); i++) { JSONObject dataObject = data.getJSONObject(i); JSONObject employeeInfo = dataObject.getJSONObject("employeeInfo"); JSONObject recordInfo = dataObject.getJSONObject("recordInfo"); Long oIdDepartment = recordInfo.getLong("oIdDepartment"); String oIdJobPosition = recordInfo.getString("oIdJobPosition"); Department department = getDepartment(oIdDepartment, accessToken); Position position = getPosition(oIdJobPosition, accessToken); Employee employeeObject = JSONObject.parseObject(employeeInfo.toJSONString(), Employee.class); if (null != department) { // 设置部门编号 employeeObject.setDepartmentCode(department.getCode()); } if (null != position) { // 设置岗位编号 employeeObject.setPositionCode(position.getCode()); } employeeList.add(employeeObject); } return getPendingEmployment(startTime, stopTime, empStatus, employType, accessToken, scrollId, employeeList); } /** * 获取access_token * * @return */ private String getToken() { JSONObject json = new JSONObject(); json.put("grant_type", "client_credentials"); json.put("app_key", APP_KEY); json.put("app_secret", APP_SECRET); rs.writeLog("获取token,入参===" + json); String response = HttpRequest.post(TOKEN_URL) .body(json.toString()) .contentType("application/json") .execute() .body(); rs.writeLog("获取token,返参===" + response); JSONObject jsonObject = JSONObject.parseObject(response); String accessToken = jsonObject.getString("access_token"); if (StringUtils.isBlank(accessToken)) { throw new RuntimeException("获取token失败"); } return accessToken; } /** * 获取部门信息 * * @param oId * @param accessToken * @return */ private Department getDepartment(Long oId, String accessToken) { JSONObject json = new JSONObject(); json.put("oIds", new Long[]{oId}); rs.writeLog("GET_DEPARTMENT_URL,入参===" + json); String response = HttpRequest.post(GET_DEPARTMENT_URL) .header("Authorization", "Bearer " + accessToken) .body(json.toString()) .contentType("application/json") .execute() .body(); rs.writeLog("GET_DEPARTMENT_URL,返参===" + response); JSONObject jsonObject = JSONObject.parseObject(response); String code = jsonObject.getString("code"); if (!"200".equals(code)) { throw new RuntimeException("GET_DEPARTMENT_URL接口调用失败"); } JSONArray data = jsonObject.getJSONArray("data"); if (data.size() == 0) { return null; } Department department = new Department(); JSONObject dataObject = data.getJSONObject(0); department.setOId(dataObject.getString("oId")); department.setName(dataObject.getString("name")); department.setShortName(dataObject.getString("shortName")); return department; } /** * 获取职位信息 * * @param oId * @param accessToken * @return */ private Position getPosition(String oId, String accessToken) { JSONObject json = new JSONObject(); json.put("oIds", new String[]{oId}); rs.writeLog("GET_POSITION_URL,入参===" + json); String response = HttpRequest.post(GET_POSITION_URL) .header("Authorization", "Bearer " + accessToken) .body(json.toString()) .contentType("application/json") .execute() .body(); rs.writeLog("GET_POSITION_URL,返参===" + response); JSONObject jsonObject = JSONObject.parseObject(response); String code = jsonObject.getString("code"); if (!"200".equals(code)) { throw new RuntimeException("GET_POSITION_URL接口调用失败"); } JSONArray data = jsonObject.getJSONArray("data"); if (data.size() == 0) { return null; } JSONObject dataObject = data.getJSONObject(0); Position position = new Position(); position.setOId(dataObject.getString("oId")); position.setName(dataObject.getString("name")); position.setCode(dataObject.getString("code")); return position; } /** * 插入一般员工数据 * * @param employee */ private void insertEmployee(Employee employee) { Map insertData = new HashMap<>(); // 判断是否存在 String objectId = employee.getObjectId(); String sql = "select id from " + EMPLOYEE_TABLE + " where modeuuid = ?"; if (StringUtils.isNotBlank(objectId)) { rs.executeQuery(sql, objectId); if (rs.next()) { rs.writeLog("查询员工信息,objectId==" + objectId + ",oaId==" + rs.getString("id")); // 重复数据不做插入操作,直接返回 return; } } else { objectId = UUID.randomUUID().toString(); } int formModeId = DataUtil.getModeIdByTableName(EMPLOYEE_TABLE); // 构建插入数据 insertData.put("xm", employee.getName()); insertData.put("xb", Util.null2String(employee.getGender())); insertData.put("nl", String.valueOf(employee.getAge())); // 政治面貌 rs.writeLog("政治面貌Status==" + employee.getPoliticalStatus()); String politicalStr = EmployeeTrans.getPoliticalStr(employee.getPoliticalStatus()); rs.writeLog("政治面貌Str==" + politicalStr); if (StringUtils.isNotBlank(politicalStr)) { String zzmm = DataUtil.getSelectValue(String.valueOf(formModeId), "zzmm", politicalStr); rs.writeLog("政治面貌Id==" + zzmm); insertData.put("zzmm", zzmm); } // 部门 String departmentId = EmployeeTrans.getDepartmentId(employee.getDepartmentCode()); if (StringUtils.isNotBlank(departmentId)) { insertData.put("ypbm", departmentId); } // 职位 String jobtitleId = EmployeeTrans.getJobtitleId(employee.getPositionCode()); if (StringUtils.isNotBlank(jobtitleId)) { insertData.put("ypgw", jobtitleId); } // 学历 String educationLevelId = EmployeeTrans.getEducationLevelId(employee.getEducationLevel()); if (StringUtils.isNotBlank(educationLevelId)) { insertData.put("xl", educationLevelId); } // 学校 // TODO insertData.put("xx", employee.getLastSchool()); // 专业 insertData.put("zy", employee.getMajor()); // 工作年限 // 构建建模基本数据 insertData.put("modeuuid", objectId); insertData.put("formmodeid", String.valueOf(formModeId)); DataUtil.buildModeInsertFields(insertData, OPERATE_ID); // 插入数据,刷新权限 DataUtil.insertData(insertData, EMPLOYEE_TABLE); DataUtil.refreshRight(objectId, EMPLOYEE_TABLE, formModeId, OPERATE_ID); } private void insertInternshipEmployee(Employee employee) { Map insertData = new HashMap<>(); // 判断是否存在 String objectId = employee.getObjectId(); String sql = "select id from " + INTERNSHIP_TABLE + " where modeuuid = ?"; if (StringUtils.isNotBlank(objectId)) { rs.executeQuery(sql, objectId); if (rs.next()) { rs.writeLog("查询实习生信息,objectId==" + objectId + ",oaId==" + rs.getString("id")); // 重复数据不做插入操作,直接返回 return; } } else { objectId = UUID.randomUUID().toString(); } int formModeId = DataUtil.getModeIdByTableName(INTERNSHIP_TABLE); // 构建插入数据 insertData.put("xm", employee.getName()); insertData.put("xb", Util.null2String(employee.getGender())); // insertData.put("nl", String.valueOf(employee.getAge())); // 政治面貌 //String politicalStr = EmployeeTrans.getPoliticalStr(employee.getPoliticalStatus()); //if (StringUtils.isNotBlank(politicalStr)) { // insertData.put("zzmm", DataUtil.getSelectValue(formModeId, "zzmm", politicalStr)); //} //// 部门 //String departmentId = EmployeeTrans.getDepartmentId(employee.getDepartmentCode()); //if (StringUtils.isNotBlank(departmentId)) { // insertData.put("ypbm", departmentId); //} // 岗位 String jobtitleId = EmployeeTrans.getJobtitleId(employee.getPositionCode()); if (StringUtils.isNotBlank(jobtitleId)) { insertData.put("gw", jobtitleId); } // 目前学历 String educationLevelId = EmployeeTrans.getEducationLevelId(employee.getEducationLevel()); if (StringUtils.isNotBlank(educationLevelId)) { insertData.put("mqxl", educationLevelId); } // 就读学校 insertData.put("jdxx", employee.getLastSchool()); // 就读专业 insertData.put("jdzy", employee.getMajor()); // 工作年限 // 构建建模基本数据 insertData.put("modeuuid", objectId); insertData.put("formmodeid", String.valueOf(formModeId)); DataUtil.buildModeInsertFields(insertData, OPERATE_ID); // 插入数据,刷新权限 DataUtil.insertData(insertData, INTERNSHIP_TABLE); DataUtil.refreshRight(objectId, INTERNSHIP_TABLE, formModeId, OPERATE_ID); } }