package com.xjrsoft.module.attendance.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.attendance.dto.AddAttendanceRuleCategoryDto;
import com.xjrsoft.module.attendance.dto.AddAttendanceUserRelationDto;
import com.xjrsoft.module.attendance.dto.UpdateAttendanceRuleCategoryDto;
import com.xjrsoft.module.attendance.entity.AttendanceRuleCategory;
import com.xjrsoft.module.attendance.entity.AttendanceRuleDetails;
import com.xjrsoft.module.attendance.entity.AttendanceUserRelation;
import com.xjrsoft.module.attendance.mapper.AttendanceRuleCategoryMapper;
import com.xjrsoft.module.attendance.mapper.AttendanceRuleDetailsMapper;
import com.xjrsoft.module.attendance.mapper.AttendanceUserRelationMapper;
import com.xjrsoft.module.attendance.service.IAttendanceRuleCategoryService;
import com.xjrsoft.module.attendance.vo.AttendanceRuleDetailsUserVo;
import com.xjrsoft.module.concat.service.IXjrUserService;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.student.entity.BaseStudent;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.teacher.entity.BaseTeacher;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.yomahub.liteflow.util.JsonUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @title: 考勤规则
* @Author dzx
* @Date: 2024-05-08
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class AttendanceRuleCategoryServiceImpl extends MPJBaseServiceImpl<AttendanceRuleCategoryMapper, AttendanceRuleCategory> implements IAttendanceRuleCategoryService {
    private final AttendanceRuleCategoryMapper categoryMapper;

    private final AttendanceRuleDetailsMapper detailsMapper;
    private final AttendanceUserRelationMapper relationMapper;
    private final IXjrUserService xjrUserService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AddAttendanceRuleCategoryDto dto) {
        AttendanceRuleCategory attendanceRuleCategory = BeanUtil.toBean(dto, AttendanceRuleCategory.class);
        attendanceRuleCategory.setUserRelation(JsonUtil.toJsonString(dto.getAttendanceUserRelationList()));

        List<XjrUser> insertList = new ArrayList<>();
        if(attendanceRuleCategory.getRoleId() != null && attendanceRuleCategory.getRoleId() == 2){
            if(attendanceRuleCategory.getAttendanceRange() == 1){
                //首先判断是否已经存在一个全体教职工的考勤规则
                Integer existCount = categoryMapper.getExistCount(dto);
                if(existCount > 0){
                    throw new MyException("已存在一个全体教职工的考勤规则，无法添加");
                }
                List<XjrUser> userList = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(BaseTeacher.class, BaseTeacher::getUserId, XjrUser::getId)
                    .eq(BaseTeacher::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                );
                insertList.addAll(userList);
            }else if(attendanceRuleCategory.getAttendanceRange() == 2){
                List<Long> deptIds = new ArrayList<>();
                for (AddAttendanceUserRelationDto relation : dto.getAttendanceUserRelationList()) {
                    if(relation.getDeptId() != null){
                        deptIds.add(relation.getDeptId());
                    }
                }
                List<Long> userIds = new ArrayList<>();
                for (AddAttendanceUserRelationDto relation : dto.getAttendanceUserRelationList()) {
                    if(relation.getUserId() != null){
                        userIds.add(relation.getUserId());
                    }
                }

                List<XjrUser> userList = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(BaseTeacher.class, BaseTeacher::getUserId, XjrUser::getId)
                    .leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, XjrUser::getId)
                    .eq(BaseTeacher::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .in(!deptIds.isEmpty(), UserDeptRelation::getDeptId, deptIds)
                    .in(!userIds.isEmpty(), XjrUser::getId, userIds).or()
                );
                //查询这部分人中是否已经有人有规则了
                List<Long> relationUserIds = userList.stream().map(XjrUser::getId).collect(Collectors.toList());
                List<XjrUser> relations = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>().distinct()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(AttendanceUserRelation.class, AttendanceUserRelation::getUserId, XjrUser::getId)
                    .eq(AttendanceUserRelation::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .in(!relationUserIds.isEmpty(), AttendanceUserRelation::getUserId, relationUserIds)
                );
//                String peopleName = "";
//                for (int i = 0; i < relations.size(); i ++){
//                    if(i > 0){
//                        peopleName += ",";
//                    }
//                    peopleName += relations.get(i).getName();
//                }
                if(!relations.isEmpty()){
                    throw new MyException("指定教职工中已有绑定考勤规则，无法添加");
                }
                insertList.addAll(userList);
            }
        }else if(attendanceRuleCategory.getRoleId() != null && attendanceRuleCategory.getRoleId() == 3){//学生
            if(attendanceRuleCategory.getAttendanceRange() == 1){
                //首先判断是否已经存在一个全体学生的考勤规则
                Integer existCount = categoryMapper.getExistCount(dto);
                if(existCount > 0){
                    throw new MyException("已存在一个全体学生的考勤规则，无法添加");
                }
                List<XjrUser> userList = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(BaseStudent.class, BaseStudent::getUserId, XjrUser::getId)
                    .eq(BaseStudent::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                );
                insertList.addAll(userList);
            }else if(attendanceRuleCategory.getAttendanceRange() == 2){
                List<Long> classIds = new ArrayList<>();
                for (AddAttendanceUserRelationDto relation : dto.getAttendanceUserRelationList()) {
                    if(relation.getClassId() != null){
                        classIds.add(relation.getClassId());
                    }
                }
                List<Long> userIds = new ArrayList<>();
                for (AddAttendanceUserRelationDto relation : dto.getAttendanceUserRelationList()) {
                    if(relation.getUserId() != null){
                        userIds.add(relation.getUserId());
                    }
                }

                List<XjrUser> userList = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
                    .eq(BaseStudentSchoolRoll::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .in(!classIds.isEmpty(), BaseStudentSchoolRoll::getClassId, classIds)
                    .in(!userIds.isEmpty(), XjrUser::getId, userIds)
                );
                //查询这部分人中是否已经有人有规则了
                List<Long> relationUserIds = userList.stream().map(XjrUser::getId).collect(Collectors.toList());
                List<XjrUser> relations = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>().distinct()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(AttendanceUserRelation.class, AttendanceUserRelation::getUserId, XjrUser::getId)
                    .eq(AttendanceUserRelation::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .in(!relationUserIds.isEmpty(), AttendanceUserRelation::getUserId, relationUserIds)
                );
//                String peopleName = "";
//                for (int i = 0; i < relations.size(); i ++){
//                    if(i > 0){
//                        peopleName += ",";
//                    }
//                    peopleName += relations.get(i).getName();
//                }
                if(!relations.isEmpty()){
                    throw new MyException("已有学生绑定考勤规则，无法添加");
                }
                insertList.addAll(userList);
            }
        }
        attendanceRuleCategory.setCreateDate(new Date());
        categoryMapper.insert(attendanceRuleCategory);
        for (AttendanceRuleDetails attendanceRuleDetails : attendanceRuleCategory.getAttendanceRuleDetailsList()) {
            attendanceRuleDetails.setAttendanceRuleCategoryId(attendanceRuleCategory.getId());
            detailsMapper.insert(attendanceRuleDetails);
        }
        for (XjrUser user : insertList) {
            AttendanceUserRelation relation = new AttendanceUserRelation();
            relation.setCreateDate(new Date());
            relation.setCreateUserId(StpUtil.getLoginIdAsLong());
            relation.setUserId(user.getId());
            relation.setAttendanceRuleCategoryId(attendanceRuleCategory.getId());
            relationMapper.insert(relation);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(UpdateAttendanceRuleCategoryDto dto) {
        AttendanceRuleCategory attendanceRuleCategory = BeanUtil.toBean(dto, AttendanceRuleCategory.class);
        attendanceRuleCategory.setUserRelation(JsonUtil.toJsonString(dto.getAttendanceUserRelationList()));
        if(attendanceRuleCategory.getAttendanceRange() == 1){
            Integer count = categoryMapper.getExistCountNEId(dto);
            if(count > 0){
                if(attendanceRuleCategory.getRoleId() == 2){
                    throw new MyException("已存在一个全体教职工的考勤规则，无法修改");
                }else if(attendanceRuleCategory.getRoleId() == 3){
                    throw new MyException("已存在一个全体学生的考勤规则，无法修改");
                }
            }
        }

        categoryMapper.updateById(attendanceRuleCategory);
        //********************************* AttendanceRuleDetails  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<AttendanceRuleDetails> attendanceRuleDetailsList = detailsMapper.selectList(Wrappers.lambdaQuery(AttendanceRuleDetails.class).eq(AttendanceRuleDetails::getAttendanceRuleCategoryId, attendanceRuleCategory.getId()).select(AttendanceRuleDetails::getId));
            List<Long> attendanceRuleDetailsIds = attendanceRuleDetailsList.stream().map(AttendanceRuleDetails::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> attendanceRuleDetailsOldIds = attendanceRuleCategory.getAttendanceRuleDetailsList().stream().map(AttendanceRuleDetails::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> attendanceRuleDetailsRemoveIds = attendanceRuleDetailsIds.stream().filter(item -> !attendanceRuleDetailsOldIds.contains(item)).collect(Collectors.toList());

            for (AttendanceRuleDetails attendanceRuleDetails : attendanceRuleCategory.getAttendanceRuleDetailsList()) {
                //如果不等于空则修改
                if (attendanceRuleDetails.getId() != null) {
                    detailsMapper.updateById(attendanceRuleDetails);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    attendanceRuleDetails.setAttendanceRuleCategoryId(attendanceRuleCategory.getId());
                    detailsMapper.insert(attendanceRuleDetails);
                }
            }
            //已经不存在的id 删除
            if(attendanceRuleDetailsRemoveIds.size() > 0){
                detailsMapper.deleteBatchIds(attendanceRuleDetailsRemoveIds);
            }
        }
        //********************************* AttendanceRuleDetails  增删改  结束 *******************************************/

        //********************************* AttendanceUserRelation  增删改  开始 *******************************************/
        List<XjrUser> insertList = new ArrayList<>();
        if(attendanceRuleCategory.getRoleId() != null && attendanceRuleCategory.getRoleId() == 2){
            if(attendanceRuleCategory.getAttendanceRange() == 1){
                List<XjrUser> userList = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(BaseTeacher.class, BaseTeacher::getUserId, XjrUser::getId)
                    .eq(BaseTeacher::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                );
                insertList.addAll(userList);
            }else if(attendanceRuleCategory.getAttendanceRange() == 2){
                List<Long> deptIds = new ArrayList<>();
                for (AddAttendanceUserRelationDto relation : dto.getAttendanceUserRelationList()) {
                    if(relation.getDeptId() != null){
                        deptIds.add(relation.getDeptId());
                    }
                }
                List<Long> userIds = new ArrayList<>();
                for (AddAttendanceUserRelationDto relation : dto.getAttendanceUserRelationList()) {
                    if(relation.getUserId() != null){
                        userIds.add(relation.getUserId());
                    }
                }

                List<XjrUser> userList = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(BaseTeacher.class, BaseTeacher::getUserId, XjrUser::getId)
                    .leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, XjrUser::getId)
                    .eq(BaseTeacher::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .in(!deptIds.isEmpty(), UserDeptRelation::getDeptId, deptIds)
                    .in(!userIds.isEmpty(), XjrUser::getId, userIds)
                );
                //查询这部分人中是否已经有人有规则了
                List<Long> relationUserIds = userList.stream().map(XjrUser::getId).collect(Collectors.toList());
                List<XjrUser> relations = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(AttendanceUserRelation.class, AttendanceUserRelation::getUserId, XjrUser::getId)
                    .eq(AttendanceUserRelation::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .in(AttendanceUserRelation::getUserId, relationUserIds)
                    .ne(AttendanceUserRelation::getAttendanceRuleCategoryId, attendanceRuleCategory.getId())
                );
                String peopleName = "";
                for (int i = 0; i < relations.size(); i ++){
                    if(i > 0){
                        peopleName += ",";
                    }
                    peopleName += relations.get(i).getName();
                }
                if(!relations.isEmpty()){
                    throw new MyException("以下教职工：【" + peopleName + "】已绑定其他考勤规则，无法添加");
                }
                insertList.addAll(userList);
            }
        }else if(attendanceRuleCategory.getRoleId() != null && attendanceRuleCategory.getRoleId() == 3){//学生
            if(attendanceRuleCategory.getAttendanceRange() == 1){
                //首先判断是否已经存在一个全体学生的考勤规则
                Long count = categoryMapper.selectCount(
                    new QueryWrapper<AttendanceRuleCategory>()
                    .lambda()
                    .eq(AttendanceRuleCategory::getRoleId, attendanceRuleCategory.getRoleId())
                    .eq(AttendanceRuleCategory::getAttendanceRange, attendanceRuleCategory.getAttendanceRange())
                    .eq(AttendanceRuleCategory::getDeleteMark, DeleteMark.NODELETE.getCode())
                );
                if(count > 0){
                    throw new MyException("已存在一个全体学生的考勤规则，无法修改");
                }
                List<XjrUser> userList = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(BaseStudent.class, BaseStudent::getUserId, XjrUser::getId)
                    .eq(BaseStudent::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                );
                insertList.addAll(userList);
            }else if(attendanceRuleCategory.getAttendanceRange() == 2){
                List<Long> classIds = new ArrayList<>();
                for (AddAttendanceUserRelationDto relation : dto.getAttendanceUserRelationList()) {
                    if(relation.getClassId() != null){
                        classIds.add(relation.getClassId());
                    }
                }
                List<Long> userIds = new ArrayList<>();
                for (AddAttendanceUserRelationDto relation : dto.getAttendanceUserRelationList()) {
                    if(relation.getUserId() != null){
                        userIds.add(relation.getUserId());
                    }
                }

                List<XjrUser> userList = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
                    .eq(BaseStudentSchoolRoll::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .in(!classIds.isEmpty(), BaseStudentSchoolRoll::getClassId, classIds)
                    .in(!userIds.isEmpty(), XjrUser::getId, userIds)
                );
                //查询这部分人中是否已经有人有规则了
                List<Long> relationUserIds = userList.stream().map(XjrUser::getId).collect(Collectors.toList());
                List<XjrUser> relations = xjrUserService.list(
                    new MPJLambdaWrapper<XjrUser>()
                    .select(XjrUser::getId)
                    .select(XjrUser.class, x -> VoToColumnUtil.fieldsToColumns(XjrUser.class).contains(x.getProperty()))
                    .leftJoin(AttendanceUserRelation.class, AttendanceUserRelation::getUserId, XjrUser::getId)
                    .eq(AttendanceUserRelation::getDeleteMark, DeleteMark.NODELETE.getCode())
                    .in(AttendanceUserRelation::getUserId, relationUserIds)
                    .ne(AttendanceUserRelation::getAttendanceRuleCategoryId, attendanceRuleCategory.getId())
                );
                String peopleName = "";
                for (int i = 0; i < relations.size(); i ++){
                    if(i > 0){
                        peopleName += ",";
                    }
                    peopleName += relations.get(i).getName();
                }
                if(!relations.isEmpty()){
                    throw new MyException("以下学生：【" + peopleName + "】已绑定其他考勤规则，无法修改");
                }
                insertList.addAll(userList);
            }
        }
        //先清空原来的全体教职工数据，再重新添加
        relationMapper.delete(
            Wrappers.lambdaQuery(AttendanceUserRelation.class)
            .eq(AttendanceUserRelation::getAttendanceRuleCategoryId, attendanceRuleCategory.getId())
        );
        for (XjrUser user : insertList) {
            AttendanceUserRelation relation = new AttendanceUserRelation();
            relation.setCreateDate(new Date());
            relation.setCreateUserId(StpUtil.getLoginIdAsLong());
            relation.setUserId(user.getId());
            relation.setAttendanceRuleCategoryId(attendanceRuleCategory.getId());
            relationMapper.insert(relation);
        }
        //********************************* AttendanceUserRelation  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        categoryMapper.deleteBatchIds(ids);
        detailsMapper.delete(Wrappers.lambdaQuery(AttendanceRuleDetails.class).in(AttendanceRuleDetails::getAttendanceRuleCategoryId, ids));
        relationMapper.delete(Wrappers.lambdaQuery(AttendanceUserRelation.class).in(AttendanceUserRelation::getAttendanceRuleCategoryId, ids));

        return true;
    }

    @Override
    public List<AttendanceRuleDetails> getTodayRules() {
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        return detailsMapper.getTodayRules(dayOfWeek.name());
    }

    @Override
    public AttendanceRuleDetails getTodayRulesByParam(String dayOfWeek) {
        return null;
    }

    @Override
    public Map<Long, AttendanceRuleDetailsUserVo> getAllTeacherTodyRule(String dayOfWeek) {
        List<AttendanceRuleDetailsUserVo> teacherTodyRule = detailsMapper.getAllTeacherTodyRule(dayOfWeek);
        Map<Long, AttendanceRuleDetailsUserVo> result = new HashMap<>();
        for (AttendanceRuleDetailsUserVo userVo : teacherTodyRule) {
            result.put(userVo.getUserId(), userVo);
        }

        return result;
    }

    @Override
    public Map<Long, AttendanceRuleDetailsUserVo> getAllStudentTodyRule(String dayOfWeek) {
        List<AttendanceRuleDetailsUserVo> teacherTodyRule = detailsMapper.getAllStudentTodyRule(dayOfWeek);
        Map<Long, AttendanceRuleDetailsUserVo> result = new HashMap<>();
        for (AttendanceRuleDetailsUserVo userVo : teacherTodyRule) {
            result.put(userVo.getUserId(), userVo);
        }

        return result;
    }

    @Override
    public Map<String, Map<Long, AttendanceRuleDetailsUserVo>> getAllTeacherTodyRule(List<String> dayOfWeek) {

        Map<String, Map<Long, AttendanceRuleDetailsUserVo>> result = new HashMap<>();
        for (String s : dayOfWeek) {
            List<AttendanceRuleDetailsUserVo> teacherTodyRule = detailsMapper.getAllTeacherTodyRule(s);
            Map<Long, AttendanceRuleDetailsUserVo> dataMap = new HashMap<>();
            for (AttendanceRuleDetailsUserVo userVo : teacherTodyRule) {
                dataMap.put(userVo.getUserId(), userVo);
            }
            result.put(s, dataMap);
        }

        return result;
    }

    @Override
    public Map<String, Map<Long, AttendanceRuleDetailsUserVo>> getAllStudentTodyRule(List<String> dayOfWeek) {
        Map<String, Map<Long, AttendanceRuleDetailsUserVo>> result = new HashMap<>();
        for (String s : dayOfWeek) {
            List<AttendanceRuleDetailsUserVo> teacherTodyRule = detailsMapper.getAllStudentTodyRule(s);
            Map<Long, AttendanceRuleDetailsUserVo> dataMap = new HashMap<>();
            for (AttendanceRuleDetailsUserVo userVo : teacherTodyRule) {
                dataMap.put(userVo.getUserId(), userVo);
            }
            result.put(s, dataMap);
        }

        return result;
    }
}
