多列分组有问题吗?

时间:2019-08-30 12:49:26

标签: java

我有这种方法:

public List<IncomeChannelCategoryMap> allIncomeChannels(final List<String> list) {
    final CriteriaQuery<IncomeChannelCategoryMap> criteriaQuery = builder.createQuery(IncomeChannelCategoryMap.class);
    final Root<IncomeChannelMapEntity> root = criteriaQuery.from(IncomeChannelMapEntity.class);

    final List<Selection<?>> selections = new ArrayList<>();
    selections.add(root.get(IncomeChannelMapEntity_.incomeChannel).get(IncomeChannelEntity_.code));
    selections.add(root.get(IncomeChannelMapEntity_.logicalUnitCode));
    selections.add(root.get(IncomeChannelMapEntity_.logicalUnitIdent));
    selections.add(root.get(IncomeChannelMapEntity_.keyword));
    criteriaQuery.multiselect(selections);
    Predicate codePredicate = root.get(IncomeChannelMapEntity_.incomeChannel).get(IncomeChannelEntity_.code).in(list);
    criteriaQuery.where(codePredicate);
    return entityManager.createQuery(criteriaQuery).getResultList();

}

这:

   @Override
    public List<IncomeChannelCategoryMap> allIncomeChannels(final EntityRequest<IncomeChannel> request) throws ApiException {
        List<String> lists = request.getEntity().getIncomeChannels();
        List<IncomeChannelCategoryMap> channels = incomeChannelMapDAO.allIncomeChannels(lists);
        return new ArrayList<>(channels.stream().collect(Collectors.toMap(IncomeChannelCategoryMap::getIncomeChannelCode,
                Function.identity(), (final IncomeChannelCategoryMap i1, final IncomeChannelCategoryMap i2) -> {
                    i1.setLogicalUnitIdent(i1.getLogicalUnitIdent() + "," + i2.getLogicalUnitIdent());
                    return i1;
                })).values());

    }

我能够做到这一点:

  {
    "incomeChannelCode": "DIRECT_SALES",
    "logicalUnitCode": "R_CATEGORY",
    "logicalUnitIdent": "7,8"
  }

来自

[
{
    "incomeChannelCode": "DIRECT_SALES",
    "logicalUnitCode": "R_CATEGORY",
    "logicalUnitIdent": "7"
  },
  {
    "incomeChannelCode": "DIRECT_SALES",
    "logicalUnitCode": "R_CATEGORY",
    "logicalUnitIdent": "8"
  }
]

一切都很好,但是有一个问题:

例如,DIRECT_SALES可以具有另一个逻辑单元代码,所以现在我只获得一个逻辑单元代码,我想像我对逻辑单元标识符那样获得逻辑单元代码。

有什么建议吗? 所以我要实现的是:

      {
        "incomeChannelCode": "DIRECT_SALES",
        "logicalUnitCode": "R_CATEGORY","R_TYPE",
        "logicalUnitIdent": "7,8"
      }

1 个答案:

答案 0 :(得分:1)

这是您更新的代码:

@Override
public List<IncomeChannelCategoryMap> allIncomeChannels(final EntityRequest<IncomeChannel> request) throws ApiException {
    List<String> lists = request.getEntity().getIncomeChannels();
    List<IncomeChannelCategoryMap> channels = incomeChannelMapDAO.allIncomeChannels(lists);
    return new ArrayList<>(channels.stream().collect(Collectors.toMap(IncomeChannelCategoryMap::getIncomeChannelCode,
            Function.identity(), (i1, i2) -> {
                i1.setLogicalUnitIdent(i1.getLogicalUnitIdent() + ", " + i2.getLogicalUnitIdent());
                if (!i1.getLogicalUnitCode().contains(i2.getLogicalUnitCode())) {
                    i1.setLogicalUnitCode(i1.getLogicalUnitCode() + ", " + i2.getLogicalUnitCode());
                }
                return i1;
            })).values());
}

就像logicalUnitIdent一样logicalUnitCode也将被分组。在这里,我假设您不想在这里重复。重复是指如果两个结果的logicalUnitCode"R_CATEGORY",那么您希望它一次作为输出。并且如果一个是"R_CATEGORY"而另一个是"R_TYPE",则希望将它们分组为"R_CATEGORY, R_TYPE"作为输出。如果我的假设正确,那么这就是您的必需答案。