//package com.yida.data.attendance.service.impl;
//
//import cc.mrbird.febs.common.redis.service.RedisService;
//import cn.hutool.core.collection.CollectionUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.yida.data.attendance.mapper.EduAttendanceStaffRecordMapper;
//import com.yida.data.attendance.service.*;
//import com.yida.data.attendance.vo.StaffRecordVO;
//import com.yida.data.common.core.entity.attendance.*;
//import com.yida.data.common.core.entity.user.EduStaff;
//import com.yida.data.common.core.entity.user.EduStaffDept;
//import com.yida.data.common.service.CommonService;
//import com.yida.data.user.feign.RemoteStaffService;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Propagation;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.util.List;
//
//import static com.yida.data.common.core.entity.constant.CachePrefixConstant.STAFF_DATA;
//
///**
// * 教职工考勤-进出记录 Service实现
// *
// * @author ccl
// * @date 2021-09-27 18:56:19
// */
//@Service
//@RequiredArgsConstructor
//@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
//public class EduAttendanceStaffRecordServiceImpBak extends ServiceImpl
//    <EduAttendanceStaffRecordMapper, EduAttendanceStaffRecord> implements EduAttendanceStaffRecordService {
//
//    private final EduAttendanceStaffResultService eduAttendanceStaffResultService;
//    private final EduAttendanceStaffRuleService eduAttendanceStaffRuleService;
//    private final EduAttendanceStaffRuleStaffService eduAttendanceStaffRuleStaffService;
//    private final EduAttendanceStaffRuleTimeService eduAttendanceStaffRuleTimeService;
//
//    private final RemoteStaffService remoteStaffService;
//    private final CommonService commonService;
//    private final RedisService redisService;
//
//
//    @Override
//    public IPage<StaffRecordVO> findPageStaffRecord(Page page, Long deptId, String nameOrPhone, String start, String end,
//        Long schoolId) {
//        List<Long> staffId = remoteStaffService.listStaffByDeptId(deptId, nameOrPhone, schoolId).getData();
//        if (CollectionUtil.isEmpty(staffId)) {
//            return page;
//        }
//        List<EduStaffDept> data = remoteStaffService.getStaffDeptByStaffIds(staffId).getData();
//        if (StringUtils.isNotEmpty(start)) {
//            start = start + " 00:00:00";
//        }
//        if (StringUtils.isNotEmpty(end)) {
//            end = end + " 23:59:59";
//        }
//
//        IPage<StaffRecordVO> staffRecordVO = baseMapper.findPageStaffRecord(page, schoolId, start, end, staffId);
//        List<StaffRecordVO> records = staffRecordVO.getRecords();
//        if (CollectionUtil.isEmpty(records)) {
//            return staffRecordVO;
//        }
//
//        for (StaffRecordVO record : records) {
//            for (EduStaffDept staffDept : data) {
//                if (staffDept.getStaffId().equals(record.getStaffId())) {
//                    EduStaff staff = (EduStaff) redisService.hget(STAFF_DATA, staffDept.getStaffId().toString());
//                    if (ObjectUtil.isEmpty(staff)) {
//                        staff = remoteStaffService.getStaff(staffDept.getStaffId()).getData();
//                    }
//                    List<EduStaffDept> staffDeptList = staff.getStaffDept();
//                    StringBuilder deptName = new StringBuilder();
//                    for (int i = 0; i < staffDeptList.size(); i++) {
//                        if (i == staffDeptList.size() - 1) {
//                            deptName.append(staffDeptList.get(i).getDeptName());
//                        } else {
//                            deptName.append(staffDeptList.get(i).getDeptName()).append(",");
//                        }
//                    }
//                    if (ObjectUtil.isEmpty(deptName.toString())) {
//                        deptName = new StringBuilder(staff.getSchoolName());
//                    }
//                    record.setDeptName(deptName.toString());
//                    record.setPhone(staffDept.getMobile());
//                    record.setStaffName(staffDept.getStaffName());
//                    break;
//                }
//            }
//            LocalDateTime time = record.getTime();
//            record.setWeek("星期" + time.getDayOfWeek().getValue());
//        }
//        return staffRecordVO;
//    }
//
//    /**
//     * 定时处理数据
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void staffRecord() {
//
//        LocalDateTime now = LocalDateTime.now();
//
//        LocalDate today = LocalDate.now();
//        //遍历所有规则0进1出
//        List<EduAttendanceStaffRule> staffRules = eduAttendanceStaffRuleService
//            .list(Wrappers.lambdaQuery(new EduAttendanceStaffRule()));
//
//        for (EduAttendanceStaffRule staffRule : staffRules) {
//            //规则对应的职工
//            List<EduAttendanceStaffRuleStaff> ruleStaffs = eduAttendanceStaffRuleStaffService
//                .list(Wrappers.lambdaQuery(new EduAttendanceStaffRuleStaff())
//                    .eq(EduAttendanceStaffRuleStaff::getRuleId, staffRule.getId()));
//            for (EduAttendanceStaffRuleStaff ruleStaff : ruleStaffs) {
//                //职工对应的结果
//
//                List<EduAttendanceStaffRuleTime> ruleTimes = eduAttendanceStaffRuleTimeService
//                    .list(Wrappers.lambdaQuery(new EduAttendanceStaffRuleTime())
//                        .eq(EduAttendanceStaffRuleTime::getRuleId, staffRule.getId())
//                        .orderByAsc(EduAttendanceStaffRuleTime::getStartTime));
//                for (int i = 0; i < ruleTimes.size(); i++) {
//
//                    if (ruleTimes.get(i).getStartTime().atDate(today).isBefore(now)) {
//
//                        if (i == 0) {
//                            //如果这个规则是第一天运行，则不生成昨天的记录
//                            if (today.isEqual(staffRule.getCreateDate().toLocalDate())) {
//                                continue;
//                            }
//
//                            //第一条则处理昨天最后一条数据
//                            EduAttendanceStaffRuleTime ruleTime = ruleTimes.get(ruleTimes.size() - 1);
//                            EduAttendanceStaffResult result = new EduAttendanceStaffResult();
//                            //查询上一天的最后一个时间段
//
//                            EduAttendanceStaffResult one = eduAttendanceStaffResultService
//                                .getOne(Wrappers.lambdaQuery(new EduAttendanceStaffResult())
//                                    .eq(EduAttendanceStaffResult::getStaffId, ruleStaff.getStaffId())
//                                    .eq(EduAttendanceStaffResult::getAttendanceDate, today.minusDays(1))
//                                    .eq(EduAttendanceStaffResult::getRuleTimeId, ruleTimes.get(ruleTimes.size() - 1).getId())
//                                    .eq(EduAttendanceStaffResult::getRuleId, staffRule.getId()));
//
//                            //如果上一天最后一个时间段有结果，则跳过
//                            if (ObjectUtil.isNotEmpty(one)) {
//                                continue;
//                            }
//
//                            //没有则生成结果
//                            one = new EduAttendanceStaffResult();
//                            result.setRuleTimeId(ruleTimes.get(ruleTimes.size() - 1).getId());
//                            result.setRuleId(staffRule.getId());
//                            result.setAttendanceDate(today.minusDays(1));
//                            result.setStartStiTime(ruleTime.getStartTime());
//                            result.setEndStiTime(ruleTime.getEndTime());
//                            result.setSchoolId(staffRule.getSchoolId());
//                            result.setStaffId(ruleStaff.getStaffId());
//
//                            EduAttendanceStaffRecord record = baseMapper
//                                .finMaxRecord(ruleStaff.getStaffId(), ruleTime.getStartTime().atDate(today));
//                            if (ObjectUtil.isEmpty(record)) {
//                                //迟到旷工
//                                List<EduAttendanceStaffRecord> staffRecords = baseMapper
//                                    .findInRecord(ruleStaff.getStaffId(), result.getStartStiTime().atDate(today),
//                                        result.getEndStiTime().atDate(today));
//                                if (CollectionUtil.isEmpty(staffRecords)) {
//                                    result.setStatus(3);
//                                    eduAttendanceStaffResultService.save(result);
//                                    continue;
//                                }
//                                //迟到
//                            } else if (record.getScope() == 0) {
//                                EduAttendanceStaffRecord inRecord = baseMapper
//                                    .fintRecord(ruleStaff.getStaffId(), ruleTime.getStartTime().atDate(today),
//                                        ruleTimes.get(ruleTimes.size() - 1).getEndTime().atDate(today.minusDays(1)));
//                                if (ObjectUtil.isNotEmpty(inRecord)) {
//                                    result.setStartRealTime(record.getCreateTime().toLocalTime());
//                                }
//
//                                //早退和正常
//                                EduAttendanceStaffRecord record2 = baseMapper
//                                    .findMinOutRecord(ruleStaff.getStaffId(), ruleTime.getEndTime().atDate(today.plusDays(-1)));
//                                //如果没有记录则扩大范围到进入，看有没有出记录
//                                if (ObjectUtil.isEmpty(record2)) {
//                                    EduAttendanceStaffRecord record3 = baseMapper.findMaxOutRecord(ruleStaff.getStaffId(),
//                                        ruleTime.getStartTime().atDate(today.plusDays(-1)));
//                                    //如果没有记录，则没有出去，记录为正常
//                                    if (ObjectUtil.isEmpty(record3)) {
//                                        result.setStatus(0);
//                                        eduAttendanceStaffResultService.save(result);
//                                        continue;
//                                    }
//                                    //有记录,则为早退
//                                    result.setStatus(2);
//                                    result.setEndRealTime(record3.getCreateTime().toLocalTime());
//                                    eduAttendanceStaffResultService.save(result);
//                                    continue;
//                                }
//                                result.setStatus(0);
//                                result.setEndRealTime(record2.getCreateTime().toLocalTime());
//                                eduAttendanceStaffResultService.save(result);
//
//                            } else if (record.getScope() == 1) {
//                                //迟到，旷工，迟到早退
//
//                                //查询结束之前没有进入的记录
//                                List<EduAttendanceStaffRecord> staffRecords = baseMapper
//                                    .findInRecord(ruleStaff.getStaffId(), result.getStartStiTime().atDate(today.plusDays(-1)),
//                                        result.getEndStiTime().atDate(today.plusDays(-1)));
//                                //如果没有进入的记录则是旷工
//                                if (CollectionUtil.isEmpty(staffRecords)) {
//                                    result.setStatus(3);
//                                    eduAttendanceStaffResultService.save(result);
//                                    continue;
//                                }
//                                //第一条为最小的一条
//                                result.setStartRealTime(staffRecords.get(0).getCreateTime().toLocalTime());
//                                //查看是否正常下班
//                                EduAttendanceStaffRecord minOutRecord = baseMapper
//                                    .findMinOutRecord(ruleStaff.getStaffId(), result.getEndStiTime().atDate(today.minusDays(1)));
//                                if (ObjectUtil.isEmpty(minOutRecord)) {
//                                    //查看是否早退
//                                    List<EduAttendanceStaffRecord> recordList = baseMapper.findOutRecord(ruleStaff.getStaffId(),
//                                        result.getStartStiTime().atDate(today.minusDays(1)));
//                                    //一直没有出去
//                                    if (CollectionUtil.isEmpty(recordList)) {
//                                        result.setStatus(1);
//                                        eduAttendanceStaffResultService.save(result);
//                                        break;
//                                    }
//                                    //迟到早退
//                                    result.setStatus(4);
//                                    result.setEndRealTime(recordList.get(0).getCreateTime().toLocalTime());
//                                    eduAttendanceStaffResultService.save(result);
//                                    break;
//
//                                }
//                                //迟到
//                                result.setEndRealTime(minOutRecord.getCreateTime().toLocalTime());
//                                result.setStatus(1);
//                                eduAttendanceStaffResultService.save(result);
//                            }
//                        }
//                        //后面一条则生成上一个时间段的结果
//                        else {
//                            EduAttendanceStaffResult one = eduAttendanceStaffResultService
//                                .getOne(Wrappers.lambdaQuery(new EduAttendanceStaffResult())
//                                    .eq(EduAttendanceStaffResult::getStaffId, ruleStaff.getStaffId())
//                                    .eq(EduAttendanceStaffResult::getAttendanceDate, today)
//                                    .eq(EduAttendanceStaffResult::getRuleTimeId, ruleTimes.get(i - 1).getId())
//                                    .eq(EduAttendanceStaffResult::getRuleId, staffRule.getId()));
//                            if (ObjectUtil.isNotEmpty(one)) {
//                                continue;
//                            }
//                            EduAttendanceStaffRuleTime ruleTime = ruleTimes.get(i - 1);
//                            EduAttendanceStaffResult result = new EduAttendanceStaffResult();
//                            result.setRuleTimeId(ruleTimes.get(i - 1).getId());
//                            result.setRuleId(staffRule.getId());
//                            result.setAttendanceDate(today);
//                            result.setStartStiTime(ruleTime.getStartTime());
//                            result.setEndStiTime(ruleTime.getEndTime());
//                            result.setSchoolId(staffRule.getSchoolId());
//                            result.setStaffId(ruleStaff.getStaffId());
//
//                            EduAttendanceStaffRecord record = baseMapper
//                                .finMaxRecord(ruleStaff.getStaffId(), ruleTime.getStartTime().atDate(today));
//                            if (ObjectUtil.isEmpty(record)) {
//                                //迟到旷工
//                                List<EduAttendanceStaffRecord> staffRecords = baseMapper
//                                    .findInRecord(ruleStaff.getStaffId(), result.getStartStiTime().atDate(today),
//                                        result.getEndStiTime().atDate(today));
//                                if (CollectionUtil.isEmpty(staffRecords)) {
//                                    //没有进的记录
//                                    result.setStatus(3);
//                                    eduAttendanceStaffResultService.save(result);
//                                    continue;
//                                }
//                            } else if (record.getScope() == 1) {
//                                //迟到，旷工，迟到早退
//
//                                //查询结束之前没有进入的记录
//                                List<EduAttendanceStaffRecord> staffRecords = baseMapper
//                                    .findInRecord(ruleStaff.getStaffId(), result.getStartStiTime().atDate(today),
//                                        result.getEndStiTime().atDate(today));
//                                //如果没有进入的记录则是旷工
//                                if (CollectionUtil.isEmpty(staffRecords)) {
//                                    result.setStatus(3);
//                                    eduAttendanceStaffResultService.save(result);
//                                    continue;
//                                }
//                                //第一条为最小的一条
//                                result.setStartRealTime(staffRecords.get(0).getCreateTime().toLocalTime());
//                                //查看是否正常下班
//                                EduAttendanceStaffRecord minOutRecord = baseMapper
//                                    .findMinOutRecord(ruleStaff.getStaffId(), result.getEndStiTime().atDate(today));
//                                if (ObjectUtil.isEmpty(minOutRecord)) {
//                                    //查看是否早退
//                                    List<EduAttendanceStaffRecord> recordList = baseMapper
//                                        .findOutRecord(ruleStaff.getStaffId(), result.getStartStiTime().atDate(today));
//                                    //一直没有出去
//                                    if (CollectionUtil.isEmpty(recordList)) {
//                                        result.setStatus(1);
//                                        eduAttendanceStaffResultService.save(result);
//                                        continue;
//                                    }
//                                    //迟到早退
//                                    result.setStatus(4);
//                                    result.setEndRealTime(recordList.get(0).getCreateTime().toLocalTime());
//                                    eduAttendanceStaffResultService.save(result);
//                                    continue;
//
//                                }
//                                //迟到
//                                result.setEndRealTime(minOutRecord.getCreateTime().toLocalTime());
//                                result.setStatus(1);
//                                eduAttendanceStaffResultService.save(result);
//                                continue;
//                            } else if (record.getScope() == 0) {
//                                EduAttendanceStaffRecord inRecord = baseMapper
//                                    .fintRecord(ruleStaff.getStaffId(), ruleTime.getStartTime().atDate(today),
//                                        ruleTimes.get(i - 1).getEndTime().atDate(today));
//                                if (ObjectUtil.isNotEmpty(inRecord)) {
//                                    result.setStartRealTime(inRecord.getCreateTime().toLocalTime());
//                                }
//
//                                //早退和正常
//                                EduAttendanceStaffRecord record2 = baseMapper
//                                    .findMinOutRecord(ruleStaff.getStaffId(), ruleTime.getEndTime().atDate(today));
//                                //如果没有记录则扩大范围到进入，看有没有出记录
//                                if (ObjectUtil.isEmpty(record2)) {
//                                    EduAttendanceStaffRecord record3 = baseMapper
//                                        .findMaxOutRecord(ruleStaff.getStaffId(), ruleTime.getStartTime().atDate(today));
//                                    //如果没有记录，则没有出去，记录为正常
//                                    if (ObjectUtil.isEmpty(record3)) {
//                                        result.setStatus(0);
//                                        eduAttendanceStaffResultService.save(result);
//                                        continue;
//                                    }
//                                    //有记录,则为早退
//                                    result.setStatus(2);
//                                    result.setEndRealTime(record3.getCreateTime().toLocalTime());
//                                    eduAttendanceStaffResultService.save(result);
//                                    continue;
//                                }
//                                result.setStatus(0);
//                                result.setEndRealTime(record2.getCreateTime().toLocalTime());
//                                eduAttendanceStaffResultService.save(result);
//
//                            }
//                        }
//                    }
//                }
//
//            }
//        }
//
//    }
//}