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

com.github.yulichang.wrapper.interfaces.QueryLabel Maven / Gradle / Ivy

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

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.support.FieldCache;
import com.github.yulichang.wrapper.resultmap.Label;
import com.github.yulichang.wrapper.resultmap.MFunc;
import com.github.yulichang.wrapper.resultmap.MybatisLabel;
import com.github.yulichang.wrapper.resultmap.MybatisLabelFree;

import java.util.Collection;
import java.util.Map;

/**
 * 对一或对多查询
 *
 * @author yulichang
 * @since 1.3.0
 */
@SuppressWarnings({"unchecked", "unused", "DuplicatedCode"})
public interface QueryLabel {

    void addLabel(Label label);

    Children getChildren();

    /**
     * 一对多查询 调用此方法发必需要调用对应的 left join / right join ... 连表方法,否则会报错
     * 

* 举例 UserDO AddressDO 为一对多关系 UserDTO 为结果类 *

     *     MPJLambdaQueryWrapper wrapper = new MPJLambdaQueryWrapper();
     *     wrapper.selectAll(UserDO.class)
     *            .selectCollection(AddressDO.class, UserDTO::getAddressListDTO)
     *            .leftJoin(AddressDO.class, ...... )
     *            .eq(...)
     *            ...
     * 
     * 会自动将 AddressDO类中相同属性的字段 以mybatis的方式映射到UserDTO.addressListDTO属性中
     *
     * @since 1.3.0
     *
     * @param child    连表数据库实体类
     * @param dtoField 包装类对应的属性
     * @param       包装类
     * @param       对多数据库实体类
     * @param       包装类集合泛型
     * @param       包装类集合字段泛型
     */
    default > Children selectCollection(Class child, SFunction dtoField) {
        return selectCollection(null, child, dtoField);
    }

    default > Children selectCollection(String prefix, Class child, SFunction dtoField) {
        String dtoFieldName = LambdaUtils.getName(dtoField);
        Class dtoClass = LambdaUtils.getEntityClass(dtoField);
        Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
        FieldCache field = fieldMap.get(dtoFieldName);
        Class genericType = MPJReflectionKit.getGenericType(field.getField());
        MybatisLabel.Builder builder;
        if (genericType == null || genericType.isAssignableFrom(child)) {
            //找不到集合泛型 List List List , 直接查询数据库实体
            builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType());
        } else {
            Class ofType = (Class) genericType;
            builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType(), ofType, true);
        }
        addLabel(builder.build());
        return getChildren();
    }

    /**
     * 一对多查询 调用此方法发必需要调用对应的 left join / right join ... 连表方法,否则会报错
     * 

* 举例 UserDO AddressDO 为一对多关系 UserDTO 为结果类 *

     *   MPJLambdaQueryWrapper wrapper = new MPJLambdaQueryWrapper();
     *   wrapper.selectAll(UserDO.class)
     *      .selectCollection(AddressDO.class, UserDTO::getAddressListDTO, map -> map
     *           .id(AddressDO::getId, AddressDTO::getId)                 //如果属性名一致 可以传一个
     *           .result(AddressDO::getUserId)                            //如果属性名一致 可以传一个
     *           .result(AddressDO::getAddress, AddressDTO::getAddress))) //如果属性名一致 可以传一个
     *      .leftJoin(AddressDO.class, ...... )
     *      .eq(...)
     *      ...
     * 
     *
     * 会自动将 AddressDO类中指定的字段 以mybatis的方式映射到UserDTO.addressListDTO属性中
     *
     * @since 1.3.0
     *
     * @param child      连表数据库实体类
     * @param dtoField   包装类对应的属性
     * @param collection collection标签内容
     * @param         包装类
     * @param         对多数据库实体类
     * @param         包装类集合泛型
     * @param         包装类集合字段泛型
     */
    default > Children selectCollection(Class child,
                                                                                   SFunction dtoField,
                                                                                   MFunc> collection) {
        return selectCollection(null, child, dtoField, collection);
    }

    default > Children selectCollection(SFunction dtoField,
                                                                                MFunc> collection) {
        //自由映射必须存在泛型Z
        String dtoFieldName = LambdaUtils.getName(dtoField);
        Class dtoClass = LambdaUtils.getEntityClass(dtoField);
        FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
        //获取集合泛型
        Class genericType = MPJReflectionKit.getGenericType(field.getField());
        Class ofType = (Class) genericType;
        MybatisLabelFree.Builder builder = new MybatisLabelFree.Builder<>(dtoFieldName, field.getType(), ofType);
        MybatisLabelFree.Builder czBuilder = collection.apply(builder);
        addLabel(czBuilder.build());
        return getChildren();
    }

    default > Children selectCollection(String prefix,
                                                                                   Class child,
                                                                                   SFunction dtoField,
                                                                                   MFunc> collection) {
        String dtoFieldName = LambdaUtils.getName(dtoField);
        Class dtoClass = LambdaUtils.getEntityClass(dtoField);
        FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
        //获取集合泛型
        Class genericType = MPJReflectionKit.getGenericType(field.getField());
        Class ofType = (Class) genericType;
        MybatisLabel.Builder builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType(), ofType, false);
        MybatisLabel.Builder czBuilder = collection.apply(builder);
        addLabel(czBuilder.build());
        return getChildren();
    }

    /**
     * 对一查询 用法参考 selectCollection
     *
     * @since 1.3.0
     */
    default  Children selectAssociation(Class child, SFunction dtoField) {
        return selectAssociation(null, child, dtoField);
    }

    default  Children selectAssociation(String prefix, Class child, SFunction dtoField) {
        String dtoFieldName = LambdaUtils.getName(dtoField);
        Class dtoClass = LambdaUtils.getEntityClass(dtoField);
        Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
        FieldCache field = fieldMap.get(dtoFieldName);
        Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
        MybatisLabel.Builder builder;
        builder = new MybatisLabel.Builder<>(StringUtils.isBlank(prefix) ? null : prefix,
                dtoFieldName, child, field.getType(), (Class) field.getType(), true);
        addLabel(builder.build());
        return getChildren();
    }

    /**
     * 对一查询 用法参考 selectCollection
     *
     * @since 1.3.0
     */
    default  Children selectAssociation(Class child, SFunction dtoField,
                                                 MFunc> collection) {
        return selectAssociation(null, child, dtoField, collection);
    }

    default  Children selectAssociation(SFunction dtoField,
                                                 MFunc> collection) {
        String dtoFieldName = LambdaUtils.getName(dtoField);
        Class dtoClass = LambdaUtils.getEntityClass(dtoField);
        FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
        Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
        MybatisLabelFree.Builder builder = new MybatisLabelFree.Builder<>(dtoFieldName, field.getType(), (Class) field.getType());
        MybatisLabelFree.Builder cfBuilder = collection.apply(builder);
        addLabel(cfBuilder.build());
        return getChildren();
    }

    default  Children selectAssociation(String prefix, Class child, SFunction dtoField,
                                                 MFunc> collection) {
        String dtoFieldName = LambdaUtils.getName(dtoField);
        Class dtoClass = LambdaUtils.getEntityClass(dtoField);
        FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
        Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
        MybatisLabel.Builder builder = new MybatisLabel.Builder<>(StringUtils.isBlank(prefix) ? null : prefix,
                dtoFieldName, child, field.getType(), (Class) field.getType(), false);
        MybatisLabel.Builder cfBuilder = collection.apply(builder);
        addLabel(cfBuilder.build());
        return getChildren();
    }
}