package com.usoftchina.bi.server.service.chart;

import com.usoftchina.bi.core.base.RepCode;
import com.usoftchina.bi.core.base.RepEntity;
import com.usoftchina.bi.core.utils.CalculationJudgeUtil;
import com.usoftchina.bi.server.dao.chart.ChartsConfigMapper;
import com.usoftchina.bi.server.dao.chart.ShowChartsMapper;
import com.usoftchina.bi.server.model.bo.*;
import com.usoftchina.bi.server.model.vo.configVo.ScatterConfigInfo;
import com.usoftchina.bi.server.model.vo.dataVo.ChartsDataInfo;
import com.usoftchina.bi.core.jdbc.DynamicDataSourceContextHolder;
import com.usoftchina.bi.server.utils.ScreenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ShowScatterService {
    @Autowired
    ShowChartsMapper showChartsMapper;
    @Autowired
    TimeConverterUtil timeConverterUtil;
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    GetChartsDataUtilService getChartsDataUtilService;
    @Autowired
    ScreenUtil screenUtil;
    @Autowired
    ChartsUtilService chartsUtilService;

    /*
    Line数据展示
     */
    public RepEntity showScatter(ScatterConfigInfo scatterConfigInfo, String token, int dashId, String styleConfig){
        ChartsDataInfo chartsDataInfo = new ChartsDataInfo();
        chartsDataInfo.setStyleConfig(styleConfig);
        TimeReture timeReture = new TimeReture();
        if (scatterConfigInfo == null || "".equals(scatterConfigInfo)){
            return new RepEntity(RepCode.Null);
        }
        int id = scatterConfigInfo.getId();
        //获取列配置
        ChartsColumnConfig chartsColumn = chartsConfigMapper.getChartsColumn(id);
        if (chartsColumn == null || "".equals(chartsColumn)){
            return new RepEntity(RepCode.Null);
        }
        String style = chartsColumn.getChartStyle();
        String config = chartsColumn.getChartConfig();
        chartsDataInfo.setChartsColumnConfig(chartsColumn);

        String tableName = chartsUtilService.getSqlStr(token, id, dashId);
        if ("".equals(tableName)){
            return new RepEntity(RepCode.NoAuthority);
        }

        int baseId = getChartsDataUtilService.getBaseId(id);

        try{
            if (!DynamicDataSourceContextHolder.isContainsDataSource(baseId)) {
                return new RepEntity(RepCode.DataSourceNull);
            } else {
                DynamicDataSourceContextHolder.setDataSourceType(baseId);
            }

        String xColumn = scatterConfigInfo.getxAxis().getColumnRename();
        String yColumn = scatterConfigInfo.getyAxis().getColumnRename();
        String xColumnType = scatterConfigInfo.getxAxis().getColumnType();

        String xAxisType = scatterConfigInfo.getxAxis().getShowDataType();
        List<String> groupBy = scatterConfigInfo.getGroups();

        if (xColumn == null || "".equals(xColumn) || yColumn == null || "".equals(yColumn)){
            return new RepEntity(RepCode.nullAxis);
        }

        String yType = scatterConfigInfo.getyAxis().getShowDataType();
        String yAxisType = CalculationJudgeUtil.Judge(yType);



        List<LineSeriesMap> value = new ArrayList<LineSeriesMap>();
        List<LineSeries> serieses = new ArrayList<LineSeries>();    //生成柱状图的系列
        List<String> xAxisData = new ArrayList<String>();  //X轴自己用
        List<String> xData = new ArrayList<String>();      //X轴前端用

            //取筛选列表
            List<Screen> screens = scatterConfigInfo.getFilters();
            ScreenStr scr = new ScreenStr();
            String screen = "";               //正常筛选条件
            String screenToColumn = "";       //跟目标列相同的筛选条件
            String screenToColumnS = "";       //跟目标列相同的筛选条件
            if ("".equals(screens) || screens == null || screens.size() == 0){
                screen = "";
            }else {
                scr = screenUtil.screensUtil(screens, xColumn, xColumnType);
                screen = scr.getRet();
                screenToColumn = scr.getWithColumnRet();
                StringBuilder sb = new StringBuilder(screenToColumn);
                if (screenToColumn != null && !("".equals(screenToColumn))){
                    screenToColumnS = screenToColumn + " " + screen;               //and
                    screenToColumn = String.valueOf(sb .replace(1, 5, "where "));
                    screen = screenToColumn + " " + screen;         //where
                } else {
                    StringBuilder sb1 = new StringBuilder(screen);
                    screenToColumnS = screen;
                    screen = String.valueOf(sb1 .replace(1, 5, "where "));
                }
            }

        //X轴
        //判断是否为日期类型
        if ("time".equals(xColumnType)){
            timeReture = timeConverterUtil.timeConverter(xColumn, tableName, xAxisType, screen, scatterConfigInfo.getMaxCount());
            xAxisData = timeReture.getValues();
            chartsDataInfo.setTooMany(timeReture.isOverdose());
            xData = timeConverterUtil.toRespons(xAxisData,xAxisType );
        }else {
                int count = showChartsMapper.getNumForX(xColumn, tableName);
                if (count > scatterConfigInfo.getMaxCount()){
                    chartsDataInfo.setTooMany(true);
                }
            xAxisData = showChartsMapper.getXAxis(xColumn, tableName, screen, scatterConfigInfo.getMaxCount());

        }

        if ("".equals(xAxisData) || xAxisData == null || xAxisData.size() == 0){
            return new RepEntity(RepCode.Null);
        }
        if (groupBy.size() == 0) {
            //无分组时Y值
            if (!"time".equals(xColumnType)) {
                if (StringUtils.isEmpty(screenToColumnS)) {
                    screenToColumnS = " and 1 = 1 ";
                }
                value = showChartsMapper.getXAndYValues(yColumn, xColumn, tableName, yAxisType, screenToColumnS);
            } else {
                Iterator itX = xAxisData.iterator();
                while (itX.hasNext()) {
                    String xdata = (String) itX.next();
                    double valueOne = 0;
                    String valueOnes = null;
                    if ("time".equals(xColumnType)) {
                        valueOnes = timeConverterUtil.getTimeValueConverter(yColumn, xColumn, tableName, yAxisType, xAxisType, xdata, screenToColumnS);
                    } else {
                        if (xdata == null || "".equals(xdata)) {
                            valueOnes = showChartsMapper.getValuesIsNull(yColumn, xColumn, tableName, yAxisType, screenToColumnS);
                        } else {
                            valueOnes = showChartsMapper.getXValue(yColumn, xColumn, tableName, yAxisType, xdata, screenToColumnS);
                        }
                    }
                    if (valueOnes == null || "".equals(valueOnes)) {
                        valueOne = 0;
                    } else {
                        valueOne = Double.parseDouble(valueOnes);
                    }
                    LineSeriesMap lineSeriesMap = new LineSeriesMap();
                    lineSeriesMap.setValue(valueOne);
                    lineSeriesMap.setDate(xdata);
                    value.add(lineSeriesMap);
                }
            }
            LineSeries series = new LineSeries();
//        series.setName(yColumn);
            series.setMdata(value);

            serieses.add(series);
        }
        //有分组
        if (groupBy.size() != 0){
            Iterator itGroup = groupBy.iterator();  //分组数

            //便利分组
            while (itGroup.hasNext()){
                String groupByName = (String) itGroup.next();
                int counts = showChartsMapper.getGroupsCount(groupByName,tableName, screenToColumnS);
                if (counts > 50){
                    chartsDataInfo.setTooMany(true);
                }
                List<String> groupsData = showChartsMapper.getGroups(groupByName,tableName, screenToColumnS, 50); //查询每个分组系列
                Iterator itGroupsData = groupsData.iterator();

                //每个分组得到得每个系列
                while(itGroupsData.hasNext()){
                    String groupsName = (String)itGroupsData.next();
                    if (groupsName == null || "".equals(groupsName)){
                        continue;
                    }

                    List<LineSeriesMap> groupsValue = new ArrayList<LineSeriesMap>();
                    Iterator itXAxisData = xAxisData.iterator();

                    //每个系列对应X轴上的值
                    while (itXAxisData.hasNext()){
                        String xAxisDataOne = (String) itXAxisData.next();
                        double groupsValueOne = 0;
                        String groupsValueOnes = null;
                        if ("time".equals(xColumnType)){
                            groupsValueOnes = timeConverterUtil.getGroupTimeConverter(yAxisType, yColumn, tableName, groupByName, xAxisType,
                                    groupsName, xColumn, xAxisDataOne,screenToColumnS);
                        }else {
                            String xColumnKey = "";
                            if (xAxisDataOne == null || "".equals(xAxisDataOne)){
                                xColumnKey  = xColumn + " is null";
                            }else
                            {
                                xColumnKey = xColumn + " = '" +xAxisDataOne +"'";
                            }

                            String groupByKey = "";
                            if (groupsName == null || "".equals(groupsName)){
                                groupByKey = groupByName +" is null";
                            }else {
                                groupByKey = groupByName + " = '" +groupsName +"'";
                            }
                            groupsValueOnes = showChartsMapper.getGroupsValue(yAxisType, yColumn, tableName, groupByKey, xColumnKey ,screenToColumnS);
                        }
                        if ("".equals(groupsValueOnes) || groupsValueOnes == null){
                            groupsValueOne = 0;
                        }else{
                            groupsValueOne = Double.parseDouble(groupsValueOnes);
                        }
                        LineSeriesMap lineSeriesMap = new LineSeriesMap();
                        lineSeriesMap.setDate(xAxisDataOne);
                        lineSeriesMap.setValue(groupsValueOne);
                        groupsValue.add(lineSeriesMap);
                    }

                    LineSeries lineSeries = new LineSeries();
                    lineSeries.setMdata(groupsValue);
                    lineSeries.setName(groupsName);
                    serieses.add(lineSeries);
                }
            }
        }

        chartsDataInfo.setSerieses(serieses);
        }catch (Exception e){
            e.printStackTrace();
            if (e.getCause().getMessage().contains("ORA-00904:")) {
                return new RepEntity(RepCode.erro.getCode(), "数据源" + e.getCause().getMessage().replace("ORA-00904:", "").replaceAll("标识符", "列"), null);
            }
            return new RepEntity(RepCode.erro.getCode(), e.getCause().getMessage(), null);
        }finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }

        return new RepEntity(RepCode.success, chartsDataInfo);
    }


}
