package cn.iocoder.yudao.module.system.controller.admin.robot;

import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnMsgPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnMsgRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnMsgSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotWarnMsgDO;
import cn.iocoder.yudao.module.system.service.robot.RobotWarnMsgService;
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.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 机器人告警信息")
@RestController
@RequestMapping("/system/robot/warn-msg")
@Validated
public class RobotWarnMsgController {

    @Resource
    private RobotWarnMsgService warnMsgService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人告警信息")
    @PreAuthorize("@ss.hasPermission('robot:warn-msg:create')")
    public CommonResult<Long> createWarnMsg(@Valid @RequestBody RobotWarnMsgSaveReqVO createReqVO) {
        return success(warnMsgService.createWarnMsg(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人告警信息")
    @PreAuthorize("@ss.hasPermission('robot:warn-msg:update')")
    public CommonResult<Boolean> updateWarnMsg(@Valid @RequestBody RobotWarnMsgSaveReqVO updateReqVO) {
        warnMsgService.updateWarnMsg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人告警信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:warn-msg:delete')")
    public CommonResult<Boolean> deleteWarnMsg(@RequestParam("id") Long id) {
        warnMsgService.deleteWarnMsg(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人告警信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:warn-msg:query')")
    public CommonResult<RobotWarnMsgRespVO> getWarnMsg(@RequestParam("id") Long id) {
        RobotWarnMsgDO warnMsg = warnMsgService.getWarnMsg(id);
        return success(BeanUtils.toBean(warnMsg, RobotWarnMsgRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人告警信息分页")
    @PreAuthorize("@ss.hasPermission('robot:warn-msg:query')")
    public CommonResult<PageResult<RobotWarnMsgRespVO>> getWarnMsgPage(@Valid RobotWarnMsgPageReqVO pageReqVO) {
        PageResult<RobotWarnMsgDO> pageResult = warnMsgService.getWarnMsgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotWarnMsgRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人告警信息 Excel")
    @PreAuthorize("@ss.hasPermission('robot:warn-msg:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarnMsgExcel(@Valid RobotWarnMsgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotWarnMsgDO> list = warnMsgService.getWarnMsgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人告警信息.xls", "数据", RobotWarnMsgRespVO.class,
                        BeanUtils.toBean(list, RobotWarnMsgRespVO.class));
    }

    @GetMapping("/readPage")
    @Operation(summary = "获得机器人告警信息分页")
    @PreAuthorize("@ss.hasPermission('robot:warn-msg:readPage')")
    public CommonResult<List<RobotWarnMsgRespVO>> getReadPage() {
        List<RobotWarnMsgDO> readPage = warnMsgService.getReadPage();
        return success(BeanUtils.toBean(readPage, RobotWarnMsgRespVO.class));
    }

    @PostMapping("/allRead")
    @Operation(summary = "全部标记为已读")
    @PreAuthorize("@ss.hasPermission('robot:warn-msg:allRead')")
    public CommonResult<String> allRead() {
        return success(warnMsgService.allRead());
    }

}