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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.ZhyhZhzsGlData;
import com.elephant.framework.galaxy.aroad.module.zhtj.mapper.ZhtjGljsdjqkMapper;
import com.elephant.framework.galaxy.aroad.module.zhtj.service.ZhtjGljsdjqkService;
import com.elephant.framework.galaxy.aroad.module.zhyh.entity.ZhyhZhzsKeyValue;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 综合统计_公路技术等级情况
 *
 * @author wangjian
 * @date 2019-03-11 17:24:55
 */
@Service
@AllArgsConstructor
public class ZhtjGljsdjqkServiceImpl extends ServiceImpl<ZhtjGljsdjqkMapper, ZhtjGljsdjqk> implements ZhtjGljsdjqkService {

    private final ZhtjGljsdjqkMapper zgtjgljsdjqkMapper;

    /**
     * 根据年份查询公路等级总体情况柱状图数据
     *
     * @return Echarts格式数据
     */
    @Override
    public BarEchartsVo getZhtjGljsdjqkztqkEchartData(String year, String type) {
        BarEchartsVo echartsVo = new BarEchartsVo();
        List<ZhtjGljsdjqk> dataList = zgtjgljsdjqkMapper.selectList(Wrappers.<ZhtjGljsdjqk>query().lambda().
                eq(ZhtjGljsdjqk::getYear, year).eq(ZhtjGljsdjqk::getType, type));

        echartsVo.setTitle("各市技术等级情况图");
        echartsVo.setSubTitle("计量单位：公里");
        List<String> legend = new ArrayList<>(Arrays.asList("高速","一级","二级","三级","四级","等外"));

        echartsVo.setLegendData(legend);
        List<String> xAxis = new ArrayList<>();
        List<BarSeriesVo> series = new ArrayList<>();
        for (String leg : legend) {
            BarSeriesVo barSeriesVo = new BarSeriesVo();
            barSeriesVo.setName(leg);
            barSeriesVo.setType("bar");
            barSeriesVo.setStack("总量");
            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> data = new ArrayList<>();
            for (int i = 0; i < dataList.size(); i++) {
                ZhtjGljsdjqk temp = dataList.get(i);
                if("合计".equals(temp.getArea())){
                    continue;
                }
                if(xAxis.size()<17){
                    xAxis.add(temp.getArea());
                }

                if("高速".equals(leg)){
                    data.add(temp.getHighRoad());
                } else if("一级".equals(leg)) {
                    data.add(temp.getFirstRoad());
                } else if("二级".equals(leg)) {
                    data.add(temp.getSecondRoad());
                } else if("三级".equals(leg)) {
                    data.add(temp.getThreeRoad());
                } else if("四级".equals(leg)) {
                    data.add(temp.getFourRoad());
                } else if("等外".equals(leg)) {
                    data.add(temp.getOtherRoad());
                }
                barSeriesVo.setData(data);
            }
            series.add(barSeriesVo);
        }
        echartsVo.setXAxisData(xAxis);
        echartsVo.setSeries(series);

        return echartsVo;
    }

    /**
     * 根据types分页查询对应的农村公路数据
     * @param types
     * @return
     */
    public IPage<List<ZhtjGljsdjqk>> getZhtjGljsdjqkNcglListByTypes(Page page,String types){
        IPage ipage = zgtjgljsdjqkMapper.selectPage(page,Wrappers.<ZhtjGljsdjqk>query().lambda().inSql(ZhtjGljsdjqk::getType,types));
        return ipage;
    }

    /**
     * 全省公路技术等级划分Echarts、全省公路行政等级划分Echarts
     * @return
     */
    @Override
    public Map<String, Object> getQsgljsxz() {
        Map<String,Object> resultMap = new HashMap<>();
        // 获取省道最新年份
        String sdnewYear = zgtjgljsdjqkMapper.newYear("'省道'");
        // 获取全省公路技术等级划分
        ZhtjGljsdjqk zhtjGljszkpd = super.getOne(Wrappers.<ZhtjGljsdjqk>query().lambda().eq(ZhtjGljsdjqk::getArea,"合计").eq(ZhtjGljsdjqk::getType,"省道").eq(ZhtjGljsdjqk::getYear,sdnewYear));
        // 获取全省行政等级最新年份
        String sdxznewYear = zgtjgljsdjqkMapper.newYear("'国道','省道','县道','乡道','村道','专用公路','总计'");
        // 获取全省行政等级划分
        List<ZhtjGljsdjqk> zhtjGljszkpdList = super.list(Wrappers.<ZhtjGljsdjqk>query().lambda().eq(ZhtjGljsdjqk::getArea,"合计").in(ZhtjGljsdjqk::getType,"国道","省道","县道","乡道","村道","专用公路","总计").eq(ZhtjGljsdjqk::getYear,sdxznewYear));
        // 存储行政等级处理结果
        List<Map<String,Object>> withinList = new ArrayList<>();
        List<Map<String,Object>> abroadData = new ArrayList<>();
        for(ZhtjGljsdjqk zhtjGljszkpd1 : zhtjGljszkpdList){
            Map<String,Object> xzMap = new HashMap<>();
            if(!"总计".equals(zhtjGljszkpd1.getType())){
                xzMap.put("name",zhtjGljszkpd1.getType());
                xzMap.put("value",zhtjGljszkpd1.getTotal());
                abroadData.add(xzMap);
            }else{
                withinList.add(new HashMap<String,Object>(){{
                    put("name","等级公路");
                    put("value",zhtjGljszkpd1.getTotal());
                }});
                withinList.add(new HashMap<String,Object>(){{
                    put("name","其他");
                    put("value",zhtjGljszkpd1.getOtherRoad());
                }});
            }
        }
        resultMap.put("gljsdjhf",zhtjGljszkpd);
        resultMap.put("glxzdjhf",new HashMap<String,Object>(){{
            put("withinList",withinList);
            put("abroadData",abroadData);
            put("glxzdjhfYear",sdxznewYear);
        }});
        return resultMap;
    }


