
package com.lideeyunji.tool.framework.sql.build.build.dql;

import com.lideeyunji.tool.framework.sql.build.ISQLExpression;
import com.lideeyunji.tool.framework.sql.build.buildmodel.SQLInterpretContext;
import com.lideeyunji.tool.framework.sql.build.buildmodel.dql.SqlInfoInsertModel;
import com.lideeyunji.tool.framework.sql.core.dql.insert.lideeYunJiInsertWrapper;
import com.lideeyunji.tool.framework.sql.core.model.SqlFormatModel;
import com.lideeyunji.tool.framework.yunji.enums.LideeYunJiFieldTypeEnum;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;

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


/**
 * 构建 insert 语句
 */
public class BuildInsert implements ISQLExpression<SqlFormatModel>  {



    @Override
    public SqlFormatModel interpret(SQLInterpretContext context){
        SqlInfoInsertModel sqlInfo = context.getInsertModel();
        String tableName = sqlInfo.getTableName();
        Map<String, LideeYunJiFieldTypeEnum> fieldTypeEnumMap = sqlInfo.getFieldTypeEnumMap();

        Map<String, Object> insertMap = sqlInfo.getInsertMap();

        lideeYunJiInsertWrapper insertWrapper = new lideeYunJiInsertWrapper<>();
        for (Map.Entry<String, Object> entry : insertMap.entrySet()) {
            insertWrapper.set(entry.getKey(),entry.getValue());
        }


        //封装
        String sqlSet = insertWrapper.getSqlSet();
        String customSqlSegment = insertWrapper.getCustomSqlSegment();
        Map<String, Object> paramNameValuePairs = insertWrapper.getParamNameValuePairs();
        for (Map.Entry<String,Object> entry : paramNameValuePairs.entrySet()) {
            Object value = entry.getValue();
            if(FuncBase.isEmpty(value)){
                entry.setValue(null);
            }
        }

        List<String> fieldList=new ArrayList<>();
        List<String> valueList=new ArrayList<>();
        List<String> setList = FuncBase.toStrList(sqlSet);
        for(String set:setList){
            String[] split = set.split("=");
            String field=split[0];
            String value=split[1];
            fieldList.add(field);
            if (!insertMap.containsKey(field) || FuncBase.isEmpty(insertMap.get(field))) {//不存在,指定jdbc类型，不然oracle报错
                if (FuncBase.isNotEmpty(fieldTypeEnumMap)) {
                    LideeYunJiFieldTypeEnum lideeYunJiFieldTypeEnum = fieldTypeEnumMap.get(field);
                    if (lideeYunJiFieldTypeEnum != null) {
                        switch (lideeYunJiFieldTypeEnum) {
                            case INTEGER:
                                value = value.substring(0, value.length() - 1) + ",jdbcType=INTEGER}";
                                break;
                            case DATE:
                                value = value.substring(0, value.length() - 1) + ",jdbcType=DATE}";
                                break;
                            case DATETIME:
                                value = value.substring(0, value.length() - 1) + ",jdbcType=TIMESTAMP}";
                                break;
                            case TIME:
                                value = value.substring(0, value.length() - 1) + ",jdbcType=TIME}";
                                break;
                            case BIGINT:
                                value = value.substring(0, value.length() - 1) + ",jdbcType=BIGINT}";
                                break;
                            case BIGDECIMAL:
                                value = value.substring(0, value.length() - 1) + ",jdbcType=DECIMAL}";
                                break;
                            default:
                                value = value.substring(0, value.length() - 1) + ",jdbcType=VARCHAR}";
                                break;
                        }
                    } else {
                        value = value.substring(0, value.length() - 1) + ",jdbcType=VARCHAR}";
                    }

                } else {
                    value = value.substring(0, value.length() - 1) + ",jdbcType=VARCHAR}";
                }
            }
            valueList.add(value);

        }

        String sql = "insert into " + tableName + "(%s) VALUE(%s) ";
        String finalSql = String.format(sql, String.join(",", fieldList), String.join(",", valueList));

        SqlFormatModel sqlFormatModel = new SqlFormatModel(finalSql,paramNameValuePairs);
        return sqlFormatModel;
    }
}
