<%@ 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" %>
<%@ page import="weaver.hrm.company.DepartmentComInfo" %>
<%@ page import="java.util.stream.Collectors" %>
<%@ page import="com.engine.matfron.exception.CustomizeRunTimeException" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.DecimalFormat" %>
<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 = getLeaveYear(subcompanyid1);

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

<%!

    public JSONObject getLeaveYear(String subcompanyid1) {
        JSONObject jsonObject = new JSONObject();
        RecordSet rs = new RecordSet();
        LinkedList<String> xData = new LinkedList<>();
        xData.add("1");
        BaseBean bb = new BaseBean();
        LinkedList<Double> barSeriesData = new LinkedList<>();
        LinkedList<BrowserParam> browser = new LinkedList<>();
        String where  = " and (s.belongto is null or s.belongto = -1 ) and s.subcompanyid1 = "+subcompanyid1;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String nowMonth = sdf.format(new Date());
        List<YearMonth> yearMonths = CommonDateUtil.getYearMonths(LocalDate.now());
        yearMonths.forEach(yearMonth -> {
            String startMonth = CommonDateUtil.getFormatYear(CommonDateUtil.toDateStartOfMonth(yearMonth));
            String endMonth = CommonDateUtil.getFormatYear(CommonDateUtil.toDateEndOfMonth(yearMonth));

            String selectMonth = sdf.format(CommonDateUtil.toDateEndOfMonth(yearMonth));
            if(selectMonth.compareTo(nowMonth)<=0){
                String sql ="select count(1) as sum from lizhi_view h left join hrmresource s on h.resourceid = s.id where 1 = 1  "+where+"  and h.changedate >= '"+startMonth+"' and h.changedate <= '"+endMonth+"' ";
                bb.writeLog("---"+sql);
                rs.executeQuery(sql);
                if (rs.next()) {
                    double num = Util.getIntValue(rs.getString("sum"));

                    double countBydeptId = countBydeptId(endMonth) + num;

                    DecimalFormat df = new DecimalFormat("#.00");
                    String result = df.format((num * 100) / countBydeptId);
                    barSeriesData.add(Double.parseDouble(result));
                }
            }
        });


//        int max = Collections.max(barSeriesData);
        int max = 8;
        int ceil =2;
        int roundedMax = CommonUtils.roundedMax(ceil, 4);

        Map data = new HashMap();
        data.put("barSeriesData",barSeriesData);

        data.put("titleText","年度离职率统计");
        data.put("yInterval",ceil);
        data.put("yMax",roundedMax);
        data.put("yMin",0);

        Map all = new HashMap();
        all.put("data",data);
        all.put("replaceDatas",browser);

//
//        OptionVO optionVo = OptionVO.builder().titleText("年度离职率统计")
//                .xData(xData)
//                .yMin(0)
//                .yMax(roundedMax)
//                .yInterval(ceil)
//                .barSeriesData(barSeriesData)
//                .build();

        jsonObject = (JSONObject)JSONObject.toJSON(all);
        return jsonObject;
    }


%>
<%!
    /**
     *
     * @return
     */
    private int countBydeptId(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   status < 4 and companystartdate <= ? " + where ,endMonth);
        rs.next();
        return Util.getIntValue(rs.getString("sum"));
    }
%>



