package weaver.formmode.recruit.modeexpand.departmentscreening;

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

/**
 * @author:dxfeng
 * @createTime: 2024/10/12
 * @version: 1.0
 */
public class EditScreenExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> params) {
        Map<String, String> result = new HashMap<>(2);
        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<>();
                    for (Property property : properties) {
                        mainDataMap.put(property.getName(), property.getValue());
                    }

                    // 发送消息提醒
                    String messageType = RecruitConstant.SCREENING_MESSAGE_TYPE;
                    String messageTitle = "筛选反馈提醒";
                    String msgContent = "$fkr$已经提交了筛选反馈<br/>" +
                            "应聘者：$ypz$；应聘职位：$ypzw$<br/>" +
                            "筛选结果：jg";
                    // 消息接收人
                    Set<String> userIdSet = new HashSet<>();
                    // 发送给创建人
                    userIdSet.add(requestInfo.getCreatorid());

                    if (CollectionUtils.isNotEmpty(userIdSet)) {
                        // 消息发送
                        List<Formfield> fieldList = RecruitModeUtil.getFieldList("uf_jcl_bmsxfk");
                        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(messageType, messageTitle, msgStr, userIdSet, user.getUID());
                    }


                }
            }
        } catch (Exception e) {
            new BaseBean().writeLog(e);
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }

        return result;
    }
}
