package weaver.interfaces.htsc.cowork.job;

import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CoworkJzrqCronJobNew extends BaseCronJob {
    @Override
    public void execute() {
        runCoWork();
        runTask();
        runDuban();
    }

    public void runCoWork(){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        PropBean propBean = new PropBean();
        ResourceComInfo resourceComInfo = null;
        try{
            bb.writeLog("进入--CoworkJzrqCronJob--定时器");
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            Map<String,String> map = null;

            String sql =" select id,bt,jbr,jzsj,cjr,'1' as type,\n" +
                    "       (round(to_number(to_date(substr(jzsj,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-120) as sjc\n" +
                    "   from uf_xzsy where cjzt=1 and xzzt in(2) \n" +
                    "   and substr(jzsj,0,10)=to_char(sysdate,'yyyy-mm-dd') \n" +
                    "   and (round(to_number(to_date(substr(jzsj,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-120)>0\n" +
                    "   and jzsj is not null\n" +
                    " union all\n" +
                    " select id,bt,jbr,jzsj,cjr,'2' as type, \n" +
                    "       round(to_number(to_date(substr(jzsj,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440-24*60) as sjc\n" +
                    "   from uf_xzsy where cjzt=1 and xzzt in(2) \n" +
                    "   and substr(jzsj,0,10)=to_char(sysdate+1,'yyyy-mm-dd')\n" +
                    "   and (round(to_number(to_date(substr(jzsj,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-24*60)>0\n" +
                    "   and jzsj is not null" +
                    " ";
            rs.execute(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String bt = Util.null2String(rs.getString("bt"));
                String jbr = Util.null2String(rs.getString("jbr"));
                String cjr = Util.null2String(rs.getString("cjr"));
                String jzrq = Util.null2String(rs.getString("jzsj"));

                map = new HashMap<String,String>();
                map.put("id",id);
                map.put("bt",bt);
                map.put("jbr",jbr);
                map.put("cjr",cjr);
                list.add(map);
            }

            if(list !=null && list.size() > 0)
            {
                Smstools Smstools = new Smstools();
                int smstypeid = Integer.parseInt(weaver.general.Util.null2String(propBean.getPropName("uf_ry_sms")));

                for (int i = 0; i < list.size(); i++)
                {
                    Map<String,String> dMap = list.get(i);
                    String billid = Util.null2String(dMap.get("id"));
                    String jbr = Util.null2String(dMap.get("jbr"));
                    String cjr = Util.null2String(dMap.get("cjr"));
                    String bt = "协作事项到期提醒";
                    String contextcjr = "您创建的协作事项“"+Util.null2String(dMap.get("bt"))+"”即将到期，请您跟踪处理";
                    bb.writeLog("--bt:"+ Util.null2String(dMap.get("bt")));

                    bb.writeLog(" bt:"+bt+" &billid="+billid+" cjr:"+cjr);
                    //String link = "/mobilemode/mobile/view.html?appid=2501#&page_3502?appid=2501&modelid=15004&uitype=1&layoutid=29503&billid="+billid;

                    String xz_mobile_url = propBean.getPropName("xz_mobile_url");
                    String mobilelink = xz_mobile_url+"&billid="+billid;

                    Set<String> cjrsetuser = new HashSet(Arrays.asList(cjr.split(",")));
                    Smstools.sendMessage(smstypeid,cjrsetuser,bt,contextcjr,mobilelink,mobilelink);

                    bb.writeLog(" bt:"+bt+" &billid="+billid+" jbr:"+jbr);
                    if(!"".equals(jbr))
                    {
                        List<String> dt2List = new ArrayList<String>();
                        sql = " select ry from uf_xzsy_dt2 where mainid=" + billid + " and ywc=0 ";
                        bb.writeLog("sql:"+sql);
                        rs.execute(sql);
                        while (rs.next()){
                            String dt2ry = Util.null2String(rs.getString("ry"));
                            dt2List.add(dt2ry);
                        }

                        List<String> jbrList = new ArrayList<String>();
                        String[] jbrArray = jbr.split(",");
                        for(int k=0;k<jbrArray.length;k++)
                        {
                            if(!dt2List.contains(jbrArray[k])){
                                jbrList.add(jbrArray[k]);
                            }
                        }

                        String contextjbr = "您参与的协作事项“"+Util.null2String(dMap.get("bt"))+"”即将到期，请您及时处理";
                        Set<String> jbrsetuser = new HashSet(jbrList);
                        Smstools.sendMessage(smstypeid,jbrsetuser,bt,contextjbr,mobilelink,mobilelink);
                    }
                }
            }

        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
    }


    public void runTask(){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        PropBean propBean = new PropBean();

//        ResourceComInfo resourceComInfo = null;

        try{
            bb.writeLog("进入--CoworkJzrqCronJob--定时器");
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            Map<String,String> map = null;


            String sql =" select id,rwbt,jbr,jzrq,cjr,'1' as type,\n" +
                    "       (round(to_number(to_date(substr(jzrq,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-120) as sjc\n" +
                    "   from uf_rw where cjzt=1 and rwzt in(2) \n" +
                    "   and substr(jzrq,0,10)=to_char(sysdate,'yyyy-mm-dd') \n" +
                    "   and (round(to_number(to_date(substr(jzrq,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-120)>0\n" +
                    "   and jzrq is not null\n" +
                    " union all\n" +
                    " select id,rwbt,jbr,jzrq,cjr,'2' as type, \n" +
                    "       round(to_number(to_date(substr(jzrq,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440-24*60) as sjc\n" +
                    "   from uf_rw where cjzt=1 and rwzt in(2) \n" +
                    "   and substr(jzrq,0,10)=to_char(sysdate+1,'yyyy-mm-dd')\n" +
                    "   and (round(to_number(to_date(substr(jzrq,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-24*60)>0\n" +
                    "   and jzrq is not null " ;

//            String sql = " select id,rwbt,jbr,jzrq,cjr from uf_rw where cjzt=1 and rwzt in(1,2) \n" +
//                         " and jzrq is not null  and (substr(jzrq,0,10)=to_char(sysdate,'yyyy-mm-dd') or substr(jzrq,0,10)=to_char(sysdate+1,'yyyy-mm-dd')) \n";
            rs.execute(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String rwbt = Util.null2String(rs.getString("rwbt"));
                String jbr = Util.null2String(rs.getString("jbr"));
                String cjr = Util.null2String(rs.getString("cjr"));
                String jzrq = Util.null2String(rs.getString("jzrq"));

                map = new HashMap<String,String>();
                map.put("id",id);
                map.put("rwbt",rwbt);
                map.put("jbr",jbr);
                map.put("cjr",cjr);
                map.put("jzrq",jzrq);

                list.add(map);
            }


            if(list !=null && list.size() > 0)
            {
                Smstools Smstools = new Smstools();
                int smstypeid = Integer.parseInt(Util.null2String(propBean.getPropName("uf_ry_sms")));

                bb.writeLog("list:"+list.size());

                for (int i = 0; i < list.size(); i++)
                {
                    Map<String,String> dMap = list.get(i);
                    String billid = Util.null2String(dMap.get("id"));
                    String jbr = Util.null2String(dMap.get("jbr"));
                    String cjr = Util.null2String(dMap.get("cjr"));
                    String bt = "任务到期提醒";
                    String contextcjr = "您创建的协作任务“"+Util.null2String(dMap.get("rwbt"))+"”即将到期，请您及时查看";
                    String contextjbr = "您经办的协作任务“"+Util.null2String(dMap.get("rwbt"))+"”即将到期，请您及时处理";

                    String rw_mobile_url = propBean.getPropName("rw_mobile_url");
                    String mobilelink = rw_mobile_url+"&billid="+billid;

                    Set<String> cjrsetuser = new HashSet(Arrays.asList(cjr.split(",")));
                    Smstools.sendMessage(smstypeid,cjrsetuser,bt,contextcjr,mobilelink,mobilelink);

                    if(!"".equals(jbr))
                    {
                        List<String> dt3List = new ArrayList<String>();
                        sql = " select ry from uf_rw_dt3 where mainid=" + billid + " and ywc=0 ";
                        bb.writeLog("sql:"+sql);
                        rs.execute(sql);
                        while (rs.next()){
                            String dt2ry = Util.null2String(rs.getString("ry"));
                            dt3List.add(dt2ry);
                        }

                        List<String> jbrList = new ArrayList<String>();
                        String[] jbrArray = jbr.split(",");
                        for(int k=0;k<jbrArray.length;k++)
                        {
                            if(!dt3List.contains(jbrArray[k])){
                                jbrList.add(jbrArray[k]);
                            }
                        }
                        Set<String> jbrsetuser = new HashSet(jbrList);
                        Smstools.sendMessage(smstypeid,jbrsetuser,bt,contextjbr,mobilelink,mobilelink);
                    }
                }
            }
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
    }


    /***
     *
     */
    public void runDuban(){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        PropBean propBean = new PropBean();
//        ResourceComInfo resourceComInfo = null;
        try{
            bb.writeLog("进入--dubanJzrq--定时器");
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            Map<String,String> map = null;

            String sql =" select id,bt,jbr,jzsj,cjr,'1' as type,\n" +
                    "       (round(to_number(to_date(substr(jzsj,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-120) as sjc\n" +
                    "   from uf_xzsx where cjzt=1 and xzzt in(2) \n" +
                    "       and substr(jzsj,0,10)=to_char(sysdate,'yyyy-mm-dd') \n" +
                    "       and (round(to_number(to_date(substr(jzsj,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-120)>0\n" +
                    "       and jzsj is not null\n" +
                    " union all\n" +
                    " select id,bt,jbr,jzsj,cjr,'2' as type, \n" +
                    "       (round(to_number(to_date(substr(jzsj,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-24*60) as sjc\n" +
                    "   from uf_xzsx where cjzt=1 and xzzt in(2) \n" +
                    "   and substr(jzsj,0,10)=to_char(sysdate+1,'yyyy-mm-dd')\n" +
                    "   and (round(to_number(to_date(substr(jzsj,0,16)||':00','yyyy-MM-dd hh24:mi:ss')-to_date(to_char(sysdate,'yyyy-MM-dd hh24:mi')||':00','yyyy-MM-dd hh24:mi:ss'))*1440)-24*60)>0\n" +
                    "   and jzsj is not null" +
                    " ";
            rs.execute(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String bt = Util.null2String(rs.getString("bt"));
                String jbr = Util.null2String(rs.getString("jbr"));
                String cjr = Util.null2String(rs.getString("cjr"));
                String jzrq = Util.null2String(rs.getString("jzsj"));

                map = new HashMap<String,String>();
                map.put("id",id);
                map.put("bt",bt);
                map.put("jbr",jbr);
                map.put("cjr",cjr);
                map.put("jzrq",jzrq);
                list.add(map);
            }

            if(list !=null && list.size() > 0)
            {
                Smstools Smstools = new Smstools();
                int smstypeid = Integer.parseInt(weaver.general.Util.null2String(propBean.getPropName("uf_ry_sms")));

                for (int i = 0; i < list.size(); i++)
                {
                    Map<String,String> dMap = list.get(i);
                    String billid = Util.null2String(dMap.get("id"));
                    String jbr = Util.null2String(dMap.get("jbr"));
                    String cjr = Util.null2String(dMap.get("cjr"));
                    String bt = "督办任务到期提醒";
                    String contextcjr = "您创建的督办任务“"+Util.null2String(dMap.get("bt"))+"”即将到期，请您跟踪处理";
                    bb.writeLog("--bt:"+ Util.null2String(dMap.get("bt")));

                    bb.writeLog(" bt:"+bt+" &billid="+billid+" cjr:"+cjr);

                    String dbrw_mobile_url = propBean.getPropName("dbrw_mobile_url");
                    String mobilelink = dbrw_mobile_url+"&billid="+billid;

                    Set<String> cjrsetuser = new HashSet(Arrays.asList(cjr.split(",")));
                    Smstools.sendMessage(smstypeid,cjrsetuser,bt,contextcjr,mobilelink,mobilelink);

                    bb.writeLog(" bt:"+bt+" &billid="+billid+" jbr:"+jbr);
                    if(!"".equals(jbr))
                    {
                        List<String> dt2List = new ArrayList<String>();
                        sql = " select ry from uf_xzsx_dt2 where mainid=" + billid + " and ywc=0 ";
                        bb.writeLog("sql:"+sql);
                        rs.execute(sql);
                        while (rs.next()){
                            String dt2ry = Util.null2String(rs.getString("ry"));
                            dt2List.add(dt2ry);
                        }

                        List<String> jbrList = new ArrayList<String>();
                        String[] jbrArray = jbr.split(",");
                        for(int k=0;k<jbrArray.length;k++)
                        {
                            if(!dt2List.contains(jbrArray[k])){
                                jbrList.add(jbrArray[k]);
                            }
                        }

                        String contextjbr = "您参与的督办任务“"+Util.null2String(dMap.get("bt"))+"”即将到期，请您及时处理";
                        Set<String> jbrsetuser = new HashSet(jbrList);
                        Smstools.sendMessage(smstypeid,jbrsetuser,bt,contextjbr,mobilelink,mobilelink);
                    }
                }
            }
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
    }
}
