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.STChartModel;
import com.lightinit.hsdatashow.service.topic.water.IFloodAndDryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author: sunxiang
 * @Date: 2018/12/22 14:47
 * @Description:
 **/
@Service
public class FloodAndDryServiceImpl implements IFloodAndDryService {

    @Autowired
    private StWaterReservoirMapper stWaterReservoirMapper;

    @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);
        if(list==null||list.size()<1){
            map.put("names",null);
            map.put("max",null);
            map.put("waters",null);
            map.put("warnings",null);
            resultState.setData(map);
            return resultState;
        }
        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 ResultState getjinribaoyu() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<STChartModel> list=stWaterReservoirMapper.getjinribaoyu();
        if(list==null||list.size()<1){
            map.put("names",null);
            map.put("max",null);
            map.put("rains",null);
            map.put("warnings",null);
            resultState.setData(map);
            return resultState;
        }
        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();
        if(list==null||list.size()<1){
            map.put("names",null);
            map.put("rains",null);
            resultState.setData(map);
            return resultState;
        }
        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 ResultState getquanshikeyongzongshuiliang() {
        ResultState resultState = new ResultState();
        Object obj= stWaterReservoirMapper.getquanshikeyongzongshuiliang();
        resultState.setData(obj);
        return resultState;
    }

    @Override
    public ResultState getquanshixushuizongliang() {
        ResultState resultState = new ResultState();
        Object obj= stWaterReservoirMapper.getquanshixushuizongliang();
        resultState.setData(obj);
        return resultState;
    }

    @Override
    public ResultState getquanshijianchezhanzongshu() {
        ResultState resultState = new ResultState();
        Object obj= stWaterReservoirMapper.getquanshijianchezhanzongshu();
        resultState.setData(obj);
        return resultState;
    }

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

    @Override
    public ResultState getshuikugeyueshuiliang() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        List<STChartModel> list=stWaterReservoirMapper.getshuikugeyueshuiliang();
        //{name:xx,value:"4-1.00,5-4.00",temp1:5}
        if(list==null||list.size()<0){
            map.put("result",null);
            resultState.setData(map);
            return resultState;
        }
        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 getgeshuikuxushuiliang() {
        ResultState resultState = new ResultState();
        HashMap<String, Object> map = new HashMap<>();
        List<STChartModel> list= stWaterReservoirMapper.getgeshuikuxushuiliang();
        if(list==null||list.size()<0){
            map.put("names",null);
            map.put("max",null);
            map.put("values",null);
            resultState.setData(map);
            return resultState;
        }
        ArrayList<String> names = new ArrayList<>();
        ArrayList<BigDecimal> values = new ArrayList<>();
        BigDecimal max=new BigDecimal(0);
        for (STChartModel stChartModel : list) {
            names.add(stChartModel.getName());
            values.add((BigDecimal)stChartModel.getValue());
        }
        max=Collections.max(values);
        map.put("names",names);
        map.put("max",max);
        map.put("values",values);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getgeshuikuxushuiliangbianhua() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        List<STChartModel> list=stWaterReservoirMapper.getgeshuikuxushuiliangbianhua();
        //{name:xx,value:"4-1.00,5-4.00",temp1:5}
        if(list==null||list.size()<0){
            map.put("result",null);
            resultState.setData(map);
            return resultState;
        }
        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 getjiancezhangouchengtu() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        List<STChartModel> list=stWaterReservoirMapper.getjiancezhangouchengtu();
        if(list==null||list.size()<0){
            map.put("result",null);
            resultState.setData(map);
            return resultState;
        }
        ArrayList<String> names = new ArrayList<>();
        for (STChartModel stChartModel : list) {
            names.add(stChartModel.getName());
        }
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }

    @Override
    public ResultState getquanshigongchengzongshu() {
        ResultState<Object> resultState = new ResultState<>();
        Object obj=stWaterReservoirMapper.getquanshigongchengzongshu();
        resultState.setData(obj);
        return resultState;
    }

   /* @Override
    public ResultState getgeqvxianguochengtu() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        List<STChartModel> list=stWaterReservoirMapper.getgeqvxianguochengtu();
        for (STChartModel stChartModel : list) {
            names.add(stChartModel.getName());
            stChartModel.setTemp1(((String)stChartModel.getTemp1()).replaceAll(",","<br/>"));
        }
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }*/
    @Override
        public ResultState getgeqvxianguochengtu(String stationType) {
            ResultState<Object> resultState = new ResultState<>();
            HashMap<String, Object> map = new HashMap<>();
            ArrayList<String> names = new ArrayList<>();
            List<STChartModel> list=stWaterReservoirMapper.getgeqvxianguochengtu(stationType);
            if(list==null||list.size()<0){
                map.put("names",null);
                map.put("list",null);
                resultState.setData(map);
                return resultState;
            }
            for (STChartModel stChartModel : list) {
                names.add(stChartModel.getName());
            }
            map.put("names",names);
            map.put("list",list);
            resultState.setData(map);
            return resultState;
        }

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

  /*  @Override
    public ResultState getqvxiangongchenggouzaotu() {
        ResultState<Object> resultState = new ResultState<>();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        List<STChartModel> list=stWaterReservoirMapper.getqvxiangongchenggouzaotu();
        for (STChartModel stChartModel : list) {
            names.add(stChartModel.getName());
            stChartModel.setTemp1(((String)stChartModel.getTemp1()).replaceAll(",","<br/>"));
        }
        map.put("names",names);
        map.put("list",list);
        resultState.setData(map);
        return resultState;
    }*/

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

    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;
    }
}
