package com.elephant.framework.galaxy.aroad.module.zhtj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elephant.framework.galaxy.aroad.module.system.vo.echarts.BarEchartsVo;
import com.elephant.framework.galaxy.aroad.module.system.vo.echarts.BarSeriesVo;
import com.elephant.framework.galaxy.aroad.module.zhtj.entity.ZhtjGljsdjqk;
import com.elephant.framework.galaxy.aroad.module.zhtj.entity.ZhtjGllmqk;
import com.elephant.framework.galaxy.aroad.module.zhtj.mapper.ZhtjGljsdjqkMapper;
import com.elephant.framework.galaxy.aroad.module.zhtj.mapper.ZhtjGllmqkMapper;
import com.elephant.framework.galaxy.aroad.module.zhtj.service.ZhtjGllmqkService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 综合统计_公路路面情况
 *
 * @author xuyanqi
 * @date 2019-03-12 11:04:15
 */
@Service
@AllArgsConstructor
public class ZhtjGllmqkServiceImpl extends ServiceImpl<ZhtjGllmqkMapper, ZhtjGllmqk> implements ZhtjGllmqkService {
    private  final ZhtjGljsdjqkMapper zhtjGljsdjqkMapper;
    private  final ZhtjGllmqkMapper zhtjGllmqkMapper;
    @Override
    public BarEchartsVo getBarZtData(String type,String year) {
        BarEchartsVo barEchartsVo = new BarEchartsVo();
         List<ZhtjGllmqk> zhtjGllmqkList =  zhtjGllmqkMapper.electZhtjGllmqks(year,type,null);
         List<String> legends = new ArrayList<>(Arrays.asList("沥青混凝土","水泥混凝土","简易铺装路面","未铺装路面"));
         barEchartsVo.setLegendData(legends);
        List<String> yAxisData  = new ArrayList<>();
        List<BarSeriesVo> barSeriesvo = new ArrayList<>();
         // 获取各图例的data数据
        for(String legend : legends){
            BarSeriesVo barSeriesVo = new BarSeriesVo();
            barSeriesVo.setName(legend);
            barSeriesVo.setType("bar");
            JSONObject json = new JSONObject();
            JSONObject json_in = new JSONObject();
            json_in.put("show", false);
            json_in.put("position", "insideRight");
            json.put("normal",json_in);
            barSeriesVo.setLabel(json);
            List<Double> douList = new ArrayList<>();
            for(ZhtjGllmqk zhtjGllmqk : zhtjGllmqkList){
                // 当柱状图Y坐标集合长度小于总集合的长度时add当前地市名称（减一为取消合计条）
                if(!"合计".equals(zhtjGllmqk.getArea())){
                    if(yAxisData.size()<(zhtjGllmqkList.size()-1)){
                        yAxisData.add(zhtjGllmqk.getArea());
                    }
                    switch (legend){
                        case "沥青混凝土":
                            douList.add(zhtjGllmqk.getYpzlmLqhnt());
                            break;
                        case "水泥混凝土":
                            douList.add(zhtjGllmqk.getYpzlmSnhnt());
                            break;
                        case "简易铺装路面":
                            douList.add(zhtjGllmqk.getJypzlm());
                            break;
                        case "未铺装路面":
                            douList.add(zhtjGllmqk.getWpzlm());
                            break;
                        default:
                            continue;
                    }
                }
            }
            barSeriesVo.setData(douList);
            barSeriesVo.setStack("总体");
            barSeriesvo.add(barSeriesVo);
        }
         barEchartsVo.setYAxisData(yAxisData);
         barEchartsVo.setSeries(barSeriesvo);
        return barEchartsVo;
    }

