Search in sources :

Example 1 with LambdaMeta

use of com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta in project diboot by dibo-software.

the class BaseServiceImpl method createOrUpdateN2NRelations.

@Override
@Transactional(rollbackFor = Exception.class)
public <R> boolean createOrUpdateN2NRelations(SFunction<R, ?> driverIdGetter, Object driverId, SFunction<R, ?> followerIdGetter, List<? extends Serializable> followerIdList, Consumer<QueryWrapper<R>> queryConsumer, Consumer<R> setConsumer) {
    if (driverId == null) {
        throw new InvalidUsageException("主动ID值不能为空!");
    }
    if (followerIdList == null) {
        log.debug("从动对象ID集合为null,不做关联关系更新处理");
        return false;
    }
    // 从getter中获取class和fieldName
    LambdaMeta lambdaMeta = LambdaUtils.extract(driverIdGetter);
    Class<R> middleTableClass = (Class<R>) lambdaMeta.getInstantiatedClass();
    EntityInfoCache entityInfo = BindingCacheManager.getEntityInfoByClass(middleTableClass);
    if (entityInfo == null) {
        throw new InvalidUsageException("未找到 " + middleTableClass.getName() + " 的 Service 或 Mapper 定义!");
    }
    boolean isExistPk = entityInfo.getIdColumn() != null;
    // 获取主动从动字段名
    String driverFieldName = PropertyNamer.methodToProperty(lambdaMeta.getImplMethodName());
    String followerFieldName = convertGetterToFieldName(followerIdGetter);
    String driverColumnName = entityInfo.getColumnByField(driverFieldName);
    String followerColumnName = entityInfo.getColumnByField(followerFieldName);
    // 查询已有关联
    QueryWrapper<R> selectOld = new QueryWrapper<R>().eq(driverColumnName, driverId);
    if (queryConsumer != null) {
        queryConsumer.accept(selectOld);
    }
    if (isExistPk) {
        selectOld.select(entityInfo.getIdColumn(), followerColumnName);
    } else {
        selectOld.select(followerColumnName);
    }
    List<Map<String, Object>> oldMap;
    IService<R> iService = entityInfo.getService();
    BaseMapper<R> baseMapper = entityInfo.getBaseMapper();
    if (iService != null) {
        oldMap = iService.listMaps(selectOld);
    } else {
        oldMap = baseMapper.selectMaps(selectOld);
    }
    // 删除失效关联
    List<Serializable> delIds = new ArrayList<>();
    for (Map<String, Object> map : oldMap) {
        if (V.notEmpty(followerIdList) && followerIdList.remove((Serializable) map.get(followerColumnName))) {
            continue;
        }
        delIds.add((Serializable) map.get(isExistPk ? entityInfo.getIdColumn() : followerColumnName));
    }
    if (!delIds.isEmpty()) {
        if (isExistPk) {
            if (iService != null) {
                iService.removeByIds(delIds);
            } else {
                baseMapper.deleteBatchIds(delIds);
            }
        } else {
            QueryWrapper<R> delOld = new QueryWrapper<R>().eq(driverColumnName, driverId).in(entityInfo.getColumnByField(followerFieldName), delIds);
            if (queryConsumer != null) {
                queryConsumer.accept(selectOld);
            }
            if (iService != null) {
                iService.remove(delOld);
            } else if (!delIds.isEmpty()) {
                baseMapper.delete(delOld);
            }
        }
    }
    // 新增关联
    if (V.notEmpty(followerIdList)) {
        List<R> n2nRelations = new ArrayList<>(followerIdList.size());
        try {
            for (Serializable followerId : followerIdList) {
                R relation = middleTableClass.newInstance();
                BeanUtils.setProperty(relation, driverFieldName, driverId);
                BeanUtils.setProperty(relation, followerFieldName, followerId);
                if (setConsumer != null) {
                    setConsumer.accept(relation);
                }
                n2nRelations.add(relation);
            }
        } catch (Exception e) {
            throw new BusinessException(Status.FAIL_EXCEPTION, e);
        }
        if (iService != null) {
            if (iService instanceof BaseService) {
                ((BaseService<R>) iService).createEntities(n2nRelations);
            } else {
                iService.saveBatch(n2nRelations);
            }
        } else {
            // 新增关联,无service只能循环插入
            for (R relation : n2nRelations) {
                baseMapper.insert(relation);
            }
        }
    }
    return true;
}
Also used : Serializable(java.io.Serializable) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) DynamicJoinQueryWrapper(com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) BusinessException(com.diboot.core.exception.BusinessException) LambdaMeta(com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta) BusinessException(com.diboot.core.exception.BusinessException) EntityInfoCache(com.diboot.core.binding.parser.EntityInfoCache) BaseService(com.diboot.core.service.BaseService) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with LambdaMeta

use of com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta in project diboot by dibo-software.

the class BaseServiceImpl method convertGetterToFieldName.

/**
 * 转换SFunction为属性名
 * @param getterFn
 * @param <R>
 * @return
 */
private <R> String convertGetterToFieldName(SFunction<R, ?> getterFn) {
    LambdaMeta lambdaMeta = LambdaUtils.extract(getterFn);
    String fieldName = PropertyNamer.methodToProperty(lambdaMeta.getImplMethodName());
    return fieldName;
}
Also used : LambdaMeta(com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta)

Aggregations

LambdaMeta (com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta)2 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 EntityInfoCache (com.diboot.core.binding.parser.EntityInfoCache)1 DynamicJoinQueryWrapper (com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper)1 BusinessException (com.diboot.core.exception.BusinessException)1 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)1 BaseService (com.diboot.core.service.BaseService)1 Serializable (java.io.Serializable)1 Transactional (org.springframework.transaction.annotation.Transactional)1