Search in sources :

Example 1 with BudgetReportModel

use of com.tony.billing.model.BudgetReportModel in project BillingDubbo by TonyJiangWJ.

the class BudgetServiceImpl method getBudgetReportByMonth.

@Override
public BudgetReportModel getBudgetReportByMonth(String monthInfo, Long userId) {
    Preconditions.checkState(YEAR_MONTH_PATTERN.matcher(monthInfo).find(), "参数错误");
    BudgetReportModel reportInfo = new BudgetReportModel();
    reportInfo.setYearMonthInfo(monthInfo);
    String year = monthInfo.substring(0, 4);
    Integer month = NumberUtils.toInt(monthInfo.substring(5));
    Budget query = new Budget();
    query.setBelongYear(year);
    query.setBelongMonth(month);
    query.setUserId(userId);
    List<Budget> budgets = mapper.findByYearMonth(query);
    if (CollectionUtils.isNotEmpty(budgets)) {
        List<Long> monthTagIds = tagInfoMapper.listTagIdsByBudgetMonth(year, month, userId, null);
        List<CostRecord> noBudgetRecords = costRecordMapper.listByMonthAndExceptTagIds(monthInfo, userId, monthTagIds);
        if (CollectionUtils.isNotEmpty(noBudgetRecords)) {
            reportInfo.setNoBudgetUsed(noBudgetRecords.stream().mapToLong(CostRecord::getMoney).sum());
        } else {
            logger.info("[{}] 未关联预算账单数据不存在", monthInfo);
        }
        List<CostRecord> budgetRecords = costRecordMapper.listByMonthAndTagIds(monthInfo, userId, monthTagIds);
        if (CollectionUtils.isNotEmpty(budgetRecords)) {
            reportInfo.setBudgetUsed(budgetRecords.stream().mapToLong(CostRecord::getMoney).sum());
        } else {
            logger.info("[{}] 预算关联账单数据不存在", monthInfo);
        }
        budgets.forEach(b -> {
            List<TagInfo> budgetTags = tagInfoMapper.listTagInfoByBudgetId(b.getId(), userId);
            Long sum = 0L;
            BudgetReportItemDTO item = new BudgetReportItemDTO();
            List<CostRecord> costRecords = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(budgetTags)) {
                item.setTagInfos(budgetTags.parallelStream().map(tag -> {
                    TagCostInfoDTO costInfoDTO = new TagCostInfoDTO();
                    List<CostRecord> tagCostRecords = costRecordMapper.listByMonthAndTagIds(monthInfo, userId, Collections.singletonList(tag.getId()));
                    if (CollectionUtils.isNotEmpty(tagCostRecords)) {
                        costInfoDTO.setAmount(tagCostRecords.stream().mapToLong(CostRecord::getMoney).sum());
                        costRecords.addAll(tagCostRecords);
                    }
                    costInfoDTO.setTagId(tag.getId());
                    costInfoDTO.setTagName(tag.getTagName());
                    return costInfoDTO;
                }).collect(Collectors.toList()));
            }
            item.setVersion(b.getVersion());
            item.setId(b.getId());
            item.setName(b.getBudgetName());
            item.setAmount(b.getBudgetMoney());
            if (CollectionUtils.isNotEmpty(costRecords)) {
                sum = costRecords.parallelStream().distinct().mapToLong(CostRecord::getMoney).sum();
            }
            item.setUsed(sum);
            item.setRemain(b.getBudgetMoney() - sum);
            reportInfo.addBudgetInfos(item);
        });
        return reportInfo.build();
    }
    return null;
}
Also used : TagCostInfoDTO(com.tony.billing.dto.TagCostInfoDTO) ArrayList(java.util.ArrayList) CostRecord(com.tony.billing.entity.CostRecord) BudgetReportItemDTO(com.tony.billing.dto.BudgetReportItemDTO) TagInfo(com.tony.billing.entity.TagInfo) Budget(com.tony.billing.entity.Budget) BudgetReportModel(com.tony.billing.model.BudgetReportModel)

Example 2 with BudgetReportModel

use of com.tony.billing.model.BudgetReportModel in project BillingDubbo by TonyJiangWJ.

the class BudgetController method getBudgetOverviewInfo.

