package com.usoftchina.bi.server.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.usoftchina.bi.server.model.bo.Screen;
import com.usoftchina.bi.server.model.bo.ScreenStr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/*
解析筛选信息
 */
@Component
public class ScreenUtil {
    @Autowired
    ObjectMapper objectMapper;

    public ScreenStr screensUtil(List<Screen> screenList, String xColumn, String xColumnType) {
        //返回值
        ScreenStr screenStr = new ScreenStr();
        if (screenList.size() == 0) {
            screenStr.setRet("");
            screenStr.setWithColumnRet("");
            return screenStr;
        }
        String ret = "";            //筛选条件
        String withColumnRet = "";   //与目标列相同的筛选条件
        Iterator isList = screenList.iterator();
        while (isList.hasNext()) {
            Screen screen = (Screen) isList.next();
            String columnName = screen.getColumnName();
            String columnType = screen.getColumnType();
            String symbol = screen.getSymbol();
            String value = screen.getValue();

            if (columnType != "time" && !("time".equals(columnType))) {
                String symbVal = getSymbAndVal(symbol, value, columnType);
                if (xColumn.equals(columnName)) {
                    withColumnRet = ret + " and " + columnName + " " + symbVal;
                } else {
                    ret = ret + " and " + columnName + " " + symbVal;
                }
            }
            if (columnType == "time" || "time".equals(columnType)) {
                String symbVal = getTimeSymbAndVal(symbol, value);
                String column = getTimeColumn(columnName, xColumnType);
                if (xColumn.equals(columnName)) {
                    withColumnRet = ret + " and " + column + " " + symbVal;
                } else {
                    ret = ret + " and " + column + " " + symbVal;
                }
            }
        }
        screenStr.setRet(ret);
        screenStr.setWithColumnRet(withColumnRet);
        return screenStr;
    }

    public String getSymbAndVal(String symbol, String value, String columnType) {
        String values = "" + value;
        String tar = "";
        if ("contain".equals(symbol)) {
            if ("categorical".equals(columnType)) {
                tar = "in " + getContainsCate(value);
            } else {
                tar = "like '%" + values + "%'";
            }
        } else if ("notContain".equals(symbol)) {
            if ("categorical".equals(columnType)) {
                tar = "not in " + getContainsCate(value);
            } else {
                tar = "not like '%" + values + "%'";
            }
        } else if ("startsWith".equals(symbol)) {
            tar = "like '" + values + "%'";
        } else if ("endsWith".equals(symbol)) {
            tar = "like '%" + values + "'";
        } else if ("null".equals(symbol)) {
            tar = "is null";
        } else if ("notNull".equals(symbol)) {
            tar = "is not null";
        } else if ("between".equals(symbol)) {
            tar = "";
            String[] str = value.split(",");
            String str1 = str[0];
            String str2 = str[1];
            tar = "between '" + str1 + "' and '" + str2 + "'";
        } else if("notInclude".equals(symbol)) {
            tar = "not like '%" + values + "%'";
        } else if("include".equals(symbol)){
            tar = "like '%" + values + "%'";
        } else if("equals".equals(symbol)){
            tar = "=" + " '" + values + "'";
        } else if("notEquals".equals(symbol)){
            tar = "!=" + " '" + values + "'";
        }else {
            tar = symbol + " '" + values + "'";
        }
        return tar;
    }

    public String getTimeColumn(String columnName, String xColumnType) {
        if ("year".equals(xColumnType)) {
            return "to_char(" + columnName + ", 'yyyy')";
        } else if ("month".equals(xColumnType)) {
            return "to_char(" + columnName + ", 'yyyy-mm')";
        } else if ("day".equals(xColumnType)) {
            return "to_char(" + columnName + ", 'yyyy-mm-dd')";
        } else if ("quarter".equals(xColumnType)) {
            return "to_char(" + columnName + ", 'yyyy-Q')";
        } else if ("week".equals(xColumnType)) {
            return "to_char(" + columnName + ", 'yyyy-ww')";
        } else {
            return "to_char(" + columnName + ", 'yyyy-mm-dd')";
        }
    }

    private String getTimeSymbAndVal(String symbol, String value) {
        String values = "" + value;
        String tar = "";
        if ("null".equals(symbol)) {
            tar = "is null";
        } else if ("notNull".equals(symbol)) {
            tar = "is not null";
        } else if ("between".equals(symbol)) {
            tar = "";
            String[] str = value.split(",");
            String str1 = str[0];
            String str2 = str[1];
            tar = "between '" + str1 + "' and '" + str2 + "'";
        } else {
            tar = symbol + " '" + values + "'";
        }
        return tar;
    }

    public String getContainsCate(String value) {
        List<String> val = new ArrayList<>();
        CollectionType javaType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
        try {
            val = objectMapper.readValue(value, javaType);   //这里不需要强制转换
        } catch (IOException e) {
            e.printStackTrace();
        }

        String valueString = "";
        Iterator isList = val.iterator();
        while (isList.hasNext()) {
            String v = String.valueOf(isList.next());
            valueString = valueString + ", '" + v + "'";
        }
        valueString = valueString.replaceFirst(",", "(") + ")";
        return valueString;
    }
}
