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

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.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.devicewarn.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.devicewarn.DeviceWarnDO;
import cn.iocoder.yudao.module.system.service.devicewarn.DeviceWarnService;

@Tag(name = "管理后台 - 设备报警")
@RestController
@RequestMapping("/system/device-warn")
@Validated
public class DeviceWarnController {

    @Resource
    private DeviceWarnService deviceWarnService;

    @PostMapping("/create")
    @Operation(summary = "创建设备报警")
    @PreAuthorize("@ss.hasPermission('system:device-warn:create')")
    public CommonResult<Long> createDeviceWarn(@Valid @RequestBody DeviceWarnSaveReqVO createReqVO) {
        return success(deviceWarnService.createDeviceWarn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备报警")
    @PreAuthorize("@ss.hasPermission('system:device-warn:update')")
    public CommonResult<Boolean> updateDeviceWarn(@Valid @RequestBody DeviceWarnSaveReqVO updateReqVO) {
        deviceWarnService.updateDeviceWarn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备报警")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:device-warn:delete')")
    public CommonResult<Boolean> deleteDeviceWarn(@RequestParam("id") Long id) {
        deviceWarnService.deleteDeviceWarn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备报警")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:device-warn:query')")
    public CommonResult<DeviceWarnRespVO> getDeviceWarn(@RequestParam("id") Long id) {
        DeviceWarnDO deviceWarn = deviceWarnService.getDeviceWarn(id);
        return success(BeanUtils.toBean(deviceWarn, DeviceWarnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备报警分页")
    @PreAuthorize("@ss.hasPermission('system:device-warn:query')")
    public CommonResult<PageResult<DeviceWarnRespVO>> getDeviceWarnPage(@Valid DeviceWarnPageReqVO pageReqVO) {
        PageResult<DeviceWarnDO> pageResult = deviceWarnService.getDeviceWarnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceWarnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备报警 Excel")
    @PreAuthorize("@ss.hasPermission('system:device-warn:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceWarnExcel(@Valid DeviceWarnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceWarnDO> list = deviceWarnService.getDeviceWarnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备报警.xls", "数据", DeviceWarnRespVO.class,
                        BeanUtils.toBean(list, DeviceWarnRespVO.class));
    }

}