package weaver.interfaces.hzzx.cronjob; import cn.hutool.http.HttpRequest; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.engine.hzzx.entity.Department; import com.engine.hzzx.entity.Employee; 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.interfaces.schedule.BaseCronJob; import java.util.ArrayList; import java.util.List; /** * @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"; RecordSet rs = new RecordSet(); @Override public void execute() { String accessToken = getToken(); // 获取近两天的数据 String startTime = DateUtil.getYesterday() + "T00:00:00"; String stopTime = DateUtil.getCurrentDate() + "T23:59:59"; 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()); } } List internshipList = new ArrayList<>(); getPendingEmployment(startTime, stopTime, empStatus, internshipType, accessToken, "", internshipList); if (CollectionUtils.isNotEmpty(internshipList)) { // 遍历实习生信息,插入数据库 for (Employee employee : internshipList) { rs.writeLog("遍历实习生信息", employee.toString()); } } } /** * 获取待入职信息 * * @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.size() == 0) { return ""; } for (int i = 0; i < data.size(); i++) { JSONObject dataObject = data.getJSONObject(i); JSONObject employeeInfo = dataObject.getJSONObject("employeeInfo"); JSONObject recordInfo = dataObject.getJSONObject("recordInfo"); String oIdDepartment = recordInfo.getString("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.toString()); 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(String oId, String accessToken) { JSONObject json = new JSONObject(); json.put("oIds", new String[]{oId}); rs.writeLog("GET_DEPARTMENT_URL,入参", json.toString()); 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.toString()); 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; } }