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

com.ly.mybatis.mapperservice.service.join.inject.Inject Maven / Gradle / Ivy

The newest version!
package com.ly.mybatis.mapperservice.service.join.inject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ly.mybatis.mapperservice.model.Result;
import com.ly.mybatis.mapperservice.util.MPSUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
public class Inject {
    protected Class from;
    protected Class join;
    protected Class searchClass;
    protected MapStream key;
    protected MapStream on;
    protected String column;
    protected Function, ? extends Collection> search;
    protected InjectStream set;
    protected Inject sub;
    protected List> next = new ArrayList<>();

    public Inject from(Class from) {
        this.from = from;
        return this;
    }

    public Inject join(Class join) {
        this.join = join;
        return this;
    }

    public Inject key(Consumer key) {
        if (this.key == null) {
            this.key = new MapStream();
        }
        key.accept(this.key);
        return this;
    }

    public Inject on(Consumer on) {
        if (this.on == null) {
            this.on = new MapStream();
        }
        on.accept(this.on);
        return this;
    }

    public Inject column(Class searchClass, String on) {
        this.searchClass = searchClass;
        column = on;
        return this;
    }

    public , O extends Collection> Inject search(Function search) {
        this.search = search;
        return this;
    }

    public Inject set(Consumer set) {
        if (this.set == null) {
            this.set = new InjectStream();
        }
        set.accept(this.set);
        return this;
    }

    public  Inject sub() {
        Inject sub = new Inject<>();
        sub.from(this.join);
        this.sub = sub;
        return sub;
    }

    public  Inject next() {
        Inject sub = new Inject<>();
        sub.from(this.join);
        this.next.add(sub);
        return sub;
    }

    public List process(List models) {
        List ms = key.construct(models);
        if (ms.isEmpty()) {
            return new ArrayList<>();
        }
        Collection ids = ms.parallelStream()
                                   .map(Model::join)
                                   .filter(Objects::nonNull)
                                   .distinct()
                                   .collect(Collectors.toList());
        List r;
        if (ids.isEmpty()) {
            r = new ArrayList<>();
        } else {
            if (search == null) {
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.in(column, ids);
                log.debug(
                        "搜索数据库, 当前: {}, 连接: {}, 查询: {}, 目标数据库语句: {}",
                        from.getSimpleName(),
                        Optional.ofNullable(searchClass).map(Class::getSimpleName).orElseGet(join::getSimpleName),
                        join.getSimpleName(),
                        wrapper.getTargetSql()
                         );
                Result> result;
                if (searchClass == null) {
                    result = MPSUtil.list(join, wrapper, false, true, null, null);
                } else {
                    result = MPSUtil.listVo(searchClass, join, wrapper, false, true, null, null);
                }
                if (!result.isSuccess()) {
                    log.error(
                            "当前: {}, 连接: {}, 查询: {}, 搜索数据库错误: {}",
                            from.getSimpleName(),
                            Optional.ofNullable(searchClass).map(Class::getSimpleName).orElseGet(join::getSimpleName),
                            join.getSimpleName(),
                            result.getMessage()
                             );
                }
                r = Model.of(result.getOrDefault(ArrayList::new));
            } else {
                log.debug("自定义搜索, 当前: {}, 连接: {}, 查询: {}", from.getSimpleName(), Optional.ofNullable(
                        searchClass).map(Class::getSimpleName).orElseGet(join::getSimpleName), join.getSimpleName());
                try {
                    r = Model.of(((Function, ? extends Collection>) search).apply(ids));
                } catch (Exception e) {
                    e.printStackTrace();
                    r = new ArrayList<>();
                }
                if (r == null) {
                    r = new ArrayList<>();
                }
            }
        }
        log.debug(
                "当前: {}, 连接: {}, 查询: {}, 搜索结果: {} 条",
                from.getSimpleName(),
                Optional.ofNullable(searchClass).map(Class::getSimpleName).orElseGet(join::getSimpleName),
                join.getSimpleName(),
                r.size()
                 );
        r = on.construct(r);
        ms = Model.merge(ms, r);
        log.debug(
                "当前: {}, 连接: {}, 查询: {}, 合并搜索结果: {} 条",
                from.getSimpleName(),
                Optional.ofNullable(searchClass).map(Class::getSimpleName).orElseGet(join::getSimpleName),
                join.getSimpleName(),
                ms.size()
                 );
        if (ms.isEmpty()) {
            return ms;
        }
        if (set != null) {
            set.process(ms);
        }
        return ms;
    }
}