<%@ 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.engine.portrait.util.PortraitUtil" %>
<%@ page import="com.engine.portrait.util.CommonUtil" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.mobile.webservices.workflow.WorkflowServiceImpl" %>
<%@ page import="com.api.doc.detail.util.DocDownloadCheckUtil" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="com.api.doc.center.service.DocCountService" %>
<%@ page import="com.engine.common.util.ServiceUtil" %>
<%@ page import="com.api.doc.center.service.impl.DocCountServiceImpl" %>
<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 userimageurl = "" ;
    String resourceimageid = "" ;
    String jobtitlename = "" ;
    String lastname = user.getLastname();
    if("sysadmin".equals(user.getLoginid())){
        jobtitlename = "系统管理员";
    }else{
        String sql =" select resourceimageid,d.jobtitlename from hrmresource h \n" +
                " left join hrmjobtitles  d on d.id = h.jobtitle \n" +
                " where h.id= ? ";
        rs.executeQuery(sql,new Object[]{userid}) ;
        if(rs.next()){
            jobtitlename = Util.null2String(rs.getString("jobtitlename"));
            resourceimageid = Util.null2String(rs.getString("resourceimageid"));
        }
        if(StringUtils.isNotEmpty(resourceimageid)){
            String fileid = "";
            if(StringUtils.isNotEmpty(resourceimageid)){
                try{
                    fileid = DocDownloadCheckUtil.checkPermission(resourceimageid+"",null);
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }
            }
            userimageurl = "/weaver/weaver.file.FileDownload?fileid="+fileid+"";
        }
    }


    String rzts = "";
    String rznf = "" ;
    String rzsyts = "" ;

//    String sql =" select case when p.rzts2 >0 then p.rzts2 else p.rzts1 end as rzts \n" +
//                " from (" +
//                " select " +
//                " isnull((select isnull(datediff(day,hr.createdate,convert(varchar(100), getdate(), 23)),0) rzts from hrmresource hr where hr.id = "+userid+"),0) as rzts1, \n" +
//                " isnull((select isnull(datediff(day,hr.created,convert(varchar(100), getdate(), 23)),0) rzts from HrmResourcemanager hr where hr.id = "+userid+"),0) as rzts2 \n" +
//                " ) p " ;
//
//    rs.executeQuery(sql);
//    if(rs.next()){
//        rzts = Util.null2String(rs.getString("rzts"));
//    }

    if(userid == 1){
        rznf = "0";
        rzsyts = "0";
        rzts = rznf + "年" + rzsyts;
    }else {
        String sql = " select wr.rznf,isnull(datediff(day,DATEADD(yy,wr.rznf,wr.companystartdate),convert(varchar(100), getdate(), 23)),0) as rzsyts\n" +
                " from (\n" +
                "   select hr.id,hr.companystartdate,\n" +
                "       case when right(convert(varchar(100), getdate(), 23),5) > right(hr.companystartdate,5)\n" +
                "       then \n" +
                "       isnull(datediff(year,hr.companystartdate,convert(varchar(100), getdate(), 23)),0) \n" +
                "       else \n" +
                "       isnull(datediff(year,hr.companystartdate,convert(varchar(100), getdate(), 23)),0)-1\n" +
                "       end rznf\n" +
                "   from hrmresource hr\n" +
                " ) wr \n" +
                " where id = " + userid + " ";
        rs.executeQuery(sql);
        if (rs.next()) {

            rznf = Util.null2String(rs.getString("rznf"));
            rzsyts = Util.null2String(rs.getString("rzsyts"));
            rzts = rznf + "年" + rzsyts;
        }
    }


//    if(userid == 1){
//        String sql = " select isnull(datediff(day,hr.created,convert(varchar(100), getdate(), 23)),0) rzts from HrmResourcemanager hr where hr.id = ?";
//        rs.executeQuery(sql,new Object[]{userid});
//        if(rs.next()){
//            rzts = Util.getIntValue(Util.null2String(rs.getString("rzts")),0);
//        }
//    }else{
//        String sql = " select isnull(datediff(day,hr.createdate,convert(varchar(100), getdate(), 23)),0) rzts from HrmResource hr where hr.id = ?";
//        rs.executeQuery(sql,new Object[]{userid});
//        if(rs.next()){
//            rzts = Util.getIntValue(Util.null2String(rs.getString("rzts")),0);
//        }
//    }


    int doc_create = 0;
    //封装request传递参数到map
    try{
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("docCountType", "1");
        Map<String,Object> apidatas = getCountService(request, response).getNumOfMy(map, user,request,response);
        if(apidatas.containsKey("number")){
            String number = Util.null2String(apidatas.get("number"));
            if(StringUtils.isNotEmpty(number)){
                doc_create = Util.getIntValue(number);
            }
        }
    }catch (Exception e){

    }

    boolean isshowprocessed = true;
    boolean belongtoshowFlag = true;


    WorkflowServiceImpl ws = new WorkflowServiceImpl();
    int wf_create = 0;
//    String sql = " select isnull(count(requestid),0) as amount from workflow_requestbase where creater=? and workflowid in(select id from workflow_base where isvalid =1)";
//    rs.executeQuery(sql,new Object[]{userid}) ;
//    if(rs.next()){
//        wf_create = Util.getIntValue(Util.null2String(rs.getString("amount")),0);
//    }

    try{
        wf_create = ws.getMyWorkflowRequestCount(user.getUID(), null,belongtoshowFlag);
    }catch (Exception e){

    }



    int wf_deal = 0;
