<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="com.engine.matfron.util.CommonUtils" %>
<%@ page import="com.engine.matfron.entity.DepartmentParam" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.engine.matfron.entity.OptionVO" %>
<%@ page import="com.fasterxml.jackson.databind.ObjectMapper" %>
<%@ page import="com.fasterxml.jackson.core.JsonProcessingException" %>
<jsp:useBean id="ResourceComInfo" class="weaver.hrm.resource.ResourceComInfo" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page" />
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    String subcompanyid1 = "62";
    JSONObject jsonObject = new JSONObject();
    OptionVO optionVO = getPortalDepartment(subcompanyid1);
    jsonObject.put("data",optionVO);

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

<%!

    public OptionVO getPortalDepartment(String subcompanyid1) {
        RecordSet rs = new RecordSet();
        LinkedList<String> xData = new LinkedList<>();
        LinkedList<Integer> yData = new LinkedList<>();
        String where  = " and (belongto is null or belongto = -1 ) and subcompanyid1 = "+subcompanyid1;
        List<Integer> ids = new ArrayList<>();
        rs.executeQuery(" select id,departmentname from hrmdepartment where supdepid = 0 and (canceled = 0 or canceled is null) and subcompanyid1 ="+subcompanyid1);
        while (rs.next()) {
            xData.add(Util.null2String(rs.getString("departmentname")));
            ids.add(Util.getIntValue(rs.getString("id")));
        }


        for(int deptid : ids){
            if(StringUtils.isNotBlank(deptid+"")){
                String deptids = "" ;
                String sql =" with tem_table(department_id,department_name,supdep_id,curlevel,canceled) as (\n" +
                            "    select id,departmentname,supdepid, 1 as level,canceled\n" +
                            "    from  HrmDepartment  where id = " + deptid +
                            "    union all \n" +
                            "    select  a.id,a.departmentname,a.supdepid, b.curlevel+1 ,a.canceled\n" +
                            "    from HrmDepartment  a \n" +
                            "    inner join tem_table b on ( b.department_id = a.supdepid )\n" +
                            " )\n" +
                            " select * from tem_table where isnull(canceled,0) !=1 " ;
                rs.executeQuery(sql);
                while (rs.next()){
                    String depid = Util.null2String(rs.getString("department_id"));
                    deptids += StringUtils.isBlank(deptids) ? depid : ","+depid ;
                }

                //select field27  from cus_fielddata where    scope='HrmCustomFieldByInfoType'  and id=541  and scopeid=3
                List<DepartmentParam> deptList = new ArrayList<>();
                sql = " SELECT count(hrmresource.id) as num FROM hrmresource , cus_fielddata   where cus_fielddata.id=hrmresource.id and scope='HrmCustomFieldByInfoType'  and scopeid=3 and  field27 in (0,1)  and  status  in (0,1) and departmentid in("+deptids+") "+where ;

                rs.executeQuery(sql);
                if (rs.next()) {
                    int num = Util.getIntValue(rs.getString("num"));
                    yData.add(num);
                    deptList.add(DepartmentParam.builder().departmentId(deptid).count(num).build());
                }
            }
        }

        int max = Collections.max(yData);
        int ceil =(int) Math.ceil((double) max / 5);
        ceil = CommonUtils.roundUpToNearestTen(ceil);
        int roundedMax = CommonUtils.roundedMax(ceil, 5);

        return OptionVO.builder()
                .titleText("一级部门人数统计")
                .xData(xData)
                .yMin(0)
                .yMax(roundedMax)
                .yInterval(ceil)
                .yData(yData)
                .build();
    }




    private static String getJsonString(Object apidatas) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(apidatas);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

%>






