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

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.holidaysetting.vo.HolidaySettingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidaysetting.vo.HolidaySettingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidaysetting.vo.HolidaySettingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidaysetting.HolidaySettingDO;
import cn.iocoder.yudao.module.system.service.holiday.holidaysetting.HolidaySettingService;
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 com.alibaba.fastjson.JSONObject;

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")
@Validated
public class HolidaySettingController {

    @Resource
    private HolidaySettingService holidaySettingService;

    @PostMapping("/createOrEdit")
    @Operation(summary = "创建修改假期设置")
    public CommonResult<Long> createOrEdit(@Valid @RequestBody HolidaySettingSaveReqVO createReqVO) {
        return success(holidaySettingService.createOrEdit(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除假期设置")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteHolidaySetting(@RequestParam("id") Long id) {
        holidaySettingService.deleteHolidaySetting(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得假期设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<HolidaySettingRespVO> getHolidaySetting(@RequestParam("id") Long id) {
        HolidaySettingRespVO vo = holidaySettingService.getHolidaySettingRespVO(id);
        return success(vo);
    }

    @GetMapping("/getLimitBalanceHolidayList")
    @Operation(summary = "获取限制余额的假期列表")
    public CommonResult<List<HolidaySettingDO>> getLimitBalanceHolidayList() {
        List<HolidaySettingDO> vos = holidaySettingService.getLimitBalanceHolidayList();
        return success(vos);
    }

    @GetMapping("/getAllHolidaySetting")
    @Operation(summary = "获取所有假期设置")
    public CommonResult<List<HolidaySettingRespVO>> getAllHolidaySetting(@RequestParam Long userId) {
        List<HolidaySettingRespVO> vos = holidaySettingService.getAllHolidaySetting(userId);
        return success(vos);
    }

    @GetMapping("/page")
    @Operation(summary = "获得假期设置分页")
    public CommonResult<PageResult<HolidaySettingRespVO>> getHolidaySettingPage(@Valid HolidaySettingPageReqVO pageReqVO) {
        PageResult<HolidaySettingDO> pageResult = holidaySettingService.getHolidaySettingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HolidaySettingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出假期设置 Excel")
    @OperateLog(type = EXPORT)
    public void exportHolidaySettingExcel(@Valid HolidaySettingPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HolidaySettingDO> list = holidaySettingService.getHolidaySettingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "假期设置.xls", "数据", HolidaySettingRespVO.class,
                BeanUtils.toBean(list, HolidaySettingRespVO.class));
    }

}
