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

import com.usoftchina.bi.server.dao.chart.ShowChartsMapper;
import com.usoftchina.bi.server.model.bo.TimeReture;
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 TimeConverterUtil {
    @Autowired
    ShowChartsMapper showChartsMapper;

    //x轴时间类型
    public TimeReture timeConverter(String xColumnName, String tableName, String timeType, String screen, int counts){
        TimeReture timeReture = new TimeReture();
        String timeGroup = "'YYYY-MM-DD'";
        List<String> value = new ArrayList<String>();
        if ("year".equals(timeType)){
            timeGroup = "'YYYY'";
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("month".equals(timeType)){
            timeGroup = "'YYYY-MM'";
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("day".equals(timeType) || "".equals(timeType)){
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("quarter".equals(timeType)){
            timeGroup = "'YYYY-Q'";
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("week".equals(timeType)){
            timeGroup = "'YYYY-WW'";
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen, counts);
            timeReture.setValues(value);
        }else if ("halfYear".equals(timeType)){
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            if (StringUtils.isEmpty(screen)) {
                screen = "where 1 = 1";
            }
            value = showChartsMapper.getTimeYear(xColumnName, tableName, screen);
            timeReture.setValues(value);
        }else {
            timeGroup = "'YYYY-MM-DD'";
            int count = showChartsMapper.getTimeDateConunt(xColumnName, tableName, timeGroup, screen);
            if (count > counts){
                timeReture.setOverdose(false);
            }else {
                timeReture.setOverdose(true);
            }
            value = showChartsMapper.getTimeDate(xColumnName, tableName, timeGroup, screen,counts);
            timeReture.setValues(value);
        }
        return timeReture;
    }

    //x轴时间类型包装
    public List<String> toRespons(List<String> dataList, String timeType){
        List<String> value = new ArrayList<String>();
        String dataLast = null;
        Iterator isDataList = dataList.iterator();
        while (isDataList.hasNext()){
            String data = (String) isDataList.next();
            if (data == null || "".equals(data)){
                continue;
            }
            if ("quarter".equals(timeType)){
                String[] str = data.split("-");
                dataLast = str[0] + "-Q" + str[1];
            }else if ("month".equals(timeType)){
                String[] str = data.split("-");
                dataLast = str[0] + "-M" + str[1];
            }else if ("week".equals(timeType)){
                String[] str = data.split("-");
                dataLast = str[0] + "-W" + str[1];
            }
            value.add(dataLast);
        }
        return value;
    }

    //无分组时间值
    public String getTimeValueConverter(String yColumn, String xColumn, String tableName, String dataType, String timeType, String xdata, String screen){

        String timeGroup = "'YYYY-MM-DD'";
        String value = null;
        //判断时间类型
        if (xdata == null || "".equals(xdata)){
            value = showChartsMapper.getTimeIsNull(dataType, yColumn, tableName, xColumn, screen);
        }else {
            if ("halfYear".equals(timeType)) {
                String[] str = xdata.split("-");
                if ("H1".equals(str[1])) {
                    String firstIndex = str[0] + "-01";
                    String afterIndex = str[0] + "-06";
                    value = showChartsMapper.getTimeValueYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, screen);
                } else {
                    String firstIndex = str[0] + "-07";
                    String afterIndex = str[0] + "-12";
                    value = showChartsMapper.getTimeValueYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, screen);
                }
            } else if ("year".equals(timeType)) {
                timeGroup = "'YYYY'";
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else if ("month".equals(timeType)) {
                timeGroup = "'YYYY-MM'";
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else if ("day".equals(timeType) || "".equals(timeType)) {
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else if ("quarter".equals(timeType)) {
                timeGroup = "'YYYY-Q'";
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else if ("week".equals(timeType)) {
                timeGroup = "'YYYY-WW'";
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            } else {
                value = showChartsMapper.getTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, xdata, screen);
            }
        }
        return value;
    }

    /**
     * 无分组时间值(柱状图)
     * @param yColumn
     * @param xColumn
     * @param tableName
     * @param dataType
     * @param timeType
     * @param xAxisData
     * @param screen
     * @return
     */
    public List<Double> getHistogramTimeValue(String yColumn, String xColumn, String tableName, String dataType, String timeType, List<String> xAxisData, String screen){
        String timeGroup = "'YYYY-MM-DD'";
        List<Double> result = new ArrayList<>();
        Double value = null;
        //判断时间类型
        if ("halfYear".equals(timeType)) {
            for(String xData : xAxisData) {
                String firstIndex = null;
                String afterIndex = null;
                String[] str = xData.split("-");
                if ("H1".equals(str[1])) {
                    firstIndex = str[0] + "-01";
                    afterIndex = str[0] + "-06";
                } else {
                    firstIndex = str[0] + "-07";
                    afterIndex = str[0] + "-12";
                }
                value = showChartsMapper.getHistogramTimeValueYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, screen);
                result.add(value);
            }
            return result;
        } else if ("year".equals(timeType)) {
            timeGroup = "'YYYY'";
        } else if ("month".equals(timeType)) {
            timeGroup = "'YYYY-MM'";
        } else if ("day".equals(timeType) || "".equals(timeType)) {
        } else if ("quarter".equals(timeType)) {
            timeGroup = "'YYYY-Q'";
        } else if ("week".equals(timeType)) {
            timeGroup = "'YYYY-WW'";
        }
        return showChartsMapper.getHistogramTimeValue(dataType, yColumn, tableName, xColumn, timeGroup, screen);
    }

    //有分组时间类型值处理
    public String getGroupTimeConverter(String dataType, String yColumn, String tableName, String groupByName, String timeType,
                                        String groupsName, String xColumn, String xAxisDataOne, String screen){
        String timeGroup = "'YYYY-MM-DD'";
        String value = null;
        if(groupsName == null || "".equals(groupsName)){
            value = showChartsMapper.getGroupsValueTimeIsNull(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                    timeGroup, xAxisDataOne, screen);
        }else {
            if ("year".equals(timeType)) {
                timeGroup = "'YYYY'";
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("month".equals(timeType)) {
                timeGroup = "'YYYY-MM'";
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("quarter".equals(timeType)) {
                timeGroup = "'YYYY-Q'";
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("week".equals(timeType)) {
                timeGroup = "'YYYY-WW'";
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("day".equals(timeType) || "".equals(timeType)) {
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            } else if ("halfYear".equals(timeType)) {
                String[] str = xAxisDataOne.split("-");
                if ("上半年".equals(str[1])) {
                    String firstIndex = str[0] + "-01";
                    String afterIndex = str[0] + "-06";
                    value = showChartsMapper.getTimeValueHalfYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, groupByName, groupsName, screen);
                } else {
                    String firstIndex = str[0] + "-07";
                    String afterIndex = str[0] + "-12";
                    value = showChartsMapper.getTimeValueHalfYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, groupByName, groupsName, screen);
                }
            } else {
                value = showChartsMapper.getGroupsValueTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn,
                        timeGroup, xAxisDataOne);
            }
        }
        return value;
    }

    /**
     * 有分组时间类型值处理(柱状图)
     * @param dataType
     * @param yColumn
     * @param tableName
     * @param groupByName
     * @param timeType
     * @param groupsName
     * @param xColumn
     * @param xAxisData
     * @param screen
     * @return
     */
    public List<Double> getHistogramGroupTime(String dataType, String yColumn, String tableName, String groupByName, String timeType,
                                        String groupsName, String xColumn, List<String> xAxisData, String screen){
        String timeGroup = "'YYYY-MM-DD'";
        Double value = null;
        List<Double> result = new ArrayList<>();
        if ("halfYear".equals(timeType)) {
            for (String xAxisDataOne : xAxisData) {
                String firstIndex = null;
                String afterIndex = null;
                String[] str = xAxisDataOne.split("-");
                if ("上半年".equals(str[1])) {
                    firstIndex = str[0] + "-01";
                    afterIndex = str[0] + "-06";
                } else {
                    firstIndex = str[0] + "-07";
                    afterIndex = str[0] + "-12";
                }
                value = showChartsMapper.getHistogramTimeValueHalfYear(dataType, yColumn, tableName, xColumn, firstIndex, afterIndex, groupByName, groupsName, screen);
                result.add(value);
            }
            return result;
        } else if ("year".equals(timeType)) {
            timeGroup = "'YYYY'";
        } else if ("month".equals(timeType)) {
            timeGroup = "'YYYY-MM'";
        } else if ("quarter".equals(timeType)) {
            timeGroup = "'YYYY-Q'";
        } else if ("week".equals(timeType)) {
            timeGroup = "'YYYY-WW'";
        }
        return showChartsMapper.getHistogramTimeValueGroupValuesTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn, timeGroup);
    }

    public static String convertToOracleDateStr(String xColumn, String timeType) {
        if ("year".equals(timeType)){
            return "TO_CHAR(" + xColumn + ", 'YYYY')";
        }else if("month".equals(timeType)) {
            return "TO_CHAR(" + xColumn + ", 'YYYY-MM')";
        }else if ("day".equals(timeType)) {
            return "TO_CHAR(" + xColumn + ", 'YYYY-MM-DD')";
        }else if ("quarter".equals(timeType)) {
            return "TO_CHAR(" + xColumn + ", 'YYYY-Q')";
        }else if ("week".equals(timeType)) {
            return "TO_CHAR(" + xColumn + ", 'YYYY-WW')";
        }else {
            return "TO_CHAR(" + xColumn + ", 'YYYY-MM-DD')";
        }
    }

}
