All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yulichang.wrapper.UpdateJoinWrapper Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
package com.github.yulichang.wrapper;

import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.adapter.AdapterHelper;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.ReflectionKit;
import com.github.yulichang.toolkit.*;
import com.github.yulichang.wrapper.interfaces.MFunction;
import com.github.yulichang.wrapper.interfaces.Update;
import com.github.yulichang.wrapper.interfaces.UpdateChain;
import com.github.yulichang.wrapper.segments.FuncConsumer;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yulichang
 * @since 1.4.5
 */
@SuppressWarnings("unused")
public class UpdateJoinWrapper extends JoinAbstractLambdaWrapper>
        implements Update>, UpdateChain {
    /**
     * SQL 更新字段内容,例如:name='1', age=2
     */
    private final SharedString sqlSetStr = new SharedString();
    /**
     * SQL 更新字段内容,例如:name='1', age=2
     */
    private List sqlSet;
    /**
     * SQL 更新字段内容,例如:name='1', age=2
     */
    private List updateSet;
    /**
     * SQL 更新实体(更新非空字段)
     */
    private List updateEntity;
    /**
     * SQL 更新实体(空字段也会更新)
     */
    private List updateEntityNull;

    private UpdateJoinWrapper() {
        super();
    }

    /**
     * 推荐使用此构造方法
     */
    public UpdateJoinWrapper(Class clazz) {
        super(clazz);
    }

    public UpdateJoinWrapper(T entity) {
        super(entity);
    }

    public UpdateJoinWrapper(Class clazz, String alias) {
        super(clazz, alias);
    }

    public UpdateJoinWrapper(T entity, String alias) {
        super(entity, alias);
    }

    /**
     * 设置更新的实体set语句部分, 更新非空字段
     * 

* 注意!!! * 这里这是的实体类是set部分, 不作为条件, where条件是wrapper.setEntity() */ public UpdateJoinWrapper setUpdateEntity(Object... entity) { if (Objects.isNull(updateEntity)) { updateEntity = new ArrayList<>(); } for (Object obj : entity) { Assert.notNull(obj, "更新实体不能为空"); updateEntity.add(obj); } return typedThis; } /** * 设置更新的实体set语句部分, 更新非空字段 *

* 注意!!! * 这里这是的实体类是set部分, 不作为条件, where条件是wrapper.setEntity() */ public UpdateJoinWrapper setUpdateEntityAndNull(Object... entity) { if (Objects.isNull(updateEntityNull)) { updateEntityNull = new ArrayList<>(); } for (Object obj : entity) { Assert.notNull(obj, "更新实体不能为空"); updateEntityNull.add(obj); } return typedThis; } @Override public UpdateJoinWrapper set(boolean condition, SFunction column, Object val, String mapping) { return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, mapping, false, null))); } @Override public UpdateJoinWrapper set(boolean condition, SFunction column, SFunction val, String mapping) { return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, mapping, false, null))); } @Override public UpdateJoinWrapper setIncrBy(boolean condition, SFunction column, Number val) { return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, null, true, Constant.PLUS))); } @Override public UpdateJoinWrapper setDecrBy(boolean condition, SFunction column, Number val) { return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, null, true, Constant.DASH))); } @Override public UpdateJoinWrapper setApply(boolean condition, String applySql, MFunction consumerFunction, Object... values) { if (condition && StringUtils.isNotBlank(applySql)) { FuncConsumer funcConsumer = consumerFunction.apply(new FuncConsumer()); UpdateSet set = new UpdateSet(); set.setApply(true); set.setFormat(applySql); set.setColumns(funcConsumer.getArgs()); set.setArgs(ArrayUtils.isNotEmpty(values) ? values : funcConsumer.getValues()); getUpdateSet().add(set); } return typedThis; } @Override public UpdateJoinWrapper setSql(boolean condition, String sql) { if (condition && StringUtils.isNotBlank(sql)) { if (Objects.isNull(sqlSet)) { sqlSet = new ArrayList<>(); } sqlSet.add(sql); } return typedThis; } @SuppressWarnings("DuplicatedCode") @Override public String getSqlSet() { if (StringUtils.isNotBlank(sqlSetStr.getStringValue())) { return sqlSetStr.getStringValue(); } StringBuilder set = new StringBuilder(StringPool.EMPTY); if (CollectionUtils.isNotEmpty(updateSet)) { String setSql = updateSet.stream().map(i -> { if (i.isApply) { String col = String.format(i.format, Arrays.stream(i.columns).map(f -> tableList.getPrefixByClass(LambdaUtils.getEntityClass(f)) + Constants.DOT + getCache(f).getColumn()).toArray()); return formatSqlMaybeWithParam(col, null, i.args); } else { String col = tableList.getPrefixByClass(LambdaUtils.getEntityClass(i.getColumn())) + Constants.DOT + getCache(i.getColumn()).getColumn(); if (i.incOrDnc) { return col + Constants.EQUALS + col + i.cal + i.value; } else { if (i.value instanceof Function) { SFunction value = (SFunction) i.getValue(); return col + Constants.EQUALS + tableList.getPrefixByClass(LambdaUtils.getEntityClass(value)) + Constants.DOT + getCache(value).getColumn(); } else { return col + Constants.EQUALS + formatParam(i.mapping, i.value); } } } }).collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA; set.append(setSql); } if (CollectionUtils.isNotEmpty(sqlSet)) { set.append(String.join(StringPool.COMMA, sqlSet)).append(StringPool.COMMA); } if (CollectionUtils.isNotEmpty(updateEntity)) { getSqlByEntity(set, true, updateEntity); } if (CollectionUtils.isNotEmpty(updateEntityNull)) { getSqlByEntity(set, false, updateEntityNull); } sqlSetStr.setStringValue(set.toString()); return set.toString(); } /** * 用于生成嵌套 sql *

故 sqlSelect 不向下传递

*/ @Override protected UpdateJoinWrapper instance() { return instance(index, null, null, null); } @Override protected UpdateJoinWrapper instanceEmpty() { return new UpdateJoinWrapper<>(); } @Override protected UpdateJoinWrapper instance(Integer index, String keyWord, Class joinClass, String tableName) { return new UpdateJoinWrapper<>(getEntity(), getEntityClass(), paramNameSeq, paramNameValuePairs, new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), this.tableList, index, keyWord, joinClass, tableName); } public List getUpdateSet() { if (updateSet == null) { updateSet = new ArrayList<>(); } return updateSet; } /** * 不建议直接 new 该实例,使用 JoinWrappers.update(User.class) */ protected UpdateJoinWrapper(T entity, Class entityClass, AtomicInteger paramNameSeq, Map paramNameValuePairs, MergeSegments mergeSegments, SharedString lastSql, SharedString sqlComment, SharedString sqlFirst, TableList tableList, Integer index, String keyWord, Class joinClass, String tableName) { super.setEntity(entity); super.setEntityClass(entityClass); this.paramNameSeq = paramNameSeq; this.paramNameValuePairs = paramNameValuePairs; this.expression = mergeSegments; this.lastSql = lastSql; this.sqlComment = sqlComment; this.sqlFirst = sqlFirst; this.tableList = tableList; this.index = index; this.keyWord = keyWord; this.joinClass = joinClass; this.tableName = tableName; } @SuppressWarnings("DuplicatedCode") private void getSqlByEntity(StringBuilder sb, boolean filterNull, List entityList) { for (Object obj : entityList) { Assert.isTrue(tableList.contain(obj.getClass()), "更新的实体不是主表或关联表 <%s>", obj.getClass().getSimpleName()); TableInfo tableInfo = TableHelper.getAssert(obj.getClass()); for (TableFieldInfo fieldInfo : tableInfo.getFieldList()) { if (AdapterHelper.getAdapter().mpjHasLogic(tableInfo) && fieldInfo.isLogicDelete()) { continue; } Object val; try { Field field = AdapterHelper.getAdapter().mpjGetField(fieldInfo, () -> { Field field1 = ReflectionKit.getFieldMap(obj.getClass()).get(fieldInfo.getProperty()); field1.setAccessible(true); return field1; }); val = field.get(obj); } catch (IllegalAccessException e) { throw new RuntimeException(e); } if (filterNull && Objects.isNull(val)) { continue; } sb.append(tableList.getPrefixByClass(obj.getClass())).append(Constants.DOT) .append(fieldInfo.getColumn()).append(Constants.EQUALS).append( formatParam(AdapterHelper.getAdapter().mpjMapping(fieldInfo), val)) .append(StringPool.COMMA); } } } @Override @SuppressWarnings("DuplicatedCode") public void clear() { super.clear(); sqlSetStr.toNull(); if (CollectionUtils.isNotEmpty(sqlSet)) { sqlSet.clear(); } if (CollectionUtils.isNotEmpty(updateSet)) { updateSet.clear(); } if (CollectionUtils.isNotEmpty(updateEntity)) { updateEntity.clear(); } if (CollectionUtils.isNotEmpty(updateEntityNull)) { updateEntityNull.clear(); } } @Data public static class UpdateSet { private SFunction column; private Object value; private String mapping; private boolean incOrDnc; private String cal; private boolean isApply = false; private String format; private SFunction[] columns; private Object[] args; public UpdateSet() { } public UpdateSet(SFunction column, Object value, String mapping, boolean incOrDnc, String cal) { this.column = column; this.value = value; this.mapping = mapping; this.incOrDnc = incOrDnc; this.cal = cal; } } }