@RequestMapping("/budget/report/list")
public BudgetOverviewResponse getBudgetOverviewInfo() {
    List<BudgetReportModel> reportModels = budgetService.getNearlySixMonth();
    BudgetOverviewResponse response;
    if (CollectionUtils.isNotEmpty(reportModels)) {
        response = ResponseUtil.success(new BudgetOverviewResponse());
        response.setReportModelList(reportModels);
    } else {
        response = ResponseUtil.dataNotExisting(new BudgetOverviewResponse());
    }
    return response;
}
Also used : BudgetOverviewResponse(com.tony.billing.response.budget.BudgetOverviewResponse) BudgetReportModel(com.tony.billing.model.BudgetReportModel) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with BudgetReportModel

use of com.tony.billing.model.BudgetReportModel in project BillingDubbo by TonyJiangWJ.

the class BudgetServiceImpl method getAndSetCache.

private BudgetReportModel getAndSetCache(String monthInfo, long cacheTime, long userId) {
    YearMonth thisMonth = YearMonth.now(TimeConstants.CHINA_ZONE);
    Optional<BudgetReportModel> reportModelOptional = Optional.empty();
    boolean usingCache = thisMonth.isAfter(YearMonth.parse(monthInfo, DateTimeFormatter.ofPattern("yyyy-MM")));
    String redisKey = MONTH_MODEL_CACHE_PREFIX + userId + "_" + monthInfo;
    if (usingCache) {
        reportModelOptional = redisUtils.get(redisKey, BudgetReportModel.class);
    }
    if (reportModelOptional.isPresent()) {
        return reportModelOptional.get();
    } else {
        BudgetReportModel reportModel = getBudgetReportByMonth(monthInfo, userId);
        if (usingCache && reportModel != null) {
            redisUtils.set(redisKey, reportModel, cacheTime);
        }
        return reportModel;
    }
}
Also used : YearMonth(java.time.YearMonth) BudgetReportModel(com.tony.billing.model.BudgetReportModel)

Example 4 with BudgetReportModel

use of com.tony.billing.model.BudgetReportModel in project BillingDubbo by TonyJiangWJ.

the class BudgetServiceImpl method getNearlySixMonth.

@Override
public List<BudgetReportModel> getNearlySixMonth() {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
    LocalDate localDate = LocalDate.now();
    List<BudgetReportModel> models = new ArrayList<>();
    List<MonthReport> months = new ArrayList<>();
    // 获取下个月的预算数据
    localDate = localDate.plusMonths(1);
    Long userId = UserIdContainer.getUserId();
    months.add(new MonthReport(localDate.format(dateTimeFormatter), 3600L, userId));
    // 获取当前月份
    localDate = localDate.plusMonths(-1);
    months.add(new MonthReport(localDate.format(dateTimeFormatter), 3600L, userId));
    // 过去五个月的预算信息
    for (int i = 0; i < 6; i++) {
        localDate = localDate.plusMonths(-1);
        months.add(new MonthReport(localDate.format(dateTimeFormatter), null, userId));
    }
    models = months.parallelStream().map(this::getAndSetCache).collect(Collectors.toList());
    return models.stream().filter(Objects::nonNull).sorted(Comparator.comparing(BudgetReportModel::getYearMonthInfo).reversed()).collect(Collectors.toList());
}
Also used : ArrayList(java.util.ArrayList) Objects(java.util.Objects) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) BudgetReportModel(com.tony.billing.model.BudgetReportModel)

Aggregations

BudgetReportModel (com.tony.billing.model.BudgetReportModel)4 ArrayList (java.util.ArrayList)2 BudgetReportItemDTO (com.tony.billing.dto.BudgetReportItemDTO)1 TagCostInfoDTO (com.tony.billing.dto.TagCostInfoDTO)1 Budget (com.tony.billing.entity.Budget)1 CostRecord (com.tony.billing.entity.CostRecord)1 TagInfo (com.tony.billing.entity.TagInfo)1 BudgetOverviewResponse (com.tony.billing.response.budget.BudgetOverviewResponse)1 LocalDate (java.time.LocalDate)1 YearMonth (java.time.YearMonth)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Objects (java.util.Objects)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1