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 CoworkPublicCronJob extends BaseCronJob {
    @Override
    public void execute() {
        runTask();
    }

    public void runTask(){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        PropBean propBean = new PropBean();
        try{
            bb.writeLog("CoworkPublicCronJob--");
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            Map<String,String> map = null;

            String sql = " select id,rwbt,jbr,to_char(to_date(rwfbsj,'yyyy-mm-dd HH24:MI:SS')+1/2,'yyyy-mm-dd HH24:MI:SS') as rwfbsj,cjr from uf_rw \n" +
                            " where cjzt=1 and rwzt = 1\n" +
                            " and rwfbsj is not null \n" +
                            " and to_char(to_date(rwfbsj,'yyyy-mm-dd HH24:MI:SS')+1/2,'yyyy-mm-dd') = to_char(sysdate,'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 rwfbsj = Util.null2String(rs.getString("rwfbsj"));

                map = new HashMap<String,String>();
                map.put("id",id);
                map.put("rwbt",rwbt);
                map.put("jbr",jbr);
                map.put("cjr",cjr);
                map.put("rwfbsj",rwfbsj);
                String rq = "";
                String sj = "";
                try{
                    if(!"".equals(rwfbsj)){
                        rq = rwfbsj.substring(0,10);
                        sj = rwfbsj.substring(11,16);
                    }
                }catch (Exception e){

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

            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 rwfbsj = Util.null2String(datamap.get("rwfbsj"));
                String id = Util.null2String(datamap.get("id"));
                bb.writeLog("rwfbsj:"+rwfbsj);
                String nowTime = sdf.format(new Date());
                int xcfzs = 0;
                sql = " select ceil(((To_date('"+rwfbsj+"','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")));
                }

                System.out.println("billid:"+id+"    xcfzs:"+xcfzs);
                if(xcfzs >=0 && xcfzs < 30 ){
                    datalist.add(datamap);
                }
            }

            if(datalist !=null && datalist.size() > 0)
            {
                bb.writeLog("datalist:"+datalist.size());

                Smstools Smstools = new Smstools();

                int smstypeid=Integer.parseInt(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 jbrnames = "" ;
                    String jbr = Util.null2String(dMap.get("jbr"));
                    sql = " select id,lastname from hrmresource where id in("+jbr+") ";
                    rs.execute(sql);
                    while (rs.next()){
                        String jbrname = Util.null2String(rs.getString("lastname"));
                        jbrnames += jbrnames=="" ? jbrname : ","+jbrname;
                    }

                    String cjrname = "";
                    String cjr = Util.null2String(dMap.get("cjr"));
                    sql = " select id,lastname from hrmresource where id in("+cjr+") ";
                    rs.execute(sql);
                    if(rs.next()){
                        cjrname = Util.null2String(rs.getString("lastname"));
                    }
                    String bt = "任务未处理提醒";
                    String contextcjr = "您创建的任务“"+Util.null2String(dMap.get("rwbt"))+"”"+jbrnames+"未及时处理，请您跟踪查看";
                    String contextjbr = cjrname+"邀请您参与协作任务“"+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);

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

}