
package com.lideeyunji.tool.framework.sql.utils;

import com.lideeyunji.tool.framework.exception.lideeYunJiException;
import com.lideeyunji.tool.framework.sql.entity.DbColunmTypesEntity;
import com.lideeyunji.tool.framework.sql.core.model.SqlFormatModel;
import com.lideeyunji.tool.framework.yunji.enums.LideeYunJiFieldTypeEnum;
import com.lideeyunji.tool.framework.yunji.enums.YNEnum;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;

import java.util.*;
import java.util.stream.Collectors;

public class PlusUtils {

    private static String NOT_NULL = "NOT NULL";//不允许为空
    private static String NULL = "NULL";//允许为空
    private static String DEFAULT = "DEFAULT";//默认值


    /**
     * 通过lideeyunji 类型来获取数据库类型
     * @param fieldTypeEnum
     * @param dbColunmTypes
     * @return
     */
    public static DbColunmTypesEntity.TypeEntity lideeyunJiType2DbType(LideeYunJiFieldTypeEnum fieldTypeEnum, DbColunmTypesEntity dbColunmTypes){
        Map<LideeYunJiFieldTypeEnum, DbColunmTypesEntity.TypeEntity> typeMap = new HashMap<>();
        typeMap.put(LideeYunJiFieldTypeEnum.STRING, dbColunmTypes.getLideeYunJiString());
        typeMap.put(LideeYunJiFieldTypeEnum.INTEGER, dbColunmTypes.getLideeYunJiInteger());
        typeMap.put(LideeYunJiFieldTypeEnum.DATE, dbColunmTypes.getLideeYunJiDate());
        typeMap.put(LideeYunJiFieldTypeEnum.DATETIME, dbColunmTypes.getLideeYunJiDateTime());
        typeMap.put(LideeYunJiFieldTypeEnum.TIME, dbColunmTypes.getLideeYunJiTime());
        typeMap.put(LideeYunJiFieldTypeEnum.BIGINT, dbColunmTypes.getLideeYunJiBigInt());
        typeMap.put(LideeYunJiFieldTypeEnum.BIGDECIMAL, dbColunmTypes.getLideeYunJiBigDecimal());
        typeMap.put(LideeYunJiFieldTypeEnum.TEXT, dbColunmTypes.getLideeYunJiText());
        typeMap.put(LideeYunJiFieldTypeEnum.LONGTEXT, dbColunmTypes.getLideeYunJiLongText());
        typeMap.put(LideeYunJiFieldTypeEnum.BLOB, dbColunmTypes.getLideeYunJiBlob());
        DbColunmTypesEntity.TypeEntity dbType = typeMap.get(fieldTypeEnum);
        if(FuncBase.isEmpty(dbType)){
            throw new lideeYunJiException("类型有误");
        }
        return dbType;
    }

    /**
     * 通过数据库 类型来获取 lideeyunji 类型
     * @param dbType 数据库类型 例如oracle对应的是number
     * @param dbColunmTypes
     * @return
     */
    public static List<DbColunmTypesEntity.TypeEntity> dbType2LideeyunJiTypeList(String dbType, DbColunmTypesEntity dbColunmTypes){
        dbType=dbType.toLowerCase();
        if(dbType.contains("(")){
            dbType =dbType.split("\\(")[0];
        }

        List<DbColunmTypesEntity.TypeEntity> resultList=new ArrayList<>();
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiString().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiString());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiInteger().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiInteger());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiDate().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiDate());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiDateTime().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiDateTime());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiTime().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiTime());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiBigInt().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiBigInt());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiBigDecimal().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiBigDecimal());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiText().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiText());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiLongText().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiLongText());
        }
        if(FuncBase.equals(dbType,dbColunmTypes.getLideeYunJiBlob().getDbType())){
            resultList.add(dbColunmTypes.getLideeYunJiBlob());
        }
        //默认字符串
        if(FuncBase.isEmpty(resultList)){
            resultList.add(dbColunmTypes.getLideeYunJiString());
        }

        return resultList;
    }


    //获取类型
    public static String getDbType(LideeYunJiFieldTypeEnum lideeYunJiFieldTypeEnum, String dbType, Integer len, Integer pointLen,Integer fieldMaxLen) {

        len = FuncBase.isEmpty(len) ? 0 : len;//长度
        pointLen = FuncBase.isEmpty(pointLen) ? 0 : pointLen;//小数

        if(FuncBase.isNotEmpty(fieldMaxLen) && fieldMaxLen>0){
            if(len>fieldMaxLen){
                len=fieldMaxLen;
            }
        }
        if(FuncBase.isNotEmpty(fieldMaxLen) && fieldMaxLen==0){
            len=0;
            pointLen=0;
        }

        if (FuncBase.equals(LideeYunJiFieldTypeEnum.STRING, lideeYunJiFieldTypeEnum)) {
            return String.format(" %s (%s)", dbType, len);
        } else if (FuncBase.equals(LideeYunJiFieldTypeEnum.INTEGER, lideeYunJiFieldTypeEnum)) {
            return len == 0 ? String.format(" %s ", dbType) : String.format(" %s (%s)", dbType, len);
        } else if (FuncBase.equals(LideeYunJiFieldTypeEnum.BIGINT, lideeYunJiFieldTypeEnum)) {
            return len == 0 ? String.format(" %s ", dbType) : String.format(" %s (%s)", dbType, len);
        } else if (FuncBase.equals(LideeYunJiFieldTypeEnum.BIGDECIMAL, lideeYunJiFieldTypeEnum)) {
            return String.format(" %s (%s, %s)", dbType, len, pointLen);
        } else{
            return String.format(" %s ", dbType);
        }

    }

    public static String getDefaultValSql(String defaultVal, String isNull,String nowIsNull) {
        String nullStr=NULL;
        String notNullStr=NOT_NULL;
        if(FuncBase.isNotEmpty(nowIsNull) && FuncBase.equals(isNull,nowIsNull)){//如果一样，则不用操作，oracle特殊
            nullStr="";
            notNullStr="";
        }
        if (FuncBase.equals(isNull, YNEnum.Y.getCode())) {//允许为空
            if (FuncBase.isNotEmpty(defaultVal)) {
                String formatStr = DEFAULT + " '%s' ";
                return String.format(formatStr, defaultVal);
            } else {
                return nullStr;
            }
        } else {//不为空
            if (FuncBase.isNotEmpty(defaultVal)) {
                String formatStr = DEFAULT + " '%s'";
                return NOT_NULL + " " + String.format(formatStr, defaultVal);
            } else {
                return notNullStr;
            }
        }

    }

    public static List<SqlFormatModel> ddl2SqlFormatModel(List<String> ddlList){
        return ddlList.stream()
                .map(ddl -> new SqlFormatModel(ddl))
                .collect(Collectors.toList());
    }

    public static List<SqlFormatModel> ddl2SqlFormatModel(String ddl){
        return Collections.singletonList(new SqlFormatModel(ddl));
    }
}
