巧用枚举替代if

61 阅读1分钟

需求:后台的某个分页列表需要不同的时间检索条件1 今日 2 昨日 3 本月 4 上月 5 今年 6 自定义 为减少if,使用枚举+函数式接口的方式

入口实现类

public Page<ProductStatisticsVO> pageList(ProductStatisticsQueryReqVO reqVO) {
    Page<ProductStatisticsVO> page = ProductStatisticsTypeEnum.pageListByType(reqVO, this);
    return new Page<>((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(),(int) page.getPages(), page.getRecords());
}

枚举类

@SuppressWarnings("AlibabaEnumConstantsMustHaveComment")
@Getter
@AllArgsConstructor
@NoArgsConstructor
public enum ProductStatisticsTypeEnum {
    TODAY(1, "今日统计", (reqVO, productStatisticsService) ->
    {
        LocalDate localDate = LocalDate.now();
        return productStatisticsService.dayStatistics(localDate.getYear(), localDate.getMonth().getValue(), localDate.getDayOfMonth(), reqVO);
    }),

    YESTERDAY(2, "昨日统计", (reqVO, productStatisticsService) ->
    {
        LocalDate localDate = LocalDate.now().minusDays(1);
        return productStatisticsService.dayStatistics(localDate.getYear(), localDate.getMonth().getValue(), localDate.getDayOfMonth(), reqVO);
    }),

    CURRENT_MONTH(3, "本月统计", (reqVO, productStatisticsService) ->
    {
        LocalDate localDate = LocalDate.now();
        return productStatisticsService.monthStatistics(localDate.getYear(), localDate.getMonth().getValue(), reqVO);
    }),

    LAST_MONTH(4, "上月统计", (reqVO, productStatisticsService) ->
    {
        LocalDate localDate = LocalDate.now().minusMonths(1);
        return productStatisticsService.monthStatistics(localDate.getYear(), localDate.getMonth().getValue(), reqVO);
    }),

    CURRENT_YEAR(5, "今年统计", (reqVO, productStatisticsService) -> productStatisticsService.yearStatistics(LocalDate.now().getYear(), reqVO)
    ),

    CUSTOM(6,"自定义统计",(reqVO, productStatisticsService)->productStatisticsService.customStatistics(reqVO)),;
    private Integer code;
    private String msg;
    private BiFunction<ProductStatisticsQueryReqVO, ProductStatisticsServiceImpl, Page<ProductStatisticsVO>> function;

    public static Page<ProductStatisticsVO> pageListByType(ProductStatisticsQueryReqVO reqVO, ProductStatisticsServiceImpl productStatisticsService) {
        for (ProductStatisticsTypeEnum value : values()) {
            if (value.getCode().equals(reqVO.getSearchDateType())) {
                return value.function.apply(reqVO, productStatisticsService);
            }
        }
        throw new StatisticsException("不支持该时间维度查询");
    }
}

具体实现方法

/**
 * day维度统计
 */
public Page<ProductStatisticsVO> dayStatistics(int year, int month, int dayOfMonth, ProductStatisticsQueryReqVO reqVO) {
    Page<ProductStatisticsVO> page = new Page<>(reqVO.getPageNum(), reqVO.getPageSize());
    List<ProductStatisticsVO> list = productStatisticsMapper.pageListByDayStatistics(page, year, month, dayOfMonth, reqVO);
    return page.setRecords(reSetList(list));
}
/**
 * 月维度统计
 */
public Page<ProductStatisticsVO> monthStatistics(int year, int month, ProductStatisticsQueryReqVO reqVO) {
    Page<ProductStatisticsVO> page = new Page<>(reqVO.getPageNum(), reqVO.getPageSize());
    List<ProductStatisticsVO> list = productStatisticsMapper.pageListByMonthStatistics(page, year, month, reqVO);
    return page.setRecords(reSetList(list));
}