package com.kingdee.eas.custom.recuritment.service;


import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.recuritment.ApplicantBeisenCollection;
import com.kingdee.eas.custom.recuritment.ApplicantBeisenFactory;
import com.kingdee.eas.custom.recuritment.ApplicantBeisenInfo;
import com.kingdee.eas.custom.recuritment.IApplicantBeisen;
import com.kingdee.util.StringUtils;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * 同步北森简历:写入北森申请ID和应聘ID
 *
 * @author qingwu
 * @date 2025/6/25
 * @apiNote
 */
public class SaveBeisenApplyIdService implements IHRMsfService {
    private static Logger logger =
            Logger.getLogger(SaveBeisenApplyIdService.class);

    @Override
    public Object process(Context ctx, Map<String, Object> map) throws EASBizException, BOSException {
        //申请id
        String applyId = (String) map.get("applyId");
        //应聘id
        String candidateId = (String) map.get("candidateId");
        if (StringUtils.isEmpty(applyId)) {
            return packageResult(500, "", "申请id不能为空!");
        }
        if (StringUtils.isEmpty(candidateId)) {
            return packageResult(500, "", "应聘id不能为空!");

        }
        try {
            IApplicantBeisen iApplicantBeisen = ApplicantBeisenFactory.getLocalInstance(ctx);
            String where = "where applyId='" + applyId + "' or candidateId = '" + candidateId + "'";
            ApplicantBeisenCollection applicantBeisenCollection = iApplicantBeisen.getApplicantBeisenCollection(where);
            if (applicantBeisenCollection.size() > 0) {
                ApplicantBeisenInfo applicantBeisenInfo = applicantBeisenCollection.get(0);
                String applyId2 = applicantBeisenInfo.getApplyId();
                String candidateId2 = applicantBeisenInfo.getCandidateId();
                String msg = "";
                if (applyId2.equals(applyId)) {
                    msg = "申请id[" + applyId + "]、";
                }
                if (candidateId2.equals(candidateId)) {
                    msg += "应聘id[" + candidateId + "]、";
                }
                msg.substring(0, msg.length() - 1);// 删除最后一个字符
                msg += "重复！";
                return packageResult(500, "", msg);
            }
            ApplicantBeisenInfo applicantBeisenInfo = new ApplicantBeisenInfo();
            applicantBeisenInfo.setApplyId(applyId);
            applicantBeisenInfo.setCandidateId(candidateId);
            //String billNumber = NumberCodeRule.readCodeRuleNumber(ctx, applicantBeisenInfo, NumberCodeRule.getMainOrgByCu(ctx));
            //applicantBeisenInfo.setNumber(billNumber);
            ////需求管理
            //RecuritmentDemandInfo recuritmentDemandInfo = getRecuritmentByApplyId(ctx, applyId);
            //if (recuritmentDemandInfo != null) {
            //    applicantBeisenInfo.setRecuritment(recuritmentDemandInfo);
            //}
            IObjectPK addnew = iApplicantBeisen.addnew(applicantBeisenInfo);
            if (addnew != null) {
                return packageResult(200, addnew.toString(), "成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return packageResult(500, "", "保存失败!");
    }

    ///**
    // * 通过申请ID获取需求管理
    // *
    // * @param applyId
    // */
    //public RecuritmentDemandInfo getRecuritmentByApplyId(Context ctx, String applyId) throws JSONException, IOException, URISyntaxException, BOSException, org.json.JSONException {
    //    JSONObject pamas = new JSONObject();
    //    JSONArray applyIds = new JSONArray();
    //    applyIds.add(applyId);
    //    pamas.put("applyIds", applyIds);
    //    String jobId = "";
    //    //根据申请ID获取申请信息
    //    JSONObject applyJson = ItalentHelper.getApplyListByApplyId(pamas);
    //    logger.error("applyJson--" + applyJson);
    //    if (null != applyJson && applyJson.size() > 0 && "200".equals(applyJson.get("code") + "")) {
    //        JSONArray data = applyJson.getJSONArray("data");
    //        for (int i = 0; i < data.size(); i++) {
    //            JSONObject applyData = data.getJSONObject(0);
    //            //职位ID
    //            jobId = applyData.getString("jobId");
    //        }
    //    } else {
    //        logger.error("根据申请ID获取申请信息失败,原因:" + applyJson.get("message"));
    //    }
    //    logger.error("jobId--" + jobId);
    //    //根据ID获取获取数据单条
    //    JSONObject entity = ItalentHelper.getEntity(jobId);
    //    logger.error("entity--" + entity);
    //    JSONObject entityData = entity.getJSONObject("data");
    //    //根据招聘需求ID获取招聘需求
    //    JSONObject columns = entityData.getJSONObject("columns");
    //    JSONObject recruit = columns.getJSONObject("RecruitRequirementIds");
    //    String recruitRequirementId = recruit.getString("value");
    //    JSONArray recruitRequirementIds = new JSONArray();
    //    recruitRequirementIds.add(recruitRequirementId);
    //    //根据招聘需求ID获取招聘需求
    //    JSONObject requirements = ItalentHelper.getRequirements(recruitRequirementIds);
    //    logger.error("requirements--" + requirements);
    //    //一个职位可能关联多个招聘需求，建议校验需求状态，获取需求状态=进行中的招聘需求；
    //    JSONArray data = requirements.getJSONArray("data");
    //    //需求Id
    //    String requirementId = "";
    //    for (int i = 0; i < data.size(); i++) {
    //        JSONObject requirementData = data.getJSONObject(i);
    //        //需求状态 10=草稿，20=审批中，30=审批未通过，40=进行中，50=已关闭，60=已完成，70=已暂停，80=审批已终止
    //        String requirementStatus = requirementData.getString("requirementStatus");
    //        if ("40".equals(requirementStatus)) {
    //            requirementId = requirementData.getString("requirementId");
    //        }
    //    }
    //    //招聘需求
    //    IRecuritmentDemand iRecuritmentDemand = RecuritmentDemandFactory.getLocalInstance(ctx);
    //    //RecuritmentDemandCollection recuritmentDemandCollection = iRecuritmentDemand.getRecuritmentDemandCollection("where requirementId='" + requirementId + "");
    //    //if (recuritmentDemandCollection.size() > 0) {
    //    //    return recuritmentDemandCollection.get(0);
    //    //}
    //    return null;
    //}

    /**
     * 封装结果
     *
     * @param code
     * @param number
     * @param msg
     * @return
     */
    public Map packageResult(int code, String number, String msg) {
        Map result = new HashMap();
        result.put("code", code);
        if (number != "") {
            result.put("number", number);
        }
        result.put("message", msg);
        return result;
    }
}
