package com.lightinit.hsdatashow.impl.topic.water;

import com.lightinit.hsdatashow.dictionary.DicWaterSupplyType;
import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.topic.water.*;
import com.lightinit.hsdatashow.service.topic.water.IWholeCityWaterService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

@Service
public class WholeCityWaterServiceImpl implements IWholeCityWaterService {
    @Autowired
    private StWaterConsumerMapper stWaterConsumerMapper;
    @Autowired
    private StWaterConsumptionMapper stWaterConsumptionMapper;
    @Autowired
    private StWaterGroundwaterMonitoringStationMapper stWaterGroundwaterMonitoringStationMapper;
    @Autowired
    private StWaterSupplyMapper stWaterSupplyMapper;
    @Autowired
    private StWaterGroundwaterMapper stWaterGroundwaterMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Override
    public List<Region> getRegion() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Long id = (long)2;
        return regionMapper.selectById(id);
    }

    @Override
    public List<NameAndValueModel> getWaterConsumer( String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return stWaterConsumerMapper.getWaterConsumer(regionName);
    }

    @Override
    public YearMonthDayModel getTotalDay(String type, String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        YearMonthDayModel model = new YearMonthDayModel();
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        List<StWaterConsumption> list = stWaterConsumptionMapper.getTotalDay(year,month,type,regionName);
        List<Integer> years = new ArrayList<>();
        List<Integer> days = new ArrayList<>();
        List<Integer> months = new ArrayList<>();
        List<BigDecimal> values = new ArrayList<>();
        for(StWaterConsumption stWaterConsumption : list) {
            years.add(stWaterConsumption.getCaptureYear());
            days.add(stWaterConsumption.getCaptureDay());
            months.add(stWaterConsumption.getCaptureMonth());
            values.add(stWaterConsumption.getConsumption());
        }
        model.setDay(days);
        model.setMonth(months);
        model.setValue(values);
        model.setYear(years);
        return model;
    }

    @Override
    public YearMonthDayModel getTotalMonth(String type, String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        YearMonthDayModel model = new YearMonthDayModel();
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        List<StWaterConsumption> list = stWaterConsumptionMapper.getTotalMonth(year,type,regionName);
        List<Integer> years = new ArrayList<>();
//        List<Integer> days = new ArrayList<>();
        List<Integer> months = new ArrayList<>();
        List<BigDecimal> values = new ArrayList<>();
        for(StWaterConsumption stWaterConsumption : list) {
            years.add(stWaterConsumption.getCaptureYear());
//            days.add(stWaterConsumption.getCaptureDay());
            months.add(stWaterConsumption.getCaptureMonth());
            values.add(stWaterConsumption.getConsumption());
        }
//        model.setDay(days);
        model.setMonth(months);
        model.setValue(values);
        model.setYear(years);
        return model;
    }

    @Override
    public YearMonthDayModel getTotalYear(String type, String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        YearMonthDayModel model = new YearMonthDayModel();
//        Calendar cale = Calendar.getInstance();
//        int year = cale.get(Calendar.YEAR);
        List<StWaterConsumption> list = stWaterConsumptionMapper.getTotalYear(type,regionName);
        List<Integer> years = new ArrayList<>();
//        List<Integer> days = new ArrayList<>();
//        List<Integer> months = new ArrayList<>();
        List<BigDecimal> values = new ArrayList<>();
        for(StWaterConsumption stWaterConsumption : list) {
            years.add(stWaterConsumption.getCaptureYear());
//            days.add(stWaterConsumption.getCaptureDay());
//            months.add(stWaterConsumption.getCaptureMonth());
            values.add(stWaterConsumption.getConsumption());
        }
//        model.setDay(days);
//        model.setMonth(months);
        model.setValue(values);
        model.setYear(years);
        return model;
    }

    @Override
    public YearMonthDayModel getYesterday(String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        YearMonthDayModel model = new YearMonthDayModel();
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        int day = cale.get(Calendar.DATE);
        int day1 = day-1;
        List<StWaterConsumption> list =stWaterConsumptionMapper.getYesterday(year,month,day1,regionName);
        List<BigDecimal> values = new ArrayList<>();
        if(list.size()>0) {

            values.add(list.get(0).getConsumption());
        }else {
            values.add(BigDecimal.valueOf(0));
        }
        model.setValue(values);
        return model;
    }

    @Override
    public YearMonthDayModel getLastMonth(String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        YearMonthDayModel model = new YearMonthDayModel();
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        int month1 = month-1;
        List<StWaterConsumption> list =stWaterConsumptionMapper.getLastMonth(year,month1,regionName);
        List<BigDecimal> values = new ArrayList<>();
        if(list.size()>0) {

            values.add(list.get(0).getConsumption());
        }else {
            values.add(BigDecimal.valueOf(0));
        }
        model.setValue(values);
        return model;
    }

    @Override
    public YearMonthDayModel getMonth(String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        YearMonthDayModel model = new YearMonthDayModel();
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        List<StWaterConsumption> list =stWaterConsumptionMapper.getMonth(year,month,regionName);
        List<BigDecimal> values = new ArrayList<>();
        if(list.size()>0) {

            values.add(list.get(0).getConsumption());
        }else {
            values.add(BigDecimal.valueOf(0));
        }
        model.setValue(values);
        return model;
    }

    @Override
    public YearMonthDayModel getYear(String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        YearMonthDayModel model = new YearMonthDayModel();
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        List<StWaterConsumption> list =stWaterConsumptionMapper.getYear(year,regionName);
        List<BigDecimal> values = new ArrayList<>();
        if(list.size()>0) {

            values.add(list.get(0).getConsumption());
        }else {
            values.add(BigDecimal.valueOf(0));
        }
        model.setValue(values);
        return model;
    }

    @Override
    public Long getJianCeZhanCount( String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
       Long aa = stWaterGroundwaterMonitoringStationMapper.getJianCeZhanCount(regionName);
        return aa;
    }

    @Override
    public WaterSupplyModel getLiNianGongShui( String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        WaterSupplyModel model = new WaterSupplyModel();
        List<Integer> years = new ArrayList<>();
        List<BigDecimal> totles = new ArrayList<>();
        List<BigDecimal> groundWaters = new ArrayList<>();
        List<BigDecimal> surfaceWaters = new ArrayList<>();
        List<GroundWaterAndSurfaceWaterModel> list = stWaterSupplyMapper.getLiNianGongShui(regionName);
//        List<StWaterSupply> list = stWaterSupplyMapper.getLiNianGongShui(regionName);
        for(GroundWaterAndSurfaceWaterModel groundWaterAndSurfaceWaterModel :list) {
            groundWaters.add(groundWaterAndSurfaceWaterModel.getGroundWater());
            surfaceWaters.add(groundWaterAndSurfaceWaterModel.getSurfaceWater());
            totles.add(groundWaterAndSurfaceWaterModel.getSurfaceWater().add(groundWaterAndSurfaceWaterModel.getGroundWater()));
            years.add(groundWaterAndSurfaceWaterModel.getYear());
        }
        model.setGroundWaters(groundWaters);
        model.setSurfaceWaters(surfaceWaters);
        model.setTotles(totles);
        model.setYears(years);
        return model;
    }

    @Override
    public ThreeYearsModel getDiXiaShuiShenAVG( String regionName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ThreeYearsModel model = new ThreeYearsModel();
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int year1 = year-2;
        List<StWaterGroundwater> list = stWaterGroundwaterMapper.getDiXiaShuiShenAVG(year1,regionName);
        List<Integer> years = new ArrayList<>();
        List<Integer> months = new ArrayList<>();
        List<BigDecimal> thisYears = new ArrayList<>();
        List<BigDecimal> beforeYears = new ArrayList<>();
        List<BigDecimal> lastYears = new ArrayList<>();
        if(list.size()>0) {
            for (StWaterGroundwater stWaterGroundwater:list) {
                if(!years.contains(stWaterGroundwater.getCaptureDay())) {
                    years.add(stWaterGroundwater.getCaptureYear());
                }
                if(!months.contains(stWaterGroundwater.getCaptureMonth())) {
                    months.add(stWaterGroundwater.getCaptureMonth());
                }
                if(year==stWaterGroundwater.getCaptureYear()) {
                    thisYears.add(stWaterGroundwater.getGroundwaterLevel());
                }
                if((year-1)==stWaterGroundwater.getCaptureYear()) {
                    lastYears.add(stWaterGroundwater.getGroundwaterLevel());
                }
                if((year-2)==stWaterGroundwater.getCaptureYear()) {
                    beforeYears.add(stWaterGroundwater.getGroundwaterLevel());
                }
            }
        }

        model.setBeforeYears(beforeYears);
        model.setLastYears(lastYears);
        model.setMonths(months);
        model.setThisYears(thisYears);
        model.setYears(years);
        return model;
    }
}
