jinpengyong
2021-12-29 48e498136c8784ee79a698da2c852ca3aa0549ab
screen-api/src/main/java/com/moral/api/service/impl/HistoryMonthlyServiceImpl.java
@@ -11,6 +11,7 @@
import com.moral.api.utils.GetCenterPointFromListOfCoordinates;
import com.moral.constant.Constants;
import com.moral.util.PollutantUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@@ -18,7 +19,10 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
/**
 * <p>
@@ -307,4 +311,39 @@
                .in("mac", macs);
        return historyMonthlyMapper.selectList(queryWrapper);
    }
    @Override
    public Double calculatedValue(List<HistoryMonthly> list, String sensorCode, String type) {
        Supplier<Stream<HistoryMonthly>> supplier = list::stream;
        DoubleStream doubleStream = supplier.get()
                .flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class);
                    Object sensorValue = dataValue.get(sensorCode);
                    if (ObjectUtils.isEmpty(sensorValue)) {
                        return null;
                    }
                    double aDouble = Double.parseDouble(sensorValue.toString());
                    return DoubleStream.of(aDouble);
                });
        Double result = null;
        OptionalDouble optionalDouble = null;
        if ("sum".equals(type)) {
            result = doubleStream.sum();
        } else {
            if ("min".equals(type)) {
                optionalDouble = doubleStream.average();
            } else if ("max".equals(type)) {
                optionalDouble = doubleStream.min();
            } else if ("avg".equals(type)) {
                optionalDouble = doubleStream.max();
            }
            if (optionalDouble.isPresent()) {
                result = optionalDouble.getAsDouble();
            }
        }
        return result;
    }
}