
package com.lideeyunji.tool.framework.sql.core.dql.insert;



import com.lideeyunji.tool.framework.sql.core.conditions.AbstractWrapper;
import com.lideeyunji.tool.framework.sql.core.conditions.SharedString;
import com.lideeyunji.tool.framework.sql.core.conditions.segments.MergeSegments;
import com.lideeyunji.tool.framework.sql.core.dql.update.Update;
import com.lideeyunji.tool.framework.sql.core.toolkit.CollectionUtils;
import com.lideeyunji.tool.framework.sql.core.toolkit.Constants;
import com.lideeyunji.tool.framework.sql.core.toolkit.StringUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author JX
 * @create 2024-03-27 17:47
 * @dedescription:
 */
public class lideeYunJiInsertWrapper<T> extends AbstractWrapper<T, String, lideeYunJiInsertWrapper<T>>
        implements Update<lideeYunJiInsertWrapper<T>, String> {
    private final List<String> sqlSet;


    public lideeYunJiInsertWrapper() {
        // 如果无参构造函数，请注意实体 NULL 情况 SET 必须有否则 SQL 异常
        this(null);
    }

    public lideeYunJiInsertWrapper(T entity) {
        super.setEntity(entity);
        super.initNeed();
        this.sqlSet = new ArrayList<>();
    }

    private lideeYunJiInsertWrapper(T entity, List<String> sqlSet, AtomicInteger paramNameSeq,
                                    Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments, SharedString paramAlias,
                                    SharedString lastSql, SharedString sqlComment, SharedString sqlFirst) {
        super.setEntity(entity);
        this.sqlSet = sqlSet;
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
        this.paramAlias = paramAlias;
        this.lastSql = lastSql;
        this.sqlComment = sqlComment;
        this.sqlFirst = sqlFirst;
    }



    @Override
    public String getSqlSet() {
        if (CollectionUtils.isEmpty(sqlSet)) {
            return null;
        }
        return String.join(Constants.COMMA, sqlSet);
    }

    @Override
    public lideeYunJiInsertWrapper<T> set(boolean condition, String column, Object val, String mapping) {
        return maybeDo(condition, () -> {
            String sql = formatParam(mapping, val);
            sqlSet.add(column + Constants.EQUALS + sql);
        });
    }

    @Override
    public lideeYunJiInsertWrapper<T> setSql(boolean condition, String setSql, Object... params) {
        if (condition && StringUtils.isNotBlank(setSql)) {
            sqlSet.add(formatSqlMaybeWithParam(setSql, params));
        }
        return typedThis;
    }


    @Override
    protected lideeYunJiInsertWrapper<T> instance() {
        return new lideeYunJiInsertWrapper<>(getEntity(), null, paramNameSeq, paramNameValuePairs, new MergeSegments(),
                paramAlias, SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString());
    }

    @Override
    public void clear() {
        super.clear();
        sqlSet.clear();
    }


}
