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

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.*;

import cn.iocoder.yudao.module.system.controller.admin.workovertime.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workovertime.WorkOvertimeMoreRuleDO;
import cn.iocoder.yudao.module.system.service.workovertime.WorkOvertimeMoreRuleService;

@Tag(name = "管理后台 - 加班规则（更多规则）")
@RestController
@RequestMapping("/system/work-overtime-more-rule")
@Validated
public class WorkOvertimeMoreRuleController {

    @Resource
    private WorkOvertimeMoreRuleService workOvertimeMoreRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建加班规则（更多规则）")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-more-rule:create')")
    public CommonResult<Long> createWorkOvertimeMoreRule(@Valid @RequestBody WorkOvertimeMoreRuleSaveReqVO createReqVO) {
        return success(workOvertimeMoreRuleService.createWorkOvertimeMoreRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新加班规则（更多规则）")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-more-rule:update')")
    public CommonResult<Boolean> updateWorkOvertimeMoreRule(@Valid @RequestBody WorkOvertimeMoreRuleSaveReqVO updateReqVO) {
        workOvertimeMoreRuleService.updateWorkOvertimeMoreRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除加班规则（更多规则）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-overtime-more-rule:delete')")
    public CommonResult<Boolean> deleteWorkOvertimeMoreRule(@RequestParam("id") Long id) {
        workOvertimeMoreRuleService.deleteWorkOvertimeMoreRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得加班规则（更多规则）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-more-rule:query')")
    public CommonResult<WorkOvertimeMoreRuleRespVO> getWorkOvertimeMoreRule(@RequestParam("id") Long id) {
        WorkOvertimeMoreRuleDO workOvertimeMoreRule = workOvertimeMoreRuleService.getWorkOvertimeMoreRule(id);
        return success(BeanUtils.toBean(workOvertimeMoreRule, WorkOvertimeMoreRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得加班规则（更多规则）分页")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-more-rule:query')")
    public CommonResult<PageResult<WorkOvertimeMoreRuleRespVO>> getWorkOvertimeMoreRulePage(@Valid WorkOvertimeMoreRulePageReqVO pageReqVO) {
        PageResult<WorkOvertimeMoreRuleDO> pageResult = workOvertimeMoreRuleService.getWorkOvertimeMoreRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkOvertimeMoreRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出加班规则（更多规则） Excel")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-more-rule:export')")
    @OperateLog(type = EXPORT)
    public void exportWorkOvertimeMoreRuleExcel(@Valid WorkOvertimeMoreRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkOvertimeMoreRuleDO> list = workOvertimeMoreRuleService.getWorkOvertimeMoreRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "加班规则（更多规则）.xls", "数据", WorkOvertimeMoreRuleRespVO.class,
                        BeanUtils.toBean(list, WorkOvertimeMoreRuleRespVO.class));
    }

}