package weaver.interfaces.zjrb.job;

import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.schedule.BaseCronJob;
import java.io.IOException;
import java.util.*;

/***
 *
 */
public class ThreeBatchRemindJob extends BaseCronJob {

    @Override
    public void execute() {
        int code = 1337;
        int creater = 1 ;
        queryExpectedLandingTime15Day(code,creater,15);
        queryExpectedLandingTime5Day(code,creater,5);
        queryExpectedLandingTime3Day(code,creater,3);
        queryExpectedLandingTimeSameDay(code,creater);
    }

    /***
     * 预计落地时间前15天，若尚未进行过程性管理录入
     * @param code
     * @param creater
     * @param day
     */
    public void queryExpectedLandingTime15Day(int code,int creater,int day){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog(":预计落地时间前15天，若尚未进行过程性管理录入，提醒人：企微提醒录入人员进行录入；</br>");
        ResourceComInfo resourceComInfo = null;
        try{
            resourceComInfo = new ResourceComInfo();
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            String sql =" select id,modedatacreater,proname,profall\n" +
                        " from uf_threebatches\n" +
                        " where id in(\n" +
                        "   select l.id \n" +
                        "   from uf_threebatches l " +
                        "   left join uf_threebatches_dt4 t on l.id = t.mainid \n" +
                        "   where l.id in(\n" +
                        "       select p.id\n" +
                        "       from uf_threebatches p\n" +
                        "       where (trunc(sysdate - to_date(p.profall,'YYYY-MM-DD' ))+1) <= " + day +
                        "       and (trunc(sysdate - to_date(p.profall,'YYYY-MM-DD' ))+1) >= 0 \n" +
                        "  )\n" +
                        "  group by l.id having count(t.id) = 0\n" +
                        ")" ;

            bb.writeLog("当前时间在预计落地时间(profall)的前15天，并且明细表4没有数据的:"+sql+"</br>");
            rs.executeQuery(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String lrry = Util.null2String(rs.getString("modedatacreater"));
                String xmmc = Util.null2String(rs.getString("proname"));
                String profall = Util.null2String(rs.getString("profall"));

                Map<String,String> map = new HashMap<String,String>();
                map.put("id",id);
                map.put("lrry",lrry);
                map.put("xmmc",xmmc);
                map.put("profall",profall);
                list.add(map);
            }

            bb.writeLog("总计:"+list.size()+" 行</br>");

            for(int i=0;i<list.size();i++){
                Map<String,String> map = list.get(i);
                String xmmc = map.get("xmmc");
                String lrry = map.get("lrry");
                String billid = map.get("id");
                String profall = map.get("profall");

                String context = "项目:"+xmmc+"未及时填报过程管理,请及时填报";
                String title = "三个一批项目提醒";
                Set<String> userSet = new HashSet<String>();
                userSet.add(lrry);
                bb.writeLog("序号:"+(i+1)+" ,建模ID:"+billid+" ,预计落地时间:"+profall+" ,提醒标题:"+title+" ,提醒内容:"+context+" ,提醒人:"+resourceComInfo.getLastname(lrry)+"("+lrry+")</br>");

                senNotice(code,title,context,userSet,billid,creater);

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

    /**
     * 若5天未录入，则企微推送支公司分管总
     * @param code
     * @param creater
     * @param day
     */
    public void queryExpectedLandingTime5Day(int code,int creater,int day){
        RecordSet rs = new RecordSet();

        BaseBean bb = new BaseBean();

        bb.writeLog("预计落地时间前5天，若尚未进行过程性管理录入,提醒人：支公司分管总；</br>");
        ResourceComInfo resourceComInfo = null;
        try{
            resourceComInfo = new ResourceComInfo();
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            String sql =" select id,modedatacreater,proname,profall\n" +
                    " from uf_threebatches\n" +
                    " where id in(\n" +
                    "   select l.id \n" +
                    "   from uf_threebatches l " +
                    "   left join uf_threebatches_dt4 t on l.id = t.mainid \n" +
                    "   where l.id in(\n" +
                    "       select p.id\n" +
                    "       from uf_threebatches p\n" +
                    "       where (trunc(sysdate - to_date(p.profall,'YYYY-MM-DD' ))+1) <= " + day +
                    "       and (trunc(sysdate - to_date(p.profall,'YYYY-MM-DD' ))+1) >= 0 \n" +
                    "  )\n" +
                    "  group by l.id having count(t.id) = 0\n" +
                    ")" ;

            bb.writeLog("当前时间在预计落地时间(profall)的前5天，并且明细表4没有数据的:"+sql+"</br>");
            rs.executeQuery(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String lrry = Util.null2String(rs.getString("modedatacreater"));
                String xmmc = Util.null2String(rs.getString("proname"));
                String profall = Util.null2String(rs.getString("profall"));
                Map<String,String> map = new HashMap<String,String>();

                map.put("id",id);
                map.put("lrry",lrry);
                map.put("xmmc",xmmc);
                map.put("profall",profall);

                list.add(map);
            }
            bb.writeLog("总计:"+list.size()+"</br>");

            for(int i=0;i<list.size();i++){
                Map<String,String> map = list.get(i);
                String xmmc = map.get("xmmc");
                String lrry = map.get("lrry");
                String billid = map.get("id");
                String profall = map.get("profall");

                Set<String> userSet = new HashSet<String>();

                String lastname = "";
                sql =   " select id\n" +
                        " from hrmresource\n" +
                        " where subcompanyid1 in (\n" +
                        "    select subcompanyid1 from hrmresource where id = "+lrry+"\n" +
                        " )\n" +
                        " and seclevel >=40 and seclevel <=50";

                bb.writeLog("获取录入人员随着支公司的分管总:"+sql+"</br>");
                rs.executeQuery(sql);
                while (rs.next()){
                    String id = Util.null2String(rs.getString("id"));

                    lastname += resourceComInfo.getLastname(id)+",";
                    userSet.add(id);
                }

                String context = "项目:"+xmmc+"未及时填报过程管理,请及时填报";
                String title = "三个一批项目提醒";

                bb.writeLog("序号:"+(i+1)+" ,建模ID:"+billid+" ,预计落地时间:"+profall+" ,提醒标题:"+title+" ,提醒内容:"+context+" ,提醒人:"+lastname+"("+userSet.toString()+")</br>");

                senNotice(code,title,context,userSet,billid,creater);
            }
        }catch (Exception e){
            bb.writeLog("queryExpectedLandingTime5Day:e:"+e);
        }
    }

    // 预计落地时间前3天，若尚未进行落地结果录入，企微提醒录入人员进行落地情况录入或者预计落地时间修改；
    public void queryExpectedLandingTime3Day(int code,int creater,int day){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();

        bb.writeLog(":预计落地时间前3天，若尚未进行落地结果录入，提醒：录入人员进行落地情况录入或者预计落地时间修改；</br>");
        ResourceComInfo resourceComInfo = null;
        try{
            resourceComInfo = new ResourceComInfo();
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            String sql =" select id,modedatacreater,proname,profall,sfcj\n" +
                    " from uf_threebatches\n" +
                    "       where (trunc(sysdate - to_date(profall,'YYYY-MM-DD' ))+1) <= " + day +
                    "       and (trunc(sysdate - to_date(profall,'YYYY-MM-DD' ))+1) >= 0 \n" +
                    " and sfcj is null " ;
            rs.executeQuery(sql);
            bb.writeLog("当前时间在预计落地时间(profall)的前3天，是否承接 为空值:"+sql+"</br>");
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String lrry = Util.null2String(rs.getString("modedatacreater"));
                String xmmc = Util.null2String(rs.getString("proname"));
                String profall = Util.null2String(rs.getString("profall"));
                String sfcj = Util.null2String(rs.getString("sfcj"));
                Map<String,String> map = new HashMap<String,String>();
                map.put("id",id);
                map.put("lrry",lrry);
                map.put("xmmc",xmmc);
                map.put("profall",profall);

                list.add(map);
            }

            bb.writeLog("总计:"+list.size()+"</br>");

            for(int i=0;i<list.size();i++){
                Map<String,String> map = list.get(i);
                String xmmc = map.get("xmmc");
                String lrry = map.get("lrry");
                String billid = map.get("id");

                String profall = map.get("profall");

                Set<String> userSet = new HashSet<String>();
                userSet.add(lrry);

                String context = "请及时维护"+xmmc+"的落地情况";
                String title = "三个一批项目提醒";

                bb.writeLog("序号:"+(i+1)+" ,建模ID:"+billid+" ,预计落地时间:"+profall+" ,提醒标题:"+title+" ,提醒内容:"+context+" ,提醒人:"+resourceComInfo.getLastname(lrry)+"("+lrry+")</br>");

                senNotice(code,title,context,userSet,billid,creater);
            }
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
    }

    // 预计落地时间到达后，若尚未进行落地结果录入，
    public void queryExpectedLandingTimeSameDay(int code,int creater){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        bb.writeLog("系统当前时间等于预计落地时，若尚未进行落地结果录入,提醒：支公司分管总 </br>");
        ResourceComInfo resourceComInfo = null;
        try{
            resourceComInfo = new ResourceComInfo();
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            String sql =" select id,modedatacreater,proname,profall,sfcj \n" +
                    " from uf_threebatches\n" +
                    " where profall = to_char(sysdate,'YYYY-MM-DD') " +
                    " and sfcj is null " ;
            rs.executeQuery(sql);
            bb.writeLog("当前时间 = 预计落地时间，是否承接 为空值:"+sql+"</br>");

            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String lrry = Util.null2String(rs.getString("modedatacreater"));
                String xmmc = Util.null2String(rs.getString("proname"));
                String profall = Util.null2String(rs.getString("profall"));
                String sfcj = Util.null2String(rs.getString("sfcj"));

                Map<String,String> map = new HashMap<String,String>();
                map.put("id",id);
                map.put("lrry",lrry);
                map.put("xmmc",xmmc);
                map.put("profall",profall);
                map.put("sfcj",sfcj);

                list.add(map);
            }
            bb.writeLog("总计:"+list.size()+"</br>");

            for(int i=0;i<list.size();i++){
                Map<String,String> map = list.get(i);
                String xmmc = map.get("xmmc");
                String lrry = map.get("lrry");
                String billid = map.get("id");
                String profall = map.get("profall");

                String lastname = "";
                Set<String> userSet = new HashSet<String>();
                sql =   " select id\n" +
                        " from hrmresource\n" +
                        " where subcompanyid1 in (\n" +
                        "    select subcompanyid1 from hrmresource where id = "+lrry+"\n" +
                        " )\n" +
                        " and seclevel >=40 and seclevel <=50";

                bb.writeLog("获取录入人员随着支公司的分管总:"+sql+"</br>");
                rs.executeQuery(sql);
                while (rs.next()){
                    String id = Util.null2String(rs.getString("id"));
                    userSet.add(id);

                    lastname += resourceComInfo.getLastname(id)+",";
                }
                String context = "请及时维护"+xmmc+"的落地情况";
                String title = "三个一批项目提醒";

                bb.writeLog("序号:"+(i+1)+" ,建模ID:"+billid+" ,预计落地时间:"+profall+" ,提醒标题:"+title+" ,提醒内容:"+context+" ,提醒人:"+lastname+"("+userSet.toString()+")</br>");

                senNotice(code,title,context,userSet,billid,creater);
            }
        }catch (Exception e){
            bb.writeLog("queryExpectedLandingTimeSameDay:e:"+e);
        }

    }


    /***
     *
     * @param code
     * @param title
     * @param context
     */
    public void senNotice(int code,String title,String context,Set<String> userSet,String billid,int creater){
        BaseBean bb = new BaseBean();
        if(userSet.size() > 0){
            /**
             * 消息来源（见文档第四点补充 必填）
             */
            MessageType messageType = MessageType.newInstance(code);
            /**
             * PC端链接
             */
            String linkUrl = "/spa/cube/index.html#/main/cube/card?type=0&modeId=26008&billid="+billid;
            /**
             * 移动端链接
             */
            String linkMobileUrl = "/mobilemode/mobile/view.html?appid=24501#&page_33506?billid="+billid;
            try {
                MessageBean messageBean = Util_Message.createMessage(messageType, userSet, title, context, linkUrl, linkMobileUrl);
                /**
                 * 创建人id
                 */
                messageBean.setCreater(creater);
                boolean isTrue = Util_Message.store(messageBean);
                bb.writeLog("MicroExaminationNoticeSendJob-senNotice:"+isTrue);
            } catch (IOException e) {
                bb.writeLog("MicroExaminationNoticeSendJob-e3:"+e);
            }
        }
    }

    /**
     *
     * @param userid
     * @return
     */
    public Set<String> getSubCompanyLeader(String userid){
        RecordSet rs = new RecordSet();
        Set<String> userSet = new HashSet<String>();
        String sql =" select id\n" +
                    " from hrmresource\n" +
                    " where subcompanyid1 in (\n" +
                    "    select subcompanyid1 from hrmresource where id = "+userid+"\n" +
                    " )\n" +
                    " and seclevel >=40 and seclevel <=50";
        rs.executeQuery(sql);
        while (rs.next()){
            String id = Util.null2String(rs.getString("id"));
            userSet.add(id);
        }

        return userSet;
    }

}
