use of org.devgateway.ocds.persistence.mongo.constants.MongoConstants.FieldNames.TENDER_PERIOD_START_DATE in project oc-explorer by devgateway.
the class AwardsWonLostController method procurementsWonLost.
@ApiOperation(value = "Counts the won, lost procurements, flags and amounts. Receives any filters, " + "but most important here is the supplierId and bidderId. Requires bid extension. Use bidderId instead " + "of supplierId.")
@RequestMapping(value = "/api/procurementsWonLost", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<ProcurementsWonLost> procurementsWonLost(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
Assert.notEmpty(filter.getBidderId(), "bidderId must not be empty!");
Assert.isTrue(CollectionUtils.isEmpty(filter.getSupplierId()), "supplierId is not allowed here! Use bidderId to show results!");
// supplier is the same thing as bidder for this particular query
filter.setSupplierId(filter.getBidderId());
Map<String, CriteriaDefinition> noSupplierCriteria = createDefaultFilterCriteriaMap(filter);
noSupplierCriteria.remove(MongoConstants.Filters.SUPPLIER_ID);
Aggregation agg1 = newAggregation(match(getYearDefaultFilterCriteria(filter, noSupplierCriteria, TENDER_PERIOD_START_DATE)), unwind("bids.details"), unwind("bids.details.tenderers"), match(getYearDefaultFilterCriteria(filter, noSupplierCriteria, TENDER_PERIOD_START_DATE)), group(BIDS_DETAILS_TENDERERS_ID).count().as("count").sum(BIDS_DETAILS_VALUE_AMOUNT).as("totalAmount").sum(FLAGS_TOTAL_FLAGGED).as("countFlags"), project("count", "totalAmount", "countFlags"));
List<CountAmountFlags> applied = releaseAgg(agg1, CountAmountFlags.class);
Aggregation agg2 = newAggregation(match(where(AWARDS_STATUS).is(Award.Status.active.toString()).andOperator(getYearDefaultFilterCriteria(filter, TENDER_PERIOD_START_DATE))), unwind("awards"), unwind("awards.suppliers"), match(where(AWARDS_STATUS).is(Award.Status.active.toString()).andOperator(getYearDefaultFilterCriteria(filter.awardFiltering(), TENDER_PERIOD_START_DATE))), group(MongoConstants.FieldNames.AWARDS_SUPPLIERS_ID).count().as("count").sum(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT).as("totalAmount").sum(FLAGS_TOTAL_FLAGGED).as("countFlags"), project("count", "totalAmount", "countFlags"));
List<CountAmountFlags> won = releaseAgg(agg2, CountAmountFlags.class);
ArrayList<ProcurementsWonLost> ret = new ArrayList<>();
applied.forEach(a -> {
ProcurementsWonLost r = new ProcurementsWonLost();
r.setApplied(a);
Optional<CountAmountFlags> optWon = won.stream().filter(w -> w.getId().equals(a.getId())).findFirst();
if (optWon.isPresent()) {
r.setWon(optWon.get());
r.setLostAmount(r.getApplied().getTotalAmount().subtract(r.getWon().getTotalAmount()));
r.setLostCount(r.getApplied().getCount() - r.getWon().getCount());
} else {
r.setLostAmount(r.getApplied().getTotalAmount());
r.setLostCount(r.getLostCount());
}
ret.add(r);
});
return ret;
}
Aggregations