    /**
     * 根据年份查询智慧养护综合展示公路数据
     * @param year 年份
     * @return
     */
    public ZhyhZhzsGlData getZhyhZhzsGlData(String year) {
        ZhyhZhzsGlData zhyhZhzsGlData = new ZhyhZhzsGlData();

        zhyhZhzsGlData.setZhyhZhzsGlOne(getZhyhZhzsGlOne(year));
        zhyhZhzsGlData.setZhyhZhzsGlTwo(getZhyhZhzsGlTwo(year));
        zhyhZhzsGlData.setZhyhZhzsGlThree(getZhyhZhzsGlThree(year));
        return zhyhZhzsGlData;
    }

    public List<ZhyhZhzsKeyValue> getZhyhZhzsGlOne(String year) {
        return zgtjgljsdjqkMapper.getZhyhZhzsGlOne(year);
    }

    public List<ZhyhZhzsKeyValue> getZhyhZhzsGlTwo(String year) {
        List<ZhyhZhzsKeyValue> zhyhZhzsKeyValues = new ArrayList<>();

        ZhtjGljsdjqk zhtjGljsdjqk = zgtjgljsdjqkMapper.selectOne(Wrappers.<ZhtjGljsdjqk>query().lambda().eq(ZhtjGljsdjqk::getType,"国省道合计").
                eq(ZhtjGljsdjqk::getArea,"合计").eq(ZhtjGljsdjqk::getYear,year));

        ZhyhZhzsKeyValue zhyhZhzsKeyValue0 = new ZhyhZhzsKeyValue();
        zhyhZhzsKeyValue0.setKey("高速");
        zhyhZhzsKeyValue0.setValue(zhtjGljsdjqk.getHighRoad().toString());
        zhyhZhzsKeyValues.add(zhyhZhzsKeyValue0);

        ZhyhZhzsKeyValue zhyhZhzsKeyValue1 = new ZhyhZhzsKeyValue();
        zhyhZhzsKeyValue1.setKey("一级");
        zhyhZhzsKeyValue1.setValue(zhtjGljsdjqk.getFirstRoad().toString());
        zhyhZhzsKeyValues.add(zhyhZhzsKeyValue1);

        ZhyhZhzsKeyValue zhyhZhzsKeyValue2 = new ZhyhZhzsKeyValue();
        zhyhZhzsKeyValue2.setKey("二级");
        zhyhZhzsKeyValue2.setValue(zhtjGljsdjqk.getSecondRoad().toString());
        zhyhZhzsKeyValues.add(zhyhZhzsKeyValue2);

        ZhyhZhzsKeyValue zhyhZhzsKeyValue3 = new ZhyhZhzsKeyValue();
        zhyhZhzsKeyValue3.setKey("三级");
        zhyhZhzsKeyValue3.setValue(zhtjGljsdjqk.getThreeRoad().toString());
        zhyhZhzsKeyValues.add(zhyhZhzsKeyValue3);

        ZhyhZhzsKeyValue zhyhZhzsKeyValue4 = new ZhyhZhzsKeyValue();
        zhyhZhzsKeyValue4.setKey("四级");
        zhyhZhzsKeyValue4.setValue(zhtjGljsdjqk.getFourRoad().toString());
        zhyhZhzsKeyValues.add(zhyhZhzsKeyValue4);

        return zhyhZhzsKeyValues;
    }

    public List<ZhyhZhzsKeyValue> getZhyhZhzsGlThree(String year) {
        List<ZhyhZhzsKeyValue> zhyhZhzsKeyValues = new ArrayList<>();
        List<ZhtjGljsdjqk> zhtjGljsdjqks = zgtjgljsdjqkMapper.selectList(Wrappers.<ZhtjGljsdjqk>query().lambda()
                .eq(ZhtjGljsdjqk::getType,"国省道合计").eq(ZhtjGljsdjqk::getYear,year).ne(ZhtjGljsdjqk::getArea,"合计"));
        for(ZhtjGljsdjqk zhtjGljsdjqk: zhtjGljsdjqks) {
            ZhyhZhzsKeyValue zhyhZhzsKeyValue = new ZhyhZhzsKeyValue();
            zhyhZhzsKeyValue.setKey(zhtjGljsdjqk.getArea());
            zhyhZhzsKeyValue.setValue(zhtjGljsdjqk.getTotal().toString());
            zhyhZhzsKeyValues.add(zhyhZhzsKeyValue);
        }
        return zhyhZhzsKeyValues;
    }
}
