package com.zksy.base.pressure.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.environment.domain.ERealTimeData;
import com.zksy.base.pressure.domain.FirefightingPressure;
import com.zksy.base.pressure.mapper.FirefightingPressureMapper;
import com.zksy.base.pressure.service.FirefightingPressureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class FirefightingPressureServiceImpl extends ServiceImpl<FirefightingPressureMapper, FirefightingPressure>
        implements FirefightingPressureService {
    @Autowired
    private FirefightingPressureMapper firefightingPressureMapper;
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private LocalDateTime parse(String time) {
        return LocalDateTime.parse(time, FORMATTER);
    }

    @Override
    public Double getAveragePressure(String startTime, String endTime) {
        LocalDateTime start = parse(startTime);
        LocalDateTime end = parse(endTime);
        List<Double> values = this.lambdaQuery()
                .between(FirefightingPressure::getObservedTime, start, end)
                .select(FirefightingPressure::getPressureValue)
                .list()
                .stream()
                .map(FirefightingPressure::getPressureValue)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        return values.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
    }

    @Override
    public List<Map<String, Object>> getDailyStats(String startTime, String endTime) {
        return firefightingPressureMapper.getDailyPressureStats(parse(startTime), parse(endTime));
    }

    @Override
    public Map<String, Long> getAlarmCounts(String startTime, String endTime) {
        List<FirefightingPressure> list = this.lambdaQuery()
                .between(FirefightingPressure::getObservedTime, parse(startTime), parse(endTime))
                .list();

        Map<String, Long> result = new HashMap<>();
        result.put("pressureHigh", list.stream().filter(x -> x.getD10() != null && x.getD10() == 1).count());
        result.put("pressureLow", list.stream().filter(x -> x.getD11() != null && x.getD11() == 1).count());
        result.put("pressureHighHigh", list.stream().filter(x -> x.getD12() != null && x.getD12() == 1).count());
        result.put("pressureLowLow", list.stream().filter(x -> x.getD13() != null && x.getD13() == 1).count());
        result.put("tilt", list.stream().filter(x -> x.getD1() != null && x.getD1() == 1).count());
        return result;
    }

    @Override
    public List<Map<String, Object>> getPressureDistribution(String startTime, String endTime) {
        return firefightingPressureMapper.getPressureDistribution(parse(startTime), parse(endTime));
    }

    @Override
    public List<Map<String, Object>> getSignalVsPressure(String startTime, String endTime) {
        return firefightingPressureMapper.getSignalVsPressure(parse(startTime), parse(endTime));
    }

    @Override
    public List<String> getDeviceGroup() {
        return lambdaQuery()
                .select(FirefightingPressure::getCentralStation)
                .groupBy(FirefightingPressure::getCentralStation)
                .list()
                .stream()
                .map(FirefightingPressure::getCentralStation)
                .map(String::valueOf)
                .collect(Collectors.toList());
    }
}
