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

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.punchrecord.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.punchrecord.AttendancePunchRecordDO;
import cn.iocoder.yudao.module.system.service.attendance.punchrecord.AttendancePunchRecordService;

@Tag(name = "管理后台 - 用户打卡记录")
@RestController
@RequestMapping("/system/attendance/punch-record")
@Validated
public class AttendancePunchRecordController {

    @Resource
    private AttendancePunchRecordService punchRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户打卡记录")
    @PreAuthorize("@ss.hasPermission('attendance:punch-record:create')")
    public CommonResult<Long> createPunchRecord(@Valid @RequestBody AttendancePunchRecordSaveReqVO createReqVO) {
        return success(punchRecordService.createPunchRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户打卡记录")
    @PreAuthorize("@ss.hasPermission('attendance:punch-record:update')")
    public CommonResult<Boolean> updatePunchRecord(@Valid @RequestBody AttendancePunchRecordSaveReqVO updateReqVO) {
        punchRecordService.updatePunchRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户打卡记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('attendance:punch-record:delete')")
    public CommonResult<Boolean> deletePunchRecord(@RequestParam("id") Long id) {
        punchRecordService.deletePunchRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户打卡记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:punch-record:query')")
    public CommonResult<AttendancePunchRecordRespVO> getPunchRecord(@RequestParam("id") Long id) {
        AttendancePunchRecordDO punchRecord = punchRecordService.getPunchRecord(id);
        return success(BeanUtils.toBean(punchRecord, AttendancePunchRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户打卡记录分页")
    @PreAuthorize("@ss.hasPermission('attendance:punch-record:query')")
    public CommonResult<PageResult<AttendancePunchRecordRespVO>> getPunchRecordPage(@Valid AttendancePunchRecordPageReqVO pageReqVO) {
        PageResult<AttendancePunchRecordDO> pageResult = punchRecordService.getPunchRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendancePunchRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户打卡记录 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:punch-record:export')")
    @OperateLog(type = EXPORT)
    public void exportPunchRecordExcel(@Valid AttendancePunchRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendancePunchRecordDO> list = punchRecordService.getPunchRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户打卡记录.xls", "数据", AttendancePunchRecordRespVO.class,
                        BeanUtils.toBean(list, AttendancePunchRecordRespVO.class));
    }

}