    @Override
    public Map<String,Object>  getGlfgqktj() {
        Map<String,Object> resultMap = new HashMap<>();
        String[] indicator = new String[]{"二级公路","等级公路","沥青公路"};
        /**
         * 查詢普通公路覆蓋情況
         *     普通公路不计算高速
         */
        ZhtjGllmqk ptglZhtjGllmqk = super.getOne(Wrappers.<ZhtjGllmqk>query().lambda().eq(ZhtjGllmqk::getArea,"合计").eq(ZhtjGllmqk::getType,"总计"));
        ZhtjGljsdjqk zhtjGljsdjqk = zhtjGljsdjqkMapper.selectZhtjGljsdjqk("合计","'总计'").get(0);
        resultMap.put("ptgl",new HashMap<String,Object>(){{
            put("serieData",new ArrayList<Object>(){{
                add(zhtjGljsdjqk.getSecondRoad());
                add(zhtjGljsdjqk.getTotal()-zhtjGljsdjqk.getHighRoad());
                add(ptglZhtjGllmqk.getYpzlmLqhnt());
            }});
            put("indicator",indicator);
        }});
        /**
         * 查询国省道公路覆盖情况
         *   国省道包含高速公路
         */
        ZhtjGllmqk gsdZhtjGllmqk = super.getOne(Wrappers.<ZhtjGllmqk>query().lambda().eq(ZhtjGllmqk::getArea,"合计").eq(ZhtjGllmqk::getType,"国省道合计"));
        ZhtjGljsdjqk gsdGljsdjqk = zhtjGljsdjqkMapper.selectZhtjGljsdjqk("合计","'国省道合计'").get(0);
        resultMap.put("gsd",new HashMap<String,Object>(){{
            put("serieData",new ArrayList<Object>(){{
                add(gsdGljsdjqk.getSecondRoad());
                add(gsdGljsdjqk.getTotal()+gsdGljsdjqk.getHighRoad());
                add(gsdZhtjGllmqk.getYpzlmLqhnt());
            }});
            put("indicator",indicator);
        }});
        /**
         * 查询普通国省道覆盖情况
         *   不包含高速公路
         */
        resultMap.put("ptgsd",new HashMap<String,Object>(){{
            put("serieData",new ArrayList<Object>(){{
                add(gsdGljsdjqk.getSecondRoad());
                add(gsdGljsdjqk.getTotal());
                add(gsdZhtjGllmqk.getYpzlmLqhnt());
            }});
            put("indicator",indicator);
        }});
        /**
         * 查询农村公路覆盖情况
         */
        List<ZhtjGllmqk>  ncglZhtjGllmqks = super.list(Wrappers.<ZhtjGllmqk>query().lambda().eq(ZhtjGllmqk::getArea,"合计").in(ZhtjGllmqk::getType,"县道","乡道","专用公路","村道"));
        List<ZhtjGljsdjqk> ncglGljsdjqks = zhtjGljsdjqkMapper.selectZhtjGljsdjqk("合计","'县道','乡道','专用公路','村道'");
        Double secondRoad = 0.0,total=0.0,ypzlmLqhnt=0.0;
        for(ZhtjGllmqk zhtjGllmqk : ncglZhtjGllmqks){
            ypzlmLqhnt+=zhtjGllmqk.getYpzlmLqhnt();
        }
        for(ZhtjGljsdjqk zhtjGljsdjqk1 : ncglGljsdjqks){
            secondRoad+=zhtjGljsdjqk1.getSecondRoad();
            total+=zhtjGljsdjqk1.getTotal();
        }
        Double finalSecondRoad = secondRoad;
        Double finalTotal = total;
        Double finalYpzlmLqhnt = ypzlmLqhnt;
        resultMap.put("ncgl",new HashMap<String,Object>(){{
            put("serieData",new ArrayList<Object>(){{
                add(finalSecondRoad);
                add(finalTotal);
                add(finalYpzlmLqhnt);
            }});
            put("indicator",indicator);
        }});
        return resultMap;
    }

    @Override
    public Map<String, Object> getOrdinary(String type, String year, String area) {
        Map<String, Object> mapOrdinary =  zhtjGllmqkMapper.selectOrdinary(type,year,area);
        return mapOrdinary;
    }

    @Override
    public String newYear(String type) {
        return zhtjGllmqkMapper.newYear(type);
    }
}
