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

import com.lightinit.hsdatashow.entity.Region;
import com.lightinit.hsdatashow.mapper.StWaterReservoirMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.topic.water.PageReturnModel;
import com.lightinit.hsdatashow.model.topic.water.STChartModel;
import com.lightinit.hsdatashow.model.topic.water.STRainModel;
import com.lightinit.hsdatashow.model.topic.water.STRainSearchModel;
import com.lightinit.hsdatashow.service.topic.water.IYuQingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author: sunxiang
 * @Date: 2018/12/25 12:10
 * @Description:
 **/
@Service
public class YuQingServiceImpl implements IYuQingService {

    @Autowired
    private StWaterReservoirMapper stWaterReservoirMapper;
    @Override
    public ResultState getjiangyuliang() {
        ResultState<Object> resultState = new ResultState<>();
        List<STChartModel> list=stWaterReservoirMapper.getjiangyuliang();
        if(list==null||list.size()<0){
            return resultState;
        }
        resultState.setData(list);
        return resultState;
    }

    @Override
    public ResultState getbenshibenyuejiangyubianhua() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        double[] array=new double[calendar.getActualMaximum(Calendar.DATE)];//[31|30]
        List<STChartModel> list=stWaterReservoirMapper.getbenshibenyuejiangyubianhua();
        if(list==null||list.size()<0){
            map.put("size",null);
            map.put("list",null);
            resultState.setData(map);
            return resultState;
        }
        for (STChartModel stChartModel : list) {
            int i=(Integer) stChartModel.getTemp2();
            array[i-1]=((BigDecimal)stChartModel.getTemp1()).doubleValue();
        }
        map.put("size",array.length);
        map.put("list",array);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getjinnianjiangyubianhua() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        double[] array=new double[12];
        List<STChartModel> list=stWaterReservoirMapper.getjinnianjiangyubianhua();
        if(list==null||list.size()<0){
            map.put("size",null);
            map.put("list",null);
            resultState.setData(map);
            return resultState;
        }
        for (STChartModel stChartModel : list) {
            int i=(Integer) stChartModel.getTemp2();
            array[i-1]=((BigDecimal)stChartModel.getTemp1()).doubleValue();
        }
        map.put("size",array.length);
        map.put("list",array);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getjinrijiangyuyuzhoubiandishibijiao() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> rains = new ArrayList<>();
        BigDecimal max = new BigDecimal(0);
        List<STChartModel> list=stWaterReservoirMapper.getjinrijiangyuyuzhoubiandishibijiao();
        if(list==null||list.size()<1){
            map.put("names",null);
            map.put("rains",null);
            map.put("max",null);
            resultState.setData(map);
            return resultState;
        }
        for (STChartModel stChartModel : list) {
            names.add((String)stChartModel.getName());
            rains.add((BigDecimal)stChartModel.getTemp1());
        }
        max= Collections.max(rains);
        map.put("names",names);
        map.put("rains",rains);
        map.put("max",max);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getbenyuejiangyuyuzhoubiandishibijiao() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> rains = new ArrayList<>();
        BigDecimal max = new BigDecimal(0);
        List<STChartModel> list=stWaterReservoirMapper.getbenyuejiangyuyuzhoubiandishibijiao();
        if(list==null||list.size()<1){
            map.put("names",null);
            map.put("rains",null);
            map.put("max",null);
            resultState.setData(map);
            return resultState;
        }
        for (STChartModel stChartModel : list) {
            names.add((String)stChartModel.getName());
            rains.add((BigDecimal)stChartModel.getTemp1());
        }
        max= Collections.max(rains);
        map.put("names",names);
        map.put("rains",rains);
        map.put("max",max);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getjinnianjiangyuyuzhoubiandishibijiao() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> rains = new ArrayList<>();
        BigDecimal max = new BigDecimal(0);
        List<STChartModel> list=stWaterReservoirMapper.getjinnianjiangyuyuzhoubiandishibijiao();
        if(list==null||list.size()<1){
            map.put("names",null);
            map.put("rains",null);
            map.put("max",null);
            resultState.setData(map);
            return resultState;
        }
        for (STChartModel stChartModel : list) {
            names.add((String)stChartModel.getName());
            rains.add((BigDecimal)stChartModel.getTemp1());
        }
        max= Collections.max(rains);
        map.put("names",names);
        map.put("rains",rains);
        map.put("max",max);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getlinianjiangyuyuzhoubianbijiao() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> reusltMap = new HashMap<>();
        List<STChartModel> list=stWaterReservoirMapper.getlinianjiangyuyuzhoubianbijiao();
        if(list==null||list.size()<1){
            reusltMap.put("names",null);
            reusltMap.put("list",null);
            resultState.setData(reusltMap);
            return resultState;
        }
        int maxYear=0;
        int minYear=3000;
        for (STChartModel stChartModel : list) {
            if((Integer)stChartModel.getTemp2()>maxYear){
                maxYear=(Integer)stChartModel.getTemp2();
            }
            if((Integer)stChartModel.getTemp3()<minYear){
                minYear=(Integer)stChartModel.getTemp3();
            }
        }
        Map<String, BigDecimal> map =  new TreeMap<>();//获取key的map
        BigDecimal baseBigDecimal = new BigDecimal(0);
        for (int i=minYear;i<=maxYear;i++) {
            map.put(i+"年",baseBigDecimal);
        }
        LinkedHashMap<String, Map<String, Object>> linkedHashMap = new LinkedHashMap<>();
        for (STChartModel stChartModel : list) {
            TreeMap<String, Object> treeMap = new TreeMap<>(map);
            String[] strings = ((String) stChartModel.getTemp1()).split(",");
            for (String strs : strings) {
                String[] key_value = strs.split("-");
                treeMap.put(key_value[0]+"年",new BigDecimal(key_value[1]));
            }
            linkedHashMap.put(stChartModel.getName(),treeMap);
        }
        ArrayList<HashMap<String, Object>> resultList = new ArrayList<>();
        for (String s : linkedHashMap.keySet()) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("name",s);
            stringObjectHashMap.put("type","line");
            stringObjectHashMap.put("data",linkedHashMap.get(s).values());
            resultList.add(stringObjectHashMap);
        }
        reusltMap.put("names",map.keySet());
        reusltMap.put("list",resultList);
        resultState.setData(reusltMap);
        return resultState;
    }

    @Override
    public ResultState getbenshilinianjiangyubianhua() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> reusltMap = new HashMap<>();
        List<STChartModel> list=stWaterReservoirMapper.getbenshilinianjiangyubianhua();
        if(list==null||list.size()<1){
            reusltMap.put("names",null);
            reusltMap.put("list",null);
            resultState.setData(reusltMap);
            return resultState;
        }
        int minYear=(Integer) list.get(0).getTemp1();
        int maxYear=(Integer) list.get(list.size()-1).getTemp1();
        Map<String, BigDecimal> map =  new TreeMap<>();//获取key的map
        BigDecimal baseBigDecimal = new BigDecimal(0);
        for(int i=minYear;i<=maxYear;i++){
            map.put(i+"年",baseBigDecimal);
        }
        for (STChartModel stChartModel : list) {
            map.put((Integer)stChartModel.getTemp1()+"年",(BigDecimal)stChartModel.getTemp2());
        }
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("name",list.get(0).getName());
        stringObjectHashMap.put("type","line");
        stringObjectHashMap.put("data",map.values());
        reusltMap.put("names",map.keySet());
        reusltMap.put("list",stringObjectHashMap);
        resultState.setData(reusltMap);
        return resultState;
    }

    @Override
    public PageReturnModel getjiangyumingxichaxun(STRainSearchModel stRainSearchModel) {
        PageReturnModel<STRainModel> pageReturnModel = new PageReturnModel<>();
        stRainSearchModel.getDateText();
        List<STRainModel> list=stWaterReservoirMapper.getjiangyumingxichaxun(stRainSearchModel);
        int total=stWaterReservoirMapper.getjiangyumingxichaxunTotal(stRainSearchModel);
        pageReturnModel.setList(list);
        pageReturnModel.setTotal(total);
        return pageReturnModel;
    }

    @Override
    public List<Region> getRegion() {
        return stWaterReservoirMapper.getRegion();
    }


}
