package cn.iocoder.yudao.module.system.api.attendance;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.module.system.api.attendance.dto.AttendancePunchRecordDTO;
import cn.iocoder.yudao.module.system.api.attendance.dto.AttendanceTimeRangeInfoDTO;
import cn.iocoder.yudao.module.system.api.attendance.vo.AttendancePunchRecordVO;
import cn.iocoder.yudao.module.system.api.attendance.vo.AttendanceTimeRangeInfoVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.punchrecord.AttendancePunchRecordDO;
import cn.iocoder.yudao.module.system.service.attendance.AttendanceService;
import cn.iocoder.yudao.module.system.service.attendance.punchrecord.AttendancePunchRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class AttendanceApiImpl implements AttendanceApi {

    @Resource
    private AttendanceService attendanceService;

    @Resource
    private AttendancePunchRecordService attendancePunchRecordService;

    @Override
    public CommonResult askingForLeaveAfterwardsToModifyAttendance(AttendancePunchRecordDTO attendancePunchRecordDTO) {
        attendancePunchRecordService.askingForLeaveAfterwardsToModifyAttendance(attendancePunchRecordDTO);
        return success("ok");
    }

    @Override
    public CommonResult<Integer> getReplacementCardNum() {
        return success(attendanceService.getReplacementCardNum());
    }

    @Override
    public CommonResult<Boolean> replacementCard(List<Long> punchRecordIds) {
        attendanceService.replacementCardBatch(punchRecordIds);
        return success(true);
    }


    @Override
    public CommonResult<Map<String, AttendanceTimeRangeInfoVO>> getAttendanceInfoByTimeRange(AttendanceTimeRangeInfoDTO attendanceTimeRangeInfoDTO) {
        return CommonResult.success(attendanceService.getAttendanceInfoByTimeRange(attendanceTimeRangeInfoDTO));
    }

    @Override
    public CommonResult<Map<String, List<AttendancePunchRecordVO>>> getAttendancePunchRecordByTimeRange(AttendancePunchRecordDTO dto) {
        List<String> dateList = DateUtils.betweenDayList(dto.getStartTime(), dto.getEndTime());
        List<AttendancePunchRecordDO> list = attendancePunchRecordService.list(new LambdaQueryWrapper<AttendancePunchRecordDO>()
                .eq(AttendancePunchRecordDO::getUserId, dto.getUserId())
                .in(AttendancePunchRecordDO::getDayTime, dateList));
        List<AttendancePunchRecordVO> vos = BeanUtil.copyToList(list, AttendancePunchRecordVO.class);
        Map<String, List<AttendancePunchRecordVO>> map = vos.stream().collect(Collectors.groupingBy(AttendancePunchRecordVO::getDayTime));
        return CommonResult.success(map);
    }
}
