package com.zksy.web.controller.base.alarm;

import com.zksy.base.alarm.domain.AlarmData;
import com.zksy.base.alarm.service.AlarmDataService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;

@RestController
@RequestMapping("/alarmData")
@Api(tags = "报警数据信息", description = "报警数据信息desc")
public class AlarmDataController {
    @Autowired
    private AlarmDataService service;

    @GetMapping("/findByPage")
    @ApiOperation(value = "报警数据信息查询分页")
    @Anonymous
    public AjaxResult findByPage(@ApiParam(value = "页码", required = true) long pageNum,
                                 @ApiParam(value = "页数", required = true) long pageSize,
                                 @ApiParam(value = "设备编码", required = false) String deviceCode,
                                 @ApiParam(value = "预警类型", required = false) String warningType,
                                 @ApiParam(value = "预警编码", required = false) String warningCode,
                                 @ApiParam(value = "报警状态 0-未处理 1-已处理", required = false) Integer alarmStatus,
                                 @ApiParam(value = "设备类型（如drainage）", required = false) String equipmentType) {
        return AjaxResult.success(service.findByPage(pageNum, pageSize, deviceCode, warningType, warningCode, alarmStatus, equipmentType));
    }

    @GetMapping("/getAlarmDataList")
    @ApiOperation(value = "报警数据信息查询")
    @Anonymous
    public AjaxResult getAlarmDataList(@ApiParam(value = "设备编码", required = false) String deviceCode,
                                         @ApiParam(value = "预警类型", required = false) String warningType,
                                         @ApiParam(value = "预警编码", required = false) String warningCode,
                                         @ApiParam(value = "报警状态 0-未处理 1-已处理", required = false) Integer alarmStatus) {
        return AjaxResult.success(service.getAlarmDataList(deviceCode, warningType, warningCode, alarmStatus));
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据Id查询报警数据信息")
    public AjaxResult getById(@PathVariable String id) {
        return AjaxResult.success(service.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "报警数据信息保存")
    public AjaxResult save(@RequestBody AlarmData entity) {
        return service.saveAlarmData(entity) ? AjaxResult.success(entity) : AjaxResult.error("保存失败");
    }

    @PostMapping("/update")
    @ApiOperation(value = "报警数据信息修改")
    public AjaxResult update(@RequestBody AlarmData entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return service.updateById(entity) ? AjaxResult.success(entity) : AjaxResult.error("修改失败");
    }

    @PostMapping("/handle")
    @ApiOperation(value = "处理报警")
    public AjaxResult handle(@RequestBody AlarmData entity) {
        entity.setAlarmStatus(1);
        entity.setHandleTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        return service.updateById(entity) ? AjaxResult.success(entity) : AjaxResult.error("处理失败");
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "报警数据信息删除")
    public AjaxResult delete(@RequestBody String[] ids) {
        return service.removeByIds(Arrays.asList(ids)) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}
