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 CoworkJzrqCronJob 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 from uf_xzsy where cjzt=1 and xzzt in(2) \n" +
                    " and jzsj is not null and (substr(jzsj,0,10)=to_char(sysdate,'yyyy-mm-dd') or substr(jzsj,0,10)=to_char(sysdate+1,'yyyy-mm-dd')) ";
            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);
                String rq = "";
                String sj = "";
                try{
                    if(!"".equals(jzrq)){
                        rq = jzrq.substring(0,10);
                        sj = jzrq.substring(11,16);
                    }
                }catch (Exception e){

                }
                map.put("rq",rq);
                map.put("sj",sj);
                list.add(map);
            }

            //bb.writeLog("list:"+list.size());
            SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
            Date today = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(today);
            cal.add(Calendar.DATE, +1);  //减一天  -1
            Date tomdate = cal.getTime();
            String tomorodate = dsdf.format(tomdate);
            String nowdate = dsdf.format(new Date());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<Map<String,String>> datalist = new ArrayList<Map<String,String>>();
            for(int i=0;i<list.size();i++)
            {
                Map<String,String> datamap = list.get(i);

                String rq = Util.null2String(datamap.get("rq"));
                String sj = Util.null2String(datamap.get("sj"));
                if(!"".equals(sj)){
                    if(sj.length() >=16){
                        sj = sj.substring(0,16);
                    }
                }

                //bb.writeLog("rq:"+rq);
                if(nowdate.equals(rq))
                {
                    String nowTime = sdf.format(new Date());
//                    String nowDate = dsdf.format(new Date());
                    int xcfzs = 0;
                    sj = sj +":00";
                    sql = " select ceil(((To_date('"+rq+" "+sj+"','yyyy-mm-dd hh24-mi-ss')-To_date('"+nowTime+"','yyyy-mm-dd hh24-mi-ss'))) * 24 * 60)  xcfzs FROM DUAL";
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        xcfzs = Util.getIntValue(Util.null2String(rs.getString("xcfzs")));
                    }
                    if((xcfzs-120) >=0 && (xcfzs-120) < 30 ){
                        datamap.put("smstype","1");
                        datalist.add(datamap);
                    }
                }else if(tomorodate.equals(rq)){

                    String nowTime = sdf.format(new Date());
                    String nowDate = dsdf.format(new Date());
                    int xcfzs = 0;
                    sj = sj +":00";
                    sql = " select ceil(((To_date('"+nowDate+" "+sj+"','yyyy-mm-dd hh24-mi-ss')-To_date('"+nowTime+"','yyyy-mm-dd hh24-mi-ss'))) * 24 * 60)  xcfzs FROM DUAL";
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        xcfzs = Util.getIntValue(Util.null2String(rs.getString("xcfzs")));
                    }
                    if(xcfzs >=0 && xcfzs < 30 ){
                        datamap.put("smstype","2");
                        datalist.add(datamap);
                    }
                }
            }
            if(datalist !=null && datalist.size() > 0)
            {
                resourceComInfo = new ResourceComInfo();
                bb.writeLog("datalist:"+datalist.size());

                Smstools Smstools = new Smstools();

                int smstypeid = Integer.parseInt(weaver.general.Util.null2String(propBean.getPropName("uf_ry_sms")));

                ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
                for (int i = 0; i < datalist.size(); i++)
                {
                    Map<String,String> dMap = datalist.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);
                    }

