package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.exception.EasException;
import com.koobietech.eas.common.result.PageData;
import com.koobietech.eas.mbg.mapper.EasArcTlsAttendanceMapper;
import com.koobietech.eas.mbg.mapper.EasEduCltRelationMapper;
import com.koobietech.eas.mbg.mapper.EasEduScheduleMapper;
import com.koobietech.eas.mbg.mapper.EasSysStudentMapper;
import com.koobietech.eas.mbg.model.*;
import com.koobietech.eas.service.EasArcTlsAttendanceService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
public class EasArcTlsAttendanceServiceImpl implements EasArcTlsAttendanceService {

    @Resource
    private EasArcTlsAttendanceMapper easArcTlsAttendanceMapper;

    @Resource
    private EasEduCltRelationMapper easEduCltRelationMapper;

    @Resource
    private EasSysStudentMapper easSysStudentMapper;

    @Resource
    private EasEduScheduleMapper easEduScheduleMapper;


    @Override
    public PageData query(EasArcTlsAttendance attendance) {
        EasArcTlsAttendanceExample easArcTlsAttendanceExample = new EasArcTlsAttendanceExample();
        EasArcTlsAttendanceExample.Criteria criteria = easArcTlsAttendanceExample.createCriteria();
        if (Objects.nonNull(attendance)) {

            if (Objects.nonNull(attendance.getId()) && attendance.getId() != 0) {
                criteria.andIdEqualTo(attendance.getId());
            }
            if (Objects.nonNull(attendance.getCheckinDate())) {
                criteria.andCheckinDateEqualTo(attendance.getCheckinDate());
            }
            if (Objects.nonNull(attendance.getScheduleId()) && attendance.getScheduleId() != 0) {
                criteria.andScheduleIdEqualTo(attendance.getScheduleId());
            }
            if (Objects.nonNull(attendance.getMonth()) && attendance.getMonth() != 0) {
                criteria.andMonthEqualTo(attendance.getMonth());
            }
            if (StringUtils.hasText(attendance.getMorning())) {
                criteria.andMorningEqualTo(attendance.getMorning());
            }
            if (StringUtils.hasText(attendance.getAfternoon())) {
                criteria.andAfternoonEqualTo(attendance.getAfternoon());
            }
            if (StringUtils.hasText(attendance.getStudentNumber())) {
                criteria.andStudentNumberLike("%" + attendance.getStudentNumber() + "%");
            }
            if (Objects.nonNull(attendance.getCreateUid()) && attendance.getCreateUid() != 0) {
                criteria.andCreateUidEqualTo(attendance.getCreateUid());
            }
            if (Objects.nonNull(attendance.getCreateTime())) {
                criteria.andCreateTimeEqualTo(attendance.getCreateTime());
            }
            if (Objects.nonNull(attendance.getModifyTime())) {
                criteria.andModifyTimeEqualTo(attendance.getModifyTime());
            }
        }

        List<EasArcTlsAttendance> easArcTlsAttendances =
                easArcTlsAttendanceMapper.selectByExample(easArcTlsAttendanceExample);
        long l = easArcTlsAttendanceMapper.countByExample(easArcTlsAttendanceExample);
        PageData ret = new PageData();
        ret.setData(easArcTlsAttendances);
        ret.setTotal(l);
        return ret;

    }

    @Override
    public Boolean add(EasArcTlsAttendance attendance) {
        //签到日期前端会自己传 所以就不需要再写一次了
        attendance.setCreateTime(new Date());
        attendance.setModifyTime(new Date());
        return easArcTlsAttendanceMapper.insert(attendance) == 1;

    }

    @Override
    public Boolean delete(EasArcTlsAttendance attendance) {
        //虽然接收的是对象，但是后端是根据checkin_date和student_number来删除的
        EasArcTlsAttendanceExample easArcTlsAttendanceExample = new EasArcTlsAttendanceExample();
        EasArcTlsAttendanceExample.Criteria criteria = easArcTlsAttendanceExample.createCriteria();
        if (Objects.nonNull(attendance)) {
            if (Objects.nonNull(attendance.getCheckinDate())
                    && StringUtils.hasText(attendance.getStudentNumber())) {
                criteria.andCheckinDateEqualTo(attendance.getCheckinDate());
                criteria.andStudentNumberEqualTo(attendance.getStudentNumber());
                return easArcTlsAttendanceMapper.deleteByExample(easArcTlsAttendanceExample) == 1;
            }
        }
        return false;
    }

    @Override
    public Boolean update(EasArcTlsAttendance attendance) {
        attendance.setModifyTime(new Date());
        return easArcTlsAttendanceMapper.updateByPrimaryKey(attendance) == 1;
    }

