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.AddAttendanceRecordDTO;
import com.yida.data.attendance.dto.AttendanceCountPageDTO;
import com.yida.data.attendance.dto.ListErrorRecordDTO;
import com.yida.data.attendance.dto.PageAttendanceRecordDTO;
import com.yida.data.attendance.service.AttendanceRecordService;
import com.yida.data.attendance.service.ErrorRecordMsgService;
import com.yida.data.attendance.vo.DownLoadAttendanceResultVO;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.ErrorRecordMsg;
import com.yida.data.common.core.entity.attendance.attendance.AttendanceRecord;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.log.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
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
@Validated
@RestController
@RequestMapping("/attendanceRecord")
@RequiredArgsConstructor
@Api(tags = "考勤-记录", value = "考勤记录")
public class AttendanceRecordController {

    private final ErrorRecordMsgService errorRecordMsgService;
    private final AttendanceRecordService attendanceRecordService;
    private final RedisService redisService;

    @ApiOperation("增加考勤记录")
    @PostMapping("/saveRecord")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "增加考勤记录", type = OperationLogTypeEnum.INSERT)
    public ResultBean addRecord(@RequestBody AddAttendanceRecordDTO dto) {
        attendanceRecordService.addRecord(dto);
        return ResultBean.buildSuccess();
    }


    @ApiOperation("分页查询通讯记录")
    @PostMapping("/pageListRecord")
    public ResultBean<IPage<AttendanceRecord>> pageListRecord(@RequestBody PageAttendanceRecordDTO dto) {
        return ResultBean.buildSuccess(attendanceRecordService.listRecordPage(dto));
    }

    @ApiOperation("分页查询通行记录错误记录")
    @PostMapping("/listErrorRecord")
    public ResultBean<Page<ErrorRecordMsg>> listErrorRecord(@RequestBody ListErrorRecordDTO dto) {
        return ResultBean.buildSuccess(errorRecordMsgService.listErrorRecord(dto));
    }

    @ApiOperation("批量删除通行记录错误记录")
    @PostMapping("/batchDeleteErrorRecord")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "批量删除通行记录错误记录", type = OperationLogTypeEnum.DELETE)
    public ResultBean<String> batchDeleteErrorRecord(@RequestBody String[] recordIds) {
        errorRecordMsgService.batchDeleteErrorRecord(recordIds);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("通行记录导出")
    @PostMapping("/downloadAttendanceRecord")
    public ResultBean<DownLoadAttendanceResultVO> downloadAttendanceRecord(@RequestBody PageAttendanceRecordDTO dto) {
        String uuid = UUID.fastUUID().toString();
        attendanceRecordService.downloadAttendanceRecord(dto, uuid);
        return ResultBean.buildSuccess(DownLoadAttendanceResultVO.builder()
            .key(uuid)
            .finish(0)
            .totalNum(100)
            .currentNum(1).build());
    }

    @ApiOperation("获取通行记录导出进度")
    @GetMapping("/getDownloadAttendanceRecordProcess")
    public ResultBean<DownLoadAttendanceResultVO> getDownloadAttendanceRecordProcess(String key) {
        return ResultBean.buildSuccess(
            (DownLoadAttendanceResultVO) redisService.hget(CachePrefixConstant.ATTENDANCE_RECORD_DOWNLOAD, key));
    }
}
