package weaver.formmode.recruit.modeexpand.offer;

import com.engine.recruit.constant.RecruitConstant;
import com.engine.recruit.enums.RecordOperateEnum;
import com.engine.recruit.util.RecruitMessageUtils;
import com.weaver.formmodel.data.model.Formfield;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
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.hrm.company.SubCompanyComInfo;
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;

public class CreateOfferModeExpand 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");
            String recordType = Util.null2String(params.get("recordType"));
            if (!RecordOperateEnum.SEND_OFFER.getOperateType().equals(recordType)) {
                return result;
            }
            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 tznr = Util.null2String(mainDataMap.get("tznr"));
                    String yjtzmb = Util.null2String(mainDataMap.get("yjtzmb"));
                    String yx = Util.null2String(mainDataMap.get("dzyx"));
                    String sjh = Util.null2String(mainDataMap.get("sjhm"));
                    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_offer");
                    String msgContent = getOfferMsgContent(String.valueOf(billId), fieldList, mainDataMap, tznr, yx);

                    if (sendEmail) {
                        String offerAttach = RecruitModeUtil.getImageFileIdsByDocIds(Util.null2String(mainDataMap.get("offerfj")));
                        RecruitMessageUtils.sendEmailWithFile(yx, emailTitle, msgContent, offerAttach);
                    }
                    if (sendSms) {
                        String name = Util.null2String(mainDataMap.get("xm"));
                        String positionName = Util.null2String(mainDataMap.get("zpzwmc"));
                        String endDate = Util.null2String(mainDataMap.get("sxrq"));
                        String companyId = Util.null2String(mainDataMap.get("rzgs"));
                        //if (StringUtils.isAnyBlank(name, positionName, endDate, companyId)) {
                        //    result.put("errmsg", "offer短信发送失败，应聘者信息获取失败");
                        //    result.put("flag", "false");
                        //}
                        String mobileContent = name + "你好，恭喜你成功通过" + new SubCompanyComInfo().getSubCompanyname(companyId) + "的" + positionName + "面试，聘用通知已发送至你的邮箱，请注意查收邮件，并于" + endDate + "前确认，逾期将失效。";

                        RecruitMessageUtils.sendSMS(sjh, mobileContent);
                    }
                    // 更新状态为已发送,更新通知内容，更新发送人、发送时间
                    RecordSet rs = new RecordSet();
                    rs.executeUpdate("update uf_jcl_offer set zt = 1,tznr = ?,fsr=?,fssj=? where id = ? ", msgContent, user.getUID(), DateUtil.getDateTime(), billId);
                }
            }
        } catch (Exception e) {
            new BaseBean().writeLog(e);
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

    /**
     * 获取offer邮件通知内容
     *
     * @param billId
     * @param fieldList
     * @param mainDataMap
     * @param tznr
     * @param yx
     * @return
     */
    public static String getOfferMsgContent(String billId, List<Formfield> fieldList, Map<String, Object> mainDataMap, String tznr, String yx) {
        Map<String, List<Formfield>> fieldMapList = fieldList.stream().collect(Collectors.groupingBy(Formfield::getLabelName));
        // 发送邮件
        String offerMobileUrl = RecruitConstant.OFFER_MOBILE_URL + "&billid=" + billId + "&dzyx=" + yx;
        String mobileLink = "<span color='red'><a href='" + offerMobileUrl + "'  target='_blank'>录用通知函</a></span>";
        tznr = tznr.replace("{录用通知函}", mobileLink);
        return RecruitModeUtil.getReplaceContent(tznr, fieldMapList, mainDataMap);
    }
}
