<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.stream.Collectors" %>
<%@ page import="com.engine.matfron.entity.OptionVO" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.time.Period" %>
<%@ page import="com.engine.matfron.util.CommonUtils" %>
<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 = getAgeAndSex(subcompanyid1);
    jsonObject.put("data",OptionVO);

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

<%!

    public OptionVO getAgeAndSex(String subcompanyid1) {
        RecordSet rs = new RecordSet();
        LinkedList<Integer> barSeriesData = new LinkedList<>();
        LinkedList<Integer> barSeriesDataTwo = new LinkedList<>();
        List<String> ageArr = Arrays.asList("0~20","21~25","26~30","31~35","36~40","41~45","46~50","51~55","56~100");
        String where  = " and (belongto is null or belongto = -1 ) and subcompanyid1 = "+subcompanyid1;
        List<String> birthdayListMen = new ArrayList<>();
        rs.executeQuery(" select birthday from hrmresource where status < 4 "+where+" and sex is not null and birthday is not null and birthday <> '' and sex = 0");
        while (rs.next()) {
            birthdayListMen.add(Util.null2String(rs.getString("birthday")));
        }
        List<Integer> menAge = birthdayListMen.stream().map(birthday -> {
                    LocalDate birthdate = LocalDate.parse(birthday);
                    LocalDate now = LocalDate.now();
                    return Period.between(birthdate, now).getYears();
                }).collect(Collectors.toList());


        for (int i = 0; i < ageArr.size(); i++) {
            String[] split = ageArr.get(i).split("~");
            List<Integer> collect = menAge.stream().filter(item -> item >= Integer.parseInt(split[0]) && item <= Integer.parseInt(split[1])).collect(Collectors.toList());
            barSeriesData.add(collect.size());
        }

        int yMax = barSeriesData.stream().max(Integer::compare).orElse(0);

        List<String> birthdayListWomen = new ArrayList<>();
        rs.executeQuery("select birthday from hrmresource where status < 4 "+where+" and sex is not null and birthday is not null and sex = 1");
        while (rs.next()) {
            birthdayListWomen.add(Util.null2String(rs.getString("birthday")));
        }
        List<Integer> womenAge = birthdayListWomen.stream()
                .map(birthday -> {
                    LocalDate birthdate = LocalDate.parse(birthday);
                    LocalDate now = LocalDate.now();
                    return Period.between(birthdate, now).getYears();
                })
                .collect(Collectors.toList());

        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < ageArr.size(); i++) {
            String[] split = ageArr.get(i).split("~");
            List<Integer> collect = womenAge.stream().filter(item -> item >= Integer.parseInt(split[0]) && item <= Integer.parseInt(split[1])).collect(Collectors.toList());
            barSeriesDataTwo.add(-collect.size());
            list.add(collect.size());
        }

        int yMin = -(list.stream().max(Integer::compare).orElse(0));
        int sum = yMax > Math.abs(yMin) ? yMax  : yMin ;

        int divisor = sum % 6;
        sum = sum + (6 - divisor) + 12;
//        int ceil =(int) Math.ceil((double) sum / 3);
        int ceil =30;
        int roundedMax = CommonUtils.roundedMax(ceil, 3);

        return OptionVO.builder()
                .titleText("年龄与性别分析")
                .yMin(-roundedMax)
                .yMax(roundedMax)
                .yInterval(ceil)
                .barSeriesData(barSeriesData)
                .barSeriesDataTwo(barSeriesDataTwo)
                .build();
    }

%>



