package com.cpp.web.controller.alarm;

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.AlarmConf;
import com.cpp.web.service.alarm.AbnormalAlarmService;
import com.cpp.web.service.alarm.AlarmConfService;
import com.cpp.web.service.alarm.AlarmLog;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 告警配置
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("alarmConf")
public class AlarmConfController {
    private final AlarmConfService alarmConfService;

    private final AbnormalAlarmService abnormalAlarmService;

    @GetMapping("/pageByExpireTime")
    public R pageByExpireTime(Long currentPage, Long pageSize, Long time) {
        Page page = new Page(currentPage, pageSize);
        page.setMaxLimit((long) -1);
        return R.ok(alarmConfService.page(page, Wrappers.lambdaQuery(AlarmConf.class).select().ge(AlarmConf::getExpireTime,new Date(time))));
    }


    @PostMapping("/save")
    public R save(@RequestBody AlarmConf alarmConf) {
        Boolean b = alarmConfService.saveOrUpdate(alarmConf);
        AlarmLog.getInstance().updateStationAlarmConfListMap(alarmConf,true);
        abnormalAlarmService.updateStatus(alarmConf);
        return R.ok(b);
    }


    /**
     * 修改
     *
     * @return R
     */
    @PostMapping("/updateById")
    public R updateById(@RequestBody AlarmConf alarmConf) {
        AlarmLog.getInstance().updateStationAlarmConfListMap(alarmConf,true);
        abnormalAlarmService.updateStatus(alarmConf);
        return R.ok(alarmConfService.updateById(alarmConf));
    }

    /**
     * 通过id删除
     *
     * @param
     * @return R
     */
    @PostMapping("/remove")
    public R removeById(@RequestBody AlarmConf alarmConf) {
        AlarmLog.getInstance().updateStationAlarmConfListMap(alarmConf,false);
        return R.ok(alarmConfService.removeById(alarmConf.getId()));
    }


}
