1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| package com.ruben.simplestreamquery.util;
import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.ruben.simplestreamquery.pojo.bo.RelationBO; import io.github.vampireachao.stream.core.bean.BeanHelper; import io.github.vampireachao.stream.core.lambda.LambdaExecutable; import io.github.vampireachao.stream.core.lambda.LambdaHelper; import io.github.vampireachao.stream.core.lambda.function.SerSupp; import io.github.vampireachao.stream.core.reflect.ReflectHelper; import io.github.vampireachao.stream.core.stream.Steam; import io.github.vampireachao.stream.plugin.mybatisplus.Database; import io.github.vampireachao.stream.plugin.mybatisplus.Many; import lombok.val;
import java.io.Serializable; import java.lang.reflect.Constructor; import java.util.Objects; import java.util.function.BiConsumer; import java.util.function.Supplier;
import static cn.hutool.core.text.CharSequenceUtil.genSetter; import static java.util.Collections.emptyList;
public class MpUtil {
public static <T, K extends Comparable<? super K> & Serializable, A, U extends Comparable<? super U> & Serializable, R> BaseDbBO<R> saveRelation(RelationBO<T, K, A, U, R> bo) { val mainList = bo.getMainList(); val mainKeys = Steam.of(mainList).map(bo.getMainKey()).toList(); val relationMainGetter = LambdaHelper.resolve(bo.getRelationMain()); val relationMainSetter = getSetter(bo.getRelationMain()); val relationAttachSetter = getSetter(bo.getRelationAttach()); val relationClass = (Class<?>) relationMainGetter.getInstantiatedTypes()[0];
val constructor = ((SerSupp<Constructor<?>>) relationClass::getConstructor).get(); val constructorLambda = LambdaHelper.revert(Supplier.class, constructor); val nameRelationCompareSetterMap = Steam.of(bo.getRelationCompares()) .map(c -> LambdaHelper.resolve((Serializable) c)) .<String, BiConsumer<R, Object>>toMap(LambdaExecutable::getName, MpUtil::getSetter); val willInsertList = Steam.of(mainList).flatMap(vo -> Steam.of(bo.getAttachGetter().apply(vo))) .filter(bo.getAttachKey(), null) .toList(); if (!willInsertList.isEmpty()) { if (bo.getInsertOnMissAttach()) { Database.saveFewSql(willInsertList); } else { throw new IllegalStateException("attach data missing"); } } val relationsFromClient = Steam.of(mainList) .flatMap(vo -> Steam.of(bo.getAttachGetter().apply(vo)) .map(attach -> { R relation = (R) constructorLambda.get(); relationMainSetter.accept(relation, bo.getMainKey().apply(vo)); relationAttachSetter.accept(relation, bo.getAttachKey().apply(attach)); Steam.of(bo.getAttachCompares()).forEach(c -> { val executable = LambdaHelper.resolve((Serializable) c); val setter = nameRelationCompareSetterMap.get(executable.getName()); setter.accept(relation, c.apply(attach)); }); return relation; }) ).toList(); val relationMain = bo.getRelationMain(); val relationsFromDb = Many.of(relationMain).in(mainKeys).query(); if (relationsFromClient.isEmpty()) { bo.setWillDeleteList(relationsFromDb); return bo; } if (relationsFromDb.isEmpty()) { bo.setWillInsertList(relationsFromClient); bo.setDataList(relationsFromClient); return bo; } val mainIdRelationsMapFromDb = Steam.of(relationsFromDb) .group(relationMain); val mainIdRelationsMapFromClient = Steam.of(relationsFromClient) .group(relationMain); mainKeys.forEach(mainKey -> { val relationListFromDb = mainIdRelationsMapFromDb.getOrDefault(mainKey, emptyList()); val relationListFromClient = mainIdRelationsMapFromClient.getOrDefault(mainKey, emptyList()); if (relationListFromDb.isEmpty() && relationListFromClient.isEmpty()) { return; } if (relationListFromDb.isEmpty()) { bo.getWillInsertList().addAll(relationListFromClient); return; } if (relationListFromClient.isEmpty()) { bo.getWillDeleteList().addAll(relationListFromDb); return; } val attachKeyRelationMapFromClient = Steam.of(relationListFromClient) .toMap(bo.getRelationAttach()); val attachKeysFromClient = attachKeyRelationMapFromClient.keySet(); val attachKeyRelationMapFromDb = Steam.of(relationListFromDb) .toMap(bo.getRelationAttach()); val attachKeysFromDb = attachKeyRelationMapFromDb.keySet(); Steam.of(attachKeysFromClient).filter(attachKey -> !attachKeysFromDb.contains(attachKey)) .map(attachKeyRelationMapFromClient::get) .forEach(bo.getWillInsertList()::add); Steam.of(attachKeysFromDb).filter(attachKey -> !attachKeysFromClient.contains(attachKey)) .map(attachKeyRelationMapFromDb::get) .forEach(bo.getWillDeleteList()::add); attachKeysFromDb.retainAll(attachKeysFromClient); attachKeysFromDb.forEach(attachKey -> { val relationFromDb = attachKeyRelationMapFromDb.get(attachKey); val relationFromClient = attachKeyRelationMapFromClient.get(attachKey); if (Objects.nonNull(relationFromDb) && Objects.nonNull(relationFromClient)) { Steam.of(bo.getRelationCompares()).forEach(ac -> { val value = ac.apply(relationFromClient); if (!Objects.equals(value, ac.apply(relationFromDb))) { val executable = LambdaHelper.resolve((Serializable) ac); val setter = nameRelationCompareSetterMap.get(executable.getName()); setter.accept(relationFromDb, value); if (!bo.getWillUpdateList().contains(relationFromDb)) { bo.getWillUpdateList().add(relationFromDb); } } }); } }); }); bo.setAfterExecuted(b -> { val relationPrimaryKey = MpUtil.getGetter((Class<R>) relationClass, TableInfoHelper.getTableInfo(relationClass).getKeyProperty()); relationsFromDb.removeIf(bo.getWillDeleteList()::contains); relationsFromDb.addAll(bo.getWillInsertList()); val idAttachMap = Steam.of(relationsFromDb).toMap(relationPrimaryKey); relationsFromClient.forEach(data -> { val pk = relationPrimaryKey.apply(data); idAttachMap.put(pk, data); }); bo.setDataList(new ArrayList<>(idAttachMap.values())); }); return bo; }
public static <T, R> SFunction<T, R> getGetter(Class<T> clazz, String property) { return LambdaHelper.revert( SFunction.class, ReflectHelper.getMethod( clazz, StrUtil.genGetter(property) ) ); }
public static <T, R> BiConsumer<T, R> getSetter(SFunction<T, R> getter) { return getSetter(LambdaHelper.resolve(getter)); }
public static <T, R> BiConsumer<T, R> getSetter(LambdaExecutable executable) { val setterName = genSetter(BeanHelper.getPropertyName(executable.getName())); val setter = Steam.of(ReflectHelper.getMethods(executable.getClazz())) .findFirst(m -> m.getName().equals(setterName)) .orElse(null); return LambdaHelper.revert(BiConsumer.class, setter); } }
|