package com.zcxk.charge.web.service;

import com.zcxk.charge.api.dto.*;
import com.zcxk.charge.api.enums.SceneTypeEnum;
import com.zcxk.charge.api.vo.ParmTypeCountVo;
import com.zcxk.charge.core.entity.DeviceParmEntity;
import com.zcxk.charge.core.entity.MonthReportEntity;
import com.zcxk.charge.core.entity.SceneEntity;
import com.zcxk.charge.web.service.impl.MonitorDataReportServiceImpl;
import com.zcxk.core.utils.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author  andy
 */
@Service
public class SecSupplyService {

    @Autowired
    private SceneService sceneService;

    @Autowired
    private MonthReportService monthReportService;

    @Autowired
    private DeviceParmService deviceParmService;

    @Autowired
    private MonitorDataReportServiceImpl monitorDataReportServiceImpl;

    /**
     * 查询30天供水量
     * @param sceneEntity
     * @return
     */
    public List<AmountDayThirtyDto> getAmountDayThirty(SceneEntity sceneEntity) {
        List<SceneEntity> sceneEntities = sceneService.selectByTypeName(sceneEntity);
        if (sceneEntities.size() == 0) {
            return null;
        }
        MonthReportEntity monthReportEntity = new MonthReportEntity();
        monthReportEntity.setParentSceneLists(sceneEntities);
        List<MonthReportEntity> monthReportEntities = monthReportService.findAmountBySceneIds(monthReportEntity);
        Map<String, Double> maps = new LinkedHashMap<>();
        for (MonthReportEntity item : monthReportEntities) {
            maps.put(item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()), item.getSumValue());
        }
        // 得到一个Calendar的实例
        Calendar begin = Calendar.getInstance();
        // 设置时间为当前时间
        begin.setTime(new Date());
        List<AmountDayThirtyDto> listC = new ArrayList<>();
        for (int i = 1; i <= 15; i++) {
            begin.add(Calendar.DATE, -1);
            Date d = new Date(begin.getTimeInMillis());
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            AmountDayThirtyDto amountDayThirtyDto = new AmountDayThirtyDto();
            if (maps.containsKey(df.format(d).toString())) {
                amountDayThirtyDto.setAmountTime(d);
                if (maps.get(df.format(d)) != null) {
                    amountDayThirtyDto.setAmount((double) Math.round(maps.get(df.format(d)) * 1000) / 1000);
                }
            } else {
                amountDayThirtyDto.setAmountTime(d);
                amountDayThirtyDto.setAmount(0d);
            }
            listC.add(amountDayThirtyDto);
        }

