class CostEffectivenessVisualsController extends GenericOCDSController {

    @ApiOperation(value = "Cost effectiveness of Awards: Displays the total amount of active awards grouped by year."
            + "The tender entity, for each award, has to have amount value. The year is calculated from "
            + MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)
    @RequestMapping(value = "/api/costEffectivenessAwardAmount",
            method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json")
    public List<Document> costEffectivenessAwardAmount(
            @ModelAttribute @Valid final YearFilterPagingRequest filter) {

        DBObject project = new BasicDBObject();
        addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
        project.put(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT, 1);
        project.put("totalAwardsWithTender", new BasicDBObject(
                "$cond",
                Arrays.asList(new BasicDBObject(
                        "$gt",
                        Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE), null)
                ), 1, 0)
        ));
        project.put(
                "awardsWithTenderValue",
                new BasicDBObject(
                        "$cond",
                        Arrays.asList(new BasicDBObject(
                                        "$gt",
                                        Arrays.asList(ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE), null)
                                ),
                                "$awards.value.amount", 0
                        )
                )
        );

        Aggregation agg = Aggregation.newAggregation(
                match(where("awards").elemMatch(where("status").is(Award.Status.active.toString()))
                        .and(MongoConstants.FieldNames.AWARDS_DATE)
                        .exists(true)
                        .and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)
                        .exists(true)),
                getMatchDefaultFilterOperation(filter), unwind("awards"),
                match(where(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString())
                        .and("awards.value")
                        .exists(true)
                        .
                                andOperator(getYearDefaultFilterCriteria(
                                        filter,
                                        MongoConstants.FieldNames.TENDER_PERIOD_START_DATE
                                ))),
                new CustomProjectionOperation(project),
                getYearlyMonthlyGroupingOperation(filter)
                        .sum("awardsWithTenderValue").as(Keys.TOTAL_AWARD_AMOUNT).count().as(Keys.TOTAL_AWARDS)
                        .sum("totalAwardsWithTender").as(Keys.TOTAL_AWARDS_WITH_TENDER),
                project(Fields.UNDERSCORE_ID, Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS, Keys.TOTAL_AWARDS_WITH_TENDER)
                        .and(Keys.TOTAL_AWARDS_WITH_TENDER).divide(Keys.TOTAL_AWARDS)
                        .as(Keys.FRACTION_AWARDS_WITH_TENDER),
                project(
                        Fields.UNDERSCORE_ID, Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS, Keys.TOTAL_AWARDS_WITH_TENDER,
                        Keys.FRACTION_AWARDS_WITH_TENDER
                ).and(Keys.FRACTION_AWARDS_WITH_TENDER).multiply(100)
                        .as(Keys.PERCENTAGE_AWARDS_WITH_TENDER),
                transformYearlyGrouping(filter).andInclude(Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_AWARDS,
                        Keys.TOTAL_AWARDS_WITH_TENDER, Keys.PERCENTAGE_AWARDS_WITH_TENDER
                ), getSortByYearMonth(filter),
                skip(filter.getSkip()), limit(filter.getPageSize())
        );


        return releaseAgg(agg);
    }

}