package com.cpp.web.service.alarm.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cpp.web.domain.alarm.AbnormalAlarm;
import com.cpp.web.domain.alarm.AlarmConf;
import com.cpp.web.domain.enums.AlarmEnum;
import com.cpp.web.mapper.alarm.AbnormalAlarmMapper;
import com.cpp.web.service.alarm.AbnormalAlarmService;
import com.cpp.web.service.alarm.AlarmConfService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 告警信息层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@AllArgsConstructor
@Slf4j
@Service
public class AbnormalAlarmServiceImpl extends ServiceImpl<AbnormalAlarmMapper, AbnormalAlarm> implements AbnormalAlarmService {

    private final AlarmConfService alarmConfService;

    @Override
    public QueryWrapper<AbnormalAlarm> findByTimeBetweenAndAlarmTypeAndStationCode(Date startTime, Date endTime, AlarmEnum alarmType, String stationCode) {

        QueryWrapper<AbnormalAlarm> wrapper = new QueryWrapper<>();
        if (startTime != null && !endTime.equals("") && endTime != null && !endTime.equals("")) {
            wrapper.and(a -> a.or(w -> w.isNull("end_time").le("start_time", endTime))
                    .or(w -> w.isNotNull("end_time").le("start_time", endTime).gt("end_time", startTime))
                    .or(w -> w.isNotNull("end_time").le("start_time", endTime).gt("start_time", startTime))
                    .or(w -> w.isNotNull("end_time").le("end_time", endTime).gt("start_time", endTime))
                    .or(w -> w.isNotNull("end_time").le("start_time", startTime).gt("end_time", endTime)));
        }
        if (alarmType != null && !alarmType.equals("")) {
            wrapper.eq("alarm_type", alarmType);
        }

        if (stationCode != null && !stationCode.equals("")) {
            wrapper.eq("station_code", stationCode);
        }
        return wrapper;
    }

    @Override
    public Boolean updateStationStatus1(String stationCode) {
        return baseMapper.updateStationStatus1(stationCode);
    }


    @Override
    public Boolean updateAllStatus1() {
        return baseMapper.updateAllStatus1();
    }

    @Override
    public Boolean updateStatus1ById(Long id) {

        UpdateWrapper<AbnormalAlarm> wrapper = new UpdateWrapper<>();

        wrapper.eq("id", id).set("status", 1);

        return update(wrapper);
    }

    @Override
    public Long countByStatusAndStationCode(Integer status, String stationCode) {

        QueryWrapper<AbnormalAlarm> wrapper = new QueryWrapper<>();
        wrapper.eq("status", status);
        wrapper.eq("station_code", stationCode);

        return baseMapper.selectCount(wrapper);
    }

    /**
     * @param alarmType
     * @return
     */
    @Override
    public QueryWrapper<AbnormalAlarm> dashboard(AlarmEnum alarmType) {

        QueryWrapper<AbnormalAlarm> wrapper = new QueryWrapper<>();
        if (alarmType != null && !alarmType.equals("")) {
            wrapper.eq("alarm_type", alarmType);
        }

        wrapper.eq("status", 0);
        wrapper.orderByDesc("start_time");

        return wrapper;
    }

    @Override
    public Long alarmCountStatus1() {
        QueryWrapper<AbnormalAlarm> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public void updateStatus(AlarmConf alarmConf) {
        List<AbnormalAlarm> abnormalAlarmList = list();
        List<AbnormalAlarm> abnormalAlarms = new ArrayList<>();
        List<AbnormalAlarm> updateStatusList = new ArrayList<>();
        if (alarmConf.getType() == 0) {
            abnormalAlarms = abnormalAlarmList.stream().filter(f -> null != f.getMsg() && f.getMsg().contains(alarmConf.getKeyword()) && f.getEndTime() == null && f.getStatus() == 0).collect(Collectors.toList());
            updateStatusList.addAll(abnormalAlarms);
        } else {
            abnormalAlarms = abnormalAlarmList.stream().filter(f -> null != f.getMsg() && f.getMsg().contains(alarmConf.getKeyword()) && f.getEndTime() == null && f.getStatus() == 0).collect(Collectors.toList());
            updateStatusList.addAll(abnormalAlarms);
        }
        abnormalAlarms.stream().forEach(f -> f.setStatus(1));
        saveOrUpdateBatch(abnormalAlarms);
    }


}
