package cn.iocoder.yudao.module.member.controller.admin.holiday;

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.member.controller.admin.holiday.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.holiday.HolidayDO;
import cn.iocoder.yudao.module.member.service.holiday.HolidayService;

@Tag(name = "管理后台 - 假期")
@RestController
@RequestMapping("/member/holiday")
@Validated
public class HolidayController {

    @Resource
    private HolidayService holidayService;

    @PostMapping("/create")
    @Operation(summary = "创建假期")
    @PreAuthorize("@ss.hasPermission('member:holiday:create')")
    public CommonResult<Long> createHoliday(@Valid @RequestBody HolidaySaveReqVO createReqVO) {
        return success(holidayService.createHoliday(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新假期")
    @PreAuthorize("@ss.hasPermission('member:holiday:update')")
    public CommonResult<Boolean> updateHoliday(@Valid @RequestBody HolidaySaveReqVO updateReqVO) {
        holidayService.updateHoliday(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除假期")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:holiday:delete')")
    public CommonResult<Boolean> deleteHoliday(@RequestParam("id") Long id) {
        holidayService.deleteHoliday(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得假期")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:holiday:query')")
    public CommonResult<HolidayRespVO> getHoliday(@RequestParam("id") Long id) {
        HolidayDO holiday = holidayService.getHoliday(id);
        return success(BeanUtils.toBean(holiday, HolidayRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得假期分页")
    @PreAuthorize("@ss.hasPermission('member:holiday:query')")
    public CommonResult<PageResult<HolidayRespVO>> getHolidayPage(@Valid HolidayPageReqVO pageReqVO) {
        PageResult<HolidayDO> pageResult = holidayService.getHolidayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HolidayRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出假期 Excel")
    @PreAuthorize("@ss.hasPermission('member:holiday:export')")
    @OperateLog(type = EXPORT)
    public void exportHolidayExcel(@Valid HolidayPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HolidayDO> list = holidayService.getHolidayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "假期.xls", "数据", HolidayRespVO.class,
                        BeanUtils.toBean(list, HolidayRespVO.class));
    }

}