package com.zcxk.service.impl;

import com.zcxk.client.UserCenterClient;
import com.zcxk.dao.HomePageReportMapper;
import com.zcxk.dao.MonitorDataReportMapper;
import com.zcxk.dto.generalView.*;
import com.zcxk.dto.homePage.CompanyCount;
import com.zcxk.dto.homePage.CompanyProduce;
import com.zcxk.dto.homePage.WaterQualityRate;
import com.zcxk.dto.homePage.WaterQualityRateForScene;
import com.zcxk.entity.MonitorDataEntity;
import com.zcxk.entity.MonitorDataValueEntity;
import com.zcxk.model.LoginUser;
import com.zcxk.service.HomePageReportService;
import com.zcxk.service.MonitorDataReportService;
import com.zcxk.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @description
 * @auto wangli
 * @data 2021/2/24 10:42
 */
@Service
public class HomePageReportServiceImpl implements HomePageReportService {

    @Resource
    private HomePageReportMapper homePageReportMapper;

    @Autowired
    private UserCenterClient userCenterClient;

    @Resource
    private MonitorDataReportMapper monitorDataReportMapper;

    @Autowired
    private MonitorDataReportService monitorDataReportService;

    @Override
    public List<DeviceWaterSupply> deviceWaterReportForSixMonth(Integer companyOrgId, String sceneType, Integer parmType) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        String tenantId = loginUser.getTenantId();
        LocalDate localDate = LocalDate.now();


        List<DeviceWaterSupply> deviceWaterSupplies = homePageReportMapper.getDeviceWaterForSixMonth(companyOrgId, sceneType, parmType, tenantId, loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());

        BigDecimal amountCount = monitorDataReportService.getAmountCount(companyOrgId, null, sceneType, parmType, 2);

        DeviceWaterSupply deviceWaterSupplyMonth = new DeviceWaterSupply();
        deviceWaterSupplyMonth.setOrderNo(1);
        deviceWaterSupplyMonth.setAmount(amountCount != null ? amountCount.divide(new BigDecimal("10000")) : BigDecimal.ZERO);
        deviceWaterSupplyMonth.setYear(localDate.getYear());
        deviceWaterSupplyMonth.setMonth(localDate.getMonthValue());
        deviceWaterSupplyMonth.setDate(localDate);
        deviceWaterSupplyMonth.setDateLabel(localDate.getYear() + "-" + localDate.getMonthValue());
        deviceWaterSupplies.add(0, deviceWaterSupplyMonth);

        for (int i = 0; i < 6; i++) {
            LocalDate newLocalDate = localDate.plusMonths(-i);
            if (i == deviceWaterSupplies.size()
                    || newLocalDate.getMonthValue() != deviceWaterSupplies.get(i).getMonth().intValue()
                    || newLocalDate.getYear() != deviceWaterSupplies.get(i).getYear().intValue()) {

                DeviceWaterSupply deviceWaterSupply = new DeviceWaterSupply();
                deviceWaterSupply.setOrderNo(i + 1);
                deviceWaterSupply.setAmount(BigDecimal.ZERO);
                deviceWaterSupply.setYear(newLocalDate.getYear());
                deviceWaterSupply.setMonth(newLocalDate.getMonthValue());
                deviceWaterSupply.setDate(newLocalDate);
                deviceWaterSupply.setDateLabel(newLocalDate.getYear() + "-" + newLocalDate.getMonthValue());
                deviceWaterSupplies.add(i, deviceWaterSupply);
            } else {
                deviceWaterSupplies.get(i).setOrderNo(i + 1);
                deviceWaterSupplies.get(i).setDate(newLocalDate);
                deviceWaterSupplies.get(i).setDateLabel(newLocalDate.getYear() + "-" + newLocalDate.getMonthValue());
            }
        }
        deviceWaterSupplies.sort(Comparator.comparing(DeviceWaterSupply::getOrderNo).reversed());


