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

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.holidayuser.vo.HolidayUserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidayuser.vo.HolidayUserRespVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidayuser.vo.HolidayUserSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidayuser.vo.HolidayUserVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidayuser.HolidayUserDO;
import cn.iocoder.yudao.module.system.service.holiday.holidayuser.HolidayUserService;
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.security.access.prepost.PreAuthorize;
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-user")
@Validated
public class HolidayUserController {

    @Resource
    private HolidayUserService holidayUserService;

    @PostMapping("/create")
    @Operation(summary = "创建员工假期")
    @PreAuthorize("@ss.hasPermission('system:holiday-user:create')")
    public CommonResult<Long> createHolidayUser(@Valid @RequestBody HolidayUserSaveReqVO createReqVO) {
        return success(holidayUserService.createHolidayUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工假期")
    @PreAuthorize("@ss.hasPermission('system:holiday-user:update')")
    public CommonResult<Boolean> updateHolidayUser(@Valid @RequestBody HolidayUserSaveReqVO updateReqVO) {
        holidayUserService.updateHolidayUser(updateReqVO);
        return success(true);
    }

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


    @GetMapping("/getHolidayUserDynamic")
    @Operation(summary = "获得员工假期动态列表")
    public CommonResult<HolidayUserVO> getHolidayUserDynamic(@Valid UserPageReqVO userPageReqVO) {
        HolidayUserVO holidayUser = holidayUserService.getHolidayUserDynamic(userPageReqVO);
        return success(holidayUser);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工假期")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:holiday-user:query')")
    public CommonResult<HolidayUserRespVO> getHolidayUser(@RequestParam("id") Long id) {
        HolidayUserDO holidayUser = holidayUserService.getHolidayUser(id);
        return success(BeanUtils.toBean(holidayUser, HolidayUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得员工假期分页")
    @PreAuthorize("@ss.hasPermission('system:holiday-user:query')")
    public CommonResult<PageResult<HolidayUserRespVO>> getHolidayUserPage(@Valid HolidayUserPageReqVO pageReqVO) {
        PageResult<HolidayUserDO> pageResult = holidayUserService.getHolidayUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HolidayUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出员工假期 Excel")
    @PreAuthorize("@ss.hasPermission('system:holiday-user:export')")
    @OperateLog(type = EXPORT)
    public void exportHolidayUserExcel(@Valid HolidayUserPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HolidayUserDO> list = holidayUserService.getHolidayUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "员工假期.xls", "数据", HolidayUserRespVO.class,
                BeanUtils.toBean(list, HolidayUserRespVO.class));
    }

}
