package com.yida.data.attendance.controller;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.attendance.dto.AttendanceCountPageDTO;
import com.yida.data.attendance.dto.AttendanceDayReportDTO;
import com.yida.data.attendance.service.AttendanceResultService;
import com.yida.data.attendance.vo.AttdanceResultDetailVO;
import com.yida.data.attendance.vo.AttdanceResultTotalVO;
import com.yida.data.attendance.vo.AttdanceResultVO;
import com.yida.data.attendance.vo.DownLoadAttendanceResultVO;
import com.yida.data.attendance.vo.StaRecordCountVO;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.system.feign.RemoteDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 考勤-考勤结果 Controller
 *
 * @date 2023-08-09 11:37:25
 */
@Slf4j
@RestController
@RequestMapping("/attendanceResult")
@RequiredArgsConstructor
@Api(tags = "考勤-结果", value = "考勤结果")
public class AttendanceResultController {

    private final AttendanceResultService attendanceResultService;

    private final RemoteDeptService remoteDeptService;

    private final RedisService redisService;

    @ApiOperation("考勤-概况统计")
    @PostMapping("/overview/sta")
    public ResultBean<IPage<AttdanceResultVO>> listResultPage(@RequestBody @Validated AttendanceCountPageDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.listResultPage(dto));
    }

    @ApiOperation("考勤-概况统计")
    @PostMapping("/overview/total")
    public ResultBean<AttdanceResultTotalVO> total(@RequestBody @Validated AttendanceCountPageDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.total(dto));
    }


    @ApiOperation("考勤-明细")
    @PostMapping("/detail")
    public ResultBean<IPage<AttdanceResultDetailVO>> listResultDetail(@RequestBody AttendanceCountPageDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.listResultDetailPage(dto));
    }

    @ApiOperation("dashboard-考勤概况")
    @GetMapping("/staRecordCount")
    public ResultBean<StaRecordCountVO> staRecordCount(@ApiParam("yyyy-MM-dd") String startDate,
        @ApiParam("yyyy-MM-dd") String endDate) {
        List<Long> schoolIds = remoteDeptService.getSchoolByArea(null, null).getData().stream().map(Dept::getDeptId)
            .collect(Collectors.toList());
        return ResultBean.buildSuccess(
            attendanceResultService.staRecordCount(LocalDate.parse(startDate), LocalDate.parse(endDate), schoolIds));
    }

    @ApiOperation("考勤结果导出")
    @PostMapping("/downloadAttendanceResult")
    public ResultBean<DownLoadAttendanceResultVO> downLocalAttendanceResult(@RequestBody AttendanceCountPageDTO dto) {
        String uuid = UUID.fastUUID().toString();
        attendanceResultService.downLocalAttendanceResult(dto, uuid);
        return ResultBean.buildSuccess(DownLoadAttendanceResultVO.builder()
            .key(uuid)
            .finish(0)
            .totalNum(100)
            .currentNum(0).build());
    }


    @ApiOperation("获取考勤结果导出进度")
    @GetMapping("/getDownloadAttendanceResultProcess")
    public ResultBean<DownLoadAttendanceResultVO> getDownLocalAttendanceResultProcess(String key) {
        return ResultBean.buildSuccess(
            (DownLoadAttendanceResultVO) redisService.hget(CachePrefixConstant.STUDENT_DORM_ATTENDANCE_RESULT_DOWNLOAD, key));
    }

    @ApiOperation("考勤日报导出")
    @PostMapping("/downloadAttendanceDayReport")
    public ResultBean<DownLoadAttendanceResultVO> downloadAttendanceDayReport(@RequestBody AttendanceDayReportDTO dto) {
        String uuid = UUID.fastUUID().toString();
        attendanceResultService.downLocalAttendanceDayReport(dto, uuid);
        return ResultBean.buildSuccess(DownLoadAttendanceResultVO.builder()
            .key(uuid)
            .finish(0)
            .totalNum(100)
            .currentNum(0).build());
    }

    @ApiOperation("获取考勤日报导出进度")
    @GetMapping("/getDownloadAttendanceDayReportProcess")
    public ResultBean<DownLoadAttendanceResultVO> getDownloadAttendanceDayReportProcess(String key) {
        return ResultBean.buildSuccess(
            (DownLoadAttendanceResultVO) redisService.hget(CachePrefixConstant.ATTENDANCE_DAY_REPORT_DOWNLOAD, key));
    }

}
