package weaver.formmode.recruit.modeexpand.interview;

import com.engine.recruit.constant.RecruitConstant;
import com.engine.recruit.util.RecruitMessageUtils;
import com.weaver.formmodel.data.model.Formfield;
import weaver.conn.RecordSet;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
import weaver.general.Util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:dxfeng
 * @createTime: 2024/03/04
 * @version: 1.0
 */
public class InterviewMsgUtil {

    /**
     * 面试相关，发送邮件、短信
     *
     * @param mainDataMap
     * @param billId
     * @param needFeedback
     */
    public static void sendEmailAndMsg(Map<String, Object> mainDataMap, int billId, boolean needFeedback) {
        String tzypz = Util.null2String(mainDataMap.get("tzypz"));
        String yjnr = Util.null2String(mainDataMap.get("yjnr"));
        String yjtzmb = Util.null2String(mainDataMap.get("yjtzmb"));
        List<String> sendTypeList = Arrays.asList(tzypz.split(","));
        boolean sendEmail = sendTypeList.contains("0");
        boolean sendSms = sendTypeList.contains("1");
        String emailTitle = RecruitModeUtil.getEmailTitle(yjtzmb);

        List<Formfield> fieldList = RecruitModeUtil.getFieldList("uf_jcl_ms");
        Map<String, List<Formfield>> fieldMapList = fieldList.stream().collect(Collectors.groupingBy(Formfield::getLabelName));
        // 发送邮件
        String msgContent = RecruitModeUtil.getReplaceContent(yjnr, fieldMapList, mainDataMap);
        String yx = Util.null2String(mainDataMap.get("dzyx"));
        String sjh = Util.null2String(mainDataMap.get("sjhm"));
        RecordSet rs = new RecordSet();
        rs.executeQuery("select modeuuid from uf_jcl_ms where id = ? ", billId);
        String uuid = "";
        if (rs.next()) {
            uuid = rs.getString("modeuuid");
        }
        String confirmUrl = RecruitConstant.INTERVIEW_FEEDBACK_URL + "?uuid=" + uuid;
        if (sendEmail) {
            if (needFeedback) {
                String emailContent = "<div style=\"border:3px solid silver;width:635px;height:55px;line-height:55px;font-family:'microsoft Yahei';font-size:18px;font-weight:bold;color:#333;padding-left:10px\">请您答复是否参加此次邀请？ <a href=\"" + confirmUrl + "&status=0\" style=\"text-decoration:underline;color:#0000cc;margin-right:10px\">参加</a> <a href=\"" + confirmUrl + "&status=1\" style=\"text-decoration:underline;color:#0000cc;\">不参加</a></div>";
                // 邮件内容拼接确认信息
                emailContent = msgContent + emailContent;
                RecruitMessageUtils.sendEmail(yx, emailTitle, emailContent);
                // 勾选了需要反馈，则需要将是否参加置空，且需要可以再次反馈
                rs.executeUpdate("update uf_jcl_ms set sfcj = null,zt = null where id = ? ", billId);
            } else {
                RecruitMessageUtils.sendEmail(yx, emailTitle, msgContent);
            }
        }
        if (sendSms) {
            if (needFeedback) {
                String messageContent = "请您答复是否参加此次邀请？" + confirmUrl;
                messageContent = msgContent + messageContent;
                RecruitMessageUtils.sendSMS(sjh, messageContent);
            } else {
                RecruitMessageUtils.sendSMS(sjh, msgContent);
            }
        }
    }
}