    @Override
    public boolean isExist(EasArcTlsAttendance attendance) {
        EasArcTlsAttendanceExample easArcTlsAttendanceExample = new EasArcTlsAttendanceExample();
        EasArcTlsAttendanceExample.Criteria criteria = easArcTlsAttendanceExample.createCriteria();
        if (Objects.nonNull(attendance)) {
            if (Objects.nonNull(attendance.getCheckinDate())
                    && StringUtils.hasText(attendance.getStudentNumber())) {
                criteria.andCheckinDateEqualTo(attendance.getCheckinDate());
                criteria.andStudentNumberEqualTo(attendance.getStudentNumber());
                return easArcTlsAttendanceMapper.countByExample(easArcTlsAttendanceExample) == 1;
            }
        }
        return false;
    }

    @Override
    public List<EasArcTlsAttendance> getStudentList(Long scheduleId) {
        // 根据课表ID查找EasEduSchedule表，获取班级ID
        EasEduSchedule easEduSchedule = easEduScheduleMapper.selectByPrimaryKey(scheduleId);
        Long classId = Optional.ofNullable(easEduSchedule)
                .map(s -> Long.valueOf(s.getClassId()))
                .orElseThrow(() -> new EasException("未查询到对应课表，请检查数据库", 9595));

        // 根据班级ID查找EasEduCltRelation表，获取学生ID列表
        EasEduCltRelationExample easEduCltRelationExample = new EasEduCltRelationExample();
        easEduCltRelationExample.createCriteria().andClassIdEqualTo(classId);

        // 查询EasEduCltRelation表，获取符合条件的学生ID列表
        List<Long> studentIds = easEduCltRelationMapper.selectByExample(easEduCltRelationExample)
                .stream()
                .map(r -> Long.valueOf(r.getStudentId()))
                .collect(Collectors.toList());

        // 根据学生ID列表查询EasSysStudent表，获取学生信息
        EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
        easSysStudentExample.createCriteria().andIdIn(studentIds);
        List<EasSysStudent> easSysStudents = easSysStudentMapper.selectByExample(easSysStudentExample);

        // 封装查询结果到PageData对象并返回
        return easSysStudents.stream()
                .map(s -> {
                    EasArcTlsAttendance easArcTlsAttendance = new EasArcTlsAttendance();
                    easArcTlsAttendance.setStudentNumber(s.getStudentNumber());
                    easArcTlsAttendance.setStudentName(s.getStudentName());
                    return easArcTlsAttendance;
                })
                .collect(Collectors.toList());
    }


//    @Override
//    public List<EasArcTlsAttendance> getStudentList(Long scheduleId) {
//        // 创建查询条件，根据课表ID查找EasEduSchedule表，获取班级ID
//        EasEduSchedule easEduSchedule = easEduScheduleMapper.selectByPrimaryKey(scheduleId);
//        if (Objects.nonNull(easEduSchedule)) {
//            Long classId = Long.valueOf(easEduSchedule.getClassId()); // 将班级ID赋值给classId变量
//            System.out.println("！！！！查出来的对应scheduleId的classId = " + classId);
//
//            // 创建查询条件，根据班级ID查找EasEduCltRelation表，获取学生ID
//            EasEduCltRelationExample easEduCltRelationExample = new EasEduCltRelationExample();
//            EasEduCltRelationExample.Criteria criteriaFindStuId = easEduCltRelationExample.createCriteria();
//            criteriaFindStuId.andClassIdEqualTo(classId); // 设置班级ID作为查询条件
//
//            // 查询EasEduCltRelation表，获取符合条件的学生ID列表
//            List<EasEduCltRelation> easEduCltRelations = easEduCltRelationMapper.selectByExample(easEduCltRelationExample);
//            List<Long> studentIds = new ArrayList<>();
//            for (EasEduCltRelation easEduCltRelation : easEduCltRelations) {
//                studentIds.add(Long.valueOf(easEduCltRelation.getStudentId())); // 将学生ID添加到列表中
//            }
//
//            // 根据学生ID列表查询EasSysStudent表，获取学生信息
//            EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
//            EasSysStudentExample.Criteria criteriaFindStu = easSysStudentExample.createCriteria();
//            if (!studentIds.isEmpty()) {
//                criteriaFindStu.andIdIn(studentIds); // 设置学生ID列表作为查询条件
//            }
//            List<EasSysStudent> easSysStudents = easSysStudentMapper.selectByExample(easSysStudentExample);
//
//            // 封装查询结果到PageData对象并返回
//            PageData ret = new PageData();
//            ret.setData(easSysStudents);
//            List<EasArcTlsAttendance> list = new ArrayList<>();
//            easSysStudents.forEach(easSysStudent -> {
//                EasArcTlsAttendance easArcTlsAttendance = new EasArcTlsAttendance();
//                easArcTlsAttendance.setStudentNumber(easSysStudent.getStudentNumber());
//                easArcTlsAttendance.setStudentName(easSysStudent.getStudentName());
//                list.add(easArcTlsAttendance);
//            });
//            return list;
//        } else {
//            throw new EasException("未查询到对应课表，请检查数据库", 9595); // 如果查询不到课表，则返回空列表
//        }
//    }


}




