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

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.IShuiQingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author: sunxiang
 * @Date: 2018/12/24 18:12
 * @Description:
 **/
@Service
public class ShuiQingServiceImpl implements IShuiQingService {

    @Autowired
    private StWaterReservoirMapper  stWaterReservoirMapper;

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

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

    @Override
    public ResultState getquanshenggeshuikuxushuiliangbianhua() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        List<STChartModel> list=stWaterReservoirMapper.getquanshenggeshuikuxushuiliangbianhua();
        if(list==null||list.size()<1){
            map.put("result",null);
            resultState.setData(map);
            return resultState;
        }
        //{name:xx,value:"4-1.00,5-4.00",temp1:5}
        int length=0;
        for (STChartModel stChartModel : list) {
            int maxMonth = (Integer)stChartModel.getTemp1();
            if(maxMonth>length){
                length=maxMonth;
            }
            String[] monthValue = ((String) stChartModel.getValue()).split(",");
            Map<Integer, BigDecimal> monthMap =null;
            monthMap=getMonthMap();
            for (String s : monthValue) {
                String[] split = s.split("-");
                monthMap.put((Integer.parseInt(split[0]))-1,new BigDecimal(split[1]));
            }
            stChartModel.setTemp2(monthMap);
        }
        //{name:xx,value:"4-1.00,5-4.00",temp1:5,temp2:Map<string,BigDeciaml>}  length
        Map<String,List<BigDecimal>> resultMap=new HashMap<>();
        for (STChartModel stChartModel : list) {
            /* names.add(stChartModel.getName());*/
            ArrayList<BigDecimal> valueList = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                BigDecimal decimal = ((Map<Integer, BigDecimal>) stChartModel.getTemp2()).get(i);
                valueList.add(decimal);
            }
            resultMap.put(stChartModel.getName(),valueList);
        }
        //{key:list}转成{name:xx,data:[....]}
        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        for (String s : resultMap.keySet()) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("name",s);
            stringObjectHashMap.put("data",resultMap.get(s));
            stringObjectHashMap.put("type","line");
            arrayList.add(stringObjectHashMap);
        }
        /*map.put("names",names);*/
        map.put("result",arrayList);
        resultState.setData(map);
        return resultState;
    }

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

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


    private Map<Integer,BigDecimal> getMonthMap(){
        HashMap<Integer, BigDecimal> map = new HashMap<>();
        BigDecimal bigDecimal_0 = new BigDecimal(0);
        map.put(0,bigDecimal_0);//一月份
        map.put(1,bigDecimal_0);
        map.put(2,bigDecimal_0);
        map.put(3,bigDecimal_0);
        map.put(4,bigDecimal_0);
        map.put(5,bigDecimal_0);
        map.put(6,bigDecimal_0);
        map.put(7,bigDecimal_0);
        map.put(8,bigDecimal_0);
        map.put(9,bigDecimal_0);
        map.put(10,bigDecimal_0);
        map.put(11,bigDecimal_0);
        return map;
    }
}
