<%@ 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()  %>

<%!

    public JSONObject getLeaveMonth(String departmentIds,String subcompanyid1) {
        JSONObject jsonObject = new JSONObject();
        RecordSet rs = new RecordSet();
        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  = " and (s.belongto is null or s.belongto = -1 ) and s.subcompanyid1 = "+subcompanyid1;

        StringBuilder sql = new StringBuilder();
        sql.append( " select t.departmentname, t.id,CASE \n" +
                    "        WHEN a.sum IS NULL THEN 0 \n" +
                    "        ELSE a.sum \n" +
                    "    END AS num " +
                    " from hrmdepartment t \n" +
                    " left join (select s.departmentid,count(1) as sum from lizhi_view h left join hrmresource s on h.resourceid = s.id where 1 = 1 "+where+" and  h.changedate >= ? and h.changedate <= ? group by s.departmentid ) a on t.id = a.departmentid " +
                    " ");

        if(StringUtils.isNotEmpty(departmentIds)) {
            sql.append(" where (t.canceled = 0 or t.canceled is null)");
            sql.append(" and t.id in (").append(departmentIds).append(")");
        }

        rs.executeQuery(sql.toString(),startMonth,endMonth);

        int i = 0;
        while (rs.next()) {
            i++;
            Integer id = Util.getIntValue(rs.getString("id"));
            String name = Util.null2String(rs.getString("departmentname"));
            int num = Util.getIntValue(rs.getString("num"));
            xData.add(name);
            int countBydeptId = countBydeptId(id, endMonth) + num;
            barSeriesData.add( countBydeptId == 0 ? 0 : (num * 100) / countBydeptId);
            browser.add(BrowserParam.builder().id(id).name(name).build());
            if(i>=5){
                break;
            }
        }

        int max = Collections.max(barSeriesData);
        max = max < 80 ? max + 20 : 100;
        int ceil =(int) Math.ceil((double) max / 4);
        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;
    }


%>
<%!
    /**
     * 根据日期部门人数统计
     * @param  departmentId
     * @return
     */
    private int countBydeptId(Integer departmentId,String endMonth) {
        RecordSet rs = new RecordSet();

        String where  = " and (belongto is null or belongto = -1 ) ";

        rs.executeQuery("select count(1) as sum from hrmresource where departmentId = ? and status < 4 and companystartdate <= ? " + where ,departmentId,endMonth);
        rs.next();
        return Util.getIntValue(rs.getString("sum"));
    }
%>



