太如意的生活便是平凡的生活,太容易获得的东西,便不是贵重的东西。——茅盾

今天是另外一种类型的关联更新封装

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
public static <T, K extends Comparable<? super K> & Serializable, S> BaseDbBO<S> saveSub(SubBO<T, K, S> bo) {
val subIds = Steam.of(bo.getMainList())
.flat(data -> Steam.of(bo.getSubIdGetters()).map(f -> f.apply(data)))
.nonNull().toList();
val subClazz = (Class<S>) Steam.of(bo.getSubGetters())
.findFirst()
.map(LambdaHelper::resolve)
.map(LambdaExecutable::getReturnType)
.orElseThrow(() -> new IllegalStateException("sub class not found"));
val primaryKeyGetter = MpUtil.<S, K>getGetter(subClazz,
TableInfoHelper.getTableInfo(subClazz).getKeyProperty());
val idSubMapFromDb = subIds.isEmpty() ? new HashMap<K, S>() :
Steam.of(Database.listByIds(subIds, subClazz)).toMap(primaryKeyGetter);
val subIdGetterSetterMap = Steam.of(bo.getSubIdGetters())
.toMap(Function.identity(), MpUtil::getSetter);
val comparesGetterSetterMap = Steam.of(bo.getSubCompares())
.toMap(Function.identity(), MpUtil::getSetter);

Steam.of(bo.getMainList()).forEach(data -> {
Steam.of(bo.getSubIdGetters()).zip(bo.getSubGetters(),
(subIdGetter, subGetter) -> {
val subFromClient = subGetter.apply(data);
val subId = subIdGetter.apply(data);
if (Objects.isNull(subFromClient)) {
return null;
}
// insert
if (Objects.isNull(subId)) {
bo.getWillInsertList().add(subFromClient);
return subFromClient;
}
val subFromDb = idSubMapFromDb.get(subId);
if (Objects.isNull(subFromDb)) {
bo.getWillInsertList().add(subFromClient);
return subFromClient;
}
// update
Steam.of(bo.getSubCompares()).forEach(ac -> {
val value = ac.apply(subFromClient);
if (!Objects.equals(value, ac.apply(subFromDb))) {
val executable = LambdaHelper.resolve((Serializable) ac);
val setter = (BiConsumer<S, Object>) comparesGetterSetterMap.get(ac);
val fieldType = ReflectHelper.getField(executable.getClazz(),
BeanHelper.getPropertyName(executable.getName())).getType();
setter.accept(subFromDb, value);
if (isComparable(fieldType) && (!bo.getWillUpdateList().contains(subFromDb))) {
bo.getWillUpdateList().add(subFromDb);
}
}
});
return subFromClient;
}).toList();
});
bo.setAfterExecuted(b -> {
val dataList = Steam.of(bo.getMainList()).flat(data ->
Steam.of(bo.getSubIdGetters()).zip(bo.getSubGetters(),
(subIdGetter, subGetter) -> {
S subFromClient = subGetter.apply(data);
if (Objects.nonNull(subFromClient)) {
val primaryKey = primaryKeyGetter.apply(subFromClient);
subIdGetterSetterMap.get(subIdGetter).accept(data, primaryKey);
}
return subFromClient;
}).nonNull().toList()).toList();
bo.setDataList(dataList);
});
return bo;
}

对应的bo

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
package com.ruben.simplestreamquery.pojo.bo;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* @author VampireAchao
* @since 2023/3/16 10:18
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class SubBO<T, K extends Comparable<? super K> & Serializable, S> extends BaseDbBO<S> {

private List<T> mainList;
private List<SFunction<T, K>> subIdGetters;
private List<SFunction<T, S>> subGetters;
private List<SFunction<S, ?>> subCompares;

public SubBO() {
super();
this.mainList = new ArrayList<>();
this.subIdGetters = new ArrayList<>();
this.subGetters = new ArrayList<>();
this.subCompares = new ArrayList<>();
}
}

目前仍然处于完善中,所以还没有集成进stream-query,临时放到另一个仓库里

地址