package cn.iocoder.yudao.module.pms.controller.admin.alarm;

import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotAlarmRecordPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotAlarmRecordRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotAlarmRecordSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.alarm.IotAlarmRecordDO;
import cn.iocoder.yudao.module.pms.service.alarm.IotAlarmRecordService;
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 = "管理后台 - 运行监控告警记录")
@RestController
@RequestMapping("/rq/iot-alarm-record")
@Validated
public class IotAlarmRecordController {

    @Resource
    private IotAlarmRecordService iotAlarmRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建运行监控告警记录")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-record:create')")
    public CommonResult<Long> createIotAlarmRecord(@Valid @RequestBody IotAlarmRecordSaveReqVO createReqVO) {
        return success(iotAlarmRecordService.createIotAlarmRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新运行监控告警记录")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-record:update')")
    public CommonResult<Boolean> updateIotAlarmRecord(@Valid @RequestBody IotAlarmRecordSaveReqVO updateReqVO) {
        iotAlarmRecordService.updateIotAlarmRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除运行监控告警记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-record:delete')")
    public CommonResult<Boolean> deleteIotAlarmRecord(@RequestParam("id") Long id) {
        iotAlarmRecordService.deleteIotAlarmRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得运行监控告警记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-record:query')")
    public CommonResult<IotAlarmRecordRespVO> getIotAlarmRecord(@RequestParam("id") Long id) {
        IotAlarmRecordDO iotAlarmRecord = iotAlarmRecordService.getIotAlarmRecord(id);
        return success(BeanUtils.toBean(iotAlarmRecord, IotAlarmRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得运行监控告警记录分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-record:query')")
    public CommonResult<PageResult<IotAlarmRecordRespVO>> getIotAlarmRecordPage(@Valid IotAlarmRecordPageReqVO pageReqVO) {
        PageResult<IotAlarmRecordDO> pageResult = iotAlarmRecordService.getIotAlarmRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotAlarmRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出运行监控告警记录 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotAlarmRecordExcel(@Valid IotAlarmRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotAlarmRecordDO> list = iotAlarmRecordService.getIotAlarmRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "运行监控告警记录.xls", "数据", IotAlarmRecordRespVO.class,
                        BeanUtils.toBean(list, IotAlarmRecordRespVO.class));
    }

}