如何降低此代码的复杂性?

布鲁斯·加拉耶夫(ElbrusGarayev)

我正在尝试通过DTO创建搜索方法。我的意思是用户可以通过一个字段来搜索产品。您能提出任何更好的方法或帮助降低这一方法的复杂性吗?

public List<ProductDTO> search(SearchProductDTO productDTO) {
    Criteria criteria = null;
    String insensitive = "i";
    if (StringUtils.isNotBlank(productDTO.getName()))
        criteria = Criteria.where(NAME.getLabel()).regex(productDTO.getName(), insensitive);
    if (StringUtils.isNotBlank(productDTO.getDescription()))
        criteria = criteria != null ? criteria.and(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive) : Criteria.where(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive);
    if (productDTO.getPrice() != 0)
        criteria = criteria != null ? criteria.and(PRICE.getLabel()).is(productDTO.getPrice()) : Criteria.where(PRICE.getLabel()).is(productDTO.getPrice());
    if (StringUtils.isNotBlank(productDTO.getBrand()))
        criteria = criteria != null ? criteria.and(BRAND.getLabel()).regex(productDTO.getBrand(), insensitive) : Criteria.where(BRAND.getLabel()).regex(productDTO.getBrand(), insensitive);
    if (productDTO.getProductSize() != null)
        criteria = criteria != null ? criteria.and(SIZE.getLabel()).is(productDTO.getProductSize()) : Criteria.where(SIZE.getLabel()).is(productDTO.getProductSize());
    if (productDTO.getStockCount() != 0)
        criteria = criteria != null ? criteria.and(STOCK_COUNT.getLabel()).is(productDTO.getStockCount()) : Criteria.where(STOCK_COUNT.getLabel()).is(productDTO.getStockCount());
    if (StringUtils.isNotBlank(productDTO.getType()))
        criteria = criteria != null ? criteria.and(TYPE.getLabel()).regex(productDTO.getType(), insensitive) : Criteria.where(TYPE.getLabel()).regex(productDTO.getType(), insensitive);
    if (StringUtils.isNotBlank(productDTO.getColor()))
        criteria = criteria != null ? criteria.and(COLOR.getLabel()).regex(productDTO.getColor(), insensitive) : Criteria.where(COLOR.getLabel()).regex(productDTO.getColor(), insensitive);
    if (productDTO.getGender() != null)
        criteria = criteria != null ? criteria.and(GENDER.getLabel()).is(productDTO.getGender()) : Criteria.where(GENDER.getLabel()).is(productDTO.getGender());
    Pageable pageable = PageRequest.of(productDTO.getPage(), productDTO.getSize());
    Query query = new Query().with(pageable);
    query = criteria != null ? query.addCriteria(criteria).with(pageable) : query;
    List<Product> products = mongoTemplate.find(query, Product.class);
    return products.stream().map(productMapper::modelToDto).collect(Collectors.toList());
}
尼古拉·舍甫琴科

您可以通过将条件动态收集到列表中并将该列表组合为单个“多”条件来摆脱空检查

List<Criteria> criterias = new ArrayList<>();

// collect
if (StringUtils.isNotBlank(productDTO.getName()))
    criterias.add(Criteria.where(NAME.getLabel()).regex(productDTO.getName(), insensitive));
if (StringUtils.isNotBlank(productDTO.getDescription()))
    criterias.add(Criteria.where(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive));
if (productDTO.getPrice() != 0)
    criterias.add(Criteria.where(PRICE.getLabel()).is(productDTO.getPrice()));
// and so on ...

// combine
Criteria multiCriteria = new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()]));

// use
Pageable pageable = PageRequest.of(productDTO.getPage(), productDTO.getSize());
Query query = new Query().with(pageable);
query.addCriteria(multiCriteria);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章