//                    final int index = i;
//                    fixedThreadPool.execute(new Runnable() {
//                        @Override
//                        public void run() {
//                            try {
//                                bb.writeLog("--线程---"+String.valueOf(index)+" bt:"+bt+" &billid="+billid+" cjr:"+cjr);
//
//                                Set<String> cjrsetuser = new HashSet(Arrays.asList(cjr.split(",")));
//                                Smstools.sendMessage(smstypeid,cjrsetuser,bt,contextcjr,link,link);
//
//                                if(!"".equals(jbr))
//                                {
//                                    String contextjbr = "您参与的协作事项“"+Util.null2String(dMap.get("bt"))+"”即将到期，请您及时处理";
//                                    Set<String> jbrsetuser = new HashSet(Arrays.asList(jbr.split(",")));
//                                    Smstools.sendMessage(smstypeid,jbrsetuser,bt,contextjbr,link,link);
//                                }
//
//
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    });



                }
            }

        }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 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);

                String rq = "";
                String sj = "";
                try{
                    if(!"".equals(jzrq)){
                        rq = jzrq.substring(0,10);
                        sj = jzrq.substring(11,16);
                    }
                }catch (Exception e){

                }
                map.put("rq",rq);
                map.put("sj",sj);
                list.add(map);
            }

            //bb.writeLog("list:"+list.size());
            SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
            Date today = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(today);
            cal.add(Calendar.DATE, +1);  //减一天  -1
            Date tomdate = cal.getTime();
            String tomorodate = dsdf.format(tomdate);
            String nowdate = dsdf.format(new Date());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<Map<String,String>> datalist = new ArrayList<Map<String,String>>();
            for(int i=0;i<list.size();i++)
            {
                Map<String,String> datamap = list.get(i);

                String rq = Util.null2String(datamap.get("rq"));
                String sj = Util.null2String(datamap.get("sj"));
                if(!"".equals(sj)){
                    if(sj.length() > 16){
                        sj = sj.substring(0,16);
                    }
                }

                bb.writeLog("rq:"+rq);
                if(nowdate.equals(rq))
                {
                    String nowTime = sdf.format(new Date());
//                    String nowDate = dsdf.format(new Date());
                    int xcfzs = 0;
                    sj = sj +":00";
                    sql = " select ceil(((To_date('"+rq+" "+sj+"','yyyy-mm-dd hh24-mi-ss')-To_date('"+nowTime+"','yyyy-mm-dd hh24-mi-ss'))) * 24 * 60)  xcfzs FROM DUAL";
                    //bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        xcfzs = Util.getIntValue(Util.null2String(rs.getString("xcfzs")));
                    }
                    if((xcfzs-120) >=0 && (xcfzs-120) < 30 ){
                        datamap.put("smstype","1");
                        datalist.add(datamap);
                    }
                }else if(tomorodate.equals(rq)){

                    String nowTime = sdf.format(new Date());
                    String nowDate = dsdf.format(new Date());
                    int xcfzs = 0;
                    sj = sj +":00";
                    sql = " select ceil((To_date('"+nowDate+" "+sj+"','yyyy-mm-dd hh24-mi-ss')-(To_date('"+nowTime+"','yyyy-mm-dd hh24-mi-ss') )) * 24 * 60)  xcfzs FROM DUAL";
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        xcfzs = Util.getIntValue(Util.null2String(rs.getString("xcfzs")));
                    }
                    if(xcfzs >=0 && xcfzs < 30 ){
                        datalist.add(datamap);
                    }
                }
            }
            if(datalist !=null && datalist.size() > 0)
            {
//                resourceComInfo = new ResourceComInfo();

                Smstools Smstools = new Smstools();
                int smstypeid=Integer.parseInt(Util.null2String(propBean.getPropName("uf_ry_sms")));

                bb.writeLog("datalist:"+datalist.size());
//                ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
                for (int i = 0; i < datalist.size(); i++)
                {
                    Map<String,String> dMap = datalist.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 link = "/mobilemode/mobile/view.html?appHomepageId=4002#&page_4001?appid=2501&modelid=15003&uitype=1&layoutid=29501&billid="+billid;

                    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 from uf_xzsx where cjzt=1 and xzzt in(2) \n" +
                         " and jzsj is not null and (substr(jzsj,0,10)=to_char(sysdate,'yyyy-mm-dd') or substr(jzsj,0,10)=to_char(sysdate+1,'yyyy-mm-dd')) ";
            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);
                String rq = "";
                String sj = "";
                try{
                    if(!"".equals(jzrq)){
                        rq = jzrq.substring(0,10);
                        sj = jzrq.substring(11,16);
                    }
                }catch (Exception e){

                }
                map.put("rq",rq);
                map.put("sj",sj);
                list.add(map);
            }

            //bb.writeLog("list:"+list.size());
            SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
            Date today = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(today);
            cal.add(Calendar.DATE, +1);  //减一天  -1
            Date tomdate = cal.getTime();
            String tomorodate = dsdf.format(tomdate);
            String nowdate = dsdf.format(new Date());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<Map<String,String>> datalist = new ArrayList<Map<String,String>>();
            for(int i=0;i<list.size();i++)
            {
                Map<String,String> datamap = list.get(i);

                String rq = Util.null2String(datamap.get("rq"));
                String sj = Util.null2String(datamap.get("sj"));
                //bb.writeLog("rq:"+rq);
                if(nowdate.equals(rq))
                {
                    String nowTime = sdf.format(new Date());
//                    String nowDate = dsdf.format(new Date());
                    int xcfzs = 0;
                    sj = sj +":00";
                    sql = " select ceil(((To_date('"+rq+" "+sj+"','yyyy-mm-dd hh24-mi-ss')-To_date('"+nowTime+"','yyyy-mm-dd hh24-mi-ss'))) * 24 * 60)  xcfzs FROM DUAL";
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        xcfzs = Util.getIntValue(Util.null2String(rs.getString("xcfzs")));
                    }
                    if((xcfzs-120) >=0 && (xcfzs-120) < 30 ){
                        datamap.put("smstype","1");
                        datalist.add(datamap);
                    }
                }else if(tomorodate.equals(rq)){

                    String nowTime = sdf.format(new Date());
                    String nowDate = dsdf.format(new Date());
                    int xcfzs = 0;
                    sj = sj +":00";
                    sql = " select ceil((To_date('"+nowDate+" "+sj+"','yyyy-mm-dd hh24-mi-ss')-(To_date('"+nowTime+"','yyyy-mm-dd hh24-mi-ss') )) * 24 * 60)  xcfzs FROM DUAL";
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        xcfzs = Util.getIntValue(Util.null2String(rs.getString("xcfzs")));
                    }
                    if(xcfzs >=0 && xcfzs < 30 ){
                        datamap.put("smstype","2");
                        datalist.add(datamap);
                    }
                }
            }
            if(datalist !=null && datalist.size() > 0)
            {
//                resourceComInfo = new ResourceComInfo();
//                bb.writeLog("datalist:"+datalist.size());

                Smstools Smstools = new Smstools();
                int smstypeid = Integer.parseInt(weaver.general.Util.null2String(propBean.getPropName("uf_ry_sms")));

                for (int i = 0; i < datalist.size(); i++)
                {
                    Map<String,String> dMap = datalist.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")));

//                    String jbr = "" ;
//                    sql = " select jbr from uf_rw t where t.xzsy = "+billid+" and rwzt in(1,2) ";
//                    rs.execute(sql);
//                    while (rs.next()){
//                        String twjbr = Util.null2String(rs.getString("jbr"));
//                        jbr +=  jbr == "" ? twjbr : ","+twjbr ;
//                    }

                    bb.writeLog(" bt:"+bt+" &billid="+billid+" cjr:"+cjr);
                    // String link = "/mobilemode/mobile/view.html?appHomepageId=6503#&page_6002?appid=2501&modelid=53503&billid="+billid;

                    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);
        }
    }
}