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

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.fixed.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.fixed.AttendanceFixedDO;
import cn.iocoder.yudao.module.system.service.attendance.fixed.AttendanceFixedService;

@Tag(name = "管理后台 - 固定班制考勤设置")
@RestController
@RequestMapping("/system/attendance/fixed")
@Validated
public class AttendanceFixedController {

    @Resource
    private AttendanceFixedService fixedService;

    @PostMapping("/create")
    @Operation(summary = "创建固定班制考勤设置")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:create')")
    public CommonResult<Long> createFixed(@Valid @RequestBody AttendanceFixedSaveReqVO createReqVO) {
        return success(fixedService.createFixed(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新固定班制考勤设置")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:update')")
    public CommonResult<Boolean> updateFixed(@Valid @RequestBody AttendanceFixedSaveReqVO updateReqVO) {
        fixedService.updateFixed(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除固定班制考勤设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('attendance:fixed:delete')")
    public CommonResult<Boolean> deleteFixed(@RequestParam("id") Long id) {
        fixedService.deleteFixed(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得固定班制考勤设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:query')")
    public CommonResult<AttendanceFixedRespVO> getFixed(@RequestParam("id") Long id) {
        AttendanceFixedDO fixed = fixedService.getFixed(id);
        return success(BeanUtils.toBean(fixed, AttendanceFixedRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得固定班制考勤设置分页")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:query')")
    public CommonResult<PageResult<AttendanceFixedRespVO>> getFixedPage(@Valid AttendanceFixedPageReqVO pageReqVO) {
        PageResult<AttendanceFixedDO> pageResult = fixedService.getFixedPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceFixedRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出固定班制考勤设置 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:export')")
    @OperateLog(type = EXPORT)
    public void exportFixedExcel(@Valid AttendanceFixedPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceFixedDO> list = fixedService.getFixedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "固定班制考勤设置.xls", "数据", AttendanceFixedRespVO.class,
                        BeanUtils.toBean(list, AttendanceFixedRespVO.class));
    }

}