package weaver.formmode.recruit.modeexpand.written;

import com.engine.recruit.util.RecruitMessageUtils;
import com.weaver.formmodel.data.model.Formfield;
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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author ml
 * @Date 2023/10/20 15:36
 * @Description 笔试保存按钮拓展接口
 * @Since version-1.0
 */
public class CreateWrittenModeExpand extends AbstractModeExpandJavaCodeNew {

    @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 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_bs");
                    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("yx"));
                    String sjh = Util.null2String(mainDataMap.get("sjh"));
                    if (sendEmail) {
                        RecruitMessageUtils.sendEmail(yx, emailTitle, msgContent);

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

        return result;
    }

}
