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

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.holidayworkingage.vo.HolidayWorkingAgePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidayworkingage.vo.HolidayWorkingAgeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidayworkingage.vo.HolidayWorkingAgeSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidayworkingage.HolidayWorkingAgeDO;
import cn.iocoder.yudao.module.system.service.holiday.holidayworkingage.HolidayWorkingAgeService;
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-working-age")
@Validated
public class HolidayWorkingAgeController {

    @Resource
    private HolidayWorkingAgeService holidayWorkingAgeService;

    @PostMapping("/create")
    @Operation(summary = "创建假期额度工龄配置")
    public CommonResult<Long> createHolidayWorkingAge(@Valid @RequestBody HolidayWorkingAgeSaveReqVO createReqVO) {
        return success(holidayWorkingAgeService.createHolidayWorkingAge(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新假期额度工龄配置")
    public CommonResult<Boolean> updateHolidayWorkingAge(@Valid @RequestBody HolidayWorkingAgeSaveReqVO updateReqVO) {
        holidayWorkingAgeService.updateHolidayWorkingAge(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除假期额度工龄配置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteHolidayWorkingAge(@RequestParam("id") Long id) {
        holidayWorkingAgeService.deleteHolidayWorkingAge(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得假期额度工龄配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<HolidayWorkingAgeRespVO> getHolidayWorkingAge(@RequestParam("id") Long id) {
        HolidayWorkingAgeDO holidayWorkingAge = holidayWorkingAgeService.getHolidayWorkingAge(id);
        return success(BeanUtils.toBean(holidayWorkingAge, HolidayWorkingAgeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得假期额度工龄配置分页")
    public CommonResult<PageResult<HolidayWorkingAgeRespVO>> getHolidayWorkingAgePage(@Valid HolidayWorkingAgePageReqVO pageReqVO) {
        PageResult<HolidayWorkingAgeDO> pageResult = holidayWorkingAgeService.getHolidayWorkingAgePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HolidayWorkingAgeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出假期额度工龄配置 Excel")
    @OperateLog(type = EXPORT)
    public void exportHolidayWorkingAgeExcel(@Valid HolidayWorkingAgePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HolidayWorkingAgeDO> list = holidayWorkingAgeService.getHolidayWorkingAgePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "假期额度工龄配置.xls", "数据", HolidayWorkingAgeRespVO.class,
                        BeanUtils.toBean(list, HolidayWorkingAgeRespVO.class));
    }

}
