package com.cpp.web.controller.alarm;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpp.common.core.domain.R;

import com.cpp.web.domain.Alarm.AbnormalAlarm;
import com.cpp.web.domain.enums.AlarmEnum;
import com.cpp.web.service.alarm.AbnormalAlarmService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 异常告警
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("abnormalAlarm")
public class AbnormalAlarmController {
    private final AbnormalAlarmService abnormalAlarmService;

    @GetMapping("/getByTimeBetweenAndAlarmTypeAndStationCode")
    public R getByTimeBetweenAndAlarmTypeAndStationCode(Long currentPage, Long pageSize, Long startTime, Long endTime, AlarmEnum alarmType, String stationCode) {
        Page page = new Page(currentPage, pageSize);
        page.setMaxLimit((long) -1);
        return R.ok(abnormalAlarmService.page(page, abnormalAlarmService.findByTimeBetweenAndAlarmTypeAndStationCode(new Date(startTime), new Date(endTime), alarmType, stationCode)));
    }


    @GetMapping("/acknowledgeByStationCode")
    public R acknowledgeByStationCode(String stationCode) {
        return R.ok(abnormalAlarmService.updateStationStatus1(stationCode));
    }

    @GetMapping("/dashboard")
    public R dashboard(Long currentPage, Long pageSize, AlarmEnum alarmType) {
        Page page = new Page(currentPage, pageSize);
        page.setMaxLimit((long) -1);
        return R.ok(abnormalAlarmService.page(page, abnormalAlarmService.dashboard(alarmType)).addOrder(OrderItem.desc("start_time")));
    }


    @GetMapping("/acknowledgeAll")
    public R acknowledgeAll() {
        return R.ok(abnormalAlarmService.updateAllStatus1());
    }

    @PostMapping("/save")
    public R save(@RequestBody AbnormalAlarm abnormalAlarm) {
        return R.ok(abnormalAlarmService.saveOrUpdate(abnormalAlarm));
    }

    @PostMapping("/updateStatus1")
    public R updateStatus1(@RequestBody AbnormalAlarm abnormalAlarm) {
        return R.ok(abnormalAlarmService.updateStatus1ById(abnormalAlarm.getId()));
    }


    @GetMapping("/getCountByStatusAndStationCode")
    public R getCountByStatus(Integer status, String stationCode) {
        return R.ok(abnormalAlarmService.countByStatusAndStationCode(status, stationCode));
    }


    @GetMapping("/alarmCountStatus1")
    public R dashboard() {
        return R.ok(abnormalAlarmService.alarmCountStatus1());
    }

}
