//package com.yida.data.attendance.service.impl;
//
//
//import cc.mrbird.febs.common.redis.service.RedisService;
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.date.LocalDateTimeUtil;
//import cn.hutool.core.util.StrUtil;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.yida.data.attendance.dto.AttendanceResultPageDTO;
//import com.yida.data.attendance.dto.ListStudentResultDTO;
//import com.yida.data.attendance.mapper.EduAttendanceRecordMapper;
//import com.yida.data.attendance.mapper.EduAttendanceResultMapper;
//import com.yida.data.attendance.mapper.EduAttendanceVerifyDormMapper;
//import com.yida.data.attendance.service.EduAttendanceConfigService;
//import com.yida.data.attendance.service.EduAttendanceResultService;
//import com.yida.data.attendance.service.GenerateAttendanceResultService;
//import com.yida.data.attendance.vo.*;
//import com.yida.data.common.core.entity.attendance.EduAttendanceConfig;
//import com.yida.data.common.core.entity.attendance.EduAttendanceRecord;
//import com.yida.data.common.core.entity.attendance.EduAttendanceResult;
//import com.yida.data.common.core.entity.attendance.EduAttendanceVerifyDorm;
//import com.yida.data.common.core.entity.constant.AppConstant;
//import com.yida.data.common.core.entity.constant.CachePrefixConstant;
//import com.yida.data.common.core.entity.notice.qywx.TextSchoolNotice;
//import com.yida.data.common.core.entity.notice.qywx.inside.Text;
//import com.yida.data.common.core.entity.school.EduDormitoryRoom;
//import com.yida.data.common.core.entity.system.EduApp;
//import com.yida.data.common.core.entity.user.EduParent;
//import com.yida.data.common.core.entity.user.EduStudent;
//import com.yida.data.common.core.enums.AttendanceResult;
//import com.yida.data.common.core.utils.EnumUtils;
//import com.yida.data.common.core.utils.ExcelUtil;
//import com.yida.data.common.core.utils.WxUtil;
//import com.yida.data.common.service.CommonService;
//import com.yida.data.school.dto.index.ListDormRoomDTO;
//import com.yida.data.school.feign.facility.RemoteDormitoryService;
//import com.yida.data.user.dto.ListStudentDTO;
//import com.yida.data.user.dto.ListStudentDTO.ListStudentDTOBuilder;
//import com.yida.data.user.feign.RemoteStudentService;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.context.config.annotation.RefreshScope;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.servlet.http.HttpServletResponse;
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * 考勤结果 Service实现
// *
// * @author wjm
// * @date 2021-09-15 09:56:24
// */
//@Slf4j
//@Service
//@RequiredArgsConstructor
//@RefreshScope
//public class EduAttendanceResultServiceImplBack extends ServiceImpl<EduAttendanceResultMapper, EduAttendanceResult>
//    implements EduAttendanceResultService {
//
//    private static final String NOTICE_URL = "/attendance/url";
//    private final EduAttendanceConfigService eduAttendanceConfigService;
//    private final EduAttendanceRecordMapper eduAttendanceRecordMapper;
//    private final EduAttendanceVerifyDormMapper eduAttendanceVerifyDormMapper;
//    private final RemoteDormitoryService remoteDormitoryService;
//    private final RemoteStudentService remoteStudentService;
//    private final RedisService redisService;
//    private final CommonService commonService;
//    private final WxUtil wxUtil;
//    private final GenerateAttendanceResultService generateAttendanceResultService;
//    @Value("${febs.pageUrl}")
//    private String pageUrl;
//
//    @Override
//    @Transactional
//    public List<StayStudentResultVO> listResultByDorm(Long dormId, String date, Integer time, String keyword, Integer status) {
//        List<StayStudentResultVO> resList = new ArrayList<>();
//        List<EduDormitoryRoom> roomList =
//            remoteDormitoryService.listDormRoom(ListDormRoomDTO.builder().dormIds(Collections.singletonList(dormId)).build())
//                .getData();
//        if (CollUtil.isNotEmpty(roomList)) {
//            List<Long> roomIds = roomList.stream().map(EduDormitoryRoom::getId).collect(Collectors.toList());
//            // 查询所有寝室的考勤记录
//            List<EduAttendanceResult> resultList = baseMapper
//                .listStudentResult(roomIds, null, LocalDateTimeUtil.parseDate(date, DateTimeFormatter.ISO_DATE),
//                    null, null, null, null, null, time, 1, status);
//            List<Long> studentIds = resultList.stream().map(EduAttendanceResult::getStudentId).distinct()
//                .collect(Collectors.toList());
//            // 查询考勤记录的学生
//            List<EduStudent> studentList = remoteStudentService
//                .listStudent(ListStudentDTO.builder().studentIds(studentIds).isQuerySchoolName(false).isQueryParent(false)
//                    .isQueryUserDept(false).build()).getData();
//            Map<Long, List<EduStudent>> studentGroup = studentList.stream().collect(Collectors.groupingBy(EduStudent::getId));
//            Map<Long, List<EduDormitoryRoom>> roomGroup = roomList.stream()
//                .collect(Collectors.groupingBy(EduDormitoryRoom::getId));
//            for (EduAttendanceResult result : resultList) {
//                if (studentGroup.containsKey(result.getStudentId())) {
//                    List<EduStudent> student = studentGroup.get(result.getStudentId());
//                    StayStudentResultVO res = new StayStudentResultVO();
//                    res.setStudentName(student.get(0).getStuName());
//                    res.setStudentId(result.getStudentId());
//                    res.setDormRoomName(roomGroup.get(result.getDormRoomId()).get(0).getName());
//                    res.setTime(result.getInTime());
//                    res.setResult(translateResult(result.getStatus()));
//                    if (StrUtil.isNotBlank(keyword)) {
//                        if (!(res.getStudentName() + res.getDormRoomName()).contains(keyword)) {
//                            continue;
//                        }
//                    }
//                    resList.add(res);
//                }
//            }
//        }
//        return resList;
//    }
//
//    @Override
//    @Transactional
//    public List<StayStudentResultVO> listStudentResByDormRoom(Long roomId, String date, Integer time) {
//        List<StayStudentResultVO> resList = new ArrayList<>();
//        // 查询考勤记录的学生
//        List<EduStudent> studentList = remoteStudentService.listStudent(
//            ListStudentDTO.builder().dormRoomIds(Collections.singletonList(roomId)).isQueryUserDept(false).isQueryParent(false)
//                .isQuerySchoolName(false).build())
//            .getData();
//        LocalDate day = LocalDate.parse(date);
//        LocalDateTime startTime = day.atStartOfDay();
//        LocalDateTime endTime = day.atTime(23, 59, 59);
//        // 循环每个学生
//        for (EduStudent student : studentList) {
//            StayStudentResultVO res = new StayStudentResultVO();
//            res.setAvatar(student.getAvatar());
//            res.setStudentNumber(student.getStuNumber());
//            res.setStudentName(student.getStuName());
//            res.setStudentId(student.getId());
//            EduAttendanceRecord lastRecord = eduAttendanceRecordMapper
//                .getLastRecordByTime(startTime, endTime, null, student.getId());
//            // 学生有在寝的记录
//            if (lastRecord == null || lastRecord.getScope() == 1) {
//                res.setStatus(1);
//            } else {
//                res.setStatus(0);
//            }
//            resList.add(res);
//        }
//        return resList;
//    }
//
//    @Override
//    @Transactional
//    public void changeStayStudentStatus(Long studentId, String date, Integer time, Integer status) {
//        EduStudent student = (EduStudent) redisService.hget(CachePrefixConstant.STUDENT_DATA, studentId.toString());
//        if (student == null) {
//            student = remoteStudentService.getStudentNoPermission(studentId).getData();
//        }
//        EduAttendanceRecord record = new EduAttendanceRecord();
//        record.setSchoolId(student.getSchoolId());
//        record.setCampusId(student.getCampusId());
//        record.setGradeId(student.getGradeId());
//        record.setGradeName(student.getGradeName());
//        record.setClassId(student.getClassId());
//        record.setClassName(student.getClassName());
//        record.setStudentId(studentId);
//        record.setStatus(-1);
//        record.setCreateTime(LocalDateTime.now());
//        record.setDormId(student.getDormId());
//        record.setDormRoomId(student.getDormRoomId());
//        record.setZone(1);
//        if (status == 0) {
//            record.setScope(0);
//        } else {
//            record.setScope(1);
//        }
//        eduAttendanceRecordMapper.insert(record);
//    }
//
//    @Override
//    @Transactional
//    public void verifyDormRoom(EduAttendanceVerifyDorm verifyDorm) {
//        eduAttendanceVerifyDormMapper.insert(verifyDorm);
//    }
//
//    @Override
//    @Transactional
//    public IPage<AttendanceResultDormVO> listAttendanceResultPage(AttendanceResultPageDTO dto) {
//        // 获取学生
//        List<Long> studentIds = null;
//        ListStudentDTO listStudentDTO = ListStudentDTO.builder().isQueryParent(false).isQuerySchoolName(false).build();
//        BeanUtils.copyProperties(dto, listStudentDTO);
//        // 筛选寝室
//        List<Long> roomList = new ArrayList<>();
//        if (dto.getDormRoomId() != null) {
//            roomList.add(dto.getDormRoomId());
//        } else if (dto.getDormId() != null) {
//            List<EduDormitoryRoom> data = remoteDormitoryService
//                .listDormRoom(ListDormRoomDTO.builder().dormIds(Collections.singletonList(dto.getDormId())).build()).getData();
//            if (CollUtil.isNotEmpty(data)) {
//                roomList = data.stream().map(EduDormitoryRoom::getId).collect(Collectors.toList());
//            }
//        }
//        listStudentDTO.setDormRoomIds(roomList);
//        // 查询匹配的学生
//        List<EduStudent> studentList = remoteStudentService.listStudent(listStudentDTO).getData();
//        if (CollUtil.isNotEmpty(studentList)) {
//            if (StrUtil.isNotBlank(dto.getKeyword())) {
//                studentList = studentList.stream()
//                    .filter(x -> x.getStuName().contains(dto.getKeyword()) || x.getStuNumber().contains(dto.getKeyword()))
//                    .collect(Collectors.toList());
//            }
//            if (CollUtil.isNotEmpty(studentList)) {
//                studentIds = studentList.stream().map(EduStudent::getId).collect(Collectors.toList());
//            }
//        }
//
//        // 查询考勤结果记录
//        Page<AttendanceResultDormVO> recordPage = Page.of(dto.getPageNum(), dto.getPageSize());
//        recordPage.setRecords(
//            baseMapper.listAttendanceResult(recordPage, studentIds, dto.getRuleId(), dto.getStartDate(), dto.getEndDate()));
//        if (CollUtil.isNotEmpty(recordPage.getRecords()) && CollUtil.isNotEmpty(studentList)) {
//            for (AttendanceResultDormVO record : recordPage.getRecords()) {
//                for (EduStudent student : studentList) {
//                    if (student.getId().equals(record.getStudentId())) {
//                        record.setCampusName(student.getCampusName());
//                        record.setStuName(student.getStuName());
//                        record.setStuNumber(student.getStuNumber());
//                        break;
//                    }
//                }
//            }
//        }
//        return recordPage;
//    }
//
//    @Override
//    @Transactional
//    public void exportAttendanceResult(AttendanceResultPageDTO dto, HttpServletResponse response) {
//        // 获取学生
//        List<Long> studentIds = null;
//        ListStudentDTO listStudentDTO = ListStudentDTO.builder().isQueryParent(false).isQuerySchoolName(false).build();
//        BeanUtils.copyProperties(dto, listStudentDTO);
//        // 筛选寝室
//        List<Long> roomList = new ArrayList<>();
//        if (dto.getDormRoomId() != null) {
//            roomList.add(dto.getDormRoomId());
//        } else if (dto.getDormId() != null) {
//            List<EduDormitoryRoom> data = remoteDormitoryService
//                .listDormRoom(ListDormRoomDTO.builder().dormIds(Collections.singletonList(dto.getDormId())).build()).getData();
//            if (CollUtil.isNotEmpty(data)) {
//                roomList = data.stream().map(EduDormitoryRoom::getId).collect(Collectors.toList());
//            }
//        }
//        listStudentDTO.setDormRoomIds(roomList);
//        // 查询匹配的学生
//        List<EduStudent> studentList = remoteStudentService.listStudent(listStudentDTO).getData();
//        if (CollUtil.isNotEmpty(studentList)) {
//            if (StrUtil.isNotBlank(dto.getKeyword())) {
//                studentList = studentList.stream()
//                    .filter(x -> x.getStuName().contains(dto.getKeyword()) || x.getStuNumber().contains(dto.getKeyword()))
//                    .collect(Collectors.toList());
//            }
//            if (CollUtil.isNotEmpty(studentList)) {
//                studentIds = studentList.stream().map(EduStudent::getId).collect(Collectors.toList());
//            }
//        }
//        // 考勤结果列表
//        List<AttendanceResultDormVO> recordList = baseMapper.listAttendanceResult(null, studentIds, dto.getRuleId(),
//            dto.getStartDate(), dto.getEndDate());
//        if (CollUtil.isNotEmpty(recordList) && CollUtil.isNotEmpty(studentList)) {
//            for (AttendanceResultDormVO record : recordList) {
//                for (EduStudent student : studentList) {
//                    if (student.getId().equals(record.getStudentId())) {
//                        record.setCampusName(student.getCampusName());
//                        record.setStuName(student.getStuName());
//                        record.setStuNumber(student.getStuNumber());
//                        break;
//                    }
//                }
//            }
//        }
//
//        if (dto.getType() == 0) {
//            ExcelUtil.export("校门考勤记录表.xlsx", AttendanceResultVO.class, recordList.stream().map(x -> {
//                AttendanceResultVO vo = new AttendanceResultVO();
//                BeanUtils.copyProperties(x, vo);
//                return vo;
//            }).collect(Collectors.toList()), response);
//        } else {
//            ExcelUtil.export("宿舍考勤记录表.xlsx", AttendanceResultDormVO.class, recordList, response);
//        }
//    }
//
//    /**
//     * 定时处理考勤记录
//     */
//    @Override
//    public void dealAttendanceResult() {
//        List<EduAttendanceConfig> configList = eduAttendanceConfigService.list();
//        if (CollUtil.isNotEmpty(configList)) {
//            // 每个学校分开查询
//            for (EduAttendanceConfig config : configList) {
//                LocalDateTime begin = LocalDateTime.now();
//                log.info("开始生成[{}]学校学生考勤结果", config.getSchoolId());
//                // 远程调用不放入事务  减少长事务
//                List<EduStudent> schoolStudentList =
//                    remoteStudentService.listStudent(
//                        ListStudentDTO.builder().isQuerySchoolName(false).isQueryParent(false).schoolId(config.getSchoolId())
//                            .build()).getData();
//                generateAttendanceResultService.generateStudentAttendanceResult(config, schoolStudentList);
//                LocalDateTime end = LocalDateTime.now();
//                log.info("完成[{}]学校学生考勤结果生成,开始时间:[{}],结束时间:[{}]", config.getSchoolId(), begin, end);
//            }
//        }
//    }
//
//
//    @Override
//    @Transactional
//    public void pushNotArriveMsg() {
//        List<EduAttendanceConfig> configList = eduAttendanceConfigService.list();
//        if (CollUtil.isNotEmpty(configList)) {
//            for (EduAttendanceConfig config : configList) {
//                // 查询学校需要推送的消息
//                LocalDateTime now = LocalDateTime.now();
//                List<EduAttendanceResult> pushMessage = baseMapper
//                    .listPushNotArrive(now.minusMinutes(config.getSchoolPushInterval()), config.getSchoolId());
//                if (CollUtil.isNotEmpty(pushMessage)) {
//                    for (EduAttendanceResult result : pushMessage) {
//                        EduStudent student = (EduStudent) redisService.hget(CachePrefixConstant.STUDENT_DATA,
//                            result.getStudentId().toString());
//                        if (student == null) {
//                            student = remoteStudentService.getStudentNoPermission(result.getStudentId()).getData();
//                        }
//                        if (student != null) {
//                            // 推送包含家长
//                            if (config.getSchoolPushObj() == 0) {
//                                for (EduParent parent : student.getParents()) {
//                                    //构建消息
//                                    String msg = String.format("【平安校园警告】尊敬的%s%s，截止%s，%s" +
//                                            "并未归寝，请您尽快确认学生位置，确保学生的人身安全。<a href=\"%s\">请点击此处确认消息</a>",
//                                        student.getStuName(), parent.getParentType(),
//                                        LocalDateTimeUtil.format(now, "yyyy年MM月dd日HH时mm分秒"),
//                                        student.getStuName(), pageUrl + NOTICE_URL + "?id=" + result.getId().toString());
//                                    // 发送通知消息
//                                    EduApp app = commonService.getAppByCodeAndSchool(AppConstant.ATTENDANCE_PARENT, null,
//                                        student.getSchoolId());
//                                    TextSchoolNotice notice = new TextSchoolNotice();
//                                    notice.setTo_parent_userid(Collections.singletonList(parent.getWxId()));
//                                    notice.setText(new Text(msg));
//                                    notice.setAgentid(app.getWxAgentId());
//                                    wxUtil.pushSchoolNotice(wxUtil.getAccessToken(app.getWxCorpId(), app.getWxSecret()),
//                                        notice);
//                                    result.setLastPushTime(now);
//                                }
//                            }
//                            result.setNotArriveConfirm(null);
//                        }
//                    }
//                    updateBatchById(pushMessage);
//                }
//            }
//        }
//    }
//
//    @Override
//    @Transactional
//    public List<RuleTimeVO> listGradeResultTime(Long gradeId, String date) {
//        return baseMapper.listGradeResultTime(gradeId, date);
//    }
//
//    @Override
//    @Transactional
//    public List<ResultDetailVO> listStudentResultDetail(ListStudentResultDTO dto) {
//        ListStudentDTOBuilder builder = ListStudentDTO.builder().isQueryParent(false).isQuerySchoolName(false)
//            .isQueryUserDept(false);
//        if (dto.getClassId() != null) {
//            builder.classId(dto.getClassId());
//        }
//        if (dto.getStudentId() != null) {
//            builder.studentIds(Collections.singletonList(dto.getStudentId()));
//        }
//        List<EduStudent> studentList = remoteStudentService.listStudent(builder.build()).getData();
//        List<ResultDetailVO> details = null;
//        if (CollUtil.isNotEmpty(studentList)) {
//            details = baseMapper.listStudentResultDetail(dto,
//                studentList.stream().map(EduStudent::getId).collect(Collectors.toList()));
//            if (CollUtil.isNotEmpty(details)) {
//                Map<Long, List<EduStudent>> studentGroup = studentList.stream().collect(Collectors.groupingBy(EduStudent::getId));
//                for (ResultDetailVO detail : details) {
//                    EduStudent student = studentGroup.get(detail.getStudentId()).get(0);
//                    detail.setStudentName(student.getStuName());
//                    detail.setStudentNum(student.getStuNumber());
//                    detail.setStudentType(student.getType());
//                }
//            }
//        }
//        return details;
//    }
//
//    private String translateResult(Integer status) {
//        AttendanceResult value = null;
//        try {
//            value = EnumUtils.valueOf(AttendanceResult.class, status, AttendanceResult.class.getMethod("getValue"));
//        } catch (NoSuchMethodException e) {
//            log.error("考勤结果值获取失败,:[{}]", status);
//        }
//        return value != null ? value.getName() : null;
//    }
//}
