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

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.scheduling.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.scheduling.AttendanceSchedulingDO;
import cn.iocoder.yudao.module.system.service.attendance.scheduling.AttendanceSchedulingService;

@Tag(name = "管理后台 - 排班制考勤设置")
@RestController
@RequestMapping("/system/attendance/scheduling")
@Validated
public class AttendanceSchedulingController {

    @Resource
    private AttendanceSchedulingService schedulingService;

    @PostMapping("/create")
    @Operation(summary = "创建排班制考勤设置")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling:create')")
    public CommonResult<Long> createScheduling(@Valid @RequestBody AttendanceSchedulingSaveReqVO createReqVO) {
        return success(schedulingService.createScheduling(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排班制考勤设置")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling:update')")
    public CommonResult<Boolean> updateScheduling(@Valid @RequestBody AttendanceSchedulingSaveReqVO updateReqVO) {
        schedulingService.updateScheduling(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得排班制考勤设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling:query')")
    public CommonResult<AttendanceSchedulingRespVO> getScheduling(@RequestParam("id") Long id) {
        AttendanceSchedulingDO scheduling = schedulingService.getScheduling(id);
        return success(BeanUtils.toBean(scheduling, AttendanceSchedulingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排班制考勤设置分页")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling:query')")
    public CommonResult<PageResult<AttendanceSchedulingRespVO>> getSchedulingPage(@Valid AttendanceSchedulingPageReqVO pageReqVO) {
        PageResult<AttendanceSchedulingDO> pageResult = schedulingService.getSchedulingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceSchedulingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排班制考勤设置 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling:export')")
    @OperateLog(type = EXPORT)
    public void exportSchedulingExcel(@Valid AttendanceSchedulingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceSchedulingDO> list = schedulingService.getSchedulingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "排班制考勤设置.xls", "数据", AttendanceSchedulingRespVO.class,
                        BeanUtils.toBean(list, AttendanceSchedulingRespVO.class));
    }

}