package com.xhkjedu.sapi.service.analyze;

import com.xhkjedu.sapi.mapper.analyze.SchoolPeopleMapper;
import com.xhkjedu.sapi.mapper.system.SchoolMapper;
import com.xhkjedu.sapi.vo.analyze.SchoolPeopleGradeVo;
import com.xhkjedu.sapi.vo.analyze.SchoolPeopleNumVo;
import com.xhkjedu.sapi.vo.analyze.SchoolPeopleStuSexVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SchoolPeopleService
 * Description TODO 全校班级、教师、学生概况
 * Author WN
 * Date 2020/8/8 11:58
 **/
@Service
public class SchoolPeopleService {

    @Autowired
    private SchoolPeopleMapper schoolPeopleMapper;
    @Resource
    private SchoolMapper schoolMapper;

    /**
     * 功能描述 全校概况--班级、师生数量
     *
     * @param *    @param schoolid
     * @param year
     * @return com.xhkjedu.vo.analyze.SchoolPeopleNumVo
     * @author WN
     * @date 2020/8/8
     */
    public SchoolPeopleNumVo getSchoolAllNum(Integer schoolid, Integer year) {
        //Integer year = schoolMapper.getYearById(schoolid);
        SchoolPeopleNumVo rtnsp = schoolPeopleMapper.getSchoolClassNum(schoolid, year);
        if (rtnsp != null) {
            //性别数量
            List<SchoolPeopleStuSexVo> sexlist = schoolPeopleMapper.listSchoolStudentSexnum(schoolid, year);
            for (SchoolPeopleStuSexVo ssex : sexlist) {
                if (ssex.getUsersex() == 1) {
                    rtnsp.setBoystunum(ssex.getSexnum());
                } else if (ssex.getUsersex() == 2) {
                    rtnsp.setGirlstunum(ssex.getSexnum());
                } else {
                    rtnsp.setWzxbstunum(ssex.getSexnum());
                }
            }
        }else{
            rtnsp = null;
        }

        return rtnsp;
    }

    /**
     * 功能描述 全校概况--年级下教师班级学生数量
     *
     * @param *    @param schoolid
     * @param year
     * @return java.util.List<com.xhkjedu.vo.analyze.SchoolPeopleGradeVo>
     * @author WN
     * @date 2020/8/8
     */
    public List<SchoolPeopleGradeVo> listSchoolGrades(Integer schoolid, Integer year) {
        //Integer year = schoolMapper.getYearById(schoolid);
        List<SchoolPeopleGradeVo> list = schoolPeopleMapper.listSchoolGrades(schoolid, year);
        Integer totalNum = list.stream().collect(Collectors.summingInt(SchoolPeopleGradeVo::getStudentnum));
        for (SchoolPeopleGradeVo p : list) {
            p.setNumrate(N_Utils.getIntegerDivideAndMulitiply(p.getStudentnum(), totalNum));
        }
        return list;
        /*if(list!=null && list.size()>0){
            return parsePercentValue(list);
        }else{
            return list;
        }*/

    }

    public List<SchoolPeopleGradeVo> parsePercentValue(List<SchoolPeopleGradeVo> list){
        int[] arr = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i).getStudentnum();
        }
        double[] seats = N_Utils.getPercentValue(arr);
        for (int i = 0; i < list.size(); i++) {
            SchoolPeopleGradeVo p = list.get(i);
            Double num = seats[i];
            p.setNumrate(num.intValue());
        }
        return list;
    }

    /**
     * 功能描述 全校概况--年级下各班级情况统计
     *
     * @param *    @param schoolid
     * @param year
     * @return java.util.List<com.xhkjedu.vo.analyze.SchoolPeopleGradeVo>
     * @author WN
     * @date 2020/8/8
     */
    public List<SchoolPeopleGradeVo> listSchoolGradeClasses(Integer schoolid, Integer year) {
        //Integer year = schoolMapper.getYearById(schoolid);
        return schoolPeopleMapper.listSchoolGradeClasses(schoolid, year);
    }

}
