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.server.dao.chart.ChartsConfigMapper;
import com.usoftchina.bi.server.dao.dataSource.DataConnectorMapper;
import com.usoftchina.bi.server.dao.chart.ShowChartsMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.usoftchina.bi.server.model.po.Databases;
import com.usoftchina.bi.server.model.vo.configVo.ShowDataChartInfo;
import com.usoftchina.bi.server.model.vo.dataVo.ColumnValuesInfo;
import com.usoftchina.bi.core.jdbc.DynamicDataSourceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class GetChartsDataUtilService {
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    DataConnectorMapper dataConnectorMapper;
    @Autowired
    ChartsUtilService chartsUtilService;
    @Autowired
    ShowChartsMapper showChartsMapper;
    @Autowired
    GetChartsDataUtilService getChartsDataUtilService;

    /*
    通过图表ID获取数据库连接ID
     */
    public int getBaseId(int id){
        //取数据源ID
        String dataConfig = chartsConfigMapper.getBaseConifg(id);
        return new Integer(dataConfig);
    }

    /*
    通过数据源ID获取数据库连接ID
     */
    public int getBaseIdByConnect(int id){
        //取数据源ID
        String dataConfig = dataConnectorMapper.getDbConfig(id);
        return new Integer(dataConfig);
    }

    //取数据源Id
    public int getConnectId(int id){
        //取数据源ID
        int ConnectId = chartsConfigMapper.getBaseId(id);
        return ConnectId;
    }

    /*
    查看图表列数据信息
     */
    public RepEntity getChartsData(String token, ShowDataChartInfo showDataChartInfo){
        int chartsId = showDataChartInfo.getChartId();
        //取表名;
        String tableName = chartsUtilService.getSqlStr(token, chartsId, 0);
        if ("".equals(tableName)){
            return new RepEntity(RepCode.NoAuthority);
        }

        int baseId = getChartsDataUtilService.getBaseId(chartsId);

        ColumnValuesInfo columnValuesInfo = new ColumnValuesInfo();

        String columnConfig = chartsConfigMapper.getConfigByChartId(chartsId);
        String sql = chartsUtilService.getSqlStr(token, chartsId, 0);
        if ("".equals(sql)) {
            return new RepEntity(RepCode.NoAuthority);
        }
        List<Map<String, Object>> values = new ArrayList<>();
        try {
            if (!DynamicDataSourceContextHolder.isContainsDataSource(baseId)) {
                return new RepEntity(RepCode.DataSourceNull);
            } else {
                DynamicDataSourceContextHolder.setDataSourceType(baseId);
            }
            values = chartsConfigMapper.getChartsData(sql, showDataChartInfo.getTestPage().enablePaging());
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(values);
            columnValuesInfo.setValues(pageInfo);
        }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();
        }
        columnValuesInfo.setColumnConfig(columnConfig);
        return new RepEntity(RepCode.success, columnValuesInfo);
    }
}
