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

import cn.iocoder.yudao.module.system.controller.admin.attendance.vo.AttendanceFactoryPunchRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.attendance.vo.AttendanceFactoryPunchRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.attendance.vo.AttendanceFactoryPunchRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.factorypunchrecord.AttendanceFactoryPunchRecordDO;
import cn.iocoder.yudao.module.system.service.attendance.factorypunchrecord.AttendanceFactoryPunchRecordService;
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.constraints.*;
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.*;


@Tag(name = "管理后台 - 工厂员工打卡记录")
@RestController
@RequestMapping("/system/attendance-factory-punch-record")
@Validated
public class AttendanceFactoryPunchRecordController {

    @Resource
    private AttendanceFactoryPunchRecordService attendanceFactoryPunchRecordService;

    @PutMapping("/update")
    @Operation(summary = "更新工厂员工打卡记录")
    @PreAuthorize("@ss.hasPermission('system:attendance-factory-punch-record:update')")
    public CommonResult<Boolean> updateAttendanceFactoryPunchRecord(@Valid @RequestBody AttendanceFactoryPunchRecordSaveReqVO updateReqVO) {
        attendanceFactoryPunchRecordService.updateAttendanceFactoryPunchRecord(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂员工打卡记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:attendance-factory-punch-record:query')")
    public CommonResult<AttendanceFactoryPunchRecordRespVO> getAttendanceFactoryPunchRecord(@RequestParam("id") Long id) {
        AttendanceFactoryPunchRecordDO attendanceFactoryPunchRecord = attendanceFactoryPunchRecordService.getAttendanceFactoryPunchRecord(id);
        return success(BeanUtils.toBean(attendanceFactoryPunchRecord, AttendanceFactoryPunchRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂员工打卡记录分页")
    @PreAuthorize("@ss.hasPermission('system:attendance-factory-punch-record:query')")
    public CommonResult<PageResult<AttendanceFactoryPunchRecordRespVO>> getAttendanceFactoryPunchRecordPage(@Valid AttendanceFactoryPunchRecordPageReqVO pageReqVO) {
        PageResult<AttendanceFactoryPunchRecordDO> pageResult = attendanceFactoryPunchRecordService.getAttendanceFactoryPunchRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceFactoryPunchRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂员工打卡记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:attendance-factory-punch-record:export')")
    @OperateLog(type = EXPORT)
    public void exportAttendanceFactoryPunchRecordExcel(@Valid AttendanceFactoryPunchRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceFactoryPunchRecordDO> list = attendanceFactoryPunchRecordService.getAttendanceFactoryPunchRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工厂员工打卡记录.xls", "数据", AttendanceFactoryPunchRecordRespVO.class,
                        BeanUtils.toBean(list, AttendanceFactoryPunchRecordRespVO.class));
    }

}
