Search in sources :

Example 6 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class BaseController method attachMoreRelatedData.

/**
 * 通用的attachMore获取数据
 *
 * @param attachMore  相应的attachMore
 * @param parentValue 父级值
 * @return labelValue集合
 */
protected List<LabelValue> attachMoreRelatedData(AttachMoreDTO attachMore, String parentValue) {
    String entityClassName = S.capFirst(S.toLowerCaseCamel(attachMore.getTarget()));
    Class<?> entityClass = BindingCacheManager.getEntityClassBySimpleName(entityClassName);
    if (V.isEmpty(entityClass)) {
        throw new BusinessException("attachMore: " + attachMore.getTarget() + " 不存在");
    }
    BaseService<?> baseService = ContextHelper.getBaseServiceByEntity(entityClass);
    if (baseService == null) {
        throw new BusinessException("attachMore: " + attachMore.getTarget() + " 的Service不存在 ");
    }
    PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entityClass);
    Function<String, String> field2column = field -> {
        if (V.notEmpty(field)) {
            String column = propInfoCache.getColumnByField(field);
            if (V.notEmpty(column)) {
                return column;
            } else {
                throw new BusinessException("attachMore: " + attachMore.getTarget() + " 无 `" + field + "` 属性");
            }
        }
        return null;
    };
    String label = field2column.apply(S.defaultIfBlank(attachMore.getLabel(), "label"));
    String value = S.defaultString(field2column.apply(attachMore.getValue()), propInfoCache.getIdColumn());
    String ext = field2column.apply(attachMore.getExt());
    // 构建查询条件
    QueryWrapper<?> queryWrapper = Wrappers.query().select(V.isEmpty(ext) ? new String[] { label, value } : new String[] { label, value, ext }).like(V.notEmpty(attachMore.getKeyword()), label, attachMore.getKeyword());
    // 解析排序
    if (V.notEmpty(attachMore.getOrderBy())) {
        String[] orderByFields = S.split(attachMore.getOrderBy(), ",");
        for (String field : orderByFields) {
            V.securityCheck(field);
            String[] fieldAndOrder = field.split(":");
            String columnName = field2column.apply(fieldAndOrder[0]);
            if (fieldAndOrder.length > 1 && Cons.ORDER_DESC.equalsIgnoreCase(fieldAndOrder[1])) {
                queryWrapper.orderByDesc(columnName);
            } else {
                queryWrapper.orderByAsc(columnName);
            }
        }
    }
    // 父级限制
    String parentColumn = field2column.apply(S.defaultIfBlank(attachMore.getParent(), attachMore.isTree() ? "parentId" : null));
    if (V.notEmpty(parentColumn)) {
        if (V.notEmpty(parentValue)) {
            queryWrapper.eq(parentColumn, parentValue);
        } else {
            queryWrapper.and(e -> e.isNull(parentColumn).or().eq(parentColumn, 0));
        }
    }
    // 构建附加条件
    buildAttachMoreCondition(attachMore, queryWrapper, field2column);
    // 获取数据并做相应填充
    List<LabelValue> labelValueList = baseService.getLabelValueList(queryWrapper);
    if (V.notEmpty(parentColumn) || attachMore.getNext() != null) {
        Boolean leaf = !attachMore.isTree() && attachMore.getNext() == null ? true : null;
        // 第一层tree与最后一层无需返回type
        String type = attachMore.isTree() || Boolean.TRUE.equals(leaf) ? null : attachMore.getTarget();
        labelValueList.forEach(item -> {
            item.setType(type);
            item.setLeaf(leaf);
            // 非异步加载
            if (!attachMore.isLazy()) {
                List<LabelValue> children = attachMoreRelatedData(attachMore, S.valueOf(item.getValue()), type);
                item.setChildren(children.isEmpty() ? null : children);
                item.setDisabled(children.isEmpty() && attachMore.getNext() != null ? true : null);
            }
        });
    }
    return labelValueList;
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) java.util(java.util) Logger(org.slf4j.Logger) ValidList(com.diboot.core.entity.ValidList) DictionaryService(com.diboot.core.service.DictionaryService) Cons(com.diboot.core.config.Cons) LoggerFactory(org.slf4j.LoggerFactory) BaseService(com.diboot.core.service.BaseService) Autowired(org.springframework.beans.factory.annotation.Autowired) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) LabelValue(com.diboot.core.vo.LabelValue) BusinessException(com.diboot.core.exception.BusinessException) Function(java.util.function.Function) Binder(com.diboot.core.binding.Binder) ContextHelper(com.diboot.core.util.ContextHelper) PropInfo(com.diboot.core.binding.parser.PropInfo) BindingCacheManager(com.diboot.core.binding.cache.BindingCacheManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) AttachMoreDTO(com.diboot.core.dto.AttachMoreDTO) S(com.diboot.core.util.S) V(com.diboot.core.util.V) QueryBuilder(com.diboot.core.binding.QueryBuilder) BusinessException(com.diboot.core.exception.BusinessException) LabelValue(com.diboot.core.vo.LabelValue) PropInfo(com.diboot.core.binding.parser.PropInfo)

