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.zhtj.entity.ZhtjGljbxxGltclc;
import com.elephant.framework.galaxy.aroad.module.zhtj.mapper.ZhtjGljbxxGltclcMapper;
import com.elephant.framework.galaxy.aroad.module.zhtj.service.ZhtjGljbxxGltclcService;
import com.elephant.framework.galaxy.aroad.module.system.vo.echarts.BarEchartsVo;
import com.elephant.framework.galaxy.aroad.module.system.vo.echarts.BarSeriesVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *  公路通车里程表
 *
 * @author xuzhitu
 * @date 2019-03-11 09:50:32
 */
@Service
@AllArgsConstructor
public class ZhtjGljbxxGltclcServiceImpl extends ServiceImpl<ZhtjGljbxxGltclcMapper, ZhtjGljbxxGltclc> implements ZhtjGljbxxGltclcService {

    private final ZhtjGljbxxGltclcMapper zhtjGljbxxGltclcMapper;

    /**
     * 根据年份查询公路通车/养护里程列表
     *
     * @return Echarts格式数据
     */
    @Override
    public BarEchartsVo getTclcEchartData(String year) {
        BarEchartsVo echartsVo = new BarEchartsVo();
        List<ZhtjGljbxxGltclc> gltclcbList = zhtjGljbxxGltclcMapper.selectList(Wrappers.<ZhtjGljbxxGltclc>query().lambda()
                .eq(ZhtjGljbxxGltclc::getYear, year));

        echartsVo.setTitle(year +"年公路通车/养护里程");
        echartsVo.setSubTitle("计量单位：公里");
        List<String> legend = new ArrayList<>(Arrays.asList("国道","省道","县道","乡道","村道"));

        echartsVo.setLegendData(legend);
        List<String> yAxis = 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 < gltclcbList.size(); i++) {
                ZhtjGljbxxGltclc temp = gltclcbList.get(i);
                if("合计".equals(temp.getArea())){
                    continue;
                }
                if(yAxis.size()<17){
                    yAxis.add(temp.getArea());
                }
                if("国道".equals(leg)){
                    data.add(temp.getGuodao());
                } else if("省道".equals(leg)) {
                    data.add(temp.getShengdao());
                } else if("县道".equals(leg)) {
                    data.add(temp.getXiandao());
                } else if("乡道".equals(leg)) {
                    data.add(temp.getXiangdao());
                } else if("村道".equals(leg)) {
                    data.add(temp.getCundao());
                }
             barSeriesVo.setData(data);
            }
            series.add(barSeriesVo);
        }
        echartsVo.setYAxisData(yAxis);
        echartsVo.setSeries(series);

        return echartsVo;
    }
}