//    sql = " select isnull(count(requestid),0) as amount from workflow_requestLog where operator=? ";
//    rs.executeQuery(sql,new Object[]{userid}) ;
//    if(rs.next()){
//        wf_deal = Util.getIntValue(Util.null2String(rs.getString("amount")),0);
//    }

    try{
        wf_deal = ws.getHendledWorkflowRequestCount(user.getUID(), isshowprocessed, null,belongtoshowFlag);
    }catch (Exception e){
    }


    String mtgtrs = "0" ;
    String dateType = "3";
    try{
        String sql = getQuerySql(userid+"","16");
        if(StringUtils.isNotEmpty(sql)){
            rs.executeQuery(sql,userid,dateType);
            if (rs.next()) {
                String itemData = Util.null2String(rs.getString("itemData"));
                if(StringUtils.isNotEmpty(itemData) && itemData.contains("amount")){
                    mtgtrs = itemData.substring(itemData.indexOf("=")+1);
                }
            }
        }
    }catch (Exception e){

    }

    String cyqlsl = "0" ;
    try{
        String sql = getQuerySql(userid+"","17");
        if(StringUtils.isNotEmpty(sql)){
            rs.executeQuery(sql,userid,dateType);
            if (rs.next())
            {
                String itemData = Util.null2String(rs.getString("itemData"));
                String itemname = Util.null2String(rs.getString("itemname"));
                String name = Util.null2String(rs.getString("name"));
                bb.writeLog("itemname:"+itemname+"  itemname:"+itemname+"   name:"+name);

                if(StringUtils.isNotEmpty(itemData) && itemData.contains("amount")){
                    cyqlsl = itemData.substring(itemData.indexOf("=")+1);
                }
            }
        }
    }catch (Exception e){

    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("lastname",lastname);
    jsonObject.put("userurl","/spa/hrm/index_mobx.html#/main/hrm/card/cardInfo");
    jsonObject.put("rzts",rzts);

    jsonObject.put("rznf",rznf);
    jsonObject.put("rzsyts",rzsyts);

    jsonObject.put("doc_create",doc_create);
    jsonObject.put("doc_create_url","/spa/document/static/index.html#/main/document/search");
    jsonObject.put("wf_create",wf_create);
    jsonObject.put("wf_create_url","/spa/workflow/static/index.html#/main/workflow/listMine");
    jsonObject.put("wf_deal",wf_deal);
    jsonObject.put("wf_deal_url","/spa/workflow/static/index.html#/main/workflow/listDone");

    jsonObject.put("jobtitlename",jobtitlename);
    jsonObject.put("userimageurl",userimageurl);

    String cyqlsl_new = "0" ;
    if(StringUtils.isNotEmpty(cyqlsl)){
        if(cyqlsl.contains(".")){
            try{
                cyqlsl_new = cyqlsl.substring(0,cyqlsl.indexOf("."));
            }catch (Exception e){
            }
        }
    }
    jsonObject.put("cyqlsl",cyqlsl_new);
    jsonObject.put("cyqlsl_old",cyqlsl);
    jsonObject.put("mtgtrs",mtgtrs);

%>
<%=jsonObject.toString()  %>


<%!

    /***
     *
     * @param userid
     * @param itemid
     * @return
     */
    private String getQuerySql(String userid,String itemid) {

        BaseBean bb = new BaseBean();
        StringBuilder sqlSB = new StringBuilder();

        if(StringUtils.isNotEmpty(itemid))
        {
            sqlSB.append(" select e.id as itemdataid,e.userid,e.itemvalue,e.otherinfo as itemdata,d.* from (SELECT a.moduleid,a.name ,b.subname,b.subid ,b.iconname,b.iconcolor,b.iconbgcolor,c.id as itemid,c.iszeroshow,c.name as itemname,c.tipsinfo,c.isability,c.isnegative,c.overcolor,c.showorder")
                    .append(" FROM wr_individualitem_module a join wr_individualitem_submodule b on a.moduleid = b.moduleid ")
                    .append(" join wr_individualitem_info c on b.subid = c.subid ")
                    .append(" where a.status = 1 and b.status = 1 and  c.status = 1 ");
            if (!CommonUtil.hasSubordinate(userid)) {
                sqlSB.append(" and a.moduleid != 3 ");
            }
            String shareSql = "";
            try {
                shareSql = PortraitUtil.getShareBizidSql("10",userid);
            } catch (Exception e) {
                bb.writeLog("获取shareSql失败,userid="+userid+",type=10,"+e);
            }
            if (StringUtils.isNotEmpty(shareSql)) {
                sqlSB.append(" and b.subid in (").append(shareSql).append(")");
            }
            sqlSB.append(" ) d join wr_individualitem_detail e on e.itemid = d.itemid ").append(" WHERE e.userid = ? and e.type = ? ");
            sqlSB.append(" and d.itemid = "+itemid);
            sqlSB.append(" order by d.moduleid asc,d.showorder asc");
        }
        return sqlSB.toString();
    }


    private DocCountService getCountService(HttpServletRequest request, HttpServletResponse response) {
        User user = com.engine.workflow.util.CommonUtil.getUserByRequest(request, response);//根据请求获取用户，包含主次账号信息
        return ServiceUtil.getService(DocCountServiceImpl.class, user);
    }
%>



