class TenderPercentagesController extends GenericOCDSController {

    @ApiOperation("Percent of awarded tenders with >1 tenderer/bidder"
            + "Count of tenders with numberOfTenderers >1 divided by total count of tenders with numberOfTenderers >0"
            + "This endpoint uses tender.tenderPeriod.startDate to calculate the tender year.")
    @RequestMapping(value = "/api/percentTendersAwardedWithTwoOrMoreTenderers",
            method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json")
    public List<Document> percentTendersAwarded(@ModelAttribute @Valid final YearFilterPagingRequest filter) {

        DBObject project1 = new BasicDBObject();
        addYearlyMonthlyProjection(filter, project1, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
        project1.put(MongoConstants.FieldNames.TENDER_NO_TENDERERS, 1);

        DBObject group = new BasicDBObject();
        addYearlyMonthlyReferenceToGroup(filter, group);
        group.put(Keys.TOTAL_TENDERS_WITH_ONE_OR_MORE_TENDERERS, new BasicDBObject("$sum", new BasicDBObject(
                "$cond",
                Arrays.asList(
                        new BasicDBObject("$gt", Arrays.asList(ref(MongoConstants.FieldNames.TENDER_NO_TENDERERS), 0)),
                        1, 0
                )
        )));

        group.put(Keys.TOTAL_TENDERS_WITH_TWO_OR_MORE_TENDERERS, new BasicDBObject("$sum", new BasicDBObject(
                "$cond",
                Arrays.asList(
                        new BasicDBObject("$gt", Arrays.asList(ref(MongoConstants.FieldNames.TENDER_NO_TENDERERS), 1)),
                        1, 0
                )
        )));

        DBObject project2 = new BasicDBObject();
        project2.put(Keys.TOTAL_TENDERS_WITH_ONE_OR_MORE_TENDERERS, 1);
        project2.put(Keys.TOTAL_TENDERS_WITH_TWO_OR_MORE_TENDERERS, 1);
        project2.put(
                Keys.PERCENT_TENDERS,
                new BasicDBObject("$multiply", Arrays.asList(
                        new BasicDBObject(
                                "$divide",
                                Arrays.asList(
                                        "$totalTendersWithTwoOrMoreTenderers",
                                        "$totalTendersWithOneOrMoreTenderers"
                                )
                        ),
                        100
                ))
        );

        Aggregation agg = newAggregation(
                match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)
                        .exists(true).and(MongoConstants.FieldNames.TENDER_NO_TENDERERS).gt(0)
                        .andOperator(getYearDefaultFilterCriteria(
                                filter,
                                MongoConstants.FieldNames.TENDER_PERIOD_START_DATE
                        ))),
                new CustomProjectionOperation(project1), new CustomGroupingOperation(group),
                new CustomProjectionOperation(project2),
                transformYearlyGrouping(filter).andInclude(Keys.TOTAL_TENDERS_WITH_ONE_OR_MORE_TENDERERS,
                        Keys.TOTAL_TENDERS_WITH_TWO_OR_MORE_TENDERERS, Keys.PERCENT_TENDERS
                ),
                getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize())
        );

        return releaseAgg(agg);
    }

}