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


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
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.eas.custom.recuritment.bizEnum.ExecuteResultEnum;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.util.StringUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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);
    private final String ERRORMSGTEMPLATE = "申请id[{0}]已经存在，请勿重复写入！";

    @Override
    public Object process(Context ctx, Map<String, Object> map) throws EASBizException, BOSException {
        String array = (String) map.get("array");
        logger.error("array--" + array);
        Map mapMsg = new HashMap();
        List listMsg = new ArrayList();
        if (StringUtils.isEmpty(array)) {
            mapMsg.put("code", 500);
            mapMsg.put("message", "参数不能为空！");
            return mapMsg;
        }
        int errorSize = 0;
        int successSize = 0;
        String applyId = "";
        JSONArray jsonArray = null;
        try {
            jsonArray = JSONArray.parseArray(array);
            logger.error("jsonArray--" + jsonArray);
        } catch (Exception e) {
            mapMsg.put("code", 500);
            mapMsg.put("listMsg", listMsg);
            mapMsg.put("errorSize", errorSize);
            mapMsg.put("successSize", successSize);
            mapMsg.put("message", "请求失败:参数格式错误！");
            return mapMsg;
        }
        try {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject data = jsonArray.getJSONObject(i);
                //申请id
                applyId = data.getString("applyId");
                //应聘id
                String candidateId = data.getString("candidateId");
                //需求名称
                String recuritmentName = data.getString("recuritmentName");
                //应聘者名称
                String candidateName = data.getString("candidateName");
                if (StringUtils.isEmpty(applyId)) {
                    throw new ShrWebBizException("申请id不能为空!");
                }
                IApplicantBeisen iApplicantBeisen = ApplicantBeisenFactory.getLocalInstance(ctx);
                ApplicantBeisenCollection applicantBeisenCollection = iApplicantBeisen.getApplicantBeisenCollection("where applyId='" + applyId + "' ");
                if (!applicantBeisenCollection.isEmpty()) {
                    ApplicantBeisenInfo applicantBeisenInfo = applicantBeisenCollection.get(0);
                    ExecuteResultEnum syncStatus = applicantBeisenInfo.getSyncStatus();
                    //同步状态为执行失败，则更新成未执行
                    if (ExecuteResultEnum.ERROR.equals(syncStatus)) {
                        SelectorItemCollection updateSic = new SelectorItemCollection();
                        updateSic.add("syncStatus");
                        updateSic.add("syncResult");
                        applicantBeisenInfo.setSyncStatus(ExecuteResultEnum.UNEXECUTE);
                        applicantBeisenInfo.setSyncResult("");
                        iApplicantBeisen.updatePartial(applicantBeisenInfo, updateSic);
                    }
                    listMsg.add(packageResult(applyId, 200, applicantBeisenInfo.getId().toString(), "成功"));
                    successSize++;
                    //String errorMsg = MessageFormat.format(ERRORMSGTEMPLATE, applyId, candidateId);
                    //throw new ShrWebBizException(errorMsg);
                } else {
                    ApplicantBeisenInfo applicantBeisenInfo = new ApplicantBeisenInfo();
                    HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
                    hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
                    applicantBeisenInfo.setHrOrgUnit(hrOrgUnitInfo);
                    applicantBeisenInfo.setApplyId(applyId);
                    applicantBeisenInfo.setRecuritmentName(recuritmentName);
                    applicantBeisenInfo.setCandidateId(candidateId);
                    applicantBeisenInfo.setCandidateName(candidateName);
                    applicantBeisenInfo.setSyncStatus(ExecuteResultEnum.UNEXECUTE);
                    IObjectPK addnew = iApplicantBeisen.addnew(applicantBeisenInfo);
                    listMsg.add(packageResult(applyId, 200, addnew.toString(), "成功"));
                    successSize++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ++errorSize;
            listMsg.add(packageResult(applyId, 500, "", e.getMessage()));
        }
        mapMsg.put("code", 200);
        mapMsg.put("listMsg", listMsg);
        mapMsg.put("errorSize", errorSize);
        mapMsg.put("successSize", successSize);
        mapMsg.put("message", "请求成功！");
        return mapMsg;
    }

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