package com.engine.htsc.jgxt.service.imp;
import com.alibaba.fastjson.JSONArray;
import com.engine.core.impl.Service;
import com.engine.htsc.jgxt.service.JgxtSendMessage;
import com.engine.htsc.jgxt.dao.Jgxtbase;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.HtSendMail;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;

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

public class JgxtSendMessageImp extends Service implements JgxtSendMessage {

    /*
     * @title sendmessage
     * @remark 监管消息提醒
     * @author xf
     * @Params：
       * @param: map
     * @updateTime 2021/9/14
     **/
    @Override
    public void sendmessage(Map<String, Object> map) {
       String requestid= Util.null2String(map.get("requestid"));
        String tablename=Util.null2String(map.get("tablename"));
       String requestname=Util.null2String(map.get("requestname"));
       PropBean propBean=new PropBean();
       if("".equals(requestid)||"".equals(tablename)|| "".equals(requestname)){
           return ;
       }
       Map<String,Object> xtmap= Jgxtbase.getttablebase(map);
       if(xtmap==null||xtmap.size()<1){
           return ;
       }

      String ycjyxw = Util.null2String(xtmap.get("ycjyxw"));
      String sjbm= Util.null2String(xtmap.get("sjbm"));
       if((","+ycjyxw+",").indexOf(',')<0){
           return ;
       }
        xtmap.put("requestname",requestname);
        int smstypeid= Util.getIntValue(propBean.getPropName("jgxt.msg.systypeid"));
        String title=Util.null2String(propBean.getPropName("jgxt.msg.title"));
        String modelvalue=Util.null2String(propBean.getPropName("jgxt.msg.content"));
        String msguser=Util.null2String(propBean.getPropName("jgxt.msg.msguser"));
        Set<String> userset=new HashSet<>();
        String content="OA监管协同流程"+requestname+"监管协同流程已发送至营业部，请关注，谢谢！";

        userset= Arrays.stream((msguser + "," + sjbm).split(",")).distinct().filter((item)->!"".equals(item)).collect(Collectors.toSet());
        String linkUrl="";
        String linkMobileUrl="";
       //开始做消息发送
        boolean flag=new Smstools().sendMessage(smstypeid,userset,title,content,linkUrl,linkMobileUrl);
        Sendmeail(userset,getcontent(modelvalue,xtmap));
        String messagelog="";
        if(flag){
            messagelog="消息提醒成功！"+requestid;
        }else{
            messagelog="消息提醒失败！"+requestid;
        }
        JAVATools.SetCmdwritelog("监管系统提醒",messagelog);
    }

    public  void Sendmeail(Set<String> userids,String text){
        Map<String,Object> map=new HashMap<>();
        map.put("subject","OA监督协同流程，请关注");
        map.put("sjrArray",getuseridtomail(userids));
        map.put("csrArray","");
        map.put("text",text);
        HtSendMail.sendemail(map);
      /*  String sjrArray= Util.null2String(map.get("sjrArray"));
        String csrArray=Util.null2String(map.get("csrArray"));
        String subject=Util.null2String(map.get("subject"));
        String text=Util.null2String(map.get("text"));*/
    }

    public JSONArray getuseridtomail(Set<String> userids){
        JSONArray jsonArray=new JSONArray();
        if(userids==null || userids.size()<=0){
            return jsonArray;
        }
        //.map((model)->"\'"+model+"\'")
       String newuserids= userids.stream().collect(Collectors.joining(","));
        String sql=" select wm_concat_old( distinct email) as email from hrmresource  where 1=1 and id in ("+newuserids+")";
        Map<String,Object> map= DBTools.getsqltomap(sql,null);
        if(map==null || map.size()<=0 || "".equals( Util.null2String(map.get("email")))){
            return jsonArray;
        }
        Arrays.stream(Util.null2String(map.get("email")).split(",")).forEach((model)->
            jsonArray.add(model)
        );
        return jsonArray;




    }


    /**
     * @title getcontent
     * @remark 消息模板拼接
     * @author xf
     * @Params：
       * @param: modelvalue
     * @param: map
     * @updateTime 2021/9/14
     * @return: java.lang.String
     */
    private  String getcontent(String modelvalue,Map<String,Object> map){
        String content="";
        String requestname=Util.null2String(map.get("requestname"));
        String sjbmname=Util.null2String(map.get("szbmname"));
        String ngrq=Util.null2String(map.get("ngrq"));
//        String lcbh=Util.null2String(map.get("lcbh"));
        String hjly=gethjlx(Util.null2String(map.get("hjly")));
        String xzjy=gettransactionype(Util.null2String(map.get("xzjy")));
        String xzsj=getdateplacetype(Util.null2String(map.get("xzsj")));
        String hjjsrq=Util.null2String(map.get("hjjsrq"));
        String yqhhrq=Util.null2String(map.get("yqhhrq"));
        content= MessageFormat.format(modelvalue,requestname,sjbmname,ngrq,"监管协同",hjly,xzjy,xzsj,hjjsrq,yqhhrq);
        JAVATools.SetCmdwritelog("监管消息:"+content);
        return content;
    }

    private  String gethjlx(String key){
        String sql="select  rank() over(order by id ) rank ,name from mode_selectitempagedetail where mainid in  ( " +
                "select id from mode_selectitempage  where cubeuuid='CAFA3091BD084A8AE0538E123FA882A8' " +
                ") order by id ";
        List<Map<String,Object>> list= JAVATools.GetTableToListMap(sql,null);
        if(list==null || list.size()<=0){
            return key;
        }
        for (Map<String,Object> map : list){
            String value=Util.null2String(Util.getIntValue(Util.null2String(map.get("rank")))-1);
            if(key.equals(value)){
                return Util.null2String(map.get("name"));
            }
        }
        return key;
    }

    private  String getdateplacetype(String key){
        String sql="select rank() over(order by id ) rank ,selectname from   workflow_selectitem where fieldid in ( " +
                "select id from  workflow_billfield t where t.fieldname ='xzsj' and billid in ( " +
                "select formid from workflow_base where unnumber='jgxtsq' and rownum=1 " +
                ")) order by id  ";
        List<Map<String,Object>> list= JAVATools.GetTableToListMap(sql,null);
        if(list==null || list.size()<=0){
            return key;
        }
        for (Map<String,Object> map : list){
            String value=Util.null2String(Util.getIntValue(Util.null2String(map.get("rank")))-1);
            if(key.equals(value)){
                return Util.null2String(map.get("selectname"));
            }
        }
        return key;
    }

    private  String gettransactionype(String key){
        String sql="  select rank() over(order by id ) rank ,selectname from   workflow_selectitem where fieldid in ( " +
                "            select id from  workflow_billfield t where t.fieldname ='xzjy' and billid in ( " +
                "            select formid from workflow_base where unnumber='jgxtsq' and rownum=1 " +
                "    )) order by id";
        List<Map<String,Object>> list= JAVATools.GetTableToListMap(sql,null);
        if(list==null || list.size()<=0){
            return key;
        }
        for (Map<String,Object> map : list){
            String value=Util.null2String(Util.getIntValue(Util.null2String(map.get("rank")))-1);
            if(key.equals(value)){
                return Util.null2String(map.get("selectname"));
            }
        }
        return key;
    }

}
