package cn.iocoder.yudao.module.iot.controller.admin.devicewarinningrecord;

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.*;

import cn.iocoder.yudao.module.iot.controller.admin.devicewarinningrecord.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicewarinningrecord.DeviceWarinningRecordDO;
import cn.iocoder.yudao.module.iot.service.devicewarinningrecord.DeviceWarinningRecordService;

@Tag(name = "管理后台 - 告警记录")
@RestController
@RequestMapping("/iot/device-warinning-record")
@Validated
public class DeviceWarinningRecordController {

    @Resource
    private DeviceWarinningRecordService deviceWarinningRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建告警记录")
    @PreAuthorize("@ss.hasPermission('iot:device-warinning-record:create')")
    public CommonResult<Long> createDeviceWarinningRecord(@Valid @RequestBody DeviceWarinningRecordSaveReqVO createReqVO) {
        return success(deviceWarinningRecordService.createDeviceWarinningRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新告警记录")
    @PreAuthorize("@ss.hasPermission('iot:device-warinning-record:update')")
    public CommonResult<Boolean> updateDeviceWarinningRecord(@Valid @RequestBody DeviceWarinningRecordSaveReqVO updateReqVO) {
        deviceWarinningRecordService.updateDeviceWarinningRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除告警记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:device-warinning-record:delete')")
    public CommonResult<Boolean> deleteDeviceWarinningRecord(@RequestParam("id") Long id) {
        deviceWarinningRecordService.deleteDeviceWarinningRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得告警记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-warinning-record:query')")
    public CommonResult<DeviceWarinningRecordRespVO> getDeviceWarinningRecord(@RequestParam("id") Long id) {
        DeviceWarinningRecordDO deviceWarinningRecord = deviceWarinningRecordService.getDeviceWarinningRecord(id);
        return success(BeanUtils.toBean(deviceWarinningRecord, DeviceWarinningRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得告警记录分页")
    @PreAuthorize("@ss.hasPermission('iot:device-warinning-record:query')")
    public CommonResult<PageResult<DeviceWarinningRecordRespVO>> getDeviceWarinningRecordPage(@Valid DeviceWarinningRecordPageReqVO pageReqVO) {
        PageResult<DeviceWarinningRecordDO> pageResult = deviceWarinningRecordService.getDeviceWarinningRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceWarinningRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出告警记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-warinning-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceWarinningRecordExcel(@Valid DeviceWarinningRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceWarinningRecordDO> list = deviceWarinningRecordService.getDeviceWarinningRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "告警记录.xls", "数据", DeviceWarinningRecordRespVO.class,
                        BeanUtils.toBean(list, DeviceWarinningRecordRespVO.class));
    }


    @GetMapping("/getList")
    @Operation(summary = "获得告警记录列表")
    @Parameter(name = "deviceId", description = "设备Id", required = true, example = "1024")
    @Parameter(name = "orgId", description = "产线组织Id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-warinning-record:query')")
    public CommonResult<List<DeviceWarinningRecordDO>> getList(@RequestParam(name = "deviceId" ,required = false) Long id,
                                                               @RequestParam(name = "orgId" ,required = false) Long orgId) {
        List<DeviceWarinningRecordDO> deviceWarinningRecord = deviceWarinningRecordService.getList(id);
        return success(deviceWarinningRecord);
    }


    @GetMapping("/getLastSevenHoursCount")
    @Operation(summary = "获取过去7小时每小时告警数量")
    @Parameter(name = "orgId", description = "产线组织Id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-warinning-record:query')")
    public CommonResult<List<Map<String, Object>>> getLastSevenHoursCount( @RequestParam(name = "orgId" ,required = false) Long orgId) {
        List<Map<String, Object>> hourCounts = deviceWarinningRecordService.getLastSevenHoursCount();
        return success(hourCounts);
    }

}