package com.xinyilu.system.service.homePage.impl;

import com.xinyilu.common.constant.Constants;
import com.xinyilu.common.core.domain.entity.SysRole;
import com.xinyilu.common.utils.SecurityUtils;
import com.xinyilu.system.domain.applogkindergarteninfo.AppLogKindergartenInfo;
import com.xinyilu.system.domain.dto.AppLogManageStatisticsDTO;
import com.xinyilu.system.domain.dto.ClassInfoDTO;
import com.xinyilu.system.domain.dto.KindergartenInfoDTO;
import com.xinyilu.system.domain.dto.StudentInfoDTO;
import com.xinyilu.system.domain.organize.OrganizeInfo;
import com.xinyilu.system.domain.systemarea.SysArea;
import com.xinyilu.system.domain.sysuserclassrelation.SysUserClassRelation;
import com.xinyilu.system.domain.vo.ClassInfoVO;
import com.xinyilu.system.domain.vo.OrganizeInfoVO;
import com.xinyilu.system.domain.vo.SysUserVO;
import com.xinyilu.system.domain.vo.applogmanagestatistics.AppLogManageStatisticsVO;
import com.xinyilu.system.domain.vo.homePage.HomePageAppletDataVO;
import com.xinyilu.system.domain.vo.homePage.HomePageBaseDataDTO;
import com.xinyilu.system.mapper.SysUserMapper;
import com.xinyilu.system.mapper.apploginfo.AppLogInfoMapper;
import com.xinyilu.system.mapper.classinfo.ClassInfoMapper;
import com.xinyilu.system.mapper.kindergarteninfo.KindergartenInfoMapper;
import com.xinyilu.system.mapper.organize.OrganizeInfoMapper;
import com.xinyilu.system.mapper.studentInfo.StudentInfoMapper;
import com.xinyilu.system.mapper.systemarea.SysAreaMapper;
import com.xinyilu.system.mapper.sysuserclassrelation.SysUserClassRelationMapper;
import com.xinyilu.system.service.homePage.HomePageService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 首页数据Service业务层处理
 *
 * @author wanghao
 * @create 2023/9/8 0008
 */
@Service
public class HomePageServiceImpl implements HomePageService {

    @Resource
    private SysUserMapper userMapper;
    @Resource
    private SysAreaMapper sysAreaMapper;

    @Resource
    private OrganizeInfoMapper organizeInfoMapper;

    @Resource
    private KindergartenInfoMapper kindergartenInfoMapper;
    @Resource
    private ClassInfoMapper classInfoMapper;
    @Resource
    private SysUserClassRelationMapper sysUserClassRelationMapper;
    @Resource
    private StudentInfoMapper studentInfoMapper;
    @Resource
    private AppLogInfoMapper appLogInfoMapper;

    /**
     * 查询幼儿园/班级/学生数量
     *
     * @return 数据
     */
    @Override
    public HomePageBaseDataDTO getBaseData() {
        //判断当前用户所属角色，根据角色显示对应数据信息
        List<String> roleKeys = SecurityUtils.getLoginUser().getUser().getRoles()
                .stream().filter(Objects::nonNull)
                .filter(item -> StringUtils.isNotBlank(item.getRoleKey()))
                .map(SysRole::getRoleKey).collect(Collectors.toList());
        Long userId = SecurityUtils.getUserId();
        SysUserVO sysUserVO = userMapper.selectUserById(userId);
        HomePageBaseDataDTO homePageBaseDataDTO = new HomePageBaseDataDTO();
        homePageBaseDataDTO.setKindergartenCount(getKindergartenCount(roleKeys, sysUserVO, userId));
        homePageBaseDataDTO.setClassCount(getClassCount(roleKeys, sysUserVO, userId));
        homePageBaseDataDTO.setStudentCount(getStudentCount(roleKeys, sysUserVO, userId));
        return homePageBaseDataDTO;
    }

    /**
     * 小程序统计-个人/幼儿园
     */
    @Override
    public HomePageAppletDataVO getAppletDataInfo() {
        //判断当前用户所属角色，根据角色显示对应数据信息
        List<String> roleKeys = SecurityUtils.getLoginUser().getUser().getRoles()
                .stream().filter(Objects::nonNull)
                .filter(item -> StringUtils.isNotBlank(item.getRoleKey()))
                .map(SysRole::getRoleKey).collect(Collectors.toList());
        Long userId = SecurityUtils.getUserId();
        SysUserVO sysUserVO = userMapper.selectUserById(userId);
        HomePageAppletDataVO homePageAppletDataVO = new HomePageAppletDataVO();
        homePageAppletDataVO.setPerson(getAppletDataByPerson(roleKeys, sysUserVO, userId));
        homePageAppletDataVO.setKindergarten(getAppletDataByKindergarten(roleKeys, sysUserVO, userId));
        return homePageAppletDataVO;
    }

    /**
     * 查询幼儿园数量
     */
    private int getKindergartenCount(List<String> roleKeys, SysUserVO sysUserVO, Long userId) {
        KindergartenInfoDTO kindergartenInfo = new KindergartenInfoDTO();
        if (roleKeys.contains(Constants.CITY)) {
            //角色为市级角色，查询该市内的所有幼儿园的信息
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            SysArea sysArea = new SysArea();
            sysArea.setParentId(areaId);
            List<Long> sysAreaIdList = sysAreaMapper.selectSysAreaList(sysArea).stream().filter(Objects::nonNull).filter(sysAreaId -> Objects.nonNull(sysAreaId.getId())).map(SysArea::getId).collect(Collectors.toList());
            List<Long> kindergartenIdList = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(sysAreaIdList)) {
                //获取该区域下的所有幼儿园信息
                kindergartenIdList = classInfoMapper.getKindergartenInfoByAreaId(sysAreaIdList, null);
            }
            if (CollectionUtils.isEmpty(kindergartenIdList)) {
                return 0;
            }
            kindergartenInfo.setKindergartenIdList(kindergartenIdList);
        } else if (roleKeys.contains(Constants.REGION_ROLE)) {
            //角色为区域角色，查询该区域内的所有幼儿园的信息
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            List<Long> organizeIdList = Lists.newArrayList();
            if (areaId != 0) {
                //获取该区域下的所有组织信息
                OrganizeInfoVO organizeInfo = new OrganizeInfoVO();
                organizeInfo.setAreaId(areaId);
                organizeIdList = organizeInfoMapper.selectOrganizeInfoList(organizeInfo).stream().filter(Objects::nonNull).map(OrganizeInfo::getId).collect(Collectors.toList());
            }
            if (CollectionUtils.isEmpty(organizeIdList)) {
                return 0;
            }
            kindergartenInfo.setOrganizeIdList(organizeIdList);
        } else if (roleKeys.contains(Constants.ENCHOU_ROLE)) {
            //角色是：园长，查询自己幼儿园的信息
            kindergartenInfo.setPrincipalId(userId);
        } else if (roleKeys.contains(Constants.HEALTHCARE_ROLE)) {
            //角色是：保健师，查找出其所属的幼儿园信息
            if (Objects.isNull(sysUserVO) || Objects.isNull(sysUserVO.getKindergartenId())) {
                return 0;
            }
            kindergartenInfo.setId(sysUserVO.getKindergartenId());
        } else if (roleKeys.contains(Constants.TEACHER_ROLE) && roleKeys.size() == 1) {
            //角色是：老师，查找出其所属的幼儿园id
            if (Objects.isNull(sysUserVO)) {
                return 0;
            }
            if (Objects.nonNull(sysUserVO.getKindergartenId())) {
                kindergartenInfo.setId(sysUserVO.getKindergartenId());
            }
        } else if (roleKeys.contains(Constants.PARENT_ROLE) && roleKeys.size() == 1) {
            //角色是：家长，查找出其所属的幼儿园id
            if (Objects.isNull(sysUserVO) || Objects.isNull(sysUserVO.getPhonenumber())) {
                return 0;
            }
            List<Long> kindergartenId = kindergartenInfoMapper.getKindergartenIdByParentId(sysUserVO.getPhonenumber());
            if (CollectionUtils.isEmpty(kindergartenId)) {
                return 0;
            }
            kindergartenInfo.setKindergartenIdList(kindergartenId);
        } else if (roleKeys.contains(Constants.TEACHER_ROLE) && roleKeys.contains(Constants.PARENT_ROLE) && roleKeys.size() == 2) {
            //角色即是家长又是老师，分别查询出老师和家长所属的幼儿园表的id
            List<Long> idList = Lists.newArrayList();
            if (Objects.nonNull(sysUserVO) && Objects.nonNull(sysUserVO.getKindergartenId()) && sysUserVO.getKindergartenId() != 0L) {
                idList.add(sysUserVO.getKindergartenId());
            }
            if (Objects.nonNull(sysUserVO) && Objects.nonNull(sysUserVO.getPhonenumber())) {
                List<Long> kindergartenIdByParentId = kindergartenInfoMapper.getKindergartenIdByParentId(sysUserVO.getPhonenumber());
                if (Objects.nonNull(kindergartenIdByParentId)) {
                    idList.addAll(kindergartenIdByParentId);
                }
            }
            List<Long> distinctList = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(idList)) {
                distinctList = idList.stream().distinct().collect(Collectors.toList());
            }
            if (CollectionUtils.isEmpty(distinctList)) {
                return 0;
            }
            kindergartenInfo.setIdList(distinctList);
        }
        return kindergartenInfoMapper.selectKindergartenInfoCount(kindergartenInfo);
    }


    /**
     * 查询班级数量
     */
    private int getClassCount(List<String> roleKeys, SysUserVO sysUserVO, Long userId) {
        ClassInfoDTO classInfo = new ClassInfoDTO();
        if (roleKeys.contains(Constants.CITY)) {
            //角色为市级角色，查询该市内的所有幼儿园的信息
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            SysArea sysArea = new SysArea();
            sysArea.setParentId(areaId);
            List<Long> sysAreaIdList = sysAreaMapper.selectSysAreaList(sysArea).stream().filter(Objects::nonNull).filter(sysAreaId -> Objects.nonNull(sysAreaId.getId())).map(SysArea::getId).collect(Collectors.toList());
            List<Long> kindergartenIdList = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(sysAreaIdList)) {
                //获取该区域下的所有幼儿园信息
                kindergartenIdList = classInfoMapper.getKindergartenInfoByAreaId(sysAreaIdList, null);
            }
            if (CollectionUtils.isEmpty(kindergartenIdList)) {
                return 0;
            }
            classInfo.setKindergartenIdList(kindergartenIdList);

        } else if (roleKeys.contains(Constants.REGION_ROLE)) {
            //角色为区域角色，查询该区域下所有幼儿园的班级信息
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            List<Long> kindergartenIdList = Lists.newArrayList();
            List<Long> sysAreaIdList = new ArrayList<>();
            sysAreaIdList.add(areaId);
            if (CollectionUtils.isNotEmpty(sysAreaIdList)) {
                //获取该区域下的所有幼儿园信息
                kindergartenIdList = classInfoMapper.getKindergartenInfoByAreaId(sysAreaIdList, 3L);
            }
            if (CollectionUtils.isEmpty(kindergartenIdList)) {
                return 0;
            }
            classInfo.setKindergartenIdList(kindergartenIdList);

        } else if (roleKeys.contains(Constants.ENCHOU_ROLE)) {
            //角色为幼儿园园长，查询其所属幼儿园下所有的班级信息
            Long kindergartenId = classInfoMapper.getKindergartenIdByPrincipalId(userId);
            if (Objects.isNull(kindergartenId) || kindergartenId == 0L) {
                return 0;
            }
            classInfo.setKindergartenId(kindergartenId);
        } else if (roleKeys.contains(Constants.HEALTHCARE_ROLE)) {
            //角色是：保健师，查找出其所属的幼儿园信息
            if (Objects.isNull(sysUserVO) || Objects.isNull(sysUserVO.getKindergartenId()) || sysUserVO.getKindergartenId() == 0L) {
                return 0;
            }
            classInfo.setKindergartenId(sysUserVO.getKindergartenId());
        } else if (roleKeys.contains(Constants.TEACHER_ROLE) && roleKeys.size() == 1) {
            //角色为老师，查询老师所属班级的信息
            List<Long> classIdList = sysUserClassRelationMapper.getSysUserClassRelationList(userId).stream().filter(Objects::nonNull).filter(classId -> Objects.nonNull(classId.getClassId())).map(SysUserClassRelation::getClassId).distinct().collect(Collectors.toList());
            if (CollectionUtils.isEmpty(classIdList)) {
                return 0;
            }
            classInfo.setClassIdList(classIdList);
        } else if (roleKeys.contains(Constants.PARENT_ROLE) && roleKeys.size() == 1) {
            //角色为家长，查询其孩子所在班级信息
            if (Objects.isNull(sysUserVO) || Objects.isNull(sysUserVO.getPhonenumber())) {
                return 0;
            }
            List<Long> classIdList = classInfoMapper.getClassIdByParentId(sysUserVO.getPhonenumber());
            if (CollectionUtils.isEmpty(classIdList)) {
                return 0;
            }
            classInfo.setClassIdList(classIdList);
        } else if (roleKeys.contains(Constants.TEACHER_ROLE) && roleKeys.contains(Constants.PARENT_ROLE) && roleKeys.size() == 2) {
            //角色即是老师又是家长，查询老师和家长所属班级的信息
            List<Long> classIdList = Lists.newArrayList();
            List<Long> classIdByUserId = sysUserClassRelationMapper.getSysUserClassRelationList(userId).stream().filter(Objects::nonNull).filter(classId -> Objects.nonNull(classId.getClassId())).map(SysUserClassRelation::getClassId).distinct().collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(classIdByUserId)) {
                classIdList.addAll(classIdByUserId);
            }
            if (Objects.nonNull(sysUserVO) && Objects.nonNull(sysUserVO.getPhonenumber())) {
                List<Long> classIdByParentId = classInfoMapper.getClassIdByParentId(sysUserVO.getPhonenumber());
                if (CollectionUtils.isNotEmpty(classIdByParentId)) {
                    classIdList.addAll(classIdByParentId);
                }
            }
            List<Long> distinctList = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(classIdList)) {
                distinctList = classIdList.stream().distinct().collect(Collectors.toList());
            }
            if (CollectionUtils.isEmpty(distinctList)) {
                return 0;
            }
            classInfo.setClassIdList(distinctList);
        }
        return classInfoMapper.selectClassInfoCount(classInfo);

    }

    /**
     * 查询班级数量
     */
    private int getStudentCount(List<String> roleKeys, SysUserVO sysUserVO, Long userId) {
        StudentInfoDTO studentInfo = new StudentInfoDTO();
        if (roleKeys.contains(Constants.CITY)) {
            //角色为市级角色，查询该该市级下所有幼儿园中的学生信息
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            List<Long> kindergartenIdList = Lists.newArrayList();
            SysArea sysArea = new SysArea();
            sysArea.setParentId(areaId);
            List<Long> sysAreaIdList = sysAreaMapper.selectSysAreaList(sysArea).stream().filter(Objects::nonNull).filter(sysAreaId -> Objects.nonNull(sysAreaId.getId())).map(SysArea::getId).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(sysAreaIdList)) {
                //获取该区域下的所有幼儿园信息
                kindergartenIdList = classInfoMapper.getKindergartenInfoByAreaId(sysAreaIdList, null);
            }
            if (CollectionUtils.isEmpty(kindergartenIdList)) {
                return 0;
            }
            //找出当前市下所有幼儿园下的所有班级信息
            List<Long> classIdList = studentInfoMapper.getClassIdByKindergartenIdList(kindergartenIdList);
            studentInfo.setClassIdList(classIdList);
        } else if (roleKeys.contains(Constants.REGION_ROLE)) {
            //角色为区域角色，查询该区域下所有幼儿园中的学生信息
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            List<Long> classIdList = Lists.newArrayList();
            if (areaId != 0) {
                //获取该区域下的所有幼儿园信息
                classIdList = studentInfoMapper.getClassIdByArea(areaId);
            }
            if (CollectionUtils.isEmpty(classIdList)) {
                return 0;
            }
            studentInfo.setClassIdList(classIdList);
        } else if (roleKeys.contains(Constants.ENCHOU_ROLE)) {
            //角色为园长，查询其幼儿园中的所有学生的信息
            List<Long> classIdByPrincipalId = studentInfoMapper.getClassIdByPrincipalId(userId);
            if (CollectionUtils.isEmpty(classIdByPrincipalId)) {
                return 0;
            }
            studentInfo.setClassIdList(classIdByPrincipalId);
        } else if (roleKeys.contains(Constants.HEALTHCARE_ROLE)) {
            //角色是：保健师，查找出其所属的幼儿园信息
            if (Objects.isNull(sysUserVO) || Objects.isNull(sysUserVO.getKindergartenId()) || sysUserVO.getKindergartenId() == 0L) {
                return 0;
            }
            ClassInfoDTO classInfo = new ClassInfoDTO();
            classInfo.setKindergartenId(sysUserVO.getKindergartenId());
            List<Long> classIdByPrincipalIds = classInfoMapper.selectClassInfoList(classInfo).stream().filter(Objects::nonNull).filter(classId -> Objects.nonNull(classId.getId())).map(ClassInfoVO::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(classIdByPrincipalIds)) {
                return 0;
            }
            studentInfo.setClassIdList(classIdByPrincipalIds);
        } else if (roleKeys.contains(Constants.TEACHER_ROLE) && roleKeys.size() == 1) {
            //角色为老师：查询班级下的所有学生信息
            List<Long> classIdByTeacherId = sysUserClassRelationMapper.getSysUserClassRelationList(userId).stream().filter(Objects::nonNull).filter(classId -> Objects.nonNull(classId.getClassId())).map(SysUserClassRelation::getClassId).distinct().collect(Collectors.toList());
            if (CollectionUtils.isEmpty(classIdByTeacherId)) {
                return 0;
            }
            studentInfo.setClassIdList(classIdByTeacherId);
        } else if (roleKeys.contains(Constants.PARENT_ROLE) && roleKeys.size() == 1) {
            //角色为家长，查询其孩子的学生信息
            if (Objects.isNull(sysUserVO) || Objects.isNull(sysUserVO.getPhonenumber())) {
                return 0;
            }
            List<Long> studentIdByParent = studentInfoMapper.getStudentIdByParent(sysUserVO.getPhonenumber());
            if (CollectionUtils.isEmpty(studentIdByParent)) {
                return 0;
            }
            studentInfo.setIdList(studentIdByParent);
        } else if (roleKeys.contains(Constants.TEACHER_ROLE) && roleKeys.contains(Constants.PARENT_ROLE) && roleKeys.size() == 2) {
            //角色即是老师又是家长，查询自己孩子和班级中所有孩子的学生信息
            List<Long> classIdByTeacherId = sysUserClassRelationMapper.getSysUserClassRelationList(userId).stream().filter(Objects::nonNull).filter(classId -> Objects.nonNull(classId.getClassId())).map(SysUserClassRelation::getClassId).distinct().collect(Collectors.toList());
            List<Long> studentIdByParent = new ArrayList<>();
            if (Objects.nonNull(sysUserVO) && Objects.nonNull(sysUserVO.getPhonenumber())) {
                studentIdByParent = studentInfoMapper.getStudentIdByParent(sysUserVO.getPhonenumber());
            }
            if (CollectionUtils.isEmpty(classIdByTeacherId) && CollectionUtils.isEmpty(studentIdByParent)) {
                return 0;
            }
            studentInfo.setClassList(classIdByTeacherId);
            studentInfo.setStudentList(studentIdByParent);
        }
        return studentInfoMapper.selectStudentInfoCount(studentInfo);
    }

    /**
     * 小程序统计-个人
     */
    private List<AppLogManageStatisticsVO> getAppletDataByPerson(List<String> roleKeys, SysUserVO sysUserVO, Long userId) {
        AppLogManageStatisticsDTO logManageStatistics = new AppLogManageStatisticsDTO();
        if (roleKeys.contains(Constants.CITY)) {
            //角色为市级角色，查询该区域下所有幼儿园下得所有班级中的家长的信息 市级
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            SysArea sysArea = new SysArea();
            sysArea.setParentId(areaId);
            List<Long> sysAreaIdList = sysAreaMapper.selectSysAreaList(sysArea).stream().filter(Objects::nonNull).filter(sysAreaId -> Objects.nonNull(sysAreaId.getId())).map(SysArea::getId).collect(Collectors.toList());
            List<Long> kindergartenIdList = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(sysAreaIdList)) {
                //获取该区域下的所有幼儿园信息
                kindergartenIdList = classInfoMapper.getKindergartenInfoByAreaId(sysAreaIdList, null);
            }
            if (CollectionUtils.isEmpty(kindergartenIdList)) {
                return new ArrayList<>();
            }
            logManageStatistics.setKindergartenIdList(kindergartenIdList);
        } else if (roleKeys.contains(Constants.REGION_ROLE)) {
            //角色为区域角色，查询该区域下所有幼儿园下得所有班级中的家长的信息 市级
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            List<Long> kindergartenIdList = Lists.newArrayList();
            List<Long> sysAreaIdList = new ArrayList<>();
            sysAreaIdList.add(areaId);
            if (CollectionUtils.isNotEmpty(sysAreaIdList)) {
                //获取该区域下的所有幼儿园信息
                kindergartenIdList = classInfoMapper.getKindergartenInfoByAreaId(sysAreaIdList, 3L);
            }
            if (CollectionUtils.isEmpty(kindergartenIdList)) {
                return new ArrayList<>();
            }
            logManageStatistics.setKindergartenIdList(kindergartenIdList);
        } else if (roleKeys.contains(Constants.ENCHOU_ROLE)) {
            //角色为园长，查询当前幼儿园下的所有家长信息
            Long kindergartenId = classInfoMapper.getKindergartenIdByPrincipalId(userId);
            if (Objects.nonNull(kindergartenId) && kindergartenId != 0L) {
                List<Long> kindergartenIdList = Lists.newArrayList();
                kindergartenIdList.add(kindergartenId);
                logManageStatistics.setKindergartenIdList(kindergartenIdList);
            } else {
                return new ArrayList<>();
            }
        } else if (roleKeys.contains(Constants.HEALTHCARE_ROLE)) {
            //角色是：保健师，查找出其所属的幼儿园信息、
            if (Objects.isNull(sysUserVO) || Objects.isNull(sysUserVO.getKindergartenId())) {
                return new ArrayList<>();
            }
            List<Long> kindergartenIdList = Lists.newArrayList();
            kindergartenIdList.add(sysUserVO.getKindergartenId());
            logManageStatistics.setKindergartenIdList(kindergartenIdList);
        }
        return appLogInfoMapper.appletPersonCountByHomePage(logManageStatistics);

    }

    /**
     * 小程序统计-幼儿园
     */
    private List<AppLogManageStatisticsVO> getAppletDataByKindergarten(List<String> roleKeys, SysUserVO sysUserVO, Long userId) {
        AppLogManageStatisticsDTO logManageStatistics = new AppLogManageStatisticsDTO();
        if (roleKeys.contains(Constants.CITY)) {
            //角色为市级角色，查询该区域内的所有幼儿园的信息
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            if (areaId == 0) {
                return new ArrayList<>();
            }
            SysArea sysArea = new SysArea();
            sysArea.setParentId(areaId);
            List<Long> sysAreaIdList = sysAreaMapper.selectSysAreaList(sysArea).stream().filter(Objects::nonNull).filter(sysAreaId -> Objects.nonNull(sysAreaId.getId())).map(SysArea::getId).collect(Collectors.toList());
            List<Long> kindergartenIdList = Lists.newArrayList();
            List<Long> organizeIdList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(sysAreaIdList)) {
                //获取该区域下的所有幼儿园信息
                kindergartenIdList = classInfoMapper.getKindergartenInfoByAreaId(sysAreaIdList, null);
            }
            if (CollectionUtils.isNotEmpty(kindergartenIdList)) {
                organizeIdList = organizeInfoMapper.selectOrganizeByKindergartenId(kindergartenIdList).stream().filter(Objects::nonNull).filter(organId -> Objects.nonNull(organId.getOrganizeId())).map(AppLogKindergartenInfo::getOrganizeId).collect(Collectors.toList());
            }
            if (CollectionUtils.isEmpty(organizeIdList)) {
                return new ArrayList<>();
            }
            logManageStatistics.setOrganizeIdList(organizeIdList);
        } else if (roleKeys.contains(Constants.REGION_ROLE)) {
            //角色为区域角色，查询该区域内的所有幼儿园的信息 区县级
            long areaId = Objects.isNull(SecurityUtils.getLoginUser().getUser().getAreaId()) ? 0 : SecurityUtils.getLoginUser().getUser().getAreaId();
            List<Long> organizeIdList = Lists.newArrayList();
            if (areaId != 0) {
                //获取该区域下的所有组织信息
                OrganizeInfoVO organizeInfo = new OrganizeInfoVO();
                organizeInfo.setAreaId(areaId);
                organizeInfo.setLevel(3L);
                organizeIdList = organizeInfoMapper.selectOrganizeInfoList(organizeInfo).stream().filter(Objects::nonNull).map(OrganizeInfo::getId).collect(Collectors.toList());
            }
            if (CollectionUtils.isEmpty(organizeIdList)) {
                return new ArrayList<>();
            }
            logManageStatistics.setOrganizeIdList(organizeIdList);
        } else if (roleKeys.contains(Constants.ENCHOU_ROLE)) {
            //角色是：园所，查询自己幼儿园的信息
            Long kindergartenId = classInfoMapper.getKindergartenIdByPrincipalId(userId);
            if (Objects.isNull(kindergartenId)) {
                return new ArrayList<>();
            }
            logManageStatistics.setKindergartenId(kindergartenId);
        } else if (roleKeys.contains(Constants.HEALTHCARE_ROLE)) {
            //角色是：保健师，查找出其所属的幼儿园信息
            if (Objects.isNull(sysUserVO) || Objects.isNull(sysUserVO.getKindergartenId())) {
                return new ArrayList<>();
            }
            Long kindergartenIds = Objects.isNull(sysUserVO.getKindergartenId()) ? 0 : sysUserVO.getKindergartenId();
            logManageStatistics.setKindergartenId(kindergartenIds);

        }
        return appLogInfoMapper.appletKindergartenCountByHomePage(logManageStatistics);
    }
}
