package weaver.formmode.recruit.modeexpand.interview;

import com.engine.recruit.conn.ApplicantCommonInfo;
import com.engine.recruit.constant.RecruitConstant;
import com.engine.recruit.enums.InterviewOperateTypeEnum;
import org.apache.commons.lang3.StringUtils;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;

/**
 * <p>聚才林招聘</p>
 * 面试保存按钮拓展接口
 *
 * @author:dxfeng
 * @createTime: 2023/09/27
 * @version: 1.0
 */
public class CreateInterviewModeExpand extends AbstractModeExpandJavaCodeNew {

    /**
     * 消息来源ID
     */
    private String messageType;

    /**
     * 消息提醒标题
     */
    private String title;

    private final BaseBean baseBean = new BaseBean();

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> params) {
        Map<String, String> result = new HashMap<>();
        try {
            //数据id
            int billId;
            //模块id
            int modeId;
            RequestInfo requestInfo = (RequestInfo) params.get("RequestInfo");
            User user = (User) params.get("user");
            if (requestInfo != null) {
                billId = Util.getIntValue(requestInfo.getRequestid());
                modeId = Util.getIntValue(requestInfo.getWorkflowid());
                if (billId > 0 && modeId > 0) {
                    MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
                    Property[] properties = mainTableInfo.getProperty();
                    Map<String, Object> mainDataMap = new HashMap<>(16);
                    for (Property property : properties) {
                        mainDataMap.put(property.getName(), property.getValue());
                    }


                    String operateType = Util.null2String(params.get("operateType"));
                    if (StringUtils.isBlank(operateType)) {
                        operateType = InterviewOperateTypeEnum.ARRANGE.getOperateType();
                    }
                    InterviewOperateTypeEnum operateTypeEnum = InterviewOperateTypeEnum.getOperateType(operateType);
                    if (operateTypeEnum == InterviewOperateTypeEnum.ARRANGE) {
                        // 创建面试评价反馈数据
                        InterviewEvaluate.createEvaluate(mainDataMap, billId, user);
                        messageType = RecruitConstant.INTERVIEW_MESSAGE_TYPE;
                        title = RecruitConstant.INTERVIEW_ADD_MESSAGE_TITLE;
                        // 消息提醒
                        arrangeInterview(user, mainDataMap);
                        // 需要应聘者反馈
                        boolean needFeedback = "1".equals(Util.null2String(mainDataMap.get("sfxyfk")));
                        // 发送邮件、短信
                        InterviewMsgUtil.sendEmailAndMsg(mainDataMap, billId, needFeedback);
                    }

                }
            }
        } catch (Exception e) {
            baseBean.writeLog(e);
            result.put("errmsg", e.getMessage());
            result.put("flag", "false");
        }

        return result;
    }


    /**
     * 安排面试拓展动作
     *
     * @param user
     * @param mainDataMap
     */
    private void arrangeInterview(User user, Map<String, Object> mainDataMap) {
        StringBuilder msgBuilder = new StringBuilder();
        msgBuilder.append(RecruitModeUtil.getResourceNames(String.valueOf(user.getUID()))).append("安排了一场面试<br/>");

        String ypz = Util.null2String(mainDataMap.get("ypz"));
        String ypzw = Util.null2String(mainDataMap.get("ypzw"));
        String msrq = Util.null2String(mainDataMap.get("msrq"));
        String msg = Util.null2String(mainDataMap.get("msg"));
        String ptmsg = Util.null2String(mainDataMap.get("ptmsg"));
        String msdd = Util.null2String(mainDataMap.get("msdd"));
        msgBuilder.append("应聘者：").append(ApplicantCommonInfo.getApplicantName(ypz));
        msgBuilder.append("；应聘职位：").append(ApplicantCommonInfo.getApplicantPosition(ypzw));
        msgBuilder.append("<br/>");
        msgBuilder.append("面试时间：").append(msrq).append("；面试官：").append(RecruitModeUtil.getResourceNames(msg)).append("，").append(RecruitModeUtil.getResourceNames(ptmsg)).append("<br/>");
        int formIdByTableName = ApplicantCommonInfo.getFormIdByTableName("uf_jcl_ms");
        String msfsValue = ApplicantCommonInfo.getSelectName(String.valueOf(formIdByTableName), "msfs", Util.null2String(mainDataMap.get("msfs")));
        msgBuilder.append("面试方式：").append(msfsValue).append("<br/>");
        msgBuilder.append("面试地址：").append(msdd);

        // 消息发送
        Set<String> userIdSet = new HashSet<>();
        userIdSet.addAll(Arrays.asList(msg.split(",")));
        userIdSet.addAll(Arrays.asList(ptmsg.split(",")));
        RecruitModeUtil.messagePush(messageType, title, msgBuilder.toString(), userIdSet, user.getUID());

    }
}
