
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.SqlInfoUpdateModel;
import com.lideeyunji.tool.framework.sql.core.dql.update.lideeYunJiUpdateWrapper;
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.*;

/**
 * 构建 update 语句
 */
public class BuildUpdate implements ISQLExpression<SqlFormatModel> {


    @Override
    public SqlFormatModel interpret(SQLInterpretContext context) {
        SqlInfoUpdateModel sqlInfo = context.getUpdateModel();
        String tableName = sqlInfo.getTableName();
        Map<String, LideeYunJiFieldTypeEnum> fieldTypeEnumMap = sqlInfo.getFieldTypeEnumMap();
        lideeYunJiUpdateWrapper updateWrapper = new lideeYunJiUpdateWrapper(tableName);


        Map<String, Object> updateMap = sqlInfo.getUpdateMap();
        updateMap.forEach((k, v) -> {
            updateWrapper.set(k, v);
        });

        String sqlSet = updateWrapper.getSqlSet();
        Map<String, Object> paramNameValuePairs = updateWrapper.getParamNameValuePairs();

        String newSqlSet = "";


        List<String> setList = FuncBase.toStrList(sqlSet);
        for (String set : setList) {
            String[] split = set.split("=");
            String field = split[0];
            String value = split[1];
            if (!updateMap.containsKey(field) || FuncBase.isEmpty(updateMap.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}";
                }
            }
            if (FuncBase.isEmpty(newSqlSet)) {
                newSqlSet = field + "=" + value;
            } else {
                newSqlSet += "," + field + "=" + value;
            }
        }

        //处理null值
        for (Map.Entry<String, Object> entry : paramNameValuePairs.entrySet()) {
            Object value = entry.getValue();
            if (FuncBase.isEmpty(value)) {
                entry.setValue(null);
            }
        }

        String sql = "update " + tableName + " set " + newSqlSet;
        SqlFormatModel sqlFormatModel = new SqlFormatModel(sql, paramNameValuePairs);

        return sqlFormatModel;
    }


}
