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

com.ly.mybatis.mapperservice.util.MPSUtil Maven / Gradle / Ivy

The newest version!
package com.ly.mybatis.mapperservice.util;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.mybatis.mapperservice.model.*;
import com.ly.mybatis.mapperservice.service.builder.RepositoryTemplate;
import com.ly.mybatis.mapperservice.service.builder.RepositoryTemplateBuilder;
import com.ly.mybatis.mapperservice.service.core.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.*;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.Environment;
import org.springframework.lang.Nullable;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.*;

@SuppressWarnings({"all"})
public class MPSUtil {
    private static final Set> entityClasses = new HashSet<>();
    private static final Map, Class> boMap = new HashMap<>();
    private static final Map, Class> formMap = new HashMap<>();
    private static final Map, Class> conditionFormMap = new HashMap<>();
    private static final Map, Batch, ?, ?, ?, ?>> batchMap = new HashMap<>();
    private static final Map, Delete, ?, ?, ?, ?>> deleteMap = new HashMap<>();
    private static final Map, Select, ?, ?, ?, ?>> selectMap = new HashMap<>();

    @Resource
    private ApplicationContext applicationContext;
    private static ApplicationContext applicationContextInstance;
    @EventListener(ContextRefreshedEvent.class)
    public void init() {
        applicationContextInstance = applicationContext;
        inject(batchMap, getBeansOfType(Batch.class));
        inject(deleteMap, getBeansOfType(Delete.class));
        inject(selectMap, getBeansOfType(Select.class));
        Map mapperMap = applicationContext.getBeansOfType(BaseMapper.class);
        for (Map.Entry entry : mapperMap.entrySet()) {
            String s = entry.getKey();
            BaseMapper mapper = entry.getValue();
            try {
                RepositoryTemplateBuilder builder = new RepositoryTemplateBuilder(applicationContext, mapper);
                if (builder.parse()) {
                    RepositoryTemplate o = builder.getTemplate();
                    batchMap.putIfAbsent(builder.getEntityClass(), o);
                    deleteMap.putIfAbsent(builder.getEntityClass(), o);
                    selectMap.putIfAbsent(builder.getEntityClass(), o);
                    entityClasses.add(builder.getEntityClass());
                    boMap.putIfAbsent(builder.getBoClass(), builder.getEntityClass());
                    formMap.putIfAbsent(builder.getFormClass(), builder.getEntityClass());
                    conditionFormMap.putIfAbsent(builder.getConditionFormClass(), builder.getEntityClass());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static boolean isEntity(Object obj) {
        return entityClasses.contains(getObjectClass(obj));
    }

    public static boolean isForm(Object obj) {
        return formMap.containsKey(getObjectClass(obj));
    }

    public static boolean isBo(Object obj) {
        return boMap.containsKey(getObjectClass(obj));
    }

    public static boolean isConditionForm(Object obj) {
        return conditionFormMap.containsKey(getObjectClass(obj));
    }

    public static Class getObjectClass(Object obj) {
        if (obj == null) {
            throw new RuntimeException("null值对象,无法解析所属类");
        }
        if (obj instanceof Class) {
            return (Class) obj;
        } else if (obj instanceof Collection) {
            Collection collection = (Collection) obj;
            return collection.stream()
                             .findFirst()
                             .map(Object::getClass)
                             .orElseThrow(RuntimeException::new);
        } else if (obj instanceof Map) {
            Map map = (Map) obj;
            return map.values().stream()
                      .findFirst()
                      .map(Object::getClass)
                      .orElseThrow(RuntimeException::new);
        } else {
            return obj.getClass();
        }
    }

    public static  Class getEntityClass(Object obj) {
        Class clazz = getObjectClass(obj);
        if (entityClasses.contains(clazz)) {
            return (Class) clazz;
        }
        if (formMap.containsKey(clazz)) {
            return (Class) formMap.get(clazz);
        }
        if (boMap.containsKey(clazz)) {
            return (Class) boMap.get(clazz);
        }
        if (conditionFormMap.containsKey(clazz)) {
            return (Class) conditionFormMap.get(clazz);
        }
        throw new RuntimeException("没有支持的类:" + clazz.getName());
    }

    // save or update
    public static  Result insert(Object object) {
        return MPSUtil.batchOptional(object)
                      .map(insert -> {
                          if (isForm(object)) {
                              return insert.insertForm((Form) object);
                          } else {
                              return insert.insert((Entity) object);
                          }
                      })
                      .orElseGet(() -> {
                          Result result = new Result<>();
                          result.setSuccess(false)
                                .setMessage("unknown error");
                          return result;
                      });
    }

    public static  BatchResult insertBatch(
            Collection objects,
            int batchSize
                                                                                   ) {
        if (objects.isEmpty()) {
            return new BatchResult<>();
        }
        return MPSUtil.batchOptional(objects)
                      .map(batch -> {
                          if (isForm(objects)) {
                              return batch.insertFormBatch((Collection
) objects, batchSize); } else { return batch.insertBatch((Collection) objects, batchSize); } }) .orElseGet(BatchResult::new); } public static BatchResult insertBatch(Collection objects) { return insertBatch(objects, 1000); } public static Result update(Object object, Wrapper wrapper) { return MPSUtil.batchOptional(object) .map(update -> { if (isForm(object)) { return update.updateForm((Form) object, wrapper); } else { return update.update((Entity) object, wrapper); } }) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result update(Object object, ConditionForm form) { return MPSUtil.batchOptional(object) .map(update -> { if (isForm(object)) { return update.updateForm((Form) object, form); } else { return update.update((Entity) object, form); } }) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result update(Serializable id, Object object) { return MPSUtil.batchOptional(object) .map(update -> { if (isForm(object)) { return update.updateForm(id, (Form) object); } else { return update.update(id, (Entity) object); } }) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result update(Object object) { return MPSUtil.batchOptional(object) .map(update -> { if (isForm(object)) { return update.updateForm((Form) object); } else { return update.update((Entity) object); } }) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static BatchResult updateBatch( Collection objects, int batchSize ) { if (objects.isEmpty()) { return new BatchResult<>(); } return MPSUtil.batchOptional(objects) .map(batch -> { if (isForm(objects)) { return batch.updateFormBatch((Collection) objects, batchSize); } else { return batch.updateBatch((Collection) objects, batchSize); } }) .orElseGet(BatchResult::new); } public static BatchResult updateBatch(Collection objects) { return updateBatch(objects, 1000); } public static Result insertOrUpdate(Object object) { return MPSUtil.batchOptional(object) .map(insert -> { if (isForm(object)) { return insert.insertOrUpdateForm((Form) object); } else { return insert.insertOrUpdate((Entity) object); } }) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static ImportBatchResult insertOrUpdateBatch( Collection objects, boolean insert, boolean update, int batchSize ) { if (objects.isEmpty()) { return new ImportBatchResult<>(); } return MPSUtil.batchOptional(objects) .map(batch -> { if (isForm(objects)) { return batch.insertOrUpdateFormBatch( (Collection) objects, insert, update, batchSize ); } else { return batch.insertOrUpdateBatch((Collection) objects, insert, update, batchSize); } }) .orElseGet(ImportBatchResult::new); } public static ImportBatchResult insertOrUpdateBatch( Collection objects, int batchSize ) { return insertOrUpdateBatch(objects, true, true, batchSize); } public static ImportBatchResult insertOrUpdateBatch( Collection objects ) { return insertOrUpdateBatch(objects, 1000); } // delete public static boolean delete( Class clazz, Collection ids ) { if (ids.isEmpty()) { return true; } return MPSUtil.deleteOptional(clazz) .map(delete -> delete.delete(ids)) .orElse(false); } public static boolean delete(ConditionForm form) { return MPSUtil.deleteOptional(form) .map(delete -> delete.delete(form)) .orElse(false); } public static boolean delete(Class clazz, Wrapper wrapper) { return MPSUtil.deleteOptional(clazz) .map(delete -> delete.delete(wrapper)) .orElse(false); } public static boolean delete(ResolveHolder> holder) { return delete(holder.getResolveType().resolveGeneric(0, 0), holder.getHolder()); } public static BatchResult deleteBatch( Collection entities, int batchSize ) { if (entities.isEmpty()) { return new BatchResult<>(); } return MPSUtil.batchOptional(entities) .map(batch -> batch.deleteBatch( entities, false, batchSize )) .orElseGet(BatchResult::new); } public static BatchResult deleteBatch(Collection entities) { return deleteBatch(entities, 1000); } // select entity public static Result detailEntity(Class clazz, Serializable id) { return MPSUtil.selectOptional(clazz) .map(select -> select.detailEntity(id)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detailEntity(ConditionForm form) { return MPSUtil.selectOptional(form) .map(select -> select.detailEntity(form)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detailEntity( Class clazz, Wrapper wrapper ) { return MPSUtil.selectOptional(clazz) .map(select -> select.detailEntity(wrapper, null)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detailEntity( ResolveHolder> holder ) { return detailEntity(holder.getResolveType().resolveGeneric(0, 0), holder.getHolder()); } public static Result> listEntity( Class clazz, Collection ids ) { return MPSUtil.selectOptional(clazz) .map(select -> select.listEntity(ids, null)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> listEntity(ConditionForm form) { return MPSUtil.selectOptional(form) .map(select -> select.listEntity(form)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> listEntity( Class clazz, Wrapper wrapper ) { return MPSUtil.selectOptional(clazz) .map(select -> select.listEntity(wrapper, null)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> listEntity( ResolveHolder> holder ) { return listEntity(holder.getResolveType().resolveGeneric(0, 0), holder.getHolder()); } public static Result> pageEntity( IPage page, ConditionForm form ) { return MPSUtil.selectOptional(form) .map(select -> select.pageEntity(page, form)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false).setMessage("unknown error"); return result; }); } public static Result> pageEntity( Class clazz, IPage page, Wrapper wrapper ) { return MPSUtil.selectOptional(clazz) .map(select -> select.pageEntity(page, wrapper, null)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false).setMessage("unknown error"); return result; }); } public static Result> pageEntity( IPage page, ResolveHolder> holder ) { return pageEntity(holder.getResolveType().resolveGeneric(0, 0), page, holder.getHolder()); } // select bo public static Result detail( Class clazz, Serializable id, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.detail(id, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detail( Class clazz, Serializable id, boolean inject, List includes, List excludes ) { return detail(clazz, id, true, inject, includes, excludes); } public static Result detail( Class clazz, Serializable id, boolean inject ) { return detail(clazz, id, inject, null, null); } public static Result detail( Class clazz, Serializable id ) { return detail(clazz, id, true); } public static Result detail( ConditionForm form, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(form) .map(select -> select.detail(form, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detail( ConditionForm form, boolean inject, List includes, List excludes ) { return detail(form, true, inject, includes, excludes); } public static Result detail(ConditionForm form, boolean inject) { return detail(form, inject, null, null); } public static Result detail(ConditionForm form) { return detail(form, true); } public static Result detail( Class clazz, Wrapper wrapper, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.detail(wrapper, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detail( Class clazz, Wrapper wrapper, boolean inject, List includes, List excludes ) { return detail(clazz, wrapper, true, inject, includes, excludes); } public static Result detail(Class clazz, Wrapper wrapper, boolean inject) { return detail(clazz, wrapper, inject, null, null); } public static Result detail(Class clazz, Wrapper wrapper) { return detail(clazz, wrapper, true); } public static Result detail( ResolveHolder> holder, boolean inject, List includes, List excludes ) { return detail(holder.getResolveType().resolveGeneric(0, 0), holder.getHolder(), inject, includes, excludes); } public static Result detail(ResolveHolder> holder, boolean inject) { return detail(holder, true, null, null); } public static Result detail(ResolveHolder> holder) { return detail(holder, true); } public static Result> list( Class clazz, Collection ids, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.list(ids, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> list( Class clazz, Collection ids, boolean inject, List includes, List excludes ) { return list(clazz, ids, true, inject, includes, excludes); } public static Result> list( Class clazz, Collection ids, boolean inject ) { return list(clazz, ids, inject, null, null); } public static Result> list( Class clazz, Collection ids ) { return list(clazz, ids, true); } public static Result> list( ConditionForm form, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(form) .map(select -> select.list(form, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> list( ConditionForm form, boolean inject, List includes, List excludes ) { return list(form, true, inject, includes, excludes); } public static Result> list( ConditionForm form, boolean inject ) { return list(form, inject, null, null); } public static Result> list( ConditionForm form ) { return list(form, true); } public static Result> list( Class clazz, Wrapper wrapper, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.list(wrapper, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> list( Class clazz, Wrapper wrapper, boolean inject, List includes, List excludes ) { return list(clazz, wrapper, true, inject, includes, excludes); } public static Result> list( Class clazz, Wrapper wrapper, boolean inject ) { return list(clazz, wrapper, inject, null, null); } public static Result> list(Class clazz, Wrapper wrapper) { return list(clazz, wrapper, true); } public static Result> list( ResolveHolder> holder, boolean autoInject, boolean inject, List includes, List excludes ) { return list( holder.getResolveType().resolveGeneric(0, 0), holder.getHolder(), autoInject, inject, includes, excludes ); } public static Result> list( ResolveHolder> holder, boolean inject, List includes, List excludes ) { return list(holder, true, inject, includes, excludes); } public static Result> list(ResolveHolder> holder, boolean inject) { return list(holder, inject, null, null); } public static Result> list(ResolveHolder> holder) { return list(holder, true); } public static Result> page( IPage page, ConditionForm form, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(form) .map(select -> select.page(page, form, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false).setMessage("unknown error"); return result; }); } public static Result> page( IPage page, ConditionForm form, boolean inject, List includes, List excludes ) { return page(page, form, true, inject, includes, excludes); } public static Result> page(IPage page, ConditionForm form, boolean inject) { return page(page, form, inject, null, null); } public static Result> page(IPage page, ConditionForm form) { return page(page, form, true); } public static Result> page( Class clazz, IPage page, Wrapper wrapper, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.page(page, wrapper, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false).setMessage("unknown error"); return result; }); } public static Result> page( Class clazz, IPage page, Wrapper wrapper, boolean inject, List includes, List excludes ) { return page(clazz, page, wrapper, true, inject, includes, excludes); } public static Result> page( Class clazz, IPage page, Wrapper wrapper, boolean inject ) { return page(clazz, page, wrapper, inject, null, null); } public static Result> page( Class clazz, IPage page, Wrapper wrapper ) { return page(clazz, page, wrapper, true); } public static Result> page( IPage page, ResolveHolder> holder, boolean inject, List includes, List excludes ) { return page( holder.getResolveType().resolveGeneric(0, 0), page, holder.getHolder(), true, inject, includes, excludes ); } public static Result> page( IPage page, ResolveHolder> holder, boolean inject ) { return page(page, holder, inject, null, null); } public static Result> page( IPage page, ResolveHolder> holder ) { return page(page, holder, true); } // select vo public static Result detailVo( Class clazz, Class voClass, Serializable id, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.detailVo(voClass, id, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detailVo( Class clazz, Class voClass, Serializable id, boolean inject, List includes, List excludes ) { return detailVo(clazz, voClass, id, true, inject, includes, excludes); } public static Result detailVo( Class clazz, Class voClass, Serializable id, boolean inject ) { return detailVo(clazz, voClass, id, inject, null, null); } public static Result detailVo( Class clazz, Class voClass, Serializable id ) { return detailVo(clazz, voClass, id, true); } public static Result detailVo( Class voClass, ConditionForm form, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(form) .map(select -> select.detailVo(voClass, form, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detailVo( Class voClass, ConditionForm form, boolean inject, List includes, List excludes ) { return detailVo(voClass, form, true, inject, includes, excludes); } public static Result detailVo( Class voClass, ConditionForm form, boolean inject ) { return detailVo(voClass, form, inject, null, null); } public static Result detailVo( Class voClass, ConditionForm form ) { return detailVo(voClass, form, true); } public static Result detailVo( Class clazz, Class voClass, Wrapper wrapper, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.detailVo(voClass, wrapper, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result detailVo( Class clazz, Class voClass, Wrapper wrapper, boolean inject, List includes, List excludes ) { return detailVo(clazz, voClass, wrapper, true, inject, includes, excludes); } public static Result detailVo( Class clazz, Class voClass, Wrapper wrapper, boolean inject ) { return detailVo(clazz, voClass, wrapper, inject, null, null); } public static Result detailVo( Class clazz, Class voClass, Wrapper wrapper ) { return detailVo(clazz, voClass, wrapper, true); } public static Result> listVo( Class clazz, Class voClass, Collection ids, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.listVo(voClass, ids, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> listVo( Class clazz, Class voClass, Collection ids, boolean inject, List includes, List excludes ) { return listVo(clazz, voClass, ids, true, inject, includes, excludes); } public static Result> listVo( Class clazz, Class voClass, Collection ids, boolean inject ) { return listVo(clazz, voClass, ids, inject, null, null); } public static Result> listVo( Class clazz, Class voClass, Collection ids ) { return listVo(clazz, voClass, ids, true); } public static Result> listVo( Class voClass, ConditionForm form, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(form) .map(select -> select.listVo(voClass, form, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> listVo( Class voClass, ConditionForm form, boolean inject, List includes, List excludes ) { return listVo(voClass, form, true, inject, includes, excludes); } public static Result> listVo( Class voClass, ConditionForm form, boolean inject ) { return listVo(voClass, form, inject, null, null); } public static Result> listVo( Class voClass, ConditionForm form ) { return listVo(voClass, form, true); } public static Result> listVo( Class clazz, Class voClass, Wrapper wrapper, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.listVo(voClass, wrapper, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false) .setMessage("unknown error"); return result; }); } public static Result> listVo( Class clazz, Class voClass, Wrapper wrapper, boolean inject, List includes, List excludes ) { return listVo(clazz, voClass, wrapper, true, inject, includes, excludes); } public static Result> listVo( Class clazz, Class voClass, Wrapper wrapper, boolean inject ) { return listVo(clazz, voClass, wrapper, inject, null, null); } public static Result> listVo( Class clazz, Class voClass, Wrapper wrapper ) { return listVo(clazz, voClass, wrapper, true); } public static Result> pageVo( Class voClass, IPage page, ConditionForm form, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(form) .map(select -> select.pageVo(voClass, page, form, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false).setMessage("unknown error"); return result; }); } public static Result> pageVo( Class voClass, IPage page, ConditionForm form, boolean inject, List includes, List excludes ) { return pageVo(voClass, page, form, true, inject, includes, excludes); } public static Result> pageVo( Class voClass, IPage page, ConditionForm form, boolean inject ) { return pageVo(voClass, page, form, inject, null, null); } public static Result> pageVo( Class voClass, IPage page, ConditionForm form ) { return pageVo(voClass, page, form, true); } public static Result> pageVo( Class clazz, Class voClass, IPage page, Wrapper wrapper, boolean autoInject, boolean inject, List includes, List excludes ) { return MPSUtil.selectOptional(clazz) .map(select -> select.pageVo(voClass, page, wrapper, autoInject, inject, includes, excludes)) .orElseGet(() -> { Result> result = new Result<>(); result.setSuccess(false).setMessage("unknown error"); return result; }); } public static Result> pageVo( Class clazz, Class voClass, IPage page, Wrapper wrapper, boolean inject, List includes, List excludes ) { return pageVo(clazz, voClass, page, wrapper, true, inject, includes, excludes); } public static Result> pageVo( Class clazz, Class voClass, IPage page, Wrapper wrapper, boolean inject ) { return pageVo(clazz, voClass, page, wrapper, inject, null, null); } public static Result> pageVo( Class clazz, Class voClass, IPage page, Wrapper wrapper ) { return pageVo(clazz, voClass, page, wrapper, true); } //count public long count(ConditionForm form) { return MPSUtil.selectOptional(form) .map(select -> select.count(form)) .orElse(0L); } public static long count(Class clazz, Wrapper wrapper) { return MPSUtil.selectOptional(clazz) .map(select -> select.count(wrapper)) .orElse(0L); } public static long count(ResolveHolder> holder) { return count(holder.getResolveType().resolveGeneric(0, 0), holder.getHolder()); } //application context @Nullable public static String getId() { return applicationContextInstance.getId(); } public static String getApplicationName() { return applicationContextInstance.getApplicationName(); } public static String getDisplayName() { return applicationContextInstance.getDisplayName(); } public static long getStartupDate() { return applicationContextInstance.getStartupDate(); } @Nullable public static ApplicationContext getParent() { return applicationContextInstance.getParent(); } public static AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException { return applicationContextInstance.getAutowireCapableBeanFactory(); } public static Environment getEnvironment() { return applicationContextInstance.getEnvironment(); } public static boolean containsBeanDefinition(String beanName) { return applicationContextInstance.containsBeanDefinition(beanName); } public static int getBeanDefinitionCount() { return applicationContextInstance.getBeanDefinitionCount(); } public static String[] getBeanDefinitionNames() { return applicationContextInstance.getBeanDefinitionNames(); } public static ObjectProvider getBeanProvider(Class requiredType, boolean allowEagerInit) { return applicationContextInstance.getBeanProvider(requiredType, allowEagerInit); } public static ObjectProvider getBeanProvider(ResolvableType requiredType, boolean allowEagerInit) { return applicationContextInstance.getBeanProvider(requiredType, allowEagerInit); } public static String[] getBeanNamesForType(ResolvableType type) { return applicationContextInstance.getBeanNamesForType(type); } public static String[] getBeanNamesForType( ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit ) { return applicationContextInstance.getBeanNamesForType(type, includeNonSingletons, allowEagerInit); } public static String[] getBeanNamesForType(Class type) { return applicationContextInstance.getBeanNamesForType(type); } public static String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) { return applicationContextInstance.getBeanNamesForType(type, includeNonSingletons, allowEagerInit); } public static Map getBeansOfType(Class type) throws BeansException { return applicationContextInstance.getBeansOfType(type); } public static Map getBeansOfType(Class type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException { return applicationContextInstance.getBeansOfType(type, includeNonSingletons, allowEagerInit); } public static String[] getBeanNamesForAnnotation(Class annotationType) { return applicationContextInstance.getBeanNamesForAnnotation(annotationType); } public static Map getBeansWithAnnotation(Class annotationType) throws BeansException { return applicationContextInstance.getBeansWithAnnotation(annotationType); } @Nullable public static A findAnnotationOnBean(String beanName, Class annotationType) throws NoSuchBeanDefinitionException { return applicationContextInstance.findAnnotationOnBean(beanName, annotationType); } @Nullable public static A findAnnotationOnBean( String beanName, Class annotationType, boolean allowFactoryBeanInit ) throws NoSuchBeanDefinitionException { return applicationContextInstance.findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit); } public static Object getBean(String name) throws BeansException { return applicationContextInstance.getBean(name); } public static T getBean(String name, Class requiredType) throws BeansException { return applicationContextInstance.getBean(name, requiredType); } public static Object getBean(String name, Object... args) throws BeansException { return applicationContextInstance.getBean(name, args); } public static T getBean(Class requiredType) throws BeansException { return applicationContextInstance.getBean(requiredType); } public static T getBean(Class requiredType, Object... args) throws BeansException { return applicationContextInstance.getBean(requiredType, args); } public static ObjectProvider getBeanProvider(Class requiredType) { return applicationContextInstance.getBeanProvider(requiredType); } public static ObjectProvider getBeanProvider(ResolvableType requiredType) { return applicationContextInstance.getBeanProvider(requiredType); } public static boolean containsBean(String name) { return applicationContextInstance.containsBean(name); } public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContextInstance.isSingleton(name); } public static boolean isPrototype(String name) throws NoSuchBeanDefinitionException { return applicationContextInstance.isPrototype(name); } public static boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException { return applicationContextInstance.isTypeMatch(name, typeToMatch); } public static boolean isTypeMatch(String name, Class typeToMatch) throws NoSuchBeanDefinitionException { return applicationContextInstance.isTypeMatch(name, typeToMatch); } @Nullable public static Class getType(String name) throws NoSuchBeanDefinitionException { return applicationContextInstance.getType(name); } @Nullable public static Class getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException { return applicationContextInstance.getType(name, allowFactoryBeanInit); } public static String[] getAliases(String name) { return applicationContextInstance.getAliases(name); } @Nullable public static BeanFactory getParentBeanFactory() { return applicationContextInstance.getParentBeanFactory(); } public static boolean containsLocalBean(String name) { return applicationContextInstance.containsLocalBean(name); } @Nullable public static String getMessage(String code, Object[] args, String defaultMessage, Locale locale) { return applicationContextInstance.getMessage(code, args, defaultMessage, locale); } public static String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException { return applicationContextInstance.getMessage(code, args, locale); } public static String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { return applicationContextInstance.getMessage(resolvable, locale); } public static void publishEvent(ApplicationEvent event) { applicationContextInstance.publishEvent(event); } public static void publishEvent(Object event) { applicationContextInstance.publishEvent(event); } public static org.springframework.core.io.Resource[] getResources(String locationPattern) throws IOException { return applicationContextInstance.getResources(locationPattern); } public static org.springframework.core.io.Resource getResource(String location) { return applicationContextInstance.getResource(location); } @Nullable public static ClassLoader getClassLoader() { return applicationContextInstance.getClassLoader(); } private static void check(Map, ?> map, Class entityClass) { if (!map.containsKey(entityClass)) { throw new UnsupportedOperationException("不支持entityClass:" + entityClass.getName()); } } private static Optional, Entity, Bo, Form, ConditionForm>> batchOptional( Object obj ) { Class entityClass = getEntityClass(obj); check(batchMap, entityClass); return Optional.of(batchMap.get(entityClass)) .map(batch -> (Batch, Entity, Bo, Form, ConditionForm>) batch); } private static Optional, Entity, Bo, Form, ConditionForm>> deleteOptional( Object obj ) { Class entityClass = getEntityClass(obj); check(deleteMap, entityClass); return Optional.of(deleteMap.get(entityClass)) .map(delete -> (Delete, Entity, Bo, Form, ConditionForm>) delete); } private static Optional, Entity, Bo, Form, ConditionForm>> selectOptional( Object obj ) { Class entityClass = getEntityClass(obj); check(selectMap, entityClass); return Optional.of(selectMap.get(entityClass)) .map(select -> (Select, Entity, Bo, Form, ConditionForm>) select); } private static void inject(Map, V1> map, Map m) { if (m != null) { m.forEach((s, v) -> { if (((Base) v).getEntityClass() != null) { map.putIfAbsent(((Base) v).getEntityClass(), (V1) v); } }); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy