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.WorkOvertimeDeductRuleDO;
import cn.iocoder.yudao.module.system.service.workovertime.WorkOvertimeDeductRuleService;

@Tag(name = "管理后台 - 加班扣除规则")
@RestController
@RequestMapping("/system/work-overtime-deduct-rule")
@Validated
public class WorkOvertimeDeductRuleController {

    @Resource
    private WorkOvertimeDeductRuleService workOvertimeDeductRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建加班扣除规则")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-deduct-rule:create')")
    public CommonResult<Long> createWorkOvertimeDeductRule(@Valid @RequestBody WorkOvertimeDeductRuleSaveReqVO createReqVO) {
        return success(workOvertimeDeductRuleService.createWorkOvertimeDeductRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新加班扣除规则")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-deduct-rule:update')")
    public CommonResult<Boolean> updateWorkOvertimeDeductRule(@Valid @RequestBody WorkOvertimeDeductRuleSaveReqVO updateReqVO) {
        workOvertimeDeductRuleService.updateWorkOvertimeDeductRule(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得加班扣除规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-deduct-rule:query')")
    public CommonResult<WorkOvertimeDeductRuleRespVO> getWorkOvertimeDeductRule(@RequestParam("id") Long id) {
        WorkOvertimeDeductRuleDO workOvertimeDeductRule = workOvertimeDeductRuleService.getWorkOvertimeDeductRule(id);
        return success(BeanUtils.toBean(workOvertimeDeductRule, WorkOvertimeDeductRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得加班扣除规则分页")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-deduct-rule:query')")
    public CommonResult<PageResult<WorkOvertimeDeductRuleRespVO>> getWorkOvertimeDeductRulePage(@Valid WorkOvertimeDeductRulePageReqVO pageReqVO) {
        PageResult<WorkOvertimeDeductRuleDO> pageResult = workOvertimeDeductRuleService.getWorkOvertimeDeductRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkOvertimeDeductRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出加班扣除规则 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-deduct-rule:export')")
    @OperateLog(type = EXPORT)
    public void exportWorkOvertimeDeductRuleExcel(@Valid WorkOvertimeDeductRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkOvertimeDeductRuleDO> list = workOvertimeDeductRuleService.getWorkOvertimeDeductRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "加班扣除规则.xls", "数据", WorkOvertimeDeductRuleRespVO.class,
                        BeanUtils.toBean(list, WorkOvertimeDeductRuleRespVO.class));
    }

}