package com.kingdee.eas.custom.recuritment.task.osf;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.bos.rabbitmq.guava.Lists;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.beisen.utils.BeisenApiClient;
import com.kingdee.eas.custom.beisen.utils.BeisenParam;
import com.kingdee.eas.custom.beisen.utils.BeisenParamByProperties;
import com.kingdee.eas.custom.recuritment.bizEnum.ExecuteResultEnum;
import com.kingdee.eas.custom.recuritment.task.osf.database.MobileTypeEnum;
import com.kingdee.eas.util.ExceptionUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.util.enums.DynamicEnum;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* 237
* description: BeisenFacadeOSFService
* date: 2025/7/2 16:51
* author: lhbj
* version: 1.0
*/
public class BeisenFacadeOSFService implements IHRMsfService {
String syncBeisenReserveTalentPool ="syncBeisenReserveTalentPool";
String syncUnBeisenReserveTalentPool ="syncUnBeisenReserveTalentPool";
@Override
public Object process(Context context, Map map) throws EASBizException, BOSException {
String mack = (String) map.get("mack");
JSONObject result = null;
try {
if (syncBeisenReserveTalentPool.equals(mack)) {
String resignBizBillID = (String) map.get("resignBizBillID");
result = _syncBeisenReserveTalentPool(context, resignBizBillID);
}else if(syncUnBeisenReserveTalentPool.equals(mack)){
String resignBizBillID = (String) map.get("resignBizBillID");
result = _syncUnBeisenReserveTalentPool(context, resignBizBillID);
}else {
result = new JSONObject();
result.put("errMsg", "未找到实现方法");
}
} catch (Exception e) {
e.printStackTrace();
result = new JSONObject();
result.put("errMsg", e.getMessage());
}
return result;
}
/**
* 入职
*
* @param ctx
* @param resignBizBillID
* @return
* @throws BOSException
* @throws SQLException
* @throws IOException
*/
protected JSONObject _syncBeisenEmpenrollbizTalentPool(Context ctx, String resignBizBillID) throws BOSException, SQLException, IOException {
return null;
}
/**
* 离职-审核通过
*
* @param ctx
* @param resignBizBillID
* @return
* @throws BOSException
* @throws SQLException
* @throws IOException
*/
protected JSONObject _syncBeisenReserveTalentPool(Context ctx, String resignBizBillID) throws Exception {
JSONObject result = null;
StringBuilder sql = new StringBuilder();
sql.append(" select top 1 rbr.CFApplyId,rbr.CFCandidateId from T_REC_Offer offer ");
sql.append(" left join T_REC_ResumeBaseRec rbr on offer.FResumeBaseRecID=rbr.fid ");
sql.append(" where offer.FPERSONID in(select fpersonid from T_HR_ResignBizBillEntry where fbillid=?) order by offer.fsendDate desc ");
IRowSet rs = DbUtil.executeQuery(ctx, sql.toString(), new String[]{resignBizBillID});
String CFApplyId = "";
String CFCandidateId = "";
while (rs.next()) {
CFCandidateId = rs.getString("CFCandidateId");
CFApplyId = rs.getString("CFApplyId");
}
if (StringUtils.isNotBlank(CFCandidateId)) {
result =this.TransferPhase(ctx,BeisenParam.BEISEN_CONFIG_RESERVE_STATUSID,resignBizBillID,CFCandidateId,CFApplyId);
} else {
result =this.CreateOrUpdateApplicant(ctx, resignBizBillID);
}
return result;
}
/**
* 离职-反审批
* @param ctx
* @param resignBizBillID
* @return
* @throws BOSException
* @throws SQLException
* @throws IOException
*/
protected JSONObject _syncUnBeisenReserveTalentPool(Context ctx, String resignBizBillID) throws Exception {
JSONObject result = null;
StringBuilder sql = new StringBuilder();
sql.append(" select top 1 rbr.CFApplyId,rbr.CFCandidateId from T_REC_Offer offer ");
sql.append(" left join T_REC_ResumeBaseRec rbr on offer.FResumeBaseRecID=rbr.fid ");
sql.append(" where offer.FPERSONID in(select fpersonid from T_HR_ResignBizBillEntry where fbillid=?) order by offer.fsendDate desc ");
IRowSet rs = DbUtil.executeQuery(ctx, sql.toString(), new String[]{resignBizBillID});
String CFApplyId = "";
String CFCandidateId = "";
while (rs.next()) {
CFCandidateId = rs.getString("CFCandidateId");
CFApplyId = rs.getString("CFApplyId");
}
if (StringUtils.isNotBlank(CFApplyId)) {
result =this.TransferPhase(ctx,BeisenParam.BEISEN_CONFIG_UNRESERVE_STATUSID,resignBizBillID,CFCandidateId,CFApplyId);
} else {
result =this.removeApplicantFromTalentPool(ctx, resignBizBillID);
}
return result;
}
/**
* 移出人才库
* @param ctx
* @param resignBizBillID
* @return
* @throws Exception
*/
protected JSONObject removeApplicantFromTalentPool(Context ctx, String resignBizBillID) throws Exception {
JSONObject result = new JSONObject();
result.put("code", "ses");
result.put("msg", "成功");
ExecuteResultEnum syncStatus = ExecuteResultEnum.SUCCESS;
try {
IRowSet rowSet = DbUtil.executeQuery(ctx,"select CFCandidateId,CFSyncBeisenResult from T_HR_ResignBizBillEntry where fbillid=?",new String[]{resignBizBillID});
List candidateIds = Lists.newArrayList();
while (rowSet.next()){
String CFSyncBeisenResult = rowSet.getString("CFSyncBeisenResult");
String CFCandidateId = rowSet.getString("CFCandidateId");
if (StringUtils.isBlank(CFCandidateId)&&StringUtils.isNotBlank(CFSyncBeisenResult)) {
JSONObject jsonObject = JSONObject.parseObject(CFSyncBeisenResult);
if (null != jsonObject && 200 == jsonObject.getInteger("code")) {
JSONObject jsonObjectSync = this.GetApplicantIdBySyncId(jsonObject);
result.put("GetApplicantIdBySyncId", jsonObjectSync);
if (null != jsonObjectSync && 200 == jsonObjectSync.getInteger("code")) {
JSONObject dataSync = jsonObjectSync.getJSONObject("data");
if (null != dataSync) {
String candidateId= dataSync.getString("applicantId");
candidateIds.add(candidateId);
syncStatus = ExecuteResultEnum.SUCCESS;
}
}
}
}
if (StringUtils.isNotBlank(CFCandidateId)) {
candidateIds.add(CFCandidateId);
}
}
if(!candidateIds.isEmpty()) {
result.put("candidateIds",candidateIds);
//离职人才库id
Map objectMap = this.getBeisenTalentPool(ctx);
String talentPoolId=objectMap.get("talentPoolId");
BeisenApiClient client = BeisenApiClient.getInstance();
JSONObject jsonData = new JSONObject();
BeisenParamByProperties byProperties = new BeisenParamByProperties();
Map config = byProperties.getConfig();
jsonData.put("talentPoolId", talentPoolId);
jsonData.put("applicantIds", candidateIds);
System.out.println(jsonData.toJSONString());
JSONObject jsonObject = client.callApi(BeisenParam.POST_REMOVEAPPLICANTFROMTALENTPOOL_URL, jsonData);
result.put("POST_REMOVEAPPLICANTFROMTALENTPOOL_URL", jsonObject);
if (null != jsonObject && 200 == jsonObject.getInteger("code")) {
syncStatus = ExecuteResultEnum.SUCCESS;
} else {
syncStatus = ExecuteResultEnum.ERROR;
}
}
} catch (Exception e) {
e.printStackTrace();
syncStatus = ExecuteResultEnum.ERROR;
throw e;
} finally {
List candidateIds = (List) result.get("candidateIds");
if(null!=candidateIds&&(!candidateIds.isEmpty())) {
JSONObject jsonData = result.getJSONObject("POST_TRANSFERPHASE_DATA");
if(null==jsonData){
jsonData=new JSONObject();
}
jsonData.put("resultMsg",result.get("msg"));
updateSyncBeisenResultBillId(ctx, resignBizBillID, syncStatus, candidateIds.get(0), jsonData);
}else {
JSONObject jsonData = result.getJSONObject("POST_TRANSFERPHASE_DATA");
if(null==jsonData){
jsonData=new JSONObject();
}
jsonData.put("resultMsg",result.get("msg"));
updateSyncBeisenResultBillId(ctx, resignBizBillID, syncStatus, "", result.getJSONObject("POST_TRANSFERPHASE_DATA"));
}
}
return result;
}
/**
* 转移阶段状态
*
* @param ctx
* @param resignBizBillID 离职
* @param candidateId 应聘者id
* @param applyId 申请Id
* @return
* @throws Exception
*/
public JSONObject TransferPhase(Context ctx,String STATUSID, String resignBizBillID, String candidateId, String applyId) throws Exception {
JSONObject result = new JSONObject();
ExecuteResultEnum syncStatus = ExecuteResultEnum.SUCCESS;
try {
BeisenApiClient client = BeisenApiClient.getInstance();
JSONObject jsonData = new JSONObject();
BeisenParamByProperties byProperties = new BeisenParamByProperties();
Map config = byProperties.getConfig();
JSONArray applyIds = new JSONArray();
applyIds.add(applyId);
jsonData.put("applyIds", applyIds);
jsonData.put("phaseId", config.get(BeisenParam.BEISEN_CONFIG_PHASEID));
jsonData.put("statusId", config.get(STATUSID));
System.out.println(jsonData.toJSONString());
JSONObject jsonObject = client.callApi(BeisenParam.POST_TRANSFERPHASE_URL, jsonData);
result.put("POST_TRANSFERPHASE_DATA",jsonObject);
if (null != jsonObject && 200 == jsonObject.getInteger("code")) {
syncStatus = ExecuteResultEnum.SUCCESS;
}else {
syncStatus = ExecuteResultEnum.ERROR;
}
} catch (Exception e) {
e.printStackTrace();
syncStatus = ExecuteResultEnum.ERROR;
throw e;
} finally {
JSONObject jsonData = result.getJSONObject("POST_TRANSFERPHASE_DATA");
if(null==jsonData){
jsonData=new JSONObject();
}
jsonData.put("resultMsg",syncStatus.getAlias());
updateSyncBeisenResultBillId(ctx,resignBizBillID,syncStatus,candidateId,result.getJSONObject("POST_TRANSFERPHASE_DATA"));
}
return result;
}
/**
* 创建应聘者
*
* @param ctx
* @param resignBizBillID
* @return
* @throws Exception
*/
public JSONObject CreateOrUpdateApplicant(Context ctx, String resignBizBillID) throws Exception {
JSONObject result = new JSONObject();
ExecuteResultEnum syncStatus = ExecuteResultEnum.ERROR;
result.put("code", "ses");
result.put("msg", "");
String candidateId = "";
try {
Map objectMap = this.getBeisenTalentPool(ctx);
BeisenApiClient client = BeisenApiClient.getInstance();
Map personData = this.getPersonData(ctx, resignBizBillID);
String code = (String) personData.get("code");
result.put("code",personData.get("code"));
result.put("msg",personData.get("msg"));
if ("ses".equals(code)) {
Map data = (Map) personData.get("data");
if(null!=data&&(!data.isEmpty())) {
JSONObject jsonData = new JSONObject();
jsonData.putAll(data);
jsonData.put("talentPoolIds", objectMap.values());
BeisenParamByProperties byProperties = new BeisenParamByProperties();
Map config = byProperties.getConfig();
jsonData.put("channelId", config.get(BeisenParam.BEISEN_CONFIG_CHANNELID));
//jsonData.put("acquireMannerId",2);
System.out.println("同步北森参数:"+jsonData.toJSONString());
JSONObject jsonObject = client.callApi(BeisenParam.POST_CREATEORUPDATEAPPLICANT_URL, jsonData);
result.put("POST_CREATEORUPDATEAPPLICANT_DATA", jsonObject);
result.put("personData", personData);
if (null != jsonObject && 200 == jsonObject.getInteger("code")) {
JSONObject jsonObjectSync = this.GetApplicantIdBySyncId(jsonObject);
result.put("GetApplicantIdBySyncId", jsonObjectSync);
if (null != jsonObjectSync && 200 == jsonObjectSync.getInteger("code")) {
JSONObject dataSync = jsonObjectSync.getJSONObject("data");
if (null != dataSync) {
candidateId = dataSync.getString("applicantId");
syncStatus = ExecuteResultEnum.SUCCESS;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
syncStatus = ExecuteResultEnum.ERROR;
result.put("msg", ExceptionUtil.getExcLinkStackTrace(e));
result.put("code", "err");
throw e;
} finally {
JSONObject jsonData = result.getJSONObject("POST_CREATEORUPDATEAPPLICANT_DATA");
if(null==jsonData){
jsonData=new JSONObject();
}
jsonData.put("resultMsg",result.get("msg"));
updateSyncBeisenResultBillId(ctx, resignBizBillID, syncStatus, candidateId, jsonData);
}
return result;
}
/**
* 根据同步Id获取应聘者Id
*
* @param jsonObject
* @return
*/
public JSONObject GetApplicantIdBySyncId(JSONObject jsonObject) throws IOException {
BeisenApiClient client = BeisenApiClient.getInstance();
String syncId = "";
if (null != jsonObject) {
syncId = jsonObject.getString("data");
}
JSONObject object = client.callApi(BeisenParam.POST_GETAPPLICANTIDBYSYNCID_URL + "?syncId=" + syncId, jsonObject);
return object;
}
/**
* shr系统数据组装北森请求参数
*
* @param ctx
* @param resignBizBillID
* @return
* @throws BOSException
* @throws SQLException
*/
protected Map getPersonData(Context ctx, String resignBizBillID) throws BOSException, SQLException {
Map result = Maps.newHashMap();
result.put("code", "ses");
result.put("msg", "");
StringBuilder sql = new StringBuilder();
sql.append("");
sql.append(" select prm.faddress address,p.fbirthday birthday, ");
sql.append(" p.fname_l2 pName,p.fnumber pNumber, ");
sql.append(" re1.fenterDate enterDate,rbe.fleftcompanydate leftcompanydate,rbe.FBIZDATE bizDate, ");
sql.append(" poli.fname_l2 poliName,poli.fnumber poliNumber, poli.CFBeisenVal poliBeisenVal, ");
sql.append(" wed.fname_l2 wedName,wed.fnumber wedNumber, wed.CFBeisenVal wedBeisenVal, ");
sql.append(" folk.fname_l2 folkName,folk.Fnumber folkNumber, folk.CFBeisenVal folkBeisenVal, ");
sql.append(" dip.fnumber dipNumber,dip.fname_l2 dipName, dip.CFBeisenVal dipBeisenVal, ");
sql.append(" p.CFDyxlbyxx dyxlbyxx,p.FWorkLocation workLocation, ");
sql.append(" p.fidcardbegindate idCardbeginDate,p.fidcardenddate idCardendDate, ");
sql.append(" p.FHeight height, (case when (isnull(p.fgender,3)-1)>0 then (isnull(p.fgender,3)-1) else 1 end) gender, ");
sql.append(" isnull(p.fidcardno,p.fpassportno) idCardNO, (case when p.fidcardno is null then 2 else 1 end) isIdCard, ");
sql.append(" prm.femail email,prm.FLinkTelNum linkTelNum, ");
sql.append(" p.FIDCardIssueOrg idCardIssueOrg,rbe.FDescription description, ");
sql.append(" pos.fname_l2 posName,pos.Fnumber posNumber, ");
sql.append(" p.CFDyxlbysj dyxlbysj,p.FnativePlace_l2 nativePlace, ");
sql.append(" p.fofficePhone officePhone, prm.FNCell NCell,p.FhjAddress hjAddress,");
sql.append(" nl.fname_l2 nlName,nl.fnumber nlNumber,nl.CFBeisenVal nlBeisenVal,");
sql.append(" prm.FLinkName linkName,");
sql.append(" '' ");
sql.append(" from T_BD_Person p ");
sql.append(" inner join T_HR_ResignBizBillEntry rbe on rbe.fpersonid=p.fid ");
sql.append(" inner join (select re1.fpersonid,max(re1.fbizdate) fbizdate,min(re1.FENTERDATE) fenterDate from T_HR_ResignBizBillEntry re1 ");
sql.append(" group by re1.fpersonid) re1 on rbe.fpersonid=re1.fpersonid and re1.fbizdate=rbe.fbizdate ");
sql.append(" inner join T_HR_ResignBizBill rb on rb.fid = rbe.FBILLID ");
sql.append(" and rb.FBILLSTATE = 3 ");
sql.append(" left join T_ORG_Position pos on pos.fid = rbe.FPositionID ");
sql.append(" left join T_BD_HRPolitical poli on poli.fid=p.fpoliticalfaceid ");
sql.append(" left join T_BD_HRWed wed on wed.fid=p.fwedid ");
sql.append(" left join T_BD_HRFolk folk on folk.fid=p.ffolkid ");
sql.append(" left join T_BD_HRDiploma dip on dip.fid=p.FHighestDegreeID ");
sql.append(" left join T_HR_PERSONCONTACTMETHOD prm on prm.fpersonid=p.fid ");
sql.append(" left join T_BD_Nationality nl on nl.fid=p.FNationalityID ");
sql.append(" where rb.FBILLSTATE = 3");
if (StringUtils.isNotBlank(resignBizBillID)) {
sql.append(" and rb.fid='" + resignBizBillID + "' ");
} else {
sql.append(" and rbe.CFSyncStatus != 1 ");
}
IRowSet rowSet = DbUtil.executeQuery(ctx, sql.toString());
List