package cn.com.oa.service.impl;

import cn.com.oa.common.constant.DataService;
import cn.com.oa.mapper.AttendanceRecordResultMapper;
import cn.com.oa.model.AttendanceRecord;
import cn.com.oa.model.AttendanceRecordResult;
import cn.com.oa.model.DTO.AttendanceRecordDTO;
import cn.com.oa.model.Group;
import cn.com.oa.model.User;
import cn.com.oa.model.vo.AttendanceRecordResultVo;
import cn.com.oa.service.AttendanceRecordService;
import cn.com.oa.service.IAttendanceRecordResultService;
import cn.com.oa.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class AttendanceRecordResultServiceImpl extends ServiceImpl<AttendanceRecordResultMapper, AttendanceRecordResult> implements IAttendanceRecordResultService {

    @Autowired
    private UserService userService;
    @Autowired
    private AttendanceRecordService attendanceRecordService;

    @Override
    public Map<String, Object> getResultMonth() {
        Map constantData = DataService.getConstantData();
        Group group = (Group)constantData.get("defaultGroup");
        AttendanceRecordResultVo attendanceRecordResult = new AttendanceRecordResultVo();
        attendanceRecordResult.setUnitId(group.getUNIVERSALID());
        Map<String, Object> map = baseMapper.getResultMonth(attendanceRecordResult);
        return map;
    }

    @Override
    public AttendanceRecordResultVo getResultDay() {
        Map constantData = DataService.getConstantData();
        Group group = (Group)constantData.get("defaultGroup");
        User user = new User();
        user.setUnitid(group.getUNIVERSALID());
        user.setUserState(1);
        // 应到人数
        int totalCount = userService.getCount(user);
        AttendanceRecord attendanceRecord = new AttendanceRecord();
        attendanceRecord.setUnitId(group.getUNIVERSALID());
        // 当日打卡数据
        List<AttendanceRecordDTO> recordList = attendanceRecordService.getRecordList(attendanceRecord);
        // 已打卡人数
        int recordCount = recordList.size();
        // 实际到人数
        int actualCount = recordCount;
        // 迟到人数
        int lateCount = 0;
        // 早退人数
        int leaveEarlyCount = 0;
        // 缺勤人数
        int absenceCount = totalCount - recordCount;
        // 迟到时间和早退时间
        LocalTime lateThreshold = LocalTime.of(9, 0, 0);
        LocalTime leaveEarlyThreshold = LocalTime.of(17, 30, 0);
        for (AttendanceRecordDTO record : recordList) {

            // 将 Date 转换为 LocalTime
            LocalTime firstTime = record.getFirstAttTime().toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
            LocalTime lastTime = record.getLastAttTime().toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
            // 标记是否已经减少 actualCount
            boolean flag = false;
            // 迟到处理
            if (firstTime.isAfter(lateThreshold)) {
                lateCount++;
                if (!flag) {
                    actualCount--;
                    flag = true;
                }
            }
            // 只打卡一次的跳过
            if (firstTime.equals(lastTime)) continue;
            // 早退处理
            if (lastTime.isBefore(leaveEarlyThreshold)) {
                leaveEarlyCount++;
                if (!flag) {
                    actualCount--;
                }
            }
        }
        AttendanceRecordResultVo attendanceRecordResultVo = new AttendanceRecordResultVo();
        attendanceRecordResultVo.setRecordCount(recordCount);
        attendanceRecordResultVo.setActualCount(actualCount);
        attendanceRecordResultVo.setLateCount(lateCount);
        attendanceRecordResultVo.setLeaveEarlyCount(leaveEarlyCount);
        attendanceRecordResultVo.setAbsenceCount(absenceCount);
        return attendanceRecordResultVo;
    }
}
