package weaver.interfaces.htsc.zgk;

import com.alibaba.fastjson.JSONObject;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @author K1810039
 * 宣传素材库 更新 文件时给部门负责人发送消息
 */
public class UpdateSendMessageAction extends AbstractModeExpandJavaCodeNew {

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<>();
        try {
            User user = (User)param.get("user");
            int billid = -1;
            int modeid = -1;
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if(billid>0&&modeid>0){
                    JAVATools.writelog("-----UpdateSendMessageAction----");
                    String sql = "";
                    String wjmc = "";
                    String gxr = "";
                    String name = "";
                    String loginId = "";
                    String deptDirectors = "";
                    // 消息接收人
                    Set<String> receivers = new HashSet<>();
                    PropBean propBean = new PropBean();
                    RecordSet rs = new RecordSet();

                    //根据billid，获取文件名，和更新人
                    sql = "select WJMC,GXR from uf_xcsck where ID = "+billid;
                    JAVATools.writelog(sql);
                    rs.execute(sql);
                    if (rs.next()){
                        wjmc = rs.getString("WJMC");
                        gxr = rs.getString("GXR");
                    }

                    // 根据id获取创建人姓名，工号
                    sql = "select LASTNAME,LOGINID from HRMRESOURCE where ID = "+gxr;
                    JAVATools.writelog(sql);
                    rs.execute(sql);
                    if (rs.next()){
                        name = rs.getString("LASTNAME");
                        loginId = rs.getString("LOGINID");
                    }

                    // 获取部门负责人
                    sql = "select d.DEPTDIRECTORS from HRMDEPARTMENTDEFINED d join HRMRESOURCE h on d.DEPTID = h.DEPARTMENTID where h.ID = "+gxr;
                    JAVATools.writelog(sql);
                    rs.execute(sql);
                    if (rs.next()){
                        deptDirectors = rs.getString("DEPTDIRECTORS");
                    }

                    if (StringUtils.isBlank(deptDirectors)){
                        result.put("flag", "true");
                        JAVATools.writelog("DEPTDIRECTORS is null !");
                        return result;
                    }

                    // 消息接收人
                    String[] res = deptDirectors.split(",");
                    for (String re : res) {
                        receivers.add(re);
                    }

                    // 获取消息绑定标识
                    int typeid = 0;
                    String messageId = propBean.getPropName("xcsck_messageId");
                    typeid = Util.getIntValue(messageId);
                    JAVATools.writelog("int转化后："+messageId);

                    // 标题
                    String title = "宣传素材库文件变更提醒";
                    // 消息正文
                    String context = name + "（"+loginId+"）" + "于素材库中更新了"+wjmc+"，请您及时查看。";
                    JAVATools.writelog("消息的正文内容是："+context);

                    //参数打印
                    Map<String,Object> paramsMap = new HashMap<>();
                    paramsMap.put("typeid",typeid);
                    paramsMap.put("receiver",receivers);
                    paramsMap.put("title",title);
                    paramsMap.put("context",context);
                    JAVATools.writelog("传递的参数是："+ JSONObject.toJSONString(paramsMap));

                    // 发送消息
                    Smstools smstools = new Smstools();
                    boolean sendResult = smstools.sendMessage(typeid, receivers, title, context, "", "");
                    if (sendResult){
                        JAVATools.writelog("send Message successfully");
                    }else {
                        JAVATools.writelog("send Message fails");
                        requestInfo.getRequestManager().setMessagecontent("消息发送失败，请检查参数" + typeid+"|"+JSONObject.toJSONString(receivers)+"|"+title+"|"+context+"|"+""+"|"+"");
                    }

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

}