<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="java.util.Comparator" %>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page" />
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    RecordSet rs = new RecordSet();
    User user = HrmUserVarify.getUser(request , response) ;
    int userid = user.getUID();
    String lastname = user.getLastname();
    JSONArray jsonArray = new JSONArray();
    String companystartdate = "" ;
    try{
        String createdate = "";
        String sql = " select createdate,companystartdate from hrmresource where  id="+userid ;
        rs.executeQuery(sql);
        if(rs.next()){
            companystartdate = Util.null2String(rs.getString("companystartdate"));
            createdate = Util.null2String(rs.getString("createdate"));
        }
        if(StringUtils.isEmpty(companystartdate)){
            companystartdate = createdate ;
        }
    }catch (Exception e){

    }
    if(StringUtils.isNotEmpty(companystartdate)){
        JSONObject dataJson = new JSONObject();
        dataJson.put("date",formatDate(companystartdate));
        dataJson.put("type","入职");
        dataJson.put("msg","今天是你入职泛微的第一天,欢迎你的加入!");
        dataJson.put("img","/interface/hrportal/image/rz.png");
        jsonArray.add(dataJson);
    }

    int count = 0;
    try{
        String sql = " select demosjdjkhtgrq,kscj from formtable_main_3085 where ksr="+userid+" and yskhjg=0 " ;
        rs.executeQuery(sql);
        if(rs.next()){
            String demosjdjkhtgrq = Util.null2String(rs.getString("demosjdjkhtgrq"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(demosjdjkhtgrq));
            dataJson.put("type","转正考核");
            dataJson.put("msg","恭喜你成功通过转正考核");
            dataJson.put("img","/interface/hrportal/image/kh.png");
            jsonArray.add(dataJson);
            count++;
        }
    }catch (Exception e){

    }
    try{
        if(count == 0)
        {
            String sql = " select realdate,result from te_examrecord where userid="+userid+" and status=6";
            rs.executeQuery(sql);
            if(rs.next())
            {
                String realdate = Util.null2String(rs.getString("realdate"));
                JSONObject dataJson = new JSONObject();
                dataJson.put("date",formatDate(realdate));
                dataJson.put("type","转正考核");
                dataJson.put("msg","恭喜你成功通过转正考核");
                dataJson.put("img","/interface/hrportal/image/kh.png");
                jsonArray.add(dataJson);
            }
        }
    }catch (Exception e){

    }

    try{
        String sql =" select t.changedate,t.type_n,h.jobtitlename,d.departmentname,p.lastname,s.subcompanyname,oldmanagerid,newmanagerid,oldDepartmentId,newDepartmentId,newDepartmentId,newSubcompanyId \n" +
                    " from HrmStatusHistory t\n" +
                    " left join hrmjobtitles h on h.id = t.newjobtitleid \n" +
                    " left join hrmdepartment d on d.id = t.newDepartmentId\n" +
                    " left join hrmresource p on p.id = t.newmanagerid\n" +
                    " left join hrmsubcompany s on s.id = t.newSubcompanyId\n" +
                    " where t.resourceid ="+userid +" and t.type_n in(2,4)";

        rs.executeQuery(sql);
        while (rs.next()){

            String type_n = rs.getString("type_n");
            String changedate = rs.getString("changedate");
            String jobtitlename = rs.getString("jobtitlename");
            String departmentname = rs.getString("departmentname");
            String managername = rs.getString("lastname");
            String subcompanyname = rs.getString("subcompanyname");

            if("2".equals(type_n)){
                JSONObject dataJson = new JSONObject();
                dataJson.put("date",formatDate(changedate));
                dataJson.put("type","转正");
                dataJson.put("msg","今天是你转正的日子,正式成为泛微的一员");
                dataJson.put("img","/interface/hrportal/image/zz.png");
                jsonArray.add(dataJson);

            }else if("4".equals(type_n)){
                JSONObject dataJson = new JSONObject();
                dataJson.put("date",formatDate(changedate));
                dataJson.put("type","调动");
//                String msg = "今天是你工作调动的日子,你现在" ;
//                if(StringUtils.isNotEmpty(jobtitlename)){
//                    msg += "岗位是:"+jobtitlename;
//                }
//                if(StringUtils.isNotEmpty(departmentname)){
//                    msg += "部门是:"+departmentname+",";
//                }
//                if(StringUtils.isNotEmpty(subcompanyname)){
//                    msg += "分部是:"+subcompanyname+",";
//                }
//                if(StringUtils.isNotEmpty(managername)){
//                    msg += "直接领导是:"+managername;
//                }
                dataJson.put("msg","今天是你工作调动的日子,你现在岗位是:"+jobtitlename);
                dataJson.put("img","/interface/hrportal/image/dd.png");
                jsonArray.add(dataJson);
            }
        }

//        String data = new HrmResourceWorkService().getResourceWorkView(request, response);
//        if(StringUtils.isNotEmpty(data)){
//            JSONObject jsonObject = JSONObject.parseObject(data);
//            if(jsonObject.containsKey("result")){
//                JSONObject resultJson = jsonObject.getJSONObject("result");
//                if(resultJson.containsKey("tables")){
//                    JSONArray tablesArray = resultJson.getJSONArray("tables");
//                    for(int i=0;i<tablesArray.size();i++){
//                        JSONObject tableJson = tablesArray.getJSONObject(i);
//                        String tabname = tableJson.getString("tabname").trim();
//                        if(tabname.contains("工作记录"))
//                        {
//                            JSONObject tabinfo = tableJson.getJSONObject("tabinfo");
//                            if(tabinfo.containsKey("datas")){
//                                JSONArray datasArray = resultJson.getJSONArray("datas");
//                                for(int k=0;k<datasArray.size();k++)
//                                {
//                                    JSONObject datasJson = datasArray.getJSONObject(k);
//                                    String type_n = datasJson.getString("type_n").trim();
//                                    String changedate = datasJson.getString("changedate").trim();
//                                    String olddepid = datasJson.getString("olddepid").trim();
//                                    if(type_n.contains("转正")){
//                                        JSONObject dataJson = new JSONObject();
//                                        dataJson.put("date",formatDate(changedate));
//                                        dataJson.put("type",type_n);
//                                        dataJson.put("msg","今天是你转正的日子,正是成为泛微的一员");
//                                        jsonArray.add(dataJson);
//                                    }else if(type_n.contains("调动")){
//                                        JSONObject dataJson = new JSONObject();
//                                        dataJson.put("date",formatDate(changedate));
//                                        dataJson.put("type",type_n);
//                                        dataJson.put("msg","今天是你工作调动的日子,你现在是"+olddepid);
//                                        jsonArray.add(dataJson);
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
    }catch (Exception e){

    }
    try{
        String sql =" select p.* from (\n" +
                    " select l.blogid,l.zan,w.createdate,row_number() over(order by l.zan desc) as xh from (\n" +
                    " select k.blogid,count(k.zantime) zan from blog_zan k\n" +
                    " where k.blogid in(select id from blog_discuss where userid = "+userid+")\n" +
                    " group by k.blogid \n" +
                    " ) l \n" +
                    " inner join blog_discuss  w on w.id = l.blogid\n" +
                    " ) p where p.xh=1" ;

        rs.executeQuery(sql);
        if(rs.next()){
            String createdate = Util.null2String(rs.getString("createdate"));
            String zan = Util.null2String(rs.getString("zan"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(createdate));
            dataJson.put("type","微博");
            dataJson.put("msg","今天是你微博被点赞最多的日子，被点赞了"+zan+"次");
            dataJson.put("img","/interface/hrportal/image/wb.png");
            jsonArray.add(dataJson);
        }
    }catch (Exception e){

    }

    try{
        String sql =" select w.* from (\n" +
                    "     select t1.year,t1.type2,t1.result,t1.finishdate,type1,row_number() over(order by t1.result desc) as xh from GP_AccessScore t1 \n" +
                    "     where t1.isvalid=1 and t1.type1=1 and t1.userid="+userid+" and len(t1.finishdate) > 0\n" +
                    " ) w where w.xh=1";
        rs.executeQuery(sql);
        if(rs.next()){
            String finishdate = Util.null2String(rs.getString("finishdate"));
            String result = Util.null2String(rs.getString("result"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(finishdate));
            dataJson.put("type","绩效");
            dataJson.put("msg","今天是你绩效成绩最高的日子，绩效成绩为"+result);
            dataJson.put("img","/interface/hrportal/image/jx.png");
            jsonArray.add(dataJson);
        }
    }catch (Exception e){

    }

    try{
        String sql =" select p.docid,p.cou,p.xh,d.docsubject,d.doccreatedate from (\n" +
                    "   select w.docid,w.cou,row_number() over(order by w.cou desc) as xh from (\n" +
                    "       select docid,count(1) as cou from DocDetailLog where operatetype = 0 and doccreater= " + userid+
                    "       group by docid\n" +
                    "    ) w \n" +
                    "  ) p \n" +
                    " inner join docdetail d on d.id = p.docid\n" +
                    " where p.xh=1 ";

        rs.executeQuery(sql);
        if(rs.next()){
            String doccreatedate = Util.null2String(rs.getString("doccreatedate"));
            String docsubject = Util.null2String(rs.getString("docsubject"));
            String cou = Util.null2String(rs.getString("cou"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(doccreatedate));
            dataJson.put("type","文档");
            dataJson.put("msg","今天是你文档查看最多的日子，文档:"+docsubject+"被查看了:"+cou+"次");
            dataJson.put("img","/interface/hrportal/image/wd.png");
            jsonArray.add(dataJson);
        }
    }catch (Exception e){

    }


//    try{
//        String sql =" select p.* from(\n" +
//                    "   select row_number() over(order by w.signTime) as xh,w.belongdate,w.signdate,w.signtime " +
//                    "       from HrmScheduleSign w where userid="+userid+" and usertype=1\n" +
//                    "   and (w.signfrom is null or w.signfrom='' or w.signfrom not like 'card%')\n" +
//                    " ) p where p.xh=1 ";
//
//        rs.executeQuery(sql);
//        if(rs.next()){
//            String belongdate = Util.null2String(rs.getString("belongdate"));
//            String signdate = Util.null2String(rs.getString("signdate"));
//            if(StringUtils.isEmpty(belongdate)){
//                belongdate = signdate ;
//            }
//            String signtime = Util.null2String(rs.getString("signtime"));
//            JSONObject dataJson = new JSONObject();
//            dataJson.put("date",formatDate(belongdate));
//            dataJson.put("img","/interface/hrportal/image/qd.png");
//            dataJson.put("type","签到");
//            dataJson.put("msg","今天是你签到最早的日子，于"+signtime+"签到成功");
//            jsonArray.add(dataJson);
//        }
//    }catch (Exception e){
//
//    }

    //消息
    try{
        String sql =" select * from (\n" +
                "   select row_number() over(order by t.mostreceivedaycount desc) as xh,t.mostreceivedaycount,t.mostreceiveday \n" +
                "   from wr_yearinventory_message t\n" +
                "   where userid= "+userid +
                " ) w where w.xh=1" ;

        rs.executeQuery(sql);
        if(rs.next()){
            String mostreceiveday = Util.null2String(rs.getString("mostreceiveday"));
            String mostreceivedaycount = Util.null2String(rs.getString("mostreceivedaycount"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(mostreceiveday));
            dataJson.put("img","/interface/hrportal/image/xx.png");
            dataJson.put("type","消息");
            dataJson.put("msg","今天是你接受消息最多的一天，共收到"+mostreceivedaycount+"条消息");
            jsonArray.add(dataJson);
        }
    }catch (Exception e){

    }

    try{
        String sql =" select * from (\n" +
                "   select row_number() over(order by t.mostdaycount desc) as xh,t.mostday,t.mostdaycount\n" +
                "   from wr_yearinventory_message t\n" +
                "   where userid=" + userid+
                " ) w where w.xh=1" ;
        rs.executeQuery(sql);
        if(rs.next()){
            String mostday = Util.null2String(rs.getString("mostday"));
            String mostdaycount = Util.null2String(rs.getString("mostdaycount"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(mostday));
            dataJson.put("img","/interface/hrportal/image/xx.png");
            dataJson.put("type","消息");
            dataJson.put("msg","今天是你发送消息最多的一天，共发出"+mostdaycount+"条消息");
            jsonArray.add(dataJson);
        }
    }catch (Exception e){

    }

    //流程
    try{
        String sql =" select * from (\n" +
                "   select row_number() over(order by t.mostdaycount desc) as xh,t.mostday,t.mostdaycount\n" +
                "   from wr_yearinventory_wf t\n" +
                "   where userid=" + userid+
                " ) w where w.xh=1" ;
        rs.executeQuery(sql);
        if(rs.next()){
            String mostday = Util.null2String(rs.getString("mostday"));
            String mostdaycount = Util.null2String(rs.getString("mostdaycount"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(mostday));
            dataJson.put("img","/interface/hrportal/image/lc.png");
            dataJson.put("type","流程");
            dataJson.put("msg","今天是你发起流程最多的一天，共发起"+mostdaycount+"条流程");
            jsonArray.add(dataJson);
        }
    }catch (Exception e){

    }

    try{
        String sql =" select * from (\n" +
                "   select row_number() over(order by t.dealmostdaycount desc) as xh,t.dealmostday,t.dealmostdaycount\n" +
                "   from wr_yearinventory_wf t\n" +
                "   where userid=" + userid+
                " ) w where w.xh=1" ;
        rs.executeQuery(sql);
        if(rs.next()){
            String dealmostday = Util.null2String(rs.getString("dealmostday"));
            String dealmostdaycount = Util.null2String(rs.getString("dealmostdaycount"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(dealmostday));
            dataJson.put("img","/interface/hrportal/image/lc.png");
            dataJson.put("type","流程");
            dataJson.put("msg","今天是你处理流程最多的一天，共处理"+dealmostdaycount+"条流程");
            jsonArray.add(dataJson);
        }
    }catch (Exception e){

    }

    //文档
    try{
        String sql =" select * from (\n" +
                "   select row_number() over(order by t.mostdaycount desc) as xh,t.mostday,t.mostdaycount\n" +
                "   from wr_yearinventory_doc t\n" +
                "   where userid=" + userid+
                " ) w where w.xh=1" ;
        rs.executeQuery(sql);
        if(rs.next()){
            String mostday = Util.null2String(rs.getString("mostday"));
            String mostdaycount = Util.null2String(rs.getString("mostdaycount"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("date",formatDate(mostday));
            dataJson.put("img","/interface/hrportal/image/wd.png");
            dataJson.put("type","文档");
            dataJson.put("msg","今天是你新建文档最多的一天，共新建"+mostdaycount+"篇文档");
            jsonArray.add(dataJson);
        }
    }catch (Exception e){

    }

    //打卡
    try{
        String sql =" select * from (\n" +
                "   select row_number() over(order by t.earliestsignin) as xh,t.earliestsignin\n" +
                "   from wr_yearinventory_schedule t\n" +
                "   where userid=" + userid+
                "   and len(t.earliestsignin) >0 "+
                " ) w where w.xh=1" ;
        rs.executeQuery(sql);
        if(rs.next()){
            String earliestsignin = Util.null2String(rs.getString("earliestsignin"));
            if(StringUtils.isNotEmpty(earliestsignin)){

                String day =  earliestsignin.substring(0,10);
                String time = earliestsignin.substring(11,19);
                JSONObject dataJson = new JSONObject();
                dataJson.put("date",formatDate(day));
                dataJson.put("img","/interface/hrportal/image/qd.png");
                dataJson.put("type","签到");
                dataJson.put("msg","今天是你上班打卡最早的一天，打卡时间"+time+"");
                jsonArray.add(dataJson);
            }
        }
    }catch (Exception e){

    }


    try{
        String sql =" select * from (\n" +
                    "   select row_number() over(order by t.latestsignout desc) as xh,t.latestsignout\n" +
                    "   from wr_yearinventory_schedule t \n" +
                    "   where userid=" + userid+
                    "   and len(t.latestsignout) >0 "+
                    " ) w where w.xh=1" ;

        rs.executeQuery(sql);
        if(rs.next()){
            String latestsignout = Util.null2String(rs.getString("latestsignout"));
            if(StringUtils.isNotEmpty(latestsignout)){

                String day = latestsignout.substring(0,10);
                String time = latestsignout.substring(11,19);
                JSONObject dataJson = new JSONObject();
                dataJson.put("date",formatDate(day));
                dataJson.put("img","/interface/hrportal/image/qd.png");
                dataJson.put("type","签到");
                dataJson.put("msg","今天是你下班打卡最晚的一天，打卡时间"+time+"");
                jsonArray.add(dataJson);
            }
        }
    }catch (Exception e){

    }


    if(jsonArray.size()>0){
        jsonArray.sort(Comparator.comparing(st ->((JSONObject)st).getString("date")).reversed());
    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("lastname",lastname);
    jsonObject.put("userid",userid);

    jsonObject.put("datas",jsonArray);
%>
<%=jsonObject.toString()  %>


<%!

    public String formatDate(String date){
        if(StringUtils.isEmpty(date)){
            return "";
        }

        String formatdate = "";
        try{
            String year = date.substring(0,4);
            String month = date.substring(5,7);
            String day = date.substring(8,10);
            formatdate = year+"年"+month+"月"+day+"日";
        }catch (Exception e){

        }

        return formatdate;
    }

%>