Example 7 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class ContextHelper method getBaseServiceByEntity.

/**
 * 根据Entity获取对应的BaseService实现
 * @param entity
 * @return
 */
public static BaseService getBaseServiceByEntity(Class entity) {
    EntityInfoCache entityInfoCache = BindingCacheManager.getEntityInfoByClass(entity);
    IService iService = entityInfoCache != null ? entityInfoCache.getService() : null;
    if (iService == null) {
        log.info("未能识别到Entity: " + entity.getName() + " 的Service实现!");
    }
    if (iService instanceof BaseService) {
        return (BaseService) iService;
    }
    return null;
}
Also used : EntityInfoCache(com.diboot.core.binding.parser.EntityInfoCache) IService(com.baomidou.mybatisplus.extension.service.IService) BaseService(com.diboot.core.service.BaseService)

Example 8 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class ServiceAdaptor method queryList.

/**
 * 查询一页的实体列表
 * @param iService
 * @param queryWrapper
 * @param pagination
 * @param <E>
 * @return
 */
public static <E> List<E> queryList(IService iService, QueryWrapper<E> queryWrapper, Pagination pagination, Class entityClass) {
    if (iService instanceof BaseService) {
        BaseService baseService = (BaseService) iService;
        return (List<E>) baseService.getEntityList(queryWrapper, pagination);
    } else {
        if (pagination != null) {
            IPage<E> page = convertToIPage(pagination, entityClass);
            page = iService.page(page, queryWrapper);
            // 如果重新执行了count进行查询,则更新pagination中的总数
            if (page.searchCount()) {
                pagination.setTotalCount(page.getTotal());
            }
            return page.getRecords();
        } else {
            return iService.list(queryWrapper);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) BaseService(com.diboot.core.service.BaseService)

Example 9 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class ExcelBindAnnoHandler method executeBindField.

/**
 * 执行绑定
 * @param bindField
 * @param nameList
 * @return
 */
private static Map<String, List> executeBindField(ExcelBindField bindField, List<String> nameList) {
    if (V.isEmpty(nameList)) {
        return Collections.emptyMap();
    }
    BaseService service = ContextHelper.getBaseServiceByEntity(bindField.entity());
    String nameColumn = S.toSnakeCase(bindField.field());
    String idColumn = ContextHelper.getIdColumnName(bindField.entity());
    QueryWrapper queryWrapper = Wrappers.query().select(nameColumn, idColumn).in(nameColumn, nameList);
    List<LabelValue> list = service.getLabelValueList(queryWrapper);
    return convertLabelValueListToMap(list);
}
Also used : LabelValue(com.diboot.core.vo.LabelValue) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) BaseService(com.diboot.core.service.BaseService)

Aggregations

BaseService (com.diboot.core.service.BaseService)9 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)4 Transactional (org.springframework.transaction.annotation.Transactional)4 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 BusinessException (com.diboot.core.exception.BusinessException)3 LabelValue (com.diboot.core.vo.LabelValue)3 EntityInfoCache (com.diboot.core.binding.parser.EntityInfoCache)2 DynamicJoinQueryWrapper (com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper)2 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)2 BaseMapper (com.baomidou.mybatisplus.core.mapper.BaseMapper)1 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)1 LambdaMeta (com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta)1 IService (com.baomidou.mybatisplus.extension.service.IService)1 Binder (com.diboot.core.binding.Binder)1 QueryBuilder (com.diboot.core.binding.QueryBuilder)1 BindingCacheManager (com.diboot.core.binding.cache.BindingCacheManager)1 PropInfo (com.diboot.core.binding.parser.PropInfo)1 Cons (com.diboot.core.config.Cons)1 AttachMoreDTO (com.diboot.core.dto.AttachMoreDTO)1 ValidList (com.diboot.core.entity.ValidList)1