add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package com.dobbinsoft.fw.support.mapper;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dobbinsoft.fw.core.util.ReflectUtil;
import com.dobbinsoft.fw.support.annotation.ForeignKey;
import com.dobbinsoft.fw.support.annotation.LeafTable;
import com.dobbinsoft.fw.support.annotation.enums.ListLeafType;
import com.dobbinsoft.fw.support.context.QueryContext;
import com.dobbinsoft.fw.support.domain.SuperDO;
import com.dobbinsoft.fw.support.model.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
 
import java.io.Serializable;
import java.lang.reflect.*;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Description:
 * User: rize
 * Date: 2020/8/1
 * Time: 10:54
 */
public interface IMapper<T> extends BaseMapper<T> {
 
    /**
     * 高效率批量插入 仅支持 MYSQL
     *
     * @param entityList
     * @return
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
 
    /**
     * 初始化表
     *
     * @return
     */
    Integer tableInit();
 
    /**
     * 查询Dto,并获取子表数据
     *
     * @param id
     * @param clazz
     * @param <DTO>
     * @return
     */
    default <DTO> DTO selectByIdDto(Long id, Class<DTO> clazz) {
        return selectByIdDto(id, clazz, getFieldList(clazz));
    }
 
    /**
     * 查询Dto,并获取子表数据
     *
     * @param id
     * @param clazz
     * @param fieldList
     * @param <DTO>
     * @return
     */
    default <DTO> DTO selectByIdDto(Long id, Class<DTO> clazz, String... fieldList) {
        T t = this.selectById(id);
        if (t == null) {
            return null;
        }
        DTO dto = null;
        try {
            dto = clazz.newInstance();
            BeanUtils.copyProperties(t, dto);
            for (String fieldStr : fieldList) {
                Field field = clazz.getDeclaredField(fieldStr);
                if (Collection.class.isAssignableFrom(field.getType())) {
                    ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
                    Class itemClazz = (Class) parameterizedType.getActualTypeArguments()[0];
                    Field[] itemFields = itemClazz.getDeclaredFields();
                    String name = null;
                    for (Field itemField : itemFields) {
                        ForeignKey annotation = itemField.getAnnotation(ForeignKey.class);
                        if (annotation != null) {
                            name = StrUtil.toUnderlineCase(itemField.getName());
                        }
                    }
                    if (name != null) {
                        IMapper mapper = MapperManager.map.get(itemClazz);
                        if (mapper == null) {
                            throw new RuntimeException("请确认 " + itemClazz.toGenericString() + " 所对应Mapper存在");
                        }
                        List list = mapper.selectList(new QueryWrapper<Object>().eq(name, id));
                        Method method = clazz.getMethod(ReflectUtil.getMethodName(fieldStr, "set"), field.getType());
                        method.invoke(dto, list);
                    }
                }
            }
        } catch (InstantiationException | IllegalAccessException | NoSuchFieldException | NoSuchMethodException | InvocationTargetException e) {
            // 反射异常
            e.printStackTrace();
        }
        return dto;
    }
 
    /**
     * 插入DTO,并插入子表
     *
     * @param dto
     * @return
     */
    default Integer insertDto(Object dto) {
        return insertDto(dto, getFieldList(dto.getClass()));
    }
 
