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

com.github.yulichang.extension.kt.KtUpdateJoinWrapper Maven / Gradle / Ivy

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

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.github.yulichang.adapter.AdapterHelper;
import com.github.yulichang.extension.kt.interfaces.Update;
import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.KtUtils;
import com.github.yulichang.toolkit.ReflectionKit;
import com.github.yulichang.toolkit.TableHelper;
import com.github.yulichang.toolkit.TableList;
import com.github.yulichang.wrapper.interfaces.UpdateChain;
import kotlin.reflect.KProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author yulichang
 * @since 1.4.6
 */
@SuppressWarnings({"unused", "DuplicatedCode"})
public class KtUpdateJoinWrapper extends KtAbstractLambdaWrapper>
        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 KtUpdateJoinWrapper() {
        super();
    }

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

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

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

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

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

* 注意!!! * 这里这是的实体类是set部分, 不作为条件, where条件是wrapper.setEntity() */ public KtUpdateJoinWrapper 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 KtUpdateJoinWrapper setUpdateEntityAndNull(Object... entity) { if (Objects.isNull(updateEntityNull)) { updateEntityNull = new ArrayList<>(); } for (Object obj : entity) { Assert.notNull(obj, "更新实体不能为空"); updateEntityNull.add(obj); } return typedThis; } @Override public KtUpdateJoinWrapper set(boolean condition, KProperty column, Object val, String mapping) { return maybeDo(condition, () -> { if (Objects.isNull(updateSet)) { updateSet = new ArrayList<>(); } updateSet.add(new UpdateSet(column, val, mapping, false, null)); }); } @Override public KtUpdateJoinWrapper setIncrBy(boolean condition, KProperty column, Number val) { return maybeDo(condition, () -> { if (Objects.isNull(updateSet)) { updateSet = new ArrayList<>(); } updateSet.add(new UpdateSet(column, val, null, true, Constant.PLUS)); }); } @Override public KtUpdateJoinWrapper setDecrBy(boolean condition, KProperty column, Number val) { return maybeDo(condition, () -> { if (Objects.isNull(updateSet)) { updateSet = new ArrayList<>(); } updateSet.add(new UpdateSet(column, val, null, true, Constant.DASH)); }); } @Override public KtUpdateJoinWrapper setSql(boolean condition, String sql) { if (condition && StringUtils.isNotBlank(sql)) { if (Objects.isNull(sqlSet)) { sqlSet = new ArrayList<>(); } sqlSet.add(sql); } return typedThis; } @Override public String getSqlSet() { if (StringUtils.isNotBlank(sqlSetStr.getStringValue())) { return sqlSetStr.getStringValue(); } StringBuilder set = new StringBuilder(StringPool.EMPTY); if (CollectionUtils.isNotEmpty(updateSet)) { set = new StringBuilder(updateSet.stream().map(i -> { String col = tableList.getPrefixByClass(KtUtils.ref(i.getColumn())) + Constants.DOT + getCache(i.getColumn()).getColumn(); if (i.incOrDnc) { return col + Constants.EQUALS + col + i.cal + i.value; } else { return col + Constants.EQUALS + formatParam(i.mapping, i.value); } }) .collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA); } 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 KtUpdateJoinWrapper instance() { return instance(index, null, null, null); } @Override protected KtUpdateJoinWrapper instanceEmpty() { return new KtUpdateJoinWrapper<>(); } @Override protected KtUpdateJoinWrapper instance(Integer index, String keyWord, Class joinClass, String tableName) { return new KtUpdateJoinWrapper<>(getEntity(), getEntityClass(), paramNameSeq, paramNameValuePairs, new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), this.tableList, index, keyWord, joinClass, tableName); } /** * 不建议直接 new 该实例,使用 JoinWrappers.update(User.class) */ public KtUpdateJoinWrapper(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; } private void getSqlByEntity(StringBuilder sb, boolean filterNull, List entityList) { for (Object obj : entityList) { Assert.isTrue(tableList.contain(obj.getClass()), "更新的实体不是主表或关联表 <%>", 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(null, val)) .append(StringPool.COMMA); } } } @Override 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 @AllArgsConstructor public static class UpdateSet { private KProperty column; private Object value; private String mapping; private boolean incOrDnc; private String cal; } }