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

import org.springframework.web.bind.annotation.*;
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 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.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.attendance.controller.admin.schedulingmanage.vo.*;
import cn.iocoder.yudao.module.attendance.dal.dataobject.schedulingmanage.AttendanceSchedulingManageDO;
import cn.iocoder.yudao.module.attendance.service.schedulingmanage.AttendanceSchedulingManageService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 考勤排班管理")
@RestController
@RequestMapping("/personnel/attendance/scheduling-manage")
@Validated
public class AttendanceSchedulingManageController {

    @Resource
    private AttendanceSchedulingManageService schedulingManageService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤排班管理")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling-manage:create')")
    public CommonResult<Long> createSchedulingManage(@Valid @RequestBody AttendanceSchedulingManageSaveReqVO createReqVO) {
        return success(schedulingManageService.createSchedulingManage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考勤排班管理")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling-manage:update')")
    public CommonResult<Boolean> updateSchedulingManage(@Valid @RequestBody AttendanceSchedulingManageSaveReqVO updateReqVO) {
        schedulingManageService.updateSchedulingManage(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得考勤排班管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling-manage:query')")
    public CommonResult<AttendanceSchedulingManageRespVO> getSchedulingManage(@RequestParam("id") Long id) {
        AttendanceSchedulingManageDO schedulingManage = schedulingManageService.getSchedulingManage(id);
        return success(BeanUtils.toBean(schedulingManage, AttendanceSchedulingManageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤排班管理分页")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling-manage:query')")
    public CommonResult<PageResult<AttendanceSchedulingManageRespVO>> getSchedulingManagePage(@Valid AttendanceSchedulingManagePageReqVO pageReqVO) {
        PageResult<AttendanceSchedulingManageDO> pageResult = schedulingManageService.getSchedulingManagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceSchedulingManageRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得考勤排班管理列表")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling-manage:query')")
    public CommonResult<List<AttendanceSchedulingManageRespVO>> getSchedulingManageList(@Valid AttendanceSchedulingManagePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceSchedulingManageDO> list = schedulingManageService.getSchedulingManagePage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, AttendanceSchedulingManageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤排班管理 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:scheduling-manage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSchedulingManageExcel(@Valid AttendanceSchedulingManagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceSchedulingManageDO> list = schedulingManageService.getSchedulingManagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤排班管理.xls", "数据", AttendanceSchedulingManageRespVO.class,
                        BeanUtils.toBean(list, AttendanceSchedulingManageRespVO.class));
    }

}