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

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.alert.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.alert.AlertDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.alertrecord.AlertRecordDO;
import cn.iocoder.yudao.module.iot.service.alert.AlertService;

@Tag(name = "管理后台 - 告警配置")
@RestController
@RequestMapping("/iot/alert")
@Validated
public class AlertController {

    @Resource
    private AlertService alertService;

    @PostMapping("/create")
    @Operation(summary = "创建告警配置")
    @PreAuthorize("@ss.hasPermission('iot:alert:create')")
    public CommonResult<Long> createAlert(@Valid @RequestBody AlertSaveReqVO createReqVO) {
        return success(alertService.createAlert(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新告警配置")
    @PreAuthorize("@ss.hasPermission('iot:alert:update')")
    public CommonResult<Boolean> updateAlert(@Valid @RequestBody AlertSaveReqVO updateReqVO) {
        alertService.updateAlert(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除告警配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:alert:delete')")
    public CommonResult<Boolean> deleteAlert(@RequestParam("id") Long id) {
        alertService.deleteAlert(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得告警配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:alert:query')")
    public CommonResult<AlertRespVO> getAlert(@RequestParam("id") Long id) {
        AlertDO alert = alertService.getAlert(id);
        return success(BeanUtils.toBean(alert, AlertRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得告警配置分页")
    @PreAuthorize("@ss.hasPermission('iot:alert:query')")
    public CommonResult<PageResult<AlertRespVO>> getAlertPage(@Valid AlertPageReqVO pageReqVO) {
        PageResult<AlertDO> pageResult = alertService.getAlertPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AlertRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出告警配置 Excel")
    @PreAuthorize("@ss.hasPermission('iot:alert:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAlertExcel(@Valid AlertPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AlertDO> list = alertService.getAlertPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "告警配置.xls", "数据", AlertRespVO.class,
                        BeanUtils.toBean(list, AlertRespVO.class));
    }

    // ==================== 子表（告警记录） ====================

    @GetMapping("/alert-record/page")
    @Operation(summary = "获得告警记录分页")
    @Parameter(name = "alertId", description = "告警ID")
    @PreAuthorize("@ss.hasPermission('iot:alert:query')")
    public CommonResult<PageResult<AlertRecordDO>> getAlertRecordPage(PageParam pageReqVO,
                                                                                        @RequestParam("alertId") Long alertId) {
        return success(alertService.getAlertRecordPage(pageReqVO, alertId));
    }

    @PostMapping("/alert-record/create")
    @Operation(summary = "创建告警记录")
    @PreAuthorize("@ss.hasPermission('iot:alert:create')")
    public CommonResult<Long> createAlertRecord(@Valid @RequestBody AlertRecordDO alertRecord) {
        return success(alertService.createAlertRecord(alertRecord));
    }

    @PutMapping("/alert-record/update")
    @Operation(summary = "更新告警记录")
    @PreAuthorize("@ss.hasPermission('iot:alert:update')")
    public CommonResult<Boolean> updateAlertRecord(@Valid @RequestBody AlertRecordDO alertRecord) {
        alertService.updateAlertRecord(alertRecord);
        return success(true);
    }

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

	@GetMapping("/alert-record/get")
	@Operation(summary = "获得告警记录")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:alert:query')")
	public CommonResult<AlertRecordDO> getAlertRecord(@RequestParam("id") Long id) {
	    return success(alertService.getAlertRecord(id));
	}

}