<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="com.engine.matfron.entity.OptionVO" %>
<%@ page import="com.engine.matfron.util.CommonUtils" %>
<%@ page import="com.engine.matfron.util.CommonDateUtil" %>
<%@ page import="java.time.YearMonth" %>
<%@ page import="com.engine.matfron.entity.BrowserParam" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<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();
    String departmentIds = Util.null2String(request.getParameter("departmentIds"));
    jsonObject = getLeaveMonth(departmentIds,subcompanyid1);


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

<%!
    // select id,departmentname from hrmdepartment where supdepid=0 and (canceled=0 or   canceled is null) and subcompanyid1=62 and id!=441
    //一级部门


    public JSONObject getLeaveMonth(String departmentIds,String subcompanyid1) {
        JSONObject jsonObject = new JSONObject();
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        BaseBean bb = new BaseBean();
        String startMonth = CommonDateUtil.getFormatYear(CommonDateUtil.toDateStartOfMonth(YearMonth.now()));
        String endMonth = CommonDateUtil.getFormatYear(CommonDateUtil.toDateEndOfMonth(YearMonth.now()));
        LinkedList<String> xData = new LinkedList<>();
        LinkedList<Integer> barSeriesData = new LinkedList<>();
        LinkedList<BrowserParam> browser = new LinkedList<>();


        String where  = " (s.belongto is null or s.belongto = -1 ) and s.subcompanyid1 = "+subcompanyid1;



        String deptsql = " select id,departmentname from hrmdepartment where supdepid=0 and ( canceled=0 or   canceled is null) and subcompanyid1=62 and id!=441";

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


            String sql =  "select count(1) as sum from lizhi_view h  ,hrmresource s where h.resourceid = s.id  and  "+where+" and  h.changedate >= '"+startMonth+"' and h.changedate <= '"+endMonth+"' ";


            Integer id = Util.getIntValue(rs.getString("id"));
            String name = Util.null2String(rs.getString("departmentname"));

            departmentIds = getAllDeptIds(id+"");
            bb.writeLog("==departmentIds=="+departmentIds);

            if(StringUtils.isNotEmpty(departmentIds)) {
                sql=sql+(" and s.departmentid in ("+departmentIds+")");
            }

            rs2.executeQuery(sql);
            rs2.next();
            bb.writeLog("----1="+sql);
            int num = Util.getIntValue(rs2.getString("sum"));

//            int countBydeptId = countBydeptId1(departmentIds, endMonth);
//            bb.writeLog("num:+"+num);
//            bb.writeLog("countBydeptId:+"+countBydeptId);
//            bb.writeLog(num *100 / countBydeptId);
//            if(num==0){
//                barSeriesData.add(0);
//            }else{
//                if(countBydeptId==0){
//                    barSeriesData.add(0);
//                }else {
//                    barSeriesData.add(num * 100 / countBydeptId);
//                }
//            }
            barSeriesData.add(num);

            xData.add(name);
            browser.add(BrowserParam.builder().id(id).name(name).build());

        }

        int max = Collections.max(barSeriesData);
        max = max < 10 ? 10 : 20;
//        int ceil =(int) Math.ceil((double) max / 4);
        int ceil = 2;
        int roundedMax = CommonUtils.roundedMax(ceil, 4);

        OptionVO optionVo = OptionVO.builder().titleText("当月部门离职统计")
                .xData(xData)
                .yMin(0)
                .yMax(roundedMax)
                .yInterval(ceil)
                .barSeriesData(barSeriesData)
                .build();

        jsonObject.put("data",optionVo);
        jsonObject.put("replaceDatas",browser);
        return jsonObject;
    }


    public String getAllDeptIds(String departmentid){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String ids = "" ;
        if(StringUtils.isNotBlank(departmentid)){
            String sql = "   WITH tmp_dept AS (" +
                    "    SELECT id, supdepid, departmentname FROM HrmDepartment WHERE id =" +departmentid+
                    "    UNION ALL " +
                    "    SELECT t.id, t.supdepid, t.departmentname FROM HrmDepartment t INNER JOIN tmp_dept r ON t.supdepid = r.id\n" +
                    "    )" +
                    "    SELECT * FROM tmp_dept";

            bb.writeLog("sql:"+sql);
            rs.executeQuery(sql);
            while (rs.next()){
                ids = ids+Util.null2String(rs.getString("id")) +",";
            }
        }
        if(ids.length()>0){
            ids = ids.substring(0,ids.length()-1);
        }
        bb.writeLog("ids:"+ids);
        return ids;
    }

%>
<%!
    /**
     * 根据日期部门人数统计
     * @param  departmentId
     * @return
     */
    private int countBydeptId1(String departmentId,String endMonth) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String where  = " and (belongto is null or belongto = -1 ) ";

        rs.executeQuery("select count(1) as sum from hrmresource where departmentId in ("+departmentId+") and status < 4 and companystartdate <= '"+endMonth+"' " + where);

        rs.next();
        return Util.getIntValue(rs.getString("sum"));
    }
%>


