<%@ 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.develop.service.growup.GrowUpListService" %>
<%@ page import="java.util.HashMap" %>
<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 belongtoshowFlag = true;
    WorkflowServiceImpl ws = new WorkflowServiceImpl();
    int wf_create = 0;
    try{
        wf_create = ws.getMyWorkflowRequestCount(user.getUID(), null,belongtoshowFlag);
    }catch (Exception e){

    }

    int cowork_create = 0;
    try{
        cowork_create = getCoworkReplayCount(userid);
    }catch (Exception e){

    }

    int blog_create = 0;
    try{
        blog_create = getBlogWriteCount(userid);
    }catch (Exception e){

    }

    int customer_create = 0;
    try{
        customer_create = getCustomerCount(user);
    }catch (Exception e){

    }

    int proj_create = 0;
    try{
        proj_create = getProjectCount(userid);
    }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("cowork_create",cowork_create);
    jsonObject.put("blog_create",blog_create);
    jsonObject.put("customer_create",customer_create);
    jsonObject.put("proj_create",proj_create);

%>
<%=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;
    }


    /***
     * 客户总和
     * @param user
     * @return
     */
    public int getCustomerCount(User user){
        BaseBean bb = new BaseBean();
        bb.writeLog("getCustomerCount:");
        int _total = 0;//总数
        int userid = user.getUID();
        GrowUpListService service = new GrowUpListService();
        //https://www.e-cology.com.cn/api/develop/growup/getData?season=&month=&week=&day=&
        // year=2022&isDecimal=1&grouptype=&datatype=&type=performances&hrmid=5796

        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("hrmid",userid);
            params.put("type","customernum");
            params.put("isDecimal","1");

            Map<String,Object> apidatas = service.getCustomerNum(user, params);
            bb.writeLog(JSONObject.toJSONString(apidatas));

            if(apidatas.containsKey("status") && apidatas.containsKey("num")){
                String status = Util.null2String(apidatas.get("status"));
                if("1".equals(status)){
                    _total = Util.getIntValue(Util.null2String(apidatas.get("num")));
                }
            }
            bb.writeLog("_total:"+_total);

        }catch (Exception e) {
            bb.writeLog("getCustomerCount-e:"+e);
        }
        return _total;
    }

    /***
     *  项目个数
     * @param userid
     * @return
     */
    public int getProjectCount(int userid)
    {
        BaseBean bb = new BaseBean();
        bb.writeLog("getProjectCount:");
        RecordSet rs = new RecordSet();
        /******************完成收款****************/
        int xmgs = 0 ;
        try{
            String userWhere = " and (t3t.hrmid in("+userid+") or t3t.qyjl in("+userid+") or t3t.dqzj in("+userid+") or t3t.qyzj in("+userid+")) " ;
            String sql =" select isnull(count(1),0) as xmgs  " +
                        " from proj_CardBaseInfo cbi " +
                        " left join lf_prjhrminfo t3t on t3t.hrmid = cbi.projmanager " +
                        " where 1=1 "+userWhere;
            bb.writeLog("sql:"+sql);
            rs.executeQuery(sql);
            if(rs.next()){
                xmgs = Util.getIntValue(Util.null2String(rs.getString("xmgs")));
            }
        }catch (Exception e){

        }
        return xmgs;
    }

%>