    /**
     * 插入DTO,并插入子表
     *
     * @param dto
     * @param fieldList
     * @return
     */
    default Integer insertDto(Object dto, String... fieldList) {
        int effect = 0;
        Class<T> entityClass = this.getEntityClass();
        if (entityClass != null) {
            try {
                T instance = (T) entityClass.newInstance();
                BeanUtils.copyProperties(dto, instance);
                effect = this.insert(instance);
                Method getIdMethod = cn.hutool.core.util.ReflectUtil.getMethod(instance.getClass(), "getId");
                Object primaryKey = getIdMethod.invoke(instance);
                try {
                    Method method = dto.getClass().getMethod("setId", Long.class);
                    method.invoke(dto, primaryKey);
                } catch (NoSuchMethodException e) {
 
                }
                // 子表
                Class<?> dtoClass = dto.getClass();
                for (String filedStr : fieldList) {
                    Method get = dtoClass.getMethod(ReflectUtil.getMethodName(filedStr, "get"));
                    if (get.invoke(dto) instanceof Collection) {
                        Collection subList = (Collection) get.invoke(dto);
                        if (!CollectionUtils.isEmpty(subList)) {
                            // 找到子表的Mapper
                            Class subClazz = null;
                            Method fKeyMethod = null;
                            for (Object subObj : subList) {
                                if (subClazz == null) {
                                    subClazz = subObj.getClass();
                                    // 找出子表外键,并将primaryKey设置进去
                                    Field[] subClazzDeclaredFields = subClazz.getDeclaredFields();
                                    for (Field subField : subClazzDeclaredFields) {
                                        ForeignKey foreignKey = subField.getAnnotation(ForeignKey.class);
                                        if (foreignKey != null) {
                                            fKeyMethod = cn.hutool.core.util.ReflectUtil.getMethod(subClazz,
                                                    ReflectUtil.getMethodName(subField.getName(), "set"), primaryKey.getClass());
                                        }
                                    }
                                }
                                if (fKeyMethod == null) {
                                    throw new RuntimeException("子表未设置外键:" + subClazz.getName());
                                }
                                // 设置外键
                                fKeyMethod.invoke(subObj, primaryKey);
                            }
                            IMapper mapper = MapperManager.map.get(subClazz);
                            if (mapper == null) {
                                throw new RuntimeException("请确认 " + subClazz.toGenericString() + " 所对应Mapper存在");
                            }
                            Field field = dtoClass.getDeclaredField(filedStr);
                            boolean batch = false;
                            LeafTable leafTable = field.getAnnotation(LeafTable.class);
                            if (leafTable != null) {
                                batch = leafTable.batch();
                            }
                            if (batch) {
                                // 若是批量插入
                                mapper.insertBatchSomeColumn(subList);
                            } else {
                                // 单个插入
                                for (Object subItem : subList) {
                                    mapper.insert(subItem);
                                }
                            }
                        }
                    }
                }
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return effect;
    }
 
    default Integer deleteByIdDto(Serializable id, Class<?> clazz) {
        return deleteByIdDto(id, clazz, getFieldList(clazz));
    }
 
    /**
     * 删除DTO,并删除其子表数据
     *
     * @param id        主键
     * @param clazz
     * @param fieldList
     * @return
     */
    default Integer deleteByIdDto(Serializable id, Class<?> clazz, String... fieldList) {
        // 1.删除主表
        int effect = this.deleteById(id);
        // 2.找到从表
        if (effect > 0) {
            for (String fieldStr : fieldList) {
                try {
                    Field field = clazz.getDeclaredField(fieldStr);
                    Type genericType = field.getGenericType();
                    if (genericType instanceof ParameterizedType) {
                        ParameterizedType pt = (ParameterizedType) genericType;
                        Type actualTypeArgument = pt.getActualTypeArguments()[0];
                        if (actualTypeArgument instanceof Class) {
                            if (SuperDO.class.isAssignableFrom((Class<?>) actualTypeArgument)) {
                                // 若是DO派生类
                                Class subActualType = (Class) actualTypeArgument;
                                // 找到DO的外键
                                Field[] subActualTypeDeclaredFields = subActualType.getDeclaredFields();
                                for (Field subField : subActualTypeDeclaredFields) {
                                    if (subField.getAnnotation(ForeignKey.class) != null) {
                                        IMapper mapper = MapperManager.map.get(subActualType);
                                        if (mapper == null) {
                                            throw new RuntimeException("请确认 " + subActualType.toGenericString() + " 所对应Mapper存在");
                                        }
                                        mapper.delete(new QueryWrapper<Object>().eq(StrUtil.toUnderlineCase(subField.getName()), id));
                                    }
                                }
                            }
 
                        }
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            }
        }
        return effect;
    }
 
 
    /**
     * 从查询上下文中获取条件,并且分页查询
     *
     * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
     */
    default <E extends IPage<T>> E selectPage(E page) {
        return this.selectPage(page, (Wrapper<T>) QueryContext.get());
    }
 
    /**
     * 从查询上下文中获取条件
     */
    default List<T> selectList() {
        return this.selectList(QueryContext.get());
    }
 
    /**
     * 分页查取dto
     *
     * @param page
     * @param queryWrapper
     * @param clazz
     * @param <DTO>
     * @return
     */
    default <DTO> Page<DTO> selectPageDto(Page<T> page, Wrapper<T> queryWrapper, Class<DTO> clazz) {
        Page<T> domainPage = this.selectPage(page, queryWrapper);
        Class<T> entityClass = this.getEntityClass();
        if (entityClass != null) {
            String[] fieldListInList = this.getFieldListInList(clazz);
            Map<String, Map<Object, List>> paramsMap = new HashMap<>();
            if (fieldListInList.length > 0) {
                Method getId = cn.hutool.core.util.ReflectUtil.getMethod(entityClass, "getId");
                Set<Object> ids = domainPage.getRecords().stream().map(item -> {
                    try {
                        Object id = getId.invoke(item);
                        return id;
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                        throw new RuntimeException("该对象未设置主键");
                    }
                }).collect(Collectors.toSet());
                if (!CollectionUtils.isEmpty(ids)) {
                    // 若不为空
                    try {
                        for (String fieldStr : fieldListInList) {
                            Field field = clazz.getDeclaredField(fieldStr);
                            if (Collection.class.isAssignableFrom(field.getType())) {
                                ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
                                // 列表内元素类
                                Class itemClazz = (Class) parameterizedType.getActualTypeArguments()[0];
                                // 获取外键
                                Field[] itemFields = itemClazz.getDeclaredFields();
                                inner:
                                for (Field itemField : itemFields) {
                                    ForeignKey foreignKey = itemField.getAnnotation(ForeignKey.class);
                                    if (foreignKey != null) {
                                        String name = StrUtil.toUnderlineCase(itemField.getName());
                                        IMapper itemMapper = MapperManager.map.get(itemClazz);
                                        List childrenList = itemMapper.selectList((Wrapper) new QueryWrapper().in(name, ids));
                                        // 以外键属性分组
                                        Map<Object, List> map = (Map<Object, List>) childrenList.stream().collect(Collectors.groupingBy(item -> {
                                            Object foreignKeyId = null;
                                            try {
                                                Method getForeignKeyMethod = itemClazz.getMethod(ReflectUtil.getMethodName(itemField.getName(), "get"));
                                                foreignKeyId = getForeignKeyMethod.invoke(item);
                                            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                                                e.printStackTrace();
                                            }
                                            return foreignKeyId;
                                        }));
                                        paramsMap.put(fieldStr, map);
                                        break inner;
                                    }
                                }
                            }
                        }
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }
                    // 循环将这个trans进去
 
                }
            }
            return domainPage.trans(item -> {
                DTO dto = null;
                try {
                    dto = clazz.newInstance();
                    BeanUtils.copyProperties(item, dto);
                    Set<String> keys = paramsMap.keySet();
                    // key dto 中 Collection属性中的 field 字符串名字
                    for (String key : keys) {
                        Map<Object, List> paramMap = paramsMap.get(key);
                        if (!CollectionUtils.isEmpty(paramMap)) {
                            // 取出Item的主键
                            Method getIdMethod = item.getClass().getMethod("getId");
                            Object mainPk = getIdMethod.invoke(item);
                            List list = paramMap.get(mainPk);
                            Method setListMethod = clazz.getMethod(ReflectUtil.getMethodName(key, "set"), List.class);
                            setListMethod.invoke(dto, list);
                        }
                    }
                } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                    e.printStackTrace();
                }
                return dto;
            });
        }
        return null;
    }
 
 
    /********************** 以下可以理解为私有方法 *********************/
 
    /**
     * 通过注解获取属性,抽取方法
     *
     * @param clazz
     * @param <DTO>
     * @return
     */
    default <DTO> String[] getFieldList(Class<DTO> clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();
        List<String> list = new LinkedList<>();
        for (Field field : declaredFields) {
            if (field.getAnnotation(LeafTable.class) != null) {
                list.add(field.getName());
            }
        }
        return list.toArray(new String[0]);
    }
 
    /**
     * 通过注解获取,需要被 selectPageDto 和 selectListDto 查询到的叶子子表
     *
     * @param clazz
     * @param <DTO>
     * @return
     */
    default <DTO> String[] getFieldListInList(Class<DTO> clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();
        List<String> list = new LinkedList<>();
        for (Field field : declaredFields) {
            LeafTable leafTable = field.getAnnotation(LeafTable.class);
            if (leafTable != null) {
                if (leafTable.containsList() == ListLeafType.WITH) {
                    list.add(field.getName());
                }
            }
        }
        return list.toArray(new String[0]);
    }
 
 
    default Class<T> getEntityClass() {
        Class<? extends IMapper> clazz = this.getClass();
        Type[] genericInterfaces = clazz.getGenericInterfaces();
        for (Type type : genericInterfaces) {
            if (type instanceof Class) {
                Class mapperClazz = (Class) type;
                Type[] iMapperTypes = mapperClazz.getGenericInterfaces();
                if (iMapperTypes.length > 0) {
                    for (Type t : iMapperTypes) {
                        if (t instanceof ParameterizedType) {
                            ParameterizedType pt = (ParameterizedType) t;
                            if (IMapper.class.isAssignableFrom((Class) pt.getRawType())) {
                                // 若是IMapper的子类
                                Class entityClass = (Class) pt.getActualTypeArguments()[0];
                                return entityClass;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
 
}