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

@Tag(name = "管理后台 - 加班规则子表(按日类型区分)")
@RestController
@RequestMapping("/system/work-overtime-rule-item")
@Validated
public class WorkOvertimeRuleItemController {

    @Resource
    private WorkOvertimeRuleItemService workOvertimeRuleItemService;

    @PostMapping("/create")
    @Operation(summary = "创建加班规则子表(按日类型区分)")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-rule-item:create')")
    public CommonResult<Long> createWorkOvertimeRuleItem(@Valid @RequestBody WorkOvertimeRuleItemSaveReqVO createReqVO) {
        return success(workOvertimeRuleItemService.createWorkOvertimeRuleItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新加班规则子表(按日类型区分)")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-rule-item:update')")
    public CommonResult<Boolean> updateWorkOvertimeRuleItem(@Valid @RequestBody WorkOvertimeRuleItemSaveReqVO updateReqVO) {
        workOvertimeRuleItemService.updateWorkOvertimeRuleItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除加班规则子表(按日类型区分)")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-overtime-rule-item:delete')")
    public CommonResult<Boolean> deleteWorkOvertimeRuleItem(@RequestParam("id") Long id) {
        workOvertimeRuleItemService.deleteWorkOvertimeRuleItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得加班规则子表(按日类型区分)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-rule-item:query')")
    public CommonResult<WorkOvertimeRuleItemRespVO> getWorkOvertimeRuleItem(@RequestParam("id") Long id) {
        WorkOvertimeRuleItemDO workOvertimeRuleItem = workOvertimeRuleItemService.getWorkOvertimeRuleItem(id);
        return success(BeanUtils.toBean(workOvertimeRuleItem, WorkOvertimeRuleItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得加班规则子表(按日类型区分)分页")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-rule-item:query')")
    public CommonResult<PageResult<WorkOvertimeRuleItemRespVO>> getWorkOvertimeRuleItemPage(@Valid WorkOvertimeRuleItemPageReqVO pageReqVO) {
        PageResult<WorkOvertimeRuleItemDO> pageResult = workOvertimeRuleItemService.getWorkOvertimeRuleItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkOvertimeRuleItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出加班规则子表(按日类型区分) Excel")
    @PreAuthorize("@ss.hasPermission('system:work-overtime-rule-item:export')")
    @OperateLog(type = EXPORT)
    public void exportWorkOvertimeRuleItemExcel(@Valid WorkOvertimeRuleItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkOvertimeRuleItemDO> list = workOvertimeRuleItemService.getWorkOvertimeRuleItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "加班规则子表(按日类型区分).xls", "数据", WorkOvertimeRuleItemRespVO.class,
                        BeanUtils.toBean(list, WorkOvertimeRuleItemRespVO.class));
    }

}