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

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.bpm.api.oa.BpmOAReplacementCardApi;
import cn.iocoder.yudao.module.system.controller.admin.punchrecord.vo.AttendancePunchRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.punchrecord.vo.AttendancePunchRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.punchrecord.vo.AttendancePunchRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.punchrecord.AttendancePunchRecordDO;
import cn.iocoder.yudao.module.system.service.attendance.punchrecord.AttendancePunchRecordService;
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/punch-record")
@Validated
public class AttendancePunchRecordController {

    @Resource
    private AttendancePunchRecordService punchRecordService;

    @Resource
    private BpmOAReplacementCardApi replacementCardApi;

    @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("/getMissingCardRecord")
    @Operation(summary = "获得本月用户缺卡记录")
//    @PreAuthorize("@ss.hasPermission('attendance:punch-record:query')")
    public CommonResult<List<AttendancePunchRecordRespVO>> getPunchRecord() {

        // 获取当前用户已申请的补卡记录
        List<Long> punchRecordIds = replacementCardApi.getPunchRecordIds().getCheckedData();
        // 获得本月用户缺卡记录
        List<AttendancePunchRecordDO> punchRecords = punchRecordService.getMissingCardRecord(punchRecordIds);

        return success(BeanUtils.toBean(punchRecords, 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));
    }

}
