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.chart.ShowChartsMapper;
import com.github.pagehelper.PageInfo;
import com.usoftchina.bi.server.model.bo.ChartsColumnConfig;
import com.usoftchina.bi.server.model.bo.Screen;
import com.usoftchina.bi.server.model.bo.ScreenStr;
import com.usoftchina.bi.server.model.vo.configVo.IndividualConfigInfo;
import com.usoftchina.bi.server.model.vo.dataVo.PopAndIndDataInfo;
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 java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/*
个人统计表
 */
@Service
public class ShowIndividualService {
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    GetChartsDataUtilService getChartsDataUtilService;
    @Autowired
    ShowChartsMapper showChartsMapper;
    @Autowired
    ScreenUtil screenUtil;
    @Autowired
    ChartsUtilService chartsUtilService;

    public RepEntity showIndividual(IndividualConfigInfo individualConfigInfo, String token, int dashId){
        if (individualConfigInfo == null || "".equals(individualConfigInfo)){
            return new RepEntity(RepCode.Null);
        }
        PopAndIndDataInfo popAndIndDataInfo = new PopAndIndDataInfo();

        List<LinkedHashMap<String, Object>> targetValue = new ArrayList<>();
        //取表名
        int id = individualConfigInfo.getId();
        //获取列配置
        ChartsColumnConfig chartsColumn = chartsConfigMapper.getChartsColumn(id);
        if (chartsColumn == null || "".equals(chartsColumn)){
            return new RepEntity(RepCode.Null);
        }
        popAndIndDataInfo.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);
            }

            //取筛选列表
            List<Screen> screens = individualConfigInfo.getFilters();
            ScreenStr scr = new ScreenStr();
            String screen = "";
            String screenStr = "";
            if ("".equals(screens) || screens == null || screens.size() == 0){
                screen = "";
                screenStr = "";
            }else {
                scr = screenUtil.screensUtil(screens, "", "");
                screen = scr.getRet();
                StringBuilder sb = new StringBuilder(screen);
                screenStr = String.valueOf(sb .replace(1, 5, "where "));
            }

        String columnName = individualConfigInfo.getSortColumn();
        //查询其他列
        String columnListName = individualConfigInfo.getColumnListName().toString();
        String sort = individualConfigInfo.getSort();

        String tableListName = this.getString(columnListName);

        targetValue = showChartsMapper.getOtherData(tableListName, tableName, columnName, sort, screenStr, individualConfigInfo.getTestPage().enablePaging());
        PageInfo<LinkedHashMap<String, Object>> pageInfo = new PageInfo<>(targetValue);
        popAndIndDataInfo.setValueList(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();
    }
        return new RepEntity(RepCode.success, popAndIndDataInfo);
    }


    private String getString(String str){
        String st1 = "[";
        String st2 = "]";
        String strA = str.replace(st1, "");
        String strB = strA.replace(st2, "");
        return strB;
    }
}
