package cn.iocoder.yudao.server.controller.admin;

import cn.iocoder.yudao.server.controller.admin.vo.oa.IotOaNoticePageReqVO;
import cn.iocoder.yudao.server.controller.admin.vo.oa.IotOaNoticeRespVO;
import cn.iocoder.yudao.server.controller.admin.vo.oa.IotOaNoticeSaveReqVO;
import cn.iocoder.yudao.server.dal.dataobject.OaNoticeDO;
import cn.iocoder.yudao.server.service.oa.OaNoticeService;
import org.springframework.web.bind.annotation.*;
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 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.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - OA消息通知")
@RestController
@RequestMapping("/rq/iot-oa-notice")
@Validated
public class OaNoticeController {

    @Resource
    private OaNoticeService iotOaNoticeService;

    @PostMapping("/create")
    @Operation(summary = "创建OA消息通知")
    @PreAuthorize("@ss.hasPermission('rq:iot-oa-notice:create')")
    public CommonResult<Long> createIotOaNotice(@Valid @RequestBody IotOaNoticeSaveReqVO createReqVO) {
        return success(iotOaNoticeService.createIotOaNotice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新OA消息通知")
    @PreAuthorize("@ss.hasPermission('rq:iot-oa-notice:update')")
    public CommonResult<Boolean> updateIotOaNotice(@Valid @RequestBody IotOaNoticeSaveReqVO updateReqVO) {
        iotOaNoticeService.updateIotOaNotice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除OA消息通知")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-oa-notice:delete')")
    public CommonResult<Boolean> deleteIotOaNotice(@RequestParam("id") Long id) {
        iotOaNoticeService.deleteIotOaNotice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得OA消息通知")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-oa-notice:query')")
    public CommonResult<IotOaNoticeRespVO> getIotOaNotice(@RequestParam("id") Long id) {
        OaNoticeDO iotOaNotice = iotOaNoticeService.getIotOaNotice(id);
        return success(BeanUtils.toBean(iotOaNotice, IotOaNoticeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得OA消息通知分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-oa-notice:query')")
    public CommonResult<PageResult<IotOaNoticeRespVO>> getIotOaNoticePage(@Valid IotOaNoticePageReqVO pageReqVO) {
        PageResult<OaNoticeDO> pageResult = iotOaNoticeService.getIotOaNoticePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotOaNoticeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出OA消息通知 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-oa-notice:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotOaNoticeExcel(@Valid IotOaNoticePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OaNoticeDO> list = iotOaNoticeService.getIotOaNoticePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "OA消息通知.xls", "数据", IotOaNoticeRespVO.class,
                        BeanUtils.toBean(list, IotOaNoticeRespVO.class));
    }

}