        return listC;
    }

    /**
     * 查询供水及瞬时流量
     * @param sceneEntity
     * @return
     */
    public AmountTotalDto findAmountTotalByTypeName(SceneEntity sceneEntity) {
        BigDecimal multipleData;
        if (SceneTypeEnum.PUMPING_STATION.getMessage().equals(sceneEntity.getSceneTypeName())){
            multipleData = new BigDecimal("1");
        } else {
            multipleData = new BigDecimal("10000");
        }
        AmountTotalDto amountTotalDto = new AmountTotalDto();
        BigDecimal  instantFlow = monitorDataReportServiceImpl.getAmountCount(null,null,sceneEntity.getSceneTypeName(),14,1);
        BigDecimal  dayAmount = monitorDataReportServiceImpl.getAmountCount(null,null,sceneEntity.getSceneTypeName(),3,1);
        BigDecimal  monthAmount = monitorDataReportServiceImpl.getAmountCount(null,null,sceneEntity.getSceneTypeName(),3,2);
        BigDecimal  yearAmount = monitorDataReportServiceImpl.getAmountCount(null,null,sceneEntity.getSceneTypeName(),3,3);
        amountTotalDto.setInstantFlow(instantFlow.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue());
        amountTotalDto.setDayAmount(dayAmount.divide(multipleData,3,BigDecimal.ROUND_HALF_UP).doubleValue());
        amountTotalDto.setMonthAmount(monthAmount.divide(multipleData,3,BigDecimal.ROUND_HALF_UP).doubleValue());
        amountTotalDto.setYearAmount(yearAmount.divide(multipleData,3,BigDecimal.ROUND_HALF_UP).doubleValue());
        return amountTotalDto;
    }

    public WaterPieDto selectWaterQualityByTypeName(SceneEntity sceneEntity) {
        List<SceneEntity> sceneEntities = sceneService.selectByTypeName(sceneEntity);
        if (sceneEntities.size() == 0) {
            return null;
        }
        WaterPieDto waterPieDto = new WaterPieDto();
        DeviceParmEntity deviceParmEntity = new DeviceParmEntity();
        deviceParmEntity.setSceneEntities(sceneEntities);
        List<ParmTypeCountVo> listTotal = deviceParmService.findAlarmTotalCount(deviceParmEntity);
        List<ParmTypeCountVo> listAlarmTotal =deviceParmService.findAlarmCountTotalCount(deviceParmEntity);
        WaterPieStateDto waterPieStateDto4 = new WaterPieStateDto();
        waterPieStateDto4.setNormalCount(listTotal.get(0).getTotalCount()- listAlarmTotal.get(0).getNbnormalCount());
        waterPieStateDto4.setNbnormalCount(listAlarmTotal.get(0).getNbnormalCount());
        waterPieStateDto4.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- listAlarmTotal.get(0).getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
        waterPieDto.setWaterQuality(waterPieStateDto4);
        List<ParmTypeCountVo> list = deviceParmService.findAlarmCount(deviceParmEntity);
        // 余氯11 浊度9  PH8  cod 15   15,8,9,11,18,19
        for (ParmTypeCountVo item : list)  {
            switch(item.getParmType()) {
                case 8:
                    WaterPieStateDto waterPieStateDto1 = new WaterPieStateDto();
                    waterPieStateDto1.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
                    waterPieStateDto1.setNbnormalCount(item.getNbnormalCount());
                    waterPieStateDto1.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
                    waterPieDto.setPh(waterPieStateDto1);
                    break;
                case 10:
                    WaterPieStateDto waterPieStateDto2 = new WaterPieStateDto();
                    waterPieStateDto2.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
                    waterPieStateDto2.setNbnormalCount(item.getNbnormalCount());
                    waterPieStateDto2.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
                    waterPieDto.setTurbidity(waterPieStateDto2);
                    break;
                case 11:
                    WaterPieStateDto waterPieStateDto3 = new WaterPieStateDto();
                    waterPieStateDto3.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
                    waterPieStateDto3.setNbnormalCount(item.getNbnormalCount());
                    waterPieStateDto3.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
                    waterPieDto.setResidualChlorine(waterPieStateDto3);
                    break;
                default:
                    break;
            }
        }

        if (waterPieDto.getPh()==null) {
            waterPieDto.setPh(convertWaterPieStateDto(listTotal.get(0)));
        }
        if (waterPieDto.getTurbidity()==null) {
            waterPieDto.setTurbidity(convertWaterPieStateDto(listTotal.get(0)));
        }
        if (waterPieDto.getResidualChlorine()==null) {
            WaterPieStateDto waterPieStateDto3 = new WaterPieStateDto();
            waterPieStateDto3.setNormalCount(listTotal.get(0).getTotalCount());
            waterPieStateDto3.setNbnormalCount(0);
            waterPieStateDto3.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
            waterPieDto.setResidualChlorine(waterPieStateDto3);
        }
        return waterPieDto;
    }

    private WaterPieStateDto convertWaterPieStateDto(ParmTypeCountVo vo){
        WaterPieStateDto data = new WaterPieStateDto();
        data.setNormalCount(vo.getTotalCount());
        data.setNbnormalCount(0);
        data.setPercentage((double) (Math.round((Double.valueOf(vo.getTotalCount())/Double.valueOf(vo.getTotalCount()))*100)));
        return data;
    }
}
