package weaver.formmode.recruit.modeexpand.interview;

import com.engine.recruit.constant.RecruitConstant;
import com.weaver.formmodel.data.model.Formfield;
import org.apache.commons.collections.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
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.*;
import java.util.stream.Collectors;

/**
 * @author:dxfeng
 * @createTime: 2024/10/12
 * @version: 1.0
 */
public class UrgeInterviewEvaluationExpand extends AbstractModeExpandJavaCodeNew {
    private static final String MODE_TABLE_NAME = "uf_jcl_ms";

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

                    // 发送消息

                    String msgContent = "您有一个面试还未评价，请尽快填写评价<br/>" +
                            "应聘者：$ypz$ ；应聘职位：$ypzw$；面试时间：$msrq$";
                    Set<String> userIdSet = new HashSet<>();

                    // 查询未反馈面试评价的面试官
                    rs.executeQuery("select msg from uf_jcl_mspjfk where zt = 0 and msid = ?", billId);
                    while (rs.next()) {
                        userIdSet.add(rs.getString("msg"));
                    }
                    if (CollectionUtils.isNotEmpty(userIdSet)) {
                        // 消息发送
                        List<Formfield> fieldList = RecruitModeUtil.getFieldList(MODE_TABLE_NAME);
                        Map<String, Formfield> fieldMap = fieldList.stream().collect(Collectors.toMap(Formfield::getFieldname, item -> item, (k1, k2) -> k1));
                        String msgStr = RecruitModeUtil.getMsgReplaceStr(msgContent, fieldMap, mainDataMap);
                        RecruitModeUtil.messagePush(RecruitConstant.INTERVIEW_MESSAGE_TYPE, RecruitConstant.INTERVIEW_EVALUATE_MESSAGE_TITLE, msgStr, userIdSet, user.getUID());
                    }
                }

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

        return result;
    }
}
