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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotAlarmSettingPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotAlarmSettingRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotAlarmSettingSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.alarm.IotAlarmSettingDO;
import cn.iocoder.yudao.module.pms.service.alarm.IotAlarmSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 告警设置")
@RestController
@RequestMapping("/rq/iot-alarm-setting")
@Validated
public class IotAlarmSettingController {

    @Resource
    private IotAlarmSettingService iotAlarmSettingService;

    @PostMapping("/create")
    @Operation(summary = "创建告警设置")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-setting:create')")
    public CommonResult<Long> createIotAlarmSetting(@Valid @RequestBody IotAlarmSettingSaveReqVO createReqVO) {
        return success(iotAlarmSettingService.createIotAlarmSetting(createReqVO));
    }

    @PostMapping("/save")
    @Operation(summary = "创建告警设置")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-setting:create')")
    public CommonResult<IotAlarmSettingDO> saveIotAlarmSetting(@Valid @RequestBody IotAlarmSettingSaveReqVO createReqVO) {
        return success(iotAlarmSettingService.saveIotAlarmSetting(createReqVO));
    }

    @PostMapping("/batch/update")
    @Operation(summary = "创建告警设置")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-setting:create')")
    public CommonResult<String> batchUpdateIotAlarmSetting(@Valid @RequestBody List<IotAlarmSettingSaveReqVO> alarms) {
        alarms.forEach(alarm -> iotAlarmSettingService.updateIotAlarmSetting(alarm));
        return success("完成");
    }

    @PutMapping("/update")
    @Operation(summary = "更新告警设置")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-setting:update')")
    public CommonResult<Boolean> updateIotAlarmSetting(@Valid @RequestBody IotAlarmSettingSaveReqVO updateReqVO) {
        iotAlarmSettingService.updateIotAlarmSetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除告警设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-setting:delete')")
    public CommonResult<Boolean> deleteIotAlarmSetting(@RequestParam("id") Long id) {
        iotAlarmSettingService.deleteIotAlarmSetting(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得告警设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-setting:query')")
    public CommonResult<IotAlarmSettingRespVO> getIotAlarmSetting(@RequestParam("id") Long id) {
        IotAlarmSettingDO iotAlarmSetting = iotAlarmSettingService.getIotAlarmSetting(id);
        return success(BeanUtils.toBean(iotAlarmSetting, IotAlarmSettingRespVO.class));
    }

    @GetMapping("/page/{if}")
    @Operation(summary = "获得告警设置分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-setting:query')")
    public CommonResult<PageResult<IotAlarmSettingRespVO>> getIotAlarmSettingPage(@Valid IotAlarmSettingPageReqVO pageReqVO, @PathVariable("if") String ifDevice) {
        PageResult<IotAlarmSettingDO> pageResult = iotAlarmSettingService.getIotAlarmSettingPage(pageReqVO, ifDevice);
        return success(BeanUtils.toBean(pageResult, IotAlarmSettingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出告警设置 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-alarm-setting:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotAlarmSettingExcel(@Valid IotAlarmSettingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotAlarmSettingDO> list = iotAlarmSettingService.getIotAlarmSettingPage(pageReqVO,"1").getList();
        // 导出 Excel
        ExcelUtils.write(response, "告警设置.xls", "数据", IotAlarmSettingRespVO.class,
                        BeanUtils.toBean(list, IotAlarmSettingRespVO.class));
    }

    @GetMapping("/get-device-range")
    @Operation(summary = "获取设备标识")
    public CommonResult<IotAlarmSettingRespVO> getDeviceIdentifier(@RequestParam("deviceName") String deviceName, @RequestParam("identifier") String identifier) {
        IotAlarmSettingDO deviceRange = iotAlarmSettingService.getDeviceRange(deviceName, identifier);
        if (Objects.isNull(deviceRange)) {
            return success(new IotAlarmSettingRespVO());
        }
        return success(BeanUtils.toBean(deviceRange, IotAlarmSettingRespVO.class));
    }

}
