package weaver.interfaces.htsc.cowork.job;

import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.schedule.BaseCronJob;
import java.text.SimpleDateFormat;
import java.util.*;

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

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

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

            String sql = " select zqtx,zqtxyf,zqtxsj,id,rwbt,jbr,cjr from uf_rw where cjzt=1 and rwzt in(1,2) and zqtx in(1,2)\n" +
                    " and zqtxyf is not null and zqtxsj is not null ";
            rs.execute(sql);
            while (rs.next()){
                String zqtx = Util.null2String(rs.getString("zqtx"));
                String zqtxyf = Util.null2String(rs.getString("zqtxyf"));
                String zqtxsj = Util.null2String(rs.getString("zqtxsj"));
                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"));

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


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

            Date today = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(today);
            int weekday = cal.get(Calendar.DAY_OF_WEEK);
            System.out.println("weekday:"+weekday);
            int day = cal.get(Calendar.DATE);
            System.out.println("day:"+day);

//            PropBean pb = new PropBean();
//            String jgfes = "30";
            int zqtx_jgfes = 30;
//            if(!"".equals(jgfes)){
//                try{
//                    zqtx_jgfes = Util.getIntValue(jgfes);
//                }catch (Exception e){
//                    bb.writeLog("e:"+e);
//                    zqtx_jgfes = 30;
//                }
//            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
            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 zqtx = Util.null2String(datamap.get("zqtx"));
                String zqtxyf = Util.null2String(datamap.get("zqtxyf"));
                String zqtxsj = Util.null2String(datamap.get("zqtxsj"));
                if(!"".equals(zqtxsj)){
                    if(zqtxsj.length() > 16){
                        zqtxsj = zqtxsj.substring(0,16);
                    }
                }
//                String id = Util.null2String(datamap.get("id"));
//                String rwbt = Util.null2String(datamap.get("rwbt"));
//                String jbr = Util.null2String(datamap.get("jbr"));

                String xzrw_zqtx_fieldid = propBean.getPropName("xzrw_zqtx_fieldid");
                bb.writeLog("zqtx:"+zqtx+" zqtxyf:"+zqtxyf+" zqtxsj:"+zqtxsj);
                if("1".equals(zqtx))
                {
                    String selectname = "";
                    sql = " select selectname  from workflow_selectitem where fieldid="+xzrw_zqtx_fieldid+" and selectvalue="+zqtxyf;
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        selectname = Util.null2String(rs.getString("selectname"));
                    }
                    int zqweek = 0;
                    if("周一".equals(selectname)){
                        zqweek = 2;
                    }else if("周二".equals(selectname)){
                        zqweek = 3;
                    }else if("周三".equals(selectname)){
                        zqweek = 4;
                    }else if("周四".equals(selectname)){
                        zqweek = 5;
                    }else if("周五".equals(selectname)){
                        zqweek = 6;
                    }else if("周六".equals(selectname)){
                        zqweek = 7;
                    }else if("周日".equals(selectname)){
                        zqweek = 1;
                    }
                    bb.writeLog("zqweek:"+zqweek);
                    if(weekday == zqweek)
                    {
                        int xcfzs = 0;
                        String nowTime = sdf.format(new Date());
                        String nowDate = dsdf.format(new Date());
                        zqtxsj = zqtxsj +":00";
                        sql = " select ceil(((To_date('"+nowDate+" "+zqtxsj+"','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 < zqtx_jgfes ){
                            datalist.add(datamap);
                        }
                    }
                }else if("2".equals(zqtx)){
                    String selectname = "";
                    sql = " select selectname  from workflow_selectitem where fieldid="+xzrw_zqtx_fieldid+" and selectvalue="+zqtxyf;
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        selectname = Util.null2String(rs.getString("selectname"));
                    }
                    bb.writeLog("selectname:"+selectname);
                    if(selectname.startsWith("0")){
                        selectname = selectname.substring(1);
                    }
                    bb.writeLog("selectname:"+selectname);
                    int zqday = 0 ;
                    try{
                        zqday = Util.getIntValue(selectname.replace("日",""));
                    }catch (Exception e){
                        bb.writeLog("e:"+e);
                    }
                    bb.writeLog("zqday:"+zqday);
                    if(day == zqday)
                    {
                        int xcfzs = 0;
                        String nowTime = sdf.format(new Date());
                        String nowDate = dsdf.format(new Date());
                        zqtxsj = zqtxsj +":00";
                        sql = " select ceil((To_date('"+nowDate+" "+zqtxsj+"','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 < zqtx_jgfes ){
                            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 cjrname = resourceComInfo.getLastname(cjr);

                    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;

                    String bt = "任务定期提醒";

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

                    Set<String> jbrsetuser = new HashSet(Arrays.asList(jbr.split(",")));
                    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();

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

            String sql = " select zqtx,zqtxyf,zqtxsj,id,bt,jbr,cjr from uf_xzsx where cjzt=1 and xzzt in(1,2) and zqtx in(1,2)\n" +
                    " and zqtxyf is not null and zqtxsj is not null ";
            rs.execute(sql);
            while (rs.next()){
                String zqtx = Util.null2String(rs.getString("zqtx"));
                String zqtxyf = Util.null2String(rs.getString("zqtxyf"));
                String zqtxsj = Util.null2String(rs.getString("zqtxsj"));
                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"));

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

            Date today = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(today);
            int weekday = cal.get(Calendar.DAY_OF_WEEK);
            System.out.println("weekday:"+weekday);
            int day = cal.get(Calendar.DATE);
            System.out.println("day:"+day);

            int zqtx_jgfes = 30;

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
            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 zqtx = Util.null2String(datamap.get("zqtx"));
                String zqtxyf = Util.null2String(datamap.get("zqtxyf"));
                String zqtxsj = Util.null2String(datamap.get("zqtxsj"));
                if(!"".equals(zqtxsj)){
                    if(zqtxsj.length() > 16){
                        zqtxsj = zqtxsj.substring(0,16);
                    }
                }

                String duban_zqtx_fieldid = "" ;
                sql = " select id from workflow_billfield where billid in(\n" +
                        " select id from workflow_bill where tablename='uf_xzsx')\n" +
                        " and fieldname = 'zqtxyf'" ;
                rs.execute(sql);
                if(rs.next()){
                    duban_zqtx_fieldid = Util.null2String(rs.getString("id"));
                }

                bb.writeLog("zqtx:"+zqtx+" zqtxyf:"+zqtxyf+" zqtxsj:"+zqtxsj);
                if("1".equals(zqtx))
                {
                    String selectname = "";
                    sql = " select selectname  from workflow_selectitem where fieldid="+duban_zqtx_fieldid+" and selectvalue="+zqtxyf;
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        selectname = Util.null2String(rs.getString("selectname"));
                    }
                    int zqweek = 0;
                    if("周一".equals(selectname)){
                        zqweek = 2;
                    }else if("周二".equals(selectname)){
                        zqweek = 3;
                    }else if("周三".equals(selectname)){
                        zqweek = 4;
                    }else if("周四".equals(selectname)){
                        zqweek = 5;
                    }else if("周五".equals(selectname)){
                        zqweek = 6;
                    }else if("周六".equals(selectname)){
                        zqweek = 7;
                    }else if("周日".equals(selectname)){
                        zqweek = 1;
                    }
                    bb.writeLog("zqweek:"+zqweek);
                    if(weekday == zqweek)
                    {
                        int xcfzs = 0;
                        String nowTime = sdf.format(new Date());
                        String nowDate = dsdf.format(new Date());
                        zqtxsj = zqtxsj +":00";
                        sql = " select ceil(((To_date('"+nowTime+"','yyyy-mm-dd hh24-mi-ss')-To_date('"+nowDate+" "+zqtxsj+"','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 < zqtx_jgfes ){
                            datalist.add(datamap);
                        }
                    }
                }else if("2".equals(zqtx)){
                    String selectname = "";
                    sql = " select selectname  from workflow_selectitem where fieldid="+duban_zqtx_fieldid+" and selectvalue="+zqtxyf;
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        selectname = Util.null2String(rs.getString("selectname"));
                    }
                    bb.writeLog("selectname:"+selectname);
                    if(selectname.startsWith("0")){
                        selectname = selectname.substring(1);
                    }
                    bb.writeLog("selectname:"+selectname);
                    int zqday = 0 ;
                    try{
                        zqday = Util.getIntValue(selectname.replace("日",""));
                    }catch (Exception e){
                        bb.writeLog("e:"+e);
                    }
                    bb.writeLog("zqday:"+zqday);
                    if(day == zqday)
                    {
                        int xcfzs = 0;
                        String nowTime = sdf.format(new Date());
                        String nowDate = dsdf.format(new Date());
                        zqtxsj = zqtxsj +":00";
                        sql = " select ceil(((To_date('"+nowTime+"','yyyy-mm-dd hh24-mi-ss')-To_date('"+nowDate+" "+zqtxsj+"','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 < zqtx_jgfes ){
                            datalist.add(datamap);
                        }
                    }
                }
            }

            if(datalist !=null && datalist.size() > 0)
            {
                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 jbrzzhs = "" ;
                    sql = " select id from hrmresource where workcode in(select workcode from hrmresource where id in("+jbr+"))\n" +
                            " and belongto is null ";
                    rs.execute(sql);
                    while(rs.next()){
                        String jbrzzh = Util.null2String(rs.getString("id"));
                        jbrzzhs += jbrzzhs=="" ? jbrzzh : ","+jbrzzh ;
                    }

                    String cjrzzhs = "" ;
                    sql = " select id from hrmresource where workcode in(select workcode from hrmresource where id in("+cjr+"))\n" +
                            " and belongto is null ";
                    rs.execute(sql);
                    while(rs.next()){
                        String cjrzzh = Util.null2String(rs.getString("id"));
                        cjrzzhs += cjrzzhs=="" ? cjrzzh : ","+cjrzzh ;
                    }

                    String contextcjr = "您创建的督办任务“"+Util.null2String(dMap.get("bt"))+"”收到定期提醒，请您及时查看";
                    String contextjbr = "您经办的督办任务“"+Util.null2String(dMap.get("bt"))+"”收到定期提醒，请您及时处理";

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

                    String bt = "督办任务定期提醒";

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

                    bb.writeLog("jbr:"+jbrzzhs);
                    Set<String> jbrsetuser = new HashSet(Arrays.asList(jbrzzhs.split(",")));
                    Smstools.sendMessage(smstypeid,jbrsetuser,bt,contextjbr,mobilelink,mobilelink);

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







}