        return deviceWaterSupplies;
    }

    @Override
    public CompanyCount countSceneByType(Integer companyOrgId) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        CompanyCount companyCount = homePageReportMapper.countSceneByType(companyOrgId, loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        Integer company = userCenterClient.countCompanyByUser();
        companyCount.setCompany(company != null ? company : 0);
        return companyCount;
    }


    @Override
    public CompanyProduce deviceWaterAmount(Integer companyOrgId) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        String tenantId = loginUser.getTenantId();
        Map<Integer, MonitorDataEntity> MonitorDataMap = monitorDataReportService.getIntegerMonitorDataEntityMap(loginUser);

        LocalDate localDate = LocalDate.now();

        CompanyProduce companyProduce = homePageReportMapper.getWaterSalesDetails(companyOrgId, loginUser.getTenantId(), loginUser.getType(), loginUser.getProgramItemList());

        if (companyProduce == null) {
            companyProduce = new CompanyProduce();
        }
        BigDecimal waterSupplyAmountForDay = BigDecimal.ZERO;
        BigDecimal waterIntakeAmountForDay = BigDecimal.ZERO;

        //3供水 4取水
        List<DeviceWaterDetail> deviceSupplyWaterDetailsForDay = monitorDataReportMapper.getDeviceWaterDetailForDay(companyOrgId, null, "水厂", 3, tenantId, localDate.plusDays(-1).getYear(), localDate.plusDays(-1).getMonthValue(), localDate.plusDays(-1).getDayOfMonth(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        List<DeviceWaterDetail> deviceIntakeWaterDetailsForDay = monitorDataReportMapper.getDeviceWaterDetailForDay(companyOrgId, null, "水源", 4, tenantId, localDate.plusDays(-1).getYear(), localDate.plusDays(-1).getMonthValue(), localDate.plusDays(-1).getDayOfMonth(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());

        //水厂获取实时流量并计算出当天流量
        for (DeviceWaterDetail deviceWaterDetail : deviceSupplyWaterDetailsForDay) {
            MonitorDataEntity monitorDataEntity = MonitorDataMap.get(deviceWaterDetail.getDeviceId());
            if (monitorDataEntity != null && monitorDataEntity.getDataValues() != null && monitorDataEntity.getDataValues().size() > 0
                    && deviceWaterDetail != null && deviceWaterDetail.getLatestValue() != null) {
                for (MonitorDataValueEntity monitorDataValueEntity : monitorDataEntity.getDataValues()) {
                    if (monitorDataValueEntity.getAttributeId().equals(deviceWaterDetail.getAttributeId())) {
                        //累计当天流量
                        waterSupplyAmountForDay = waterSupplyAmountForDay.add(new BigDecimal(monitorDataValueEntity.getDataValue()).subtract(deviceWaterDetail.getLatestValue()));
                    }
                }
            }
        }
        //水源获取实时流量并计算出当天流量
        for (DeviceWaterDetail deviceWaterDetail : deviceIntakeWaterDetailsForDay) {
            MonitorDataEntity monitorDataEntity = MonitorDataMap.get(deviceWaterDetail.getDeviceId());
            if (monitorDataEntity != null && monitorDataEntity.getDataValues() != null && monitorDataEntity.getDataValues().size() > 0
                    && deviceWaterDetail != null && deviceWaterDetail.getLatestValue() != null) {
                for (MonitorDataValueEntity monitorDataValueEntity : monitorDataEntity.getDataValues()) {
                    if (monitorDataValueEntity.getAttributeId().equals(deviceWaterDetail.getAttributeId())) {
                        //累计当天流量
                        waterIntakeAmountForDay = waterIntakeAmountForDay.add(new BigDecimal(monitorDataValueEntity.getDataValue()).subtract(deviceWaterDetail.getLatestValue()));
                    }
                }
            }
        }

        BigDecimal waterSupplyAmountForMonth = monitorDataReportMapper.getDeviceWaterDetailForMonth(companyOrgId, null, "水厂", 3, tenantId, localDate.getYear(), localDate.getMonthValue(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        if (waterSupplyAmountForMonth == null) {
            waterSupplyAmountForMonth = BigDecimal.ZERO;
        }
        //水厂当月流量
        companyProduce.setWaterSupply(
                (waterSupplyAmountForMonth.add(waterSupplyAmountForDay)).divide(new BigDecimal("10000"), 3, BigDecimal.ROUND_HALF_UP));

        BigDecimal waterIntakeAmountForMonth = monitorDataReportMapper.getDeviceWaterDetailForMonth(companyOrgId, null, "水源", 4, tenantId, localDate.getYear(), localDate.getMonthValue(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        if (waterIntakeAmountForMonth == null) {
            waterIntakeAmountForMonth = BigDecimal.ZERO;
        }
        //水源当月流量
        companyProduce.setWaterIntake(
                (waterIntakeAmountForMonth.add(waterIntakeAmountForDay)).divide(new BigDecimal("10000"), 3, BigDecimal.ROUND_HALF_UP));

        return companyProduce;
    }


    @Override
    public WaterQualityRate getWaterQualification(Integer companyOrgId) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        WaterQualityRate waterQualityRate = new WaterQualityRate();
        List<Integer> list1 = Stream.of(15, 7, 9, 11, 18, 19).collect(Collectors.toList()); //取水
        List<Integer> list2 = Stream.of(15, 8, 10, 11, 18, 19).collect(Collectors.toList()); //供水
        //管网水质
        WaterQualityRate pipeNetworkQuality = homePageReportMapper.getPipeNetworkQualification(companyOrgId, loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //水源水质
        WaterQualityRate waterSourceQuality = homePageReportMapper.getWaterQualification(companyOrgId, "水源", list1, loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //水厂水质
        WaterQualityRate waterFactoryQuality = homePageReportMapper.getWaterQualification(companyOrgId, "水厂", list2, loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //二供水质
        WaterQualityRate pumpingStationQuality = homePageReportMapper.getWaterQualification(companyOrgId, "泵站", list2, loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());

        if (pipeNetworkQuality != null) {
            waterQualityRate.setAbnormal(waterQualityRate.getAbnormal() + pipeNetworkQuality.getAbnormal());
            waterQualityRate.setPipeNetworkQuality(pipeNetworkQuality.getAverageQuality());
            waterQualityRate.setPipeNetworkAbnormal(pipeNetworkQuality.getAbnormal());
            waterQualityRate.setAverageQuality(pipeNetworkQuality.getAverageQuality());
        } else {
            waterQualityRate.setAverageQuality(new BigDecimal("100"));
        }
        if (waterSourceQuality != null) {
            waterQualityRate.setAbnormal(waterQualityRate.getAbnormal() + waterSourceQuality.getAbnormal());
            waterQualityRate.setWaterSourceQuality(waterSourceQuality.getAverageQuality());
            waterQualityRate.setWaterSourceAbnormal(waterSourceQuality.getAbnormal());
            waterQualityRate.setAverageQuality(waterQualityRate.getAverageQuality().add(waterSourceQuality.getAverageQuality()));
        } else {
            waterQualityRate.setAverageQuality(waterQualityRate.getAverageQuality().add(new BigDecimal("100")));
        }
        if (waterFactoryQuality != null) {
            waterQualityRate.setAbnormal(waterQualityRate.getAbnormal() + waterFactoryQuality.getAbnormal());
            waterQualityRate.setWaterFactoryQuality(waterFactoryQuality.getAverageQuality());
            waterQualityRate.setWaterFactoryAbnormal(waterFactoryQuality.getAbnormal());
            waterQualityRate.setAverageQuality(waterQualityRate.getAverageQuality().add(waterFactoryQuality.getAverageQuality()));
        } else {
            waterQualityRate.setAverageQuality(waterQualityRate.getAverageQuality().add(new BigDecimal("100")));
        }
        if (pumpingStationQuality != null) {
            waterQualityRate.setAbnormal(waterQualityRate.getAbnormal() + pumpingStationQuality.getAbnormal());
            waterQualityRate.setPumpingStationQuality(pumpingStationQuality.getAverageQuality());
            waterQualityRate.setPumpingStationAbnormal(pumpingStationQuality.getAbnormal());
            waterQualityRate.setAverageQuality(waterQualityRate.getAverageQuality().add(pumpingStationQuality.getAverageQuality()));
        } else {
            waterQualityRate.setAverageQuality(waterQualityRate.getAverageQuality().add(new BigDecimal("100")));
        }
        waterQualityRate.setAverageQuality(waterQualityRate.getAverageQuality().divide(new BigDecimal("4"), 3, BigDecimal.ROUND_HALF_UP));

        return waterQualityRate;
    }


    /**
     * 水厂泵站 3 水源 4 水位 12
     *
     * @return
     */
    @Override
    public List<DeviceWaterSupply> monitorDataFifteenDays(Integer sceneId, String sceneType, Integer parmType) {
        BigDecimal multipleData;
        if (sceneType.equals("泵站")) {
            multipleData = new BigDecimal("1");
        } else {
            multipleData = new BigDecimal("10000");
        }
        LoginUser loginUser = UserUtil.getCurrentUser();

        List<DeviceWaterSupply> deviceWaterSupplies = homePageReportMapper.getMonitorDataFifteenDays(sceneId, sceneType, parmType,
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());

        LocalDate localDate = LocalDate.now();
        //当天数据
        BigDecimal amountCount = monitorDataReportService.getAmountCount(null, sceneId, sceneType, parmType, 1);
        DeviceWaterSupply deviceWaterSupplyToday = new DeviceWaterSupply();
        deviceWaterSupplyToday.setOrderNo(1);
        deviceWaterSupplyToday.setAmount(amountCount != null ? amountCount : BigDecimal.ZERO);
        deviceWaterSupplyToday.setYear(localDate.getYear());
        deviceWaterSupplyToday.setMonth(localDate.getMonthValue());
        deviceWaterSupplyToday.setDay(localDate.getDayOfMonth());
        deviceWaterSupplyToday.setDate(localDate);
        deviceWaterSupplies.add(0, deviceWaterSupplyToday);

        for (int i = 0; i < 15; i++) {
            LocalDate newLocalDate = localDate.plusDays(-i);
            if (i == deviceWaterSupplies.size()
                    || !newLocalDate.isEqual(LocalDate.of(deviceWaterSupplies.get(i).getYear(),
                    deviceWaterSupplies.get(i).getMonth(),
                    deviceWaterSupplies.get(i).getDay()))) {

                DeviceWaterSupply deviceWaterSupply = new DeviceWaterSupply();
                deviceWaterSupply.setOrderNo(i + 1);
                deviceWaterSupply.setAmount(BigDecimal.ZERO);
                deviceWaterSupply.setYear(newLocalDate.getYear());
                deviceWaterSupply.setMonth(newLocalDate.getMonthValue());
                deviceWaterSupply.setDay(newLocalDate.getDayOfMonth());
                deviceWaterSupply.setDate(newLocalDate);
                deviceWaterSupplies.add(i, deviceWaterSupply);
            } else {
                deviceWaterSupplies.get(i).setOrderNo(i + 1);
                if (parmType != 12) { //不是水位就除以1000 转化为万
                    deviceWaterSupplies.get(i).amountConvert(multipleData);
                }
                deviceWaterSupplies.get(i).setDate(newLocalDate);
            }
        }
        deviceWaterSupplies.sort(Comparator.comparing(DeviceWaterSupply::getOrderNo).reversed());

        return deviceWaterSupplies;
    }


    @Override
    public WaterQualityRateForScene waterQualityRateForWaterSource(Integer sceneId) {

        LoginUser loginUser = UserUtil.getCurrentUser();
        LocalDate localDate = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        //余氯
        BigDecimal chlorine = homePageReportMapper.getWaterQualificationByMonth(localDate, 11, sceneId, "水源",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //浊度
        BigDecimal turbidity = homePageReportMapper.getWaterQualificationByMonth(localDate, 9, sceneId, "水源",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //COD
        BigDecimal cod = homePageReportMapper.getWaterQualificationByMonth(localDate, 15, sceneId, "水源",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //PH
        BigDecimal ph = homePageReportMapper.getWaterQualificationByMonth(localDate, 7, sceneId, "水源",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //压力
        BigDecimal pressure = homePageReportMapper.getWaterQualificationByMonth(localDate, 13, sceneId, "水源",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());

        WaterQualityRateForScene waterQualityRateForScene = new WaterQualityRateForScene();
        waterQualityRateForScene.setChlorine(chlorine != null ? chlorine : new BigDecimal("100"));
        waterQualityRateForScene.setCod(cod != null ? cod : new BigDecimal("100"));
        waterQualityRateForScene.setTurbidity(turbidity != null ? turbidity : new BigDecimal("100"));
        waterQualityRateForScene.setPh(ph != null ? ph : new BigDecimal("100"));
        waterQualityRateForScene.setPressure(pressure != null ? pressure : new BigDecimal("100"));
        return waterQualityRateForScene;
    }

    @Override
    public WaterQualityRateForScene waterQualityRateForPumpingStation(Integer sceneId) {

        LoginUser loginUser = UserUtil.getCurrentUser();
        LocalDate localDate = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        //余氯
        BigDecimal chlorine = homePageReportMapper.getWaterQualificationByMonth(localDate, 11, sceneId, "泵站",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //浊度
        BigDecimal turbidity = homePageReportMapper.getWaterQualificationByMonth(localDate, 10, sceneId, "泵站",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //COD
        BigDecimal cod = homePageReportMapper.getWaterQualificationByMonth(localDate, 15, sceneId, "泵站",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //PH
        BigDecimal ph = homePageReportMapper.getWaterQualificationByMonth(localDate, 8, sceneId, "泵站",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //压力
        BigDecimal pressure = homePageReportMapper.getWaterQualificationByMonth(localDate, 13, sceneId, "泵站",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());

        WaterQualityRateForScene waterQualityRateForScene = new WaterQualityRateForScene();
        waterQualityRateForScene.setChlorine(chlorine != null ? chlorine : new BigDecimal("100"));
        waterQualityRateForScene.setCod(cod != null ? cod : new BigDecimal("100"));
        waterQualityRateForScene.setTurbidity(turbidity != null ? turbidity : new BigDecimal("100"));
        waterQualityRateForScene.setPh(ph != null ? ph : new BigDecimal("100"));
        waterQualityRateForScene.setPressure(pressure != null ? pressure : new BigDecimal("100"));
        return waterQualityRateForScene;
    }

    @Override
    public WaterQualityRateForScene waterQualityRateForWaterFactory(Integer sceneId) {

        LoginUser loginUser = UserUtil.getCurrentUser();
        LocalDate localDate = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        //余氯
        BigDecimal chlorine = homePageReportMapper.getWaterQualificationByMonth(localDate, 11, sceneId, "水厂",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //浊度
        BigDecimal turbidity = homePageReportMapper.getWaterQualificationByMonth(localDate, 10, sceneId, "水厂",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //PH
        BigDecimal ph = homePageReportMapper.getWaterQualificationByMonth(localDate, 8, sceneId, "水厂",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //COD
        BigDecimal cod = homePageReportMapper.getWaterQualificationByMonth(localDate, 15, sceneId, "水厂",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());
        //压力
        BigDecimal pressure = homePageReportMapper.getWaterQualificationByMonth(localDate, 13, sceneId, "水厂",
                loginUser.getTenantId(), loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());

        WaterQualityRateForScene waterQualityRateForScene = new WaterQualityRateForScene();
        waterQualityRateForScene.setChlorine(chlorine != null ? chlorine : new BigDecimal("100"));
        waterQualityRateForScene.setTurbidity(turbidity != null ? turbidity : new BigDecimal("100"));
        waterQualityRateForScene.setCod(cod != null ? cod : new BigDecimal("100"));
        waterQualityRateForScene.setPh(ph != null ? ph : new BigDecimal("100"));
        waterQualityRateForScene.setPressure(pressure != null ? pressure : new BigDecimal("100"));
        return waterQualityRateForScene;
    }

    //制水、药耗、电耗对比曲线
    @Override
    public List<WaterSupplyChart> waterEnergyConsumption(Integer sceneId, String sceneType) {
        BigDecimal multipleData;
        if (sceneType.equals("泵站")) {
            multipleData = new BigDecimal("1");
        } else {
            multipleData = new BigDecimal("10000");
        }

        LoginUser loginUser = UserUtil.getCurrentUser();
        String tenantId = loginUser.getTenantId();

        List<WaterSupplyData> waterSupplyDatas = homePageReportMapper.getWaterSupplyData(sceneId, sceneType, tenantId, loginUser.getType(), loginUser.getPermissonType(), loginUser.getProgramItemList());

        LocalDate localDate = LocalDate.now();
        //当天数据
        BigDecimal waterDataCountDay = monitorDataReportService.getAmountCount(null, sceneId, sceneType, 3, 1);
        BigDecimal powerDataCountDay = monitorDataReportService.getAmountCount(null, sceneId, sceneType, 5, 1);
        BigDecimal drugDataCountDay = monitorDataReportService.getAmountCount(null, sceneId, sceneType, 6, 1);
        WaterSupplyChart waterSupplyChartToday = new WaterSupplyChart();
        waterSupplyChartToday.setSort(localDate.getDayOfMonth());
        waterSupplyChartToday.setDate(DateTimeFormatter.ofPattern("yyyy-MM-dd").format(localDate));
        waterSupplyChartToday.setWaterData(waterDataCountDay.divide(multipleData, 3, BigDecimal.ROUND_HALF_UP));
        waterSupplyChartToday.setPowerData(powerDataCountDay.divide(multipleData, 3, BigDecimal.ROUND_HALF_UP));
        waterSupplyChartToday.setDrugData(drugDataCountDay);


        List<WaterSupplyChart> waterSupplyCharts = new ArrayList<>();
        waterSupplyDatas.stream()
                .collect(Collectors.groupingBy(item -> item.getYear() + "-" + item.getMonth() + "-" + item.getDay()))
                .forEach((key, value) -> {
                    WaterSupplyChart waterSupplyChart = new WaterSupplyChart();
                    waterSupplyChart.setDate(key);
                    value.stream().forEach(waterSupplyData -> {
                        waterSupplyChart.setSort(waterSupplyData.getDay());

                        if (waterSupplyData.getParmType() == 3&&waterSupplyData.getAmount()!=null) { //供水
                            waterSupplyChart.setWaterData(waterSupplyData.getAmount().divide(multipleData, 3, BigDecimal.ROUND_HALF_UP));
                        } else if (waterSupplyData.getParmType() == 5&&waterSupplyData.getAmount()!=null) {//电耗
                            waterSupplyChart.setPowerData(waterSupplyData.getAmount().divide(multipleData, 3, BigDecimal.ROUND_HALF_UP));
                        } else if (waterSupplyData.getParmType() == 6&&waterSupplyData.getAmount()!=null) {//药耗
                            waterSupplyChart.setDrugData(waterSupplyData.getAmount());
                        }
                    });
                    waterSupplyCharts.add(waterSupplyChart);
                });

        waterSupplyCharts.sort(Comparator.comparing(WaterSupplyChart::getSort));

        Integer maxDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
        for (int i = 0; i < maxDay; i++) {
            if (i + 1 == localDate.getDayOfMonth()) {
                waterSupplyCharts.add(i, waterSupplyChartToday);
            } else if (i == waterSupplyCharts.size() || !waterSupplyCharts.get(i).getSort().equals(i + 1)) {
                waterSupplyCharts.add(i, new WaterSupplyChart(i + 1, localDate.getYear() + "-" + localDate.getMonthValue() + "-" + (i + 1)));
            }

        }
//        waterSupplyCharts.sort(Comparator.comparing(WaterSupplyChart::getSort).reversed());
        return waterSupplyCharts;
    }
}
