package cn.iocoder.yudao.module.system.controller.admin.holiday.holidaysettingrange;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidaysettingrange.vo.HolidaySettingRangePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidaysettingrange.vo.HolidaySettingRangeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidaysettingrange.vo.HolidaySettingRangeSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidaysettingrange.HolidaySettingRangeDO;
import cn.iocoder.yudao.module.system.service.holiday.holidaysettingrange.HolidaySettingRangeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 假期规则人员范围")
@RestController
@RequestMapping("/system/holiday-setting-range")
@Validated
public class HolidaySettingRangeController {

    @Resource
    private HolidaySettingRangeService holidaySettingRangeService;

    @PostMapping("/create")
    @Operation(summary = "创建假期规则人员范围")
    public CommonResult<Long> createHolidaySettingRange(@Valid @RequestBody HolidaySettingRangeSaveReqVO createReqVO) {
        return success(holidaySettingRangeService.createHolidaySettingRange(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新假期规则人员范围")
    public CommonResult<Boolean> updateHolidaySettingRange(@Valid @RequestBody HolidaySettingRangeSaveReqVO updateReqVO) {
        holidaySettingRangeService.updateHolidaySettingRange(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除假期规则人员范围")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteHolidaySettingRange(@RequestParam("id") Long id) {
        holidaySettingRangeService.deleteHolidaySettingRange(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得假期规则人员范围")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<HolidaySettingRangeRespVO> getHolidaySettingRange(@RequestParam("id") Long id) {
        HolidaySettingRangeDO holidaySettingRange = holidaySettingRangeService.getHolidaySettingRange(id);
        return success(BeanUtils.toBean(holidaySettingRange, HolidaySettingRangeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得假期规则人员范围分页")
    public CommonResult<PageResult<HolidaySettingRangeRespVO>> getHolidaySettingRangePage(@Valid HolidaySettingRangePageReqVO pageReqVO) {
        PageResult<HolidaySettingRangeDO> pageResult = holidaySettingRangeService.getHolidaySettingRangePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HolidaySettingRangeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出假期规则人员范围 Excel")
    @OperateLog(type = EXPORT)
    public void exportHolidaySettingRangeExcel(@Valid HolidaySettingRangePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HolidaySettingRangeDO> list = holidaySettingRangeService.getHolidaySettingRangePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "假期规则人员范围.xls", "数据", HolidaySettingRangeRespVO.class,
                        BeanUtils.toBean(list, HolidaySettingRangeRespVO.class));
    }

}
