package cn.iocoder.yudao.module.system.controller.admin.attendance;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.attendance.vo.AttendanceStatisticsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.attendance.vo.AttendanceStatisticsRespVO;
import cn.iocoder.yudao.module.system.controller.admin.attendance.vo.AttendanceStatisticsSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.statistics.AttendanceStatisticsDO;
import cn.iocoder.yudao.module.system.service.attendance.statistics.AttendanceStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 考勤统计")
@RestController
@RequestMapping("/system.attendance/attendance-statistics")
@Validated
public class AttendanceStatisticsController {

    @Resource
    private AttendanceStatisticsService attendanceStatisticsService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤统计")
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-statistics:create')")
    public CommonResult<Long> createAttendanceStatistics(@Valid @RequestBody AttendanceStatisticsSaveReqVO createReqVO) {
        return success(attendanceStatisticsService.createAttendanceStatistics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考勤统计")
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-statistics:update')")
    public CommonResult<Boolean> updateAttendanceStatistics(@Valid @RequestBody AttendanceStatisticsSaveReqVO updateReqVO) {
        attendanceStatisticsService.updateAttendanceStatistics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考勤统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-statistics:delete')")
    public CommonResult<Boolean> deleteAttendanceStatistics(@RequestParam("id") Long id) {
        attendanceStatisticsService.deleteAttendanceStatistics(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考勤统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-statistics:query')")
    public CommonResult<AttendanceStatisticsRespVO> getAttendanceStatistics(@RequestParam("id") Long id) {
        AttendanceStatisticsDO attendanceStatistics = attendanceStatisticsService.getAttendanceStatistics(id);
        return success(BeanUtils.toBean(attendanceStatistics, AttendanceStatisticsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤统计分页")
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-statistics:query')")
    public CommonResult<PageResult<AttendanceStatisticsRespVO>> getAttendanceStatisticsPage(@Valid AttendanceStatisticsPageReqVO pageReqVO) {
        PageResult<AttendanceStatisticsDO> pageResult = attendanceStatisticsService.getAttendanceStatisticsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceStatisticsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤统计 Excel")
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-statistics:export')")
    @OperateLog(type = EXPORT)
    public void exportAttendanceStatisticsExcel(@Valid AttendanceStatisticsPageReqVO pageReqVO,
                                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceStatisticsDO> list = attendanceStatisticsService.getAttendanceStatisticsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤统计.xls", "数据", AttendanceStatisticsRespVO.class,
                BeanUtils.toBean(list, AttendanceStatisticsRespVO.class));
    }

}