package com.zksy.web.controller.alarm;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.alarm.domain.AlarmData;
import com.zksy.base.alarm.service.AlarmDataService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/api/alarm")
@Api(tags = "监测预警")
public class AlarmController {

    @Autowired
    private AlarmDataService service;

    @GetMapping("/findByPage")
    @ApiOperation("报警分页查询")
    @Anonymous
    public AjaxResult findByPage(@ApiParam("页码") long pageNum,
                                  @ApiParam("每页条数") long pageSize,
                                  @RequestParam(required = false) String warningType,
                                  @RequestParam(required = false) Integer alarmStatus) {
        Page<AlarmData> page = service.findByPage(pageNum, pageSize, null, warningType, null, alarmStatus, null);
        return AjaxResult.success(page);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询报警")
    @Anonymous
    public AjaxResult getById(@PathVariable String id) {
        AlarmData entity = service.getById(id);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("报警信息不存在");
    }

    @PutMapping("/handle/{alarmId}")
    @ApiOperation("处理报警")
    @Log(title = "处理报警", businessType = BusinessType.UPDATE)
    public AjaxResult handle(@PathVariable String alarmId,
                              @RequestParam String handleUser,
                              @RequestParam(required = false) String handleRemark) {
        AlarmData entity = service.getById(alarmId);
        if (entity == null) {
            return AjaxResult.error("报警信息不存在");
        }
        entity.setAlarmStatus(1);
        entity.setHandleTime(LocalDateTime.now());
        entity.setHandleUser(handleUser);
        entity.setHandleRemark(handleRemark);
        entity.setUpdateTime(LocalDateTime.now());
        boolean updated = service.updateById(entity);
        return updated ? AjaxResult.success("处理成功") : AjaxResult.error("处理失败");
    }

    @GetMapping("/active-points")
    @ApiOperation("获取未处理报警点位（报警一张图）")
    @Anonymous
    public AjaxResult getActivePoints() {
        return AjaxResult.success(service.getAlarmDataList(null, null, null, 0));
    }
}
