package com.engine.recruit.service.impl;

import com.engine.core.impl.Service;
import com.engine.recruit.conn.ApplicantCommonInfo;
import com.engine.recruit.conn.RecruitRecordSet;
import com.engine.recruit.constant.RecruitConstant;
import com.engine.recruit.exception.CustomizeRunTimeException;
import com.engine.recruit.service.RecruitInterviewService;
import com.weaver.formmodel.data.model.Formfield;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author:dxfeng
 * @createTime: 2023/11/28
 * @version: 1.0
 */
public class RecruitInterviewServiceImpl extends Service implements RecruitInterviewService {
    @Override
    public Map<String, Object> updateInterviewStatus(Map<String, Object> param) {
        Map<String, Object> returnMap = new HashMap<>(3);
        String status = Util.null2String(param.get("status"));
        String uuid = Util.null2String(param.get("uuid"));
        returnMap.put("type", "error");
        returnMap.put("message", "反馈失败");

        if (StringUtils.isBlank(status) || StringUtils.isBlank(uuid)) {
            returnMap.put("description", "反馈失败，请直接点击邮件/短信中的链接地址访问，请勿修改链接内容。");
            return returnMap;
        }

        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from uf_jcl_ms where modeuuid = ? ", uuid);
        Map<String, Object> mainDataMap = RecruitRecordSet.getSingleRecordMap(rs);
        String sfcj = "";
        if (!mainDataMap.isEmpty()) {
            sfcj = Util.null2String(mainDataMap.get("sfcj"));
        } else {
            returnMap.put("description", "反馈失败，面试信息有误，请联系相应HR确认");
            return returnMap;
        }


        if (StringUtils.isNotBlank(sfcj)) {
            returnMap.put("description", "您已成功反馈面试，请勿重复反馈。");
            return returnMap;
        }

        List<Formfield> fieldList = RecruitModeUtil.getFieldList("uf_jcl_ms");
        Map<String, Formfield> fieldMap = fieldList.stream().collect(Collectors.toMap(Formfield::getFieldname, item -> item, (k1, k2) -> k1));
        // 参加
        if ("0".equals(status)) {
            returnMap.put("type", "success");
            returnMap.put("message", "已确认参加面试");
            returnMap.put("description", "您已接受面试，感谢您的应聘，请按期准备面试，如有问题可以联系相应HR");

            rs.executeUpdate("update uf_jcl_ms set sfcj = ? , zt = 0 where modeuuid = ? ", 0, uuid);


            String msgContent = "应聘者：$ypz$，确认参加面试，请知悉。<br/>" +
                    "面试时间：$msrq$；面试官：$msg$";
            msgContent = RecruitModeUtil.getMsgReplaceStr(msgContent, fieldMap, mainDataMap);
            String msg = Util.null2String(mainDataMap.get("msg"));
            Set<String> userIdSet = new HashSet<>();
            userIdSet.add(Util.null2String(mainDataMap.get("modedatacreater")));
            userIdSet.addAll(Arrays.asList(msg.split(",")));
            RecruitModeUtil.messagePush(RecruitConstant.INTERVIEW_MESSAGE_TYPE, "面试反馈通知", msgContent, userIdSet, 1);
        } else if ("1".equals(status)) {
            returnMap.put("type", "info");
            returnMap.put("message", "已取消参加面试");
            returnMap.put("description", "您已取消参加面试，感谢您的应聘。希望您能够找到真正适合自己的工作。");

            rs.executeUpdate("update uf_jcl_ms set sfcj = ? , qxyy = 2 , zt = 4 where modeuuid = ? ", 1, uuid);


            String msgContent = "应聘者：$ypz$，反馈不参加面试，面试已取消，请知悉。<br/>" +
                    "面试时间：$msrq$；面试官：$msg$";
            msgContent = RecruitModeUtil.getMsgReplaceStr(msgContent, fieldMap, mainDataMap);
            String msg = Util.null2String(mainDataMap.get("msg"));
            Set<String> userIdSet = new HashSet<>();
            userIdSet.add(Util.null2String(mainDataMap.get("modedatacreater")));
            userIdSet.addAll(Arrays.asList(msg.split(",")));
            RecruitModeUtil.messagePush(RecruitConstant.INTERVIEW_MESSAGE_TYPE, "面试反馈通知", msgContent, userIdSet, 1);
        }

        return returnMap;
    }

    @Override
    public String getInterviewEvaluateStatus(Map<String, Object> param) {
        try {
            String id = Util.null2String(param.get("id"));
            String msg = Util.null2String(param.get("msg"));
            if (StringUtils.isBlank(msg)) {
                return "";
            }
            RecordSet rs = new RecordSet();
            int formId = ApplicantCommonInfo.getFormIdByTableName("uf_jcl_mspjfk");
            String[] split = msg.split(",");
            List<String> spanList = new ArrayList<>();
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            for (String s : split) {
                rs.executeQuery("select jg,zt from uf_jcl_mspjfk where msid = ? and msg = ?", id, s);
                String jg = "";
                String zt = "";
                if (rs.next()) {
                    jg = Util.null2String(rs.getString("jg"));
                    zt = Util.null2String(rs.getString("zt"));
                }

                if (StringUtils.isNotBlank(jg)) {
                    jg = ApplicantCommonInfo.getSelectName(String.valueOf(formId), "jg", jg);
                    spanList.add("<a href=javaScript:openhrm(" + s + ") onclick=pointerXY(event)>" + resourceComInfo.getLastname(s) + "(" + jg + ")</a>");
                } else {
                    String span = "<a href=javaScript:openhrm(" + s + ") onclick=pointerXY(event)>" + resourceComInfo.getLastname(s);
                    if (StringUtils.isNotBlank(zt)) {
                        zt = ApplicantCommonInfo.getSelectName(String.valueOf(formId), "zt", zt);
                        span += "(" + zt + ")";
                    }
                    span += "</a>";
                    spanList.add(span);
                }

            }


            return StringUtils.join(spanList, "<br/>");
        } catch (Exception e) {
            throw new CustomizeRunTimeException(e);
        }
    }
}
