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 org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 237
 * description: BeisenFacadeOSFService <br>
 * date: 2025/7/2 16:51 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class BeisenFacadeOSFService implements IHRMsfService {
    String syncBeisenReserveTalentPool = "syncBeisenReserveTalentPool";
    String syncUnBeisenReserveTalentPool = "syncUnBeisenReserveTalentPool";

    @Override
    public Object process(Context context, Map<String, Object> 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 _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<String> 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<String, String> objectMap = this.getBeisenTalentPool(ctx);
                String talentPoolId = objectMap.get("talentPoolId");
                BeisenApiClient client = BeisenApiClient.getInstance();
                JSONObject jsonData = new JSONObject();
                BeisenParamByProperties byProperties = new BeisenParamByProperties();
                Map<String, String> 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<String> candidateIds = (List<String>) 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<String, String> 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<String, String> objectMap = this.getBeisenTalentPool(ctx);
            BeisenApiClient client = BeisenApiClient.getInstance();
            Map<String, Object> 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<String, Object> data = (Map<String, Object>) 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<String, String> 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<String, Object> getPersonData(Context ctx, String resignBizBillID) throws BOSException, SQLException {

        Map<String, Object> 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 2 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<Map<String, Object>> list = Lists.newArrayList();
        while (rowSet.next()) {
            Map<String, Object> map = Maps.newHashMap();

            Map<String, Object> standardResume = Maps.newHashMap();
            map.put("standardResume", standardResume);
            Map<String, Object> personProfile = Maps.newHashMap();
            standardResume.put("personProfile", personProfile);
            //现居住地址（可邮寄{"code":400,"data":"00000000-0000-0000-0000-000000000000","message":"值不在数据源的范围内，字段名：Gender,值：-1"}
            String address = rowSet.getString("address");
            if (StringUtils.isNotBlank(address)) {
                personProfile.put("extxianjuzhudizhikeyouji_614492_1099396040", this.setValue(address));
            }

            //addressTX	通信地址	现居住地	OgLivingArea
            //personProfile.put("OgLivingArea", this.setValue(address));

            //height	身高	身高(厘米)	Height
            String height = rowSet.getString("height");
            if (StringUtils.isNotBlank(height)) {
                personProfile.put("Height", this.setValue(height));
            }

            //婚姻状况
            String wedName = rowSet.getString("wedName");
            String wedNumber = rowSet.getString("wedNumber");
            String wedBeisenVal = rowSet.getString("wedBeisenVal");
            if (StringUtils.isNotBlank(wedBeisenVal)) {
                personProfile.put("exthunyinzhuangkuang_614492_1285520393", this.setValue(wedBeisenVal));
            }

            //政治面貌
            String poliName = rowSet.getString("poliName");
            String poliNumber = rowSet.getString("poliNumber");
            String poliBeisenVal = rowSet.getString("poliBeisenVal");
            if (StringUtils.isNotBlank(wedBeisenVal)) {
                personProfile.put("Polity", this.setValue(poliBeisenVal));
            }
            //姓名，工号
            String pName = rowSet.getString("pName");
            String pNumber = rowSet.getString("pNumber");
            personProfile.put("Name", this.setValue(pName));
            //personProfile.put("JobNumber", this.setValue(pNumber));
//            //出生日期	OgBirthday
//            Date birthday = rowSet.getDate("birthday");
//            if (null != birthday) {
//                personProfile.put("birthday", this.setValue(birthday));
//                personProfile.put("OgBirthday", this.setValue(birthday));
//            }
            //手机号
            String NCell = rowSet.getString("NCell");
            if (StringUtils.isNotBlank(NCell)) {
                personProfile.put("Mobile", this.setValue(NCell));
                //手机号类型
                personProfile.put("MobileType", this.setValue(MobileTypeEnum.CHINESE_MAINLAND.getKey()));
            }

            //dyxlbyxx	毕业学校	毕业学校	OgLastSchool

            //String dyxlbyxx = rowSet.getString("dyxlbyxx");
            //personProfile.put("OgLastSchool", this.setValue(dyxlbyxx));
            //毕业时间
//            Date dyxlbysj = rowSet.getDate("dyxlbysj");
//            if(null!=dyxlbysj) {
//                personProfile.put("GraduationDate", this.setValue(dyxlbysj));
//               // personProfile.put("OgGraduationDate", this.setValue(dyxlbyxx));
//            }
            //HighestDegreeID	最高学历	第一学历	OgFirstEducationLevel	string	是	是	第一学历-文本
//            String dipNumber = rowSet.getString("dipNumber");
//            String dipName = rowSet.getString("dipName");
//            String dipBeisenVal = rowSet.getString("dipBeisenVal");
            //personProfile.put("OgFirstEducationLevel", this.setValue(dipBeisenVal));
            //personProfile.put("LastEducationLevel", this.setValue(dipBeisenVal));
            //idCardBeginDate	身份证开始日期	证件生效日期	CertificateValidityDate
            //idCardEndDate	身份证截止日期	证件失效日期	CertificateValidityOverDate
//            Date idCardbeginDate = rowSet.getDate("idCardbeginDate");
//            Date idCardendDate = rowSet.getDate("idCardendDate");
//            if(null!=idCardbeginDate && null!=idCardendDate) {
//                personProfile.put("CertificateValidityDate", this.setValue(idCardbeginDate));
//                personProfile.put("CertificateValidityOverDate", this.setValue(idCardendDate));
//            }
//            String idCardIssueOrg = rowSet.getString("idCardIssueOrg");
//            personProfile.put("CertificateGrantUnit", this.setValue(idCardIssueOrg));


            //籍贯
            personProfile.put("NativeArea", this.setValue(1));
            //民族
            //String folkName = rowSet.getString("folkName");
            //String folkNumber = rowSet.getString("folkNumber");
            String folkBeisenVal = rowSet.getString("folkBeisenVal");
            if (StringUtils.isNotBlank(folkBeisenVal)) {
                personProfile.put("Nation", this.setValue(folkBeisenVal));
            }
            String email = rowSet.getString("email");
            personProfile.put("Email", this.setValue(email));
            //国籍
            String nlBeisenVal = rowSet.getString("nlBeisenVal");
            personProfile.put("Nationality", this.setValue(nlBeisenVal));
//            String description = rowSet.getString("description");
//            String bizDate = rowSet.getString("bizDate");
//            String workLocation = rowSet.getString("workLocation");
            //证件类型	CertificateType	integer	是	是
            String idCardNO = rowSet.getString("idCardNO");
            personProfile.put("CertificateNumber", this.setValue(idCardNO));
            String isIdCard = rowSet.getString("isIdCard");
            personProfile.put("CertificateType", this.setValue(isIdCard));

            String gender = rowSet.getString("gender");
            personProfile.put("Gender", this.setValue(gender));
            //personProfile.put("OgGender", this.setValue(gender));

            String linkTelNum = rowSet.getString("linkTelNum");
            personProfile.put("EmergencyPhone", this.setValue(linkTelNum));
            String linkName = rowSet.getString("linkName");
            personProfile.put("EmergencyContact", this.setValue(linkName));

//            String posName = rowSet.getString("posName");
//            String posNumber = rowSet.getString("posNumber");
//
//            String nativePlace = rowSet.getString("nativePlace");
//            String officePhone = rowSet.getString("officePhone");

            if (StringUtils.isBlank(idCardNO)) {
                result.put("code", "err");
                String msg = (String) result.get("msg");
                result.put("msg", msg + "身份正号与护照号不能同时为空!");
                continue;
            }
            if (StringUtils.isBlank(email) && StringUtils.isBlank(NCell)) {
                result.put("code", "err");
                String msg = (String) result.get("msg");
                result.put("msg", msg + "邮箱与手机号不能同时为空!");
                continue;
            }
            result.put("data", map);
            list.add(map);
        }
        return result;
    }

    /**
     * syncStatus	同步北森状态	业务枚举	CFSyncStatus
     * syncBeisenResult	同步北森结果	字符串	CFSyncBeisenResult
     */
    public void updateSyncBeisenResultBillId(Context ctx, String id, ExecuteResultEnum syncStatus, String candidateId, JSONObject jsonData) throws BOSException {
        String syncBeisenResult = "";
        if (null != jsonData) {
            syncBeisenResult = jsonData.toJSONString();
            if (syncBeisenResult.length() > 1500) {
                syncBeisenResult = syncBeisenResult.substring(0, 1500);
            }
        }
        DbUtil.execute(ctx,
                "update T_HR_ResignBizBillEntry set CFSyncBeisenResult=?,CFCandidateId=?,CFSyncStatus=? where FBILLID =?",
                new String[]{syncBeisenResult, candidateId, syncStatus.getValue(), id}
        );
    }

    /**
     * syncStatus	同步北森状态	业务枚举	CFSyncStatus
     * syncBeisenResult	同步北森结果	字符串	CFSyncBeisenResult
     */
    public void updateSyncBeisenResultEntryId(Context ctx, String id, ExecuteResultEnum syncStatus, String candidateId, JSONObject jsonData) throws BOSException {
        String syncBeisenResult = jsonData.toJSONString();

        DbUtil.execute(ctx,
                "update T_HR_ResignBizBillEntry set CFSyncBeisenResult=?,CFCandidateId=?,CFSyncStatus=? where fid =?",
                new String[]{syncBeisenResult, candidateId, syncStatus.getValue(), id}
        );
    }

    public Map<String, Object> setValue(Integer val) {
        return this.setValue(val.toString());
    }

    public Map<String, Object> setValue(Date val) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        return this.setValue(dateFormat.format(val));
    }

    public Map<String, Object> setValue(Date val, SimpleDateFormat dateFormat) {
        return this.setValue(dateFormat.format(val));
    }

    public Map<String, Object> setValue(String val) {
        return this.setValue("value", val);
    }

    public Map<String, Object> setValue(String key, Object val) {
        Map<String, Object> map = Maps.newHashMap();
        map.put(key, (null == val ? "" : val));
        return map;
    }

    protected Map<String, String> getBeisenTalentPool(Context ctx) throws BOSException, SQLException {
        Map<String, String> map = Maps.newHashMap();
        IRowSet rowSet = DbUtil.executeQuery(ctx, "select top 1 cftalentPoolId,cftalentPoolType from CT_REC_BeisenTalentPool where cfstdIsDeleted='0' and cfisLieftTalentPool = 1 and FNumber='离职人才库' ");
        if (rowSet.next()) {
            String talentPoolId = rowSet.getString("cftalentPoolId");
            String talentPoolType = rowSet.getString("cftalentPoolType");
            map.put("talentPoolId", talentPoolId);
            return map;
        } else {
            throw new IllegalArgumentException("未匹配到北森离职人才库，请先在sHR系统中维护好“北森人才库”数据");
        }
    }
}
