<%@ 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="weaver.mobile.webservices.workflow.WorkflowServiceImpl" %>
<%@ 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" %>
<%@ page import="com.api.doc.center.service.ReplyAndPraiseService" %>
<%@ page import="com.engine.workflow.util.CommonUtil" %>
<%@ page import="com.api.doc.center.service.impl.ReplyAndPraiseServiceImpl" %>
<%@ page import="com.engine.kq.service.KQReportService" %>
<%@ page import="com.engine.kq.service.impl.KQReportServiceImpl" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.engine.portrait.util.PortraitUtil" %>
<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();



    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;
    try{
        wf_create = ws.getMyWorkflowRequestCount(user.getUID(), null,belongtoshowFlag);
    }catch (Exception e){

    }

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


    int knowledge_interact = 0;
    try{
        Map<String, Object> map = ParamUtil.request2Map(request);
        Map<String,Object> apidatas = getReplyAndPraiseService(request, response).getNumOfReplyAndPraise(map, user);
        knowledge_interact = Util.getIntValue(Util.null2String(apidatas.get("number")));
    }catch (Exception e){

    }

    String message_count = "0";
    String dateType = "3";
    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")){
                    message_count = itemData.substring(itemData.indexOf("=")+1);
                }
            }
        }
    }catch (Exception e){

    }

    String fromDate = "";
    String sql = " select createdate from hrmresource where id= "+userid ;
    rs.executeQuery(sql);
    if(rs.next()){
        fromDate = Util.null2String(rs.getString("createdate")) ;
    }
    String toDate = TimeUtil.getCurrentDateString();
    double attendance_count = 0.00;
    try{
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageIndex","1");
        jsonObject.put("typeselect","6");
        jsonObject.put("viewScope","3");
        jsonObject.put("fromDate",fromDate);
        jsonObject.put("toDate",toDate);
        jsonObject.put("resourceId",userid);
        jsonObject.put("resourceIdValue",lastname);

        JSONArray resourceArray = new JSONArray();
        JSONObject resourceObject = new JSONObject();
        resourceObject.put("name",lastname);
        resourceObject.put("id",userid);
        resourceObject.put("type","resource");
        resourceObject.put("lastname",lastname);
        resourceObject.put("nodeid","resource_"+userid+"x");
        resourceArray.add(resourceObject);
        jsonObject.put("resourceIdObject",resourceArray);
        jsonObject.put("status","8");
        jsonObject.put("isNoAccount","0");
        jsonObject.put("attendanceSerial","");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data",jsonObject.toJSONString());
        Map<String,Object> apidatas = getService(user).getKQReport(map, user);
        JSONObject apiJson = JSONObject.parseObject(JSONObject.toJSONString(apidatas));
        if(apiJson !=null){
            if(apiJson.containsKey("datas")){
                JSONArray datasArray = apiJson.getJSONArray("datas");
                if(datasArray.size()>0){
                    JSONObject dataJson = datasArray.getJSONObject(0);
                    String attendancedays = dataJson.getString("attendancedays");
                    if(StringUtils.isNotEmpty(attendancedays)){
                        attendance_count = Util.getDoubleValue(attendancedays);
                    }
                }
            }
        }
    }catch (Exception e){
    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("lastname",lastname);
    jsonObject.put("doc_create",doc_create);
    jsonObject.put("wf_create",wf_create);
    jsonObject.put("wf_deal",wf_deal);
    jsonObject.put("knowledge_interact",knowledge_interact);
    jsonObject.put("message_count",message_count);
    jsonObject.put("attendance_count",attendance_count);

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


<%!

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

    /***
     * 微博填写数
     * @param userid
     * @return
     */
    public int getBlogWriteCount(int userid){
        int blog_write = 0;
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("getBlogWriteCount");
        try{
            String sql = " select isnull(count(1),0) as count from blog_discuss where userid=? ";
            bb.writeLog("sql:"+sql);
            rs.executeQuery(sql,new Object[]{userid});
            if(rs.next()){
                blog_write = rs.getInt(1);
            }
            bb.writeLog("blog_write:"+blog_write);
        }catch (Exception e){
            bb.writeLog("getBlogWriteCount-e:"+e);
        }
        return blog_write;
    }



    /***
     *  沟通协作数
     * @param userid
     * @return
     */
    public int getCoworkReplayCount(int userid){
        int cwork_replay = 0;
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("getCoworkReplayCount");
        try {
            String sql = " select isnull(count(coworkid),0) as amount from cowork_discuss where discussant=? ";
            bb.writeLog("sql:"+sql);
            rs.executeQuery(sql,new Object[]{userid});
            if(rs.next()){
                cwork_replay = rs.getInt(1);
            }
            bb.writeLog("cwork_replay:"+cwork_replay);
        }catch (Exception e){
            bb.writeLog("getCoworkReplayCount-e:"+e);
        }

        return cwork_replay;
    }


    private ReplyAndPraiseService getReplyAndPraiseService(HttpServletRequest request, HttpServletResponse response) {
        User user = CommonUtil.getUserByRequest(request, response);//根据请求获取用户，包含主次账号信息
        return ServiceUtil.getService(ReplyAndPraiseServiceImpl.class, user);
    }


    private KQReportService getService(User user) {
        return (KQReportService) ServiceUtil.getService(KQReportServiceImpl.class, user);
    }


    /***
     *
     * @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 (!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();
    }

    /***
     *
     * @param userId
     * @return
     */
    public static boolean hasSubordinate(String userId){
        RecordSet rs = new RecordSet();
        String baseSql = " SELECT COUNT(ID) as num from hrmresource where managerid = ? ";
        String sqlWhere = " and status in (0,1,2,3) and loginid is not null ";
        if(!"oracle".equals(rs.getDBType())){
            sqlWhere +=" and loginid<>'' ";
        }
        rs.executeQuery(baseSql+sqlWhere,userId);
        boolean hasSubordinate = false;
        if (rs.next()) {
            hasSubordinate = rs.getInt(1) > 0 ;
        }
        return hasSubordinate;
    }

%>

