package com.zcxk.charge.web.service.impl;

import com.zcxk.charge.api.dto.query.DeviceWaterDto;
import com.zcxk.charge.api.dto.query.HomePageReportDto;
import com.zcxk.charge.api.enums.ParamTypeEnum;
import com.zcxk.charge.api.enums.SceneTypeEnum;
import com.zcxk.charge.api.vo.*;
import com.zcxk.charge.web.client.UserCenterClient;
import com.zcxk.charge.core.dao.HomePageReportMapper;
import com.zcxk.charge.core.dao.MonitorDataReportMapper;
import com.zcxk.charge.api.dto.generalview.WaterSupplyChart;
import com.zcxk.charge.api.dto.homepage.WaterQualityRate;
import com.zcxk.charge.api.dto.homepage.WaterQualityRateForScene;
import com.zcxk.charge.core.entity.MonitorDataEntity;
import com.zcxk.charge.core.entity.MonitorDataValueEntity;
import com.zcxk.charge.web.service.HomePageReportService;
import com.zcxk.charge.web.service.MonitorDataReportService;
import com.zcxk.core.common.pojo.UserCondition;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sun.reflect.misc.ConstructorUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

    @Resource
    private HomePageReportMapper homePageReportMapper;

    @Resource
    private UserCenterClient userCenterClient;

    @Resource
    private MonitorDataReportMapper monitorDataReportMapper;

    @Autowired
    private MonitorDataReportService monitorDataReportService;

    @Override
    public List<DeviceWaterSupplyVo> deviceWaterReportForSixMonth(Integer companyOrgId, String sceneType, Integer parmType) {
        LocalDate localDate = LocalDate.now();
        HomePageReportDto dto = new HomePageReportDto();
        dto.setCompanyOrgId(companyOrgId);
        dto.setSceneType(sceneType);
        dto.setParmType(parmType);
        List<DeviceWaterSupplyVo> deviceWaterSupplies = homePageReportMapper.getDeviceWaterForSixMonth(dto, UserUtil.getCurrentUser().getUserCondition());
        BigDecimal amountCount = monitorDataReportService.getAmountCount(companyOrgId, null, sceneType, parmType, 2);

        DeviceWaterSupplyVo deviceWaterSupplyMonth = convertDeviceWaterSupplyVo(1, amountCount != null ? amountCount.divide(new BigDecimal("10000")) : BigDecimal.ZERO, localDate);
        deviceWaterSupplyMonth.setDateLabel(localDate.getYear() + "-" + localDate.getMonthValue());
        deviceWaterSupplies.add(0, deviceWaterSupplyMonth);
        int length = 6;
        for (int i = 0; i < length; i++) {
            LocalDate newLocalDate = localDate.plusMonths(-i);
            if (i == deviceWaterSupplies.size()
                    || newLocalDate.getMonthValue() != deviceWaterSupplies.get(i).getMonth().intValue()
                    || newLocalDate.getYear() != deviceWaterSupplies.get(i).getYear().intValue()) {
                DeviceWaterSupplyVo deviceWaterSupply = convertDeviceWaterSupplyVo(i, BigDecimal.ZERO,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(DeviceWaterSupplyVo::getOrderNo).reversed());


        return deviceWaterSupplies;
    }

    @Override
    public CompanyCountVo countSceneByType(Integer companyOrgId) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        HomePageReportDto dto = new HomePageReportDto();
        dto.setCompanyOrgId(companyOrgId);
        CompanyCountVo companyCount = homePageReportMapper.countSceneByType(dto, loginUser.getUserCondition());
        Integer company = userCenterClient.countCompanyByUser();
        companyCount.setCompany(company != null ? company : 0);
        return companyCount;
    }


    @Override
    public CompanyProduceVo deviceWaterAmount(Integer companyOrgId) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        Map<Integer, MonitorDataEntity> monitorDataMap = monitorDataReportService.getIntegerMonitorDataEntityMap(loginUser);
        LocalDate localDate = LocalDate.now();
        HomePageReportDto homeDto = new HomePageReportDto();
        homeDto.setCompanyOrgId(companyOrgId);
        CompanyProduceVo companyProduce = homePageReportMapper.getWaterSalesDetails(homeDto, loginUser.getUserCondition());
        if (companyProduce == null) {
            companyProduce = new CompanyProduceVo();
        }
        // 3供水 4取水
        DeviceWaterDto queryDto = convertDeviceWaterDto(companyOrgId,3,  SceneTypeEnum.WATER_WORKS.getMessage(), localDate.plusDays(-1).getYear(), localDate.plusDays(-1).getMonthValue(), localDate.plusDays(-1).getDayOfMonth());
        List<DeviceWaterDetailVo> deviceSupplyWaterDetailsForDay = monitorDataReportMapper.getDeviceWaterDetailForDay(queryDto, loginUser.getUserCondition());

        queryDto.setParmType(4);
        queryDto.setSceneTypeName(SceneTypeEnum.WATER_SOURCE.getMessage());
        List<DeviceWaterDetailVo> deviceIntakeWaterDetailsForDay = monitorDataReportMapper.getDeviceWaterDetailForDay(queryDto, loginUser.getUserCondition());

        // 水厂获取实时流量并计算出当天流量
        BigDecimal waterSupplyAmountForDay = getAmountForDay(deviceSupplyWaterDetailsForDay, monitorDataMap);
        // 水源获取实时流量并计算出当天流量
        BigDecimal waterIntakeAmountForDay = getAmountForDay(deviceIntakeWaterDetailsForDay, monitorDataMap);
        DeviceWaterDto dto = convertDeviceWaterDto(companyOrgId,3,  SceneTypeEnum.WATER_WORKS.getMessage(), localDate.getYear(), localDate.getMonthValue(), null);
        // 水厂当月流量
        companyProduce.setWaterSupply(waterSupplyAmountForMonth(dto, waterSupplyAmountForDay));
        dto.setParmType(4);
        dto.setSceneTypeName(SceneTypeEnum.WATER_SOURCE.getMessage());
        // 水源当月流量
        companyProduce.setWaterIntake(waterSupplyAmountForMonth(dto, waterIntakeAmountForDay));
        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());
        HomePageReportDto homeDto = new HomePageReportDto();
        homeDto.setCompanyOrgId(companyOrgId);
        homeDto.setSceneType(SceneTypeEnum.WATER_SOURCE.getMessage());
        homeDto.setParmTypes(list1);
        // 管网水质
        WaterQualityRateVo pipeNetworkQuality = homePageReportMapper.getPipeNetworkQualification(homeDto, loginUser.getUserCondition());
        // 水源水质
        WaterQualityRateVo waterSourceQuality = homePageReportMapper.getWaterQualification(homeDto, loginUser.getUserCondition());
        // 水厂水质
        homeDto.setSceneType(SceneTypeEnum.WATER_WORKS.getMessage());
        homeDto.setParmTypes(list2);
        WaterQualityRateVo waterFactoryQuality = homePageReportMapper.getWaterQualification(homeDto, loginUser.getUserCondition());
        // 二供水质
        homeDto.setSceneType(SceneTypeEnum.PUMPING_STATION.getMessage());
        WaterQualityRateVo pumpingStationQuality = homePageReportMapper.getWaterQualification(homeDto, loginUser.getUserCondition());

        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;
    }

    @Override
    public List<DeviceWaterSupplyVo> monitorDataFifteenDays(Integer sceneId, String sceneType, Integer parmType) {
        BigDecimal multipleData   = getMultipleData(sceneType);
        HomePageReportDto homeDto = new HomePageReportDto();
        homeDto.setSceneId(sceneId);
        homeDto.setSceneType(sceneType);
        homeDto.setParmType(parmType);
        List<DeviceWaterSupplyVo> deviceWaterSupplies = homePageReportMapper.getMonitorDataFifteenDays(homeDto, UserUtil.getCurrentUser().getUserCondition());
        LocalDate localDate = LocalDate.now();
        //当天数据
        BigDecimal amountCount = monitorDataReportService.getAmountCount(null, sceneId, sceneType, parmType, 1);
        DeviceWaterSupplyVo deviceWaterSupplyToday = new DeviceWaterSupplyVo();
        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);
        int length = 15;
        for (int i = 0; i < length; 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()))) {
                DeviceWaterSupplyVo deviceWaterSupply = convertDeviceWaterSupplyVo(i, BigDecimal.ZERO, newLocalDate);
                deviceWaterSupply.setDay(newLocalDate.getDayOfMonth());
                deviceWaterSupplies.add(i, deviceWaterSupply);
            } else {
                deviceWaterSupplies.get(i).setOrderNo(i + 1);
                // 不是水位就除以1000 转化为万
                if (parmType != 12) {
                    deviceWaterSupplies.get(i).amountConvert(multipleData);
                }
                deviceWaterSupplies.get(i).setDate(newLocalDate);
            }
        }
        deviceWaterSupplies.sort(Comparator.comparing(DeviceWaterSupplyVo::getOrderNo).reversed());
        return deviceWaterSupplies;
    }

    private DeviceWaterSupplyVo convertDeviceWaterSupplyVo(int number, BigDecimal amount , LocalDate newLocalDate){
        DeviceWaterSupplyVo deviceWaterSupply = new DeviceWaterSupplyVo();
        deviceWaterSupply.setOrderNo(number + 1);
        deviceWaterSupply.setAmount(amount);
        deviceWaterSupply.setYear(newLocalDate.getYear());
        deviceWaterSupply.setMonth(newLocalDate.getMonthValue());
        deviceWaterSupply.setDate(newLocalDate);
        return deviceWaterSupply;
    }


    @Override
    public WaterQualityRateForScene waterQualityRateForWaterSource(Integer sceneId) {
        UserCondition userCondition = UserUtil.getCurrentUser().getUserCondition();
        LocalDate localDate = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        // 余氯
        HomePageReportDto homeDto = new HomePageReportDto();
        homeDto.setMonthDate(localDate);
        homeDto.setParmType(ParamTypeEnum.DPD.getCode());
        homeDto.setSceneId(sceneId);
        homeDto.setSceneType(SceneTypeEnum.WATER_SOURCE.getMessage());
        BigDecimal chlorine = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // 浊度
        homeDto.setParmType(ParamTypeEnum.TURBIDITY_9.getCode());
        BigDecimal turbidity = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // COD
        homeDto.setParmType(ParamTypeEnum.COD.getCode());
        BigDecimal cod = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // PH
        homeDto.setParmType(ParamTypeEnum.PH_7.getCode());
        BigDecimal ph = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // 压力
        homeDto.setParmType(ParamTypeEnum.PRESSURE.getCode());
        BigDecimal pressure = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        return convertWaterQualityRateForScene(chlorine, cod, ph, turbidity, pressure);
    }

    @Override
    public WaterQualityRateForScene waterQualityRateForPumpingStation(Integer sceneId) {

        UserCondition userCondition = UserUtil.getCurrentUser().getUserCondition();
        LocalDate localDate = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        // 泵站
        HomePageReportDto homeDto = new HomePageReportDto();
        homeDto.setMonthDate(localDate);
        homeDto.setSceneType(SceneTypeEnum.PUMPING_STATION.getMessage());
        homeDto.setSceneId(sceneId);
        // 余氯
        homeDto.setParmType(ParamTypeEnum.DPD.getCode());
        BigDecimal chlorine = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // 浊度
        homeDto.setParmType(ParamTypeEnum.TURBIDITY_10.getCode());
        BigDecimal turbidity = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // COD
        homeDto.setParmType(ParamTypeEnum.COD.getCode());
        BigDecimal cod = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // PH
        homeDto.setParmType(ParamTypeEnum.PH_8.getCode());
        BigDecimal ph = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // 压力
        homeDto.setParmType(ParamTypeEnum.PRESSURE.getCode());
        BigDecimal pressure = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        return convertWaterQualityRateForScene(chlorine, cod, ph, turbidity, pressure);
    }


    @Override
    public WaterQualityRateForScene waterQualityRateForWaterFactory(Integer sceneId) {

        UserCondition userCondition = UserUtil.getCurrentUser().getUserCondition();
        LocalDate localDate = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        // 水厂
        HomePageReportDto homeDto = new HomePageReportDto();
        homeDto.setMonthDate(localDate);
        homeDto.setSceneId(sceneId);
        homeDto.setSceneType(SceneTypeEnum.WATER_WORKS.getMessage());
        // 余氯
        homeDto.setParmType(ParamTypeEnum.DPD.getCode());
        BigDecimal chlorine = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // 浊度
        homeDto.setParmType(ParamTypeEnum.TURBIDITY_10.getCode());
        BigDecimal turbidity = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // PH
        homeDto.setParmType(ParamTypeEnum.PH_8.getCode());
        BigDecimal ph = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // COD
        homeDto.setParmType(ParamTypeEnum.COD.getCode());
        BigDecimal cod = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        // 压力
        homeDto.setParmType(ParamTypeEnum.PRESSURE.getCode());
        BigDecimal pressure = homePageReportMapper.getWaterQualificationByMonth(homeDto, userCondition);
        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 = getMultipleData(sceneType);
        HomePageReportDto homeDto = new HomePageReportDto();
        homeDto.setSceneId(sceneId);
        homeDto.setSceneType(sceneType);
        List<WaterSupplyDataVo> supplyDataVos = homePageReportMapper.getWaterSupplyData(homeDto, UserUtil.getCurrentUser().getUserCondition());
        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<>();
        supplyDataVos.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() == ParamTypeEnum.WATER_SUPPLY.getCode() &&waterSupplyData.getAmount()!=null) {
                        waterSupplyChart.setWaterData(waterSupplyData.getAmount().divide(multipleData, 3, BigDecimal.ROUND_HALF_UP));
                    } else if (waterSupplyData.getParmType() == ParamTypeEnum.KWH.getCode() &&waterSupplyData.getAmount()!=null) {
                        waterSupplyChart.setPowerData(waterSupplyData.getAmount().divide(multipleData, 3, BigDecimal.ROUND_HALF_UP));
                    } else if (waterSupplyData.getParmType() == ParamTypeEnum.DRUG_CONSUMPTION.getCode() &&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)));
            }
        }
        return waterSupplyCharts;
    }



    private BigDecimal getAmountForDay(List<DeviceWaterDetailVo> deviceIntakeWaterDetailsForDay, Map<Integer, MonitorDataEntity> monitorDataMap){
        BigDecimal waterIntakeAmountForDay = BigDecimal.ZERO;
        for (DeviceWaterDetailVo deviceWaterDetail : deviceIntakeWaterDetailsForDay) {
            if (deviceWaterDetail.getLatestValue() == null) {
                continue;
            }
            MonitorDataEntity monitorDataEntity = monitorDataMap.get(deviceWaterDetail.getDeviceId());
            if (monitorDataEntity == null) {
                continue;
            }
            if (CollectionUtils.isEmpty(monitorDataEntity.getDataValues())) {
                continue;
            }
            for (MonitorDataValueEntity monitorDataValueEntity : monitorDataEntity.getDataValues()) {
                if (monitorDataValueEntity.getAttributeId().equals(deviceWaterDetail.getAttributeId())) {
                    // 累计当天流量
                    waterIntakeAmountForDay = waterIntakeAmountForDay.add(new BigDecimal(monitorDataValueEntity.getDataValue()).subtract(deviceWaterDetail.getLatestValue()));
                }
            }
        }
        return waterIntakeAmountForDay;
    }

    private DeviceWaterDto convertDeviceWaterDto(Integer companyOrgId, int type, String sceneTypeName, Integer year, Integer month, Integer day){
        DeviceWaterDto dto = new DeviceWaterDto();
        dto.setCompanyOrgId(companyOrgId);
        dto.setSceneTypeName(sceneTypeName);
        dto.setParmType(type);
        dto.setYear(year);
        dto.setMonth(month);
        dto.setDay(day);
        return dto;
    }

    /**
     * 水厂
     * @author Andy
     * @date 15:19 2021/8/18
     * @param dto:
     * @return java.math.BigDecimal
     **/
    private BigDecimal waterSupplyAmountForMonth(DeviceWaterDto dto, BigDecimal waterSupplyAmountForDay){
        BigDecimal waterSupplyAmountForMonth = monitorDataReportMapper.getDeviceWaterDetailForMonth(dto, UserUtil.getCurrentUser().getUserCondition());
        if (waterSupplyAmountForMonth == null) {
            waterSupplyAmountForMonth = BigDecimal.ZERO;
        }
        return (waterSupplyAmountForMonth.add(waterSupplyAmountForDay)).divide(new BigDecimal("10000"), 3, BigDecimal.ROUND_HALF_UP);
    }

    private WaterQualityRateForScene convertWaterQualityRateForScene(BigDecimal chlorine, BigDecimal cod, BigDecimal ph, BigDecimal turbidity, BigDecimal pressure){
        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;
    }

    private BigDecimal getMultipleData(String sceneType){
        BigDecimal multipleData;
        if (SceneTypeEnum.PUMPING_STATION.getMessage().equals(sceneType)) {
            multipleData = new BigDecimal("1");
        } else {
            multipleData = new BigDecimal("10000");
        }
        return multipleData;
    }


}
