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

import com.lightinit.hsdatashow.entity.StWaterLake;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.admin.WaterqualityNameAndValueModel;
import com.lightinit.hsdatashow.model.topic.water.*;
import com.lightinit.hsdatashow.service.topic.water.IHomeShowServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class HomeShowServerImpl implements IHomeShowServer {
    @Autowired
    private StWaterReservoirMapper stWaterReservoirMapper;
    @Autowired
    private StWaterRiverMapper stWaterRiverMapper ;
    @Autowired
    private StWaterRainfallMapper stWaterRainfallMapper ;
    @Autowired
    private StWaterConsumptionMapper stWaterConsumptionMapper ;
    @Autowired
    private StWaterReservoirImpoundageMapper stWaterReservoirImpoundageMapper ;
    @Autowired
    private StWaterReservoirConsumptionMapper stWaterReservoirConsumptionMapper ;
    @Autowired
    private StWaterLakeMapper stWaterLakeMapper ;
    @Autowired
    private StWaterrRuralProjectMapper stWaterrRuralProjectMapper ;

    @Override
    public ResultState getxunqiyujing() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<STChartModel> list=stWaterReservoirMapper.getxunqiyujing();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> warnings = new ArrayList<>();
        ArrayList<BigDecimal> waters = new ArrayList<>();
        BigDecimal max=new BigDecimal(0);
        for (STChartModel stChartModel : list) {
            names.add(stChartModel.getName());
            warnings.add((BigDecimal)stChartModel.getTemp1());
            waters.add((BigDecimal)stChartModel.getTemp2());

        }
        BigDecimal max_warnings = Collections.max(warnings);
        BigDecimal max_waters = Collections.max(waters);
        max=max_warnings.compareTo(max_waters)>0?max_warnings:max_waters;
        map.put("names",names);
        map.put("max",max);
        map.put("waters",waters);
        map.put("warnings",warnings);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public HeliuGaikuangReturnModel getHeliuGaikuang() {
        HeliuGaikuangReturnModel heliuGaikuangReturnModel = new HeliuGaikuangReturnModel() ;
        List<WaterAllListReturnModel> stWaterRivers = stWaterRiverMapper.selectAllList() ;
        if (stWaterRivers.size()>0){
            int falg = 0 ;
            List<String> array1 = new ArrayList<>() ;
            List<Integer> array2 = new ArrayList<>() ;
            List<BigDecimal> riverLengths = new ArrayList<>() ;
            List<String> startSopPoint = new ArrayList<>() ;
            List<BigDecimal> riverImpoundages = new ArrayList<>() ;
            List<String> riverChiefs = new ArrayList<>() ;
            List<BigDecimal> normals = new ArrayList<>() ;
            List<BigDecimal> warnings = new ArrayList<>() ;
            List<String> flowThroughs = new ArrayList<>() ;
            List<BigDecimal> waterLevel = new ArrayList<>() ;
            for (WaterAllListReturnModel stWaterRiver : stWaterRivers) {
                array1.add(stWaterRiver.getRiverName()) ;
                array2.add(falg) ;
                riverLengths.add(stWaterRiver.getRiverLength()) ;
                startSopPoint.add(stWaterRiver.getStartStopPoint()) ;
                riverImpoundages.add(stWaterRiver.getRiverImpoundage()) ;
                riverChiefs.add(stWaterRiver.getRiverChief()) ;
                normals.add(stWaterRiver.getWaterLevelNormal()) ;
                warnings.add(stWaterRiver.getWaterLevelWarning()) ;
                flowThroughs.add(stWaterRiver.getFlowThrough()) ;
                waterLevel.add(stWaterRiver.getWaterLevel()) ;
                falg = falg+1 ;
            }
            heliuGaikuangReturnModel.setArray1(array1);
            heliuGaikuangReturnModel.setArray2(array2);
            heliuGaikuangReturnModel.setRiverLengths(riverLengths);
            heliuGaikuangReturnModel.setStartSopPoint(startSopPoint);
            heliuGaikuangReturnModel.setRiverImpoundages(riverImpoundages);
            heliuGaikuangReturnModel.setRiverChiefs(riverChiefs);
            heliuGaikuangReturnModel.setNormals(normals);
            heliuGaikuangReturnModel.setWarnings(warnings);
            heliuGaikuangReturnModel.setFlowThroughs(flowThroughs);
            heliuGaikuangReturnModel.setWaterLevels(waterLevel);
        }
        return heliuGaikuangReturnModel;
    }

    @Override
    public ResultState getjinribaoyu() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<STChartModel> list=stWaterReservoirMapper.getjinribaoyu();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> rains = new ArrayList<>();
        BigDecimal max=new BigDecimal(0);
        for (STChartModel stChartModel : list) {
            names.add(stChartModel.getName());
            rains.add((BigDecimal)stChartModel.getValue());
        }
        //填充警戒线
        BigDecimal warnging=new BigDecimal(10);
        ArrayList<BigDecimal> warnings = new ArrayList<>();
        for (int i=0;i<rains.size();i++) {
            warnings.add(warnging);
        }
        max=Collections.max(rains);
        if(max.compareTo(warnging)<0){
            max=warnging;
        }
        map.put("names",names);
        map.put("max",max);
        map.put("rains",rains);
        map.put("warnings",warnings);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getjinrijiangyubianhua() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> rains = new ArrayList<>();
        List<STChartModel> list=stWaterReservoirMapper.getjinrijiangyubianhua();for (STChartModel stChartModel : list) {
            names.add((String)stChartModel.getTemp2());
            rains.add((BigDecimal)stChartModel.getTemp1());
        }
        map.put("names",names);
        map.put("rains",rains);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public int getRiverCount() {
        int i = stWaterRiverMapper.getRiverCount() ;
        return i;
    }

    @Override
    public BigDecimal getShangyueJiangyu() {
        return stWaterRiverMapper.getShangyueJiangyu() ;
    }

    @Override
    public BigDecimal getShangyueYongshui() {
        return stWaterRiverMapper.getShangyueYongshui();
    }

    @Override
    public List<WaterqualityNameAndValueModel> getGequJiangshui() {
        return stWaterRainfallMapper.getGequJiangshui();
    }

    @Override
    public List<WaterqualityNameAndValueModel> getGequYongshui() {
        return stWaterConsumptionMapper.getGequYongshui();
    }

    @Override
    public BigDecimal getYongshui() {
        return stWaterReservoirImpoundageMapper.getYongshui();
    }

    @Override
    public ModelNameAndNameOrValue getGeshuikuYongshui() {
        ModelNameAndNameOrValue modelNameAndNameOrValues = new ModelNameAndNameOrValue() ;
        List<NameAndValue> nameAndValues =  stWaterReservoirImpoundageMapper.getGeshuikuYongshui() ;
        if (nameAndValues.size()>0){
            List<String> names = new ArrayList<>() ;
            for (NameAndValue nameAndValue : nameAndValues) {
                names.add(nameAndValue.getName()) ;
            }
            modelNameAndNameOrValues.setNames(names);
            modelNameAndNameOrValues.setList(nameAndValues);
        }
        return modelNameAndNameOrValues;
    }

    @Override
    public YueyongshuiReturnModel getYueyongshui() {
        YueyongshuiReturnModel yueyongshuiReturnModel = new YueyongshuiReturnModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int month = calendar.get(Calendar.MONTH)+1;
        List<ShuiKuYongshuiModel> shuiKuYongshuiModels = stWaterReservoirConsumptionMapper.getYueyongshui(month) ;
        if (shuiKuYongshuiModels.size()>0){
            List<String> shuikuNames = stWaterReservoirConsumptionMapper.getShuikuNzames(month) ;
            List<String> titles = new ArrayList<>() ;
            List<NameAndListModel> datas = new ArrayList<>() ;
            for (String shuikuName : shuikuNames) {
                titles.add(shuikuName) ;
                NameAndListModel nameAndListModel = new NameAndListModel() ;
                nameAndListModel.setName(shuikuName);
                BigDecimal[] values = new BigDecimal[12];
                for (ShuiKuYongshuiModel shuiKuYongshuiModel : shuiKuYongshuiModels) {
                    if (nameAndListModel.getName().equals(shuiKuYongshuiModel.getName())){
                        values[shuiKuYongshuiModel.getMonth()-1] = shuiKuYongshuiModel.getConsumption() ;
                    }
                }
                nameAndListModel.setValue(Arrays.asList(values));
                datas.add(nameAndListModel) ;
            }
            yueyongshuiReturnModel.setTitles(titles);
            yueyongshuiReturnModel.setDatas(datas);
        }
        return yueyongshuiReturnModel;
    }

    @Override
    public BigDecimal getRiverLength() {
        return stWaterRiverMapper.getRiverLength();
    }

    @Override
    public StWaterLake getHengshuiHu() {
        return stWaterLakeMapper.getHengshuiHu();
    }

    @Override
    public BigDecimal getHlXs() {
        return stWaterRiverMapper.getHlXs();
    }

    @Override
    public ListNameAndListValueModel getNongcunGongcheng() {
        ListNameAndListValueModel listNameAndListValueModel = new ListNameAndListValueModel() ;
        List<NameAndValue> nameAndValues = stWaterrRuralProjectMapper.getNongcunGongcheng() ;
        if (nameAndValues.size()>0){
            List<String> names = new ArrayList<>() ;
            List<BigDecimal> values = new ArrayList<>() ;
            for (NameAndValue nameAndValue : nameAndValues) {
                names.add(nameAndValue.getName()) ;
                values.add(nameAndValue.getValue()) ;
            }
            listNameAndListValueModel.setNames(names);
            listNameAndListValueModel.setValues(values);
        }
        return listNameAndListValueModel;
    }
}
