use of com.tony.billing.entity.Budget 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;
}
use of com.tony.billing.entity.Budget in project BillingDubbo by TonyJiangWJ.
the class BudgetServiceImpl method queryBudgetsByCondition.
@Override
public List<BudgetDTO> queryBudgetsByCondition(Budget condition) {
Preconditions.checkNotNull(condition.getUserId(), "userId must not be null");
Preconditions.checkNotNull(condition.getBelongMonth(), "month must not be null");
Preconditions.checkNotNull(condition.getBelongYear(), "year must not be null");
List<Budget> budgets = mapper.findByYearMonth(condition);
return budgets.stream().map((budget) -> {
BudgetDTO dto = new BudgetDTO();
dto.setId(budget.getId());
dto.setBudgetName(budget.getBudgetName());
dto.setBudgetMoney(MoneyUtil.fen2Yuan(budget.getBudgetMoney()));
dto.setYearMonth(YearMonth.of(Integer.parseInt(budget.getBelongYear()), budget.getBelongMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM")));
List<TagInfo> tagInfos = tagInfoMapper.listTagInfoByBudgetId(budget.getId(), budget.getUserId());
dto.setTagInfos(new TagInfoToDtoListSupplier(tagInfos).get());
return dto;
}).collect(Collectors.toList());
}
use of com.tony.billing.entity.Budget in project BillingDubbo by TonyJiangWJ.
the class BudgetController method getBudgetInfo.
@RequestMapping("/budget/get")
public BaseResponse getBudgetInfo(@ModelAttribute("request") @Validated BudgetDetailRequest request) {
Budget budget = budgetService.getById(request.getId());
if (budget != null) {
BudgetDetailResponse response = ResponseUtil.success(new BudgetDetailResponse());
BudgetDTO budgetDTO = new BudgetDTO();
budgetDTO.setId(budget.getId());
budgetDTO.setYearMonth(YearMonth.of(Integer.parseInt(budget.getBelongYear()), budget.getBelongMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM")));
budgetDTO.setBudgetName(budget.getBudgetName());
budgetDTO.setBudgetMoney(MoneyUtil.fen2Yuan(budget.getBudgetMoney()));
budgetDTO.setVersion(budget.getVersion());
budgetDTO.setTagInfos(budgetService.getTagInfosByBudgetId(budget.getId(), request.getUserId()));
response.setBudgetInfo(budgetDTO);
return response;
}
return ResponseUtil.dataNotExisting();
}
use of com.tony.billing.entity.Budget in project BillingDubbo by TonyJiangWJ.
the class BudgetController method listBudget.
@RequestMapping("/budget/list")
public BudgetListResponse listBudget(@ModelAttribute("request") @Validated BudgetListRequest request) {
BudgetListResponse response = new BudgetListResponse();
try {
Budget query = new Budget();
query.setBelongMonth(request.getMonth());
query.setBelongYear(request.getYear());
query.setUserId(request.getUserId());
List<BudgetDTO> budgets = budgetService.queryBudgetsByCondition(query);
if (CollectionUtils.isNotEmpty(budgets)) {
response.setBudgetList(budgets);
ResponseUtil.success(response);
} else {
ResponseUtil.dataNotExisting(response);
}
} catch (Exception e) {
logger.error("/budget/list error", e);
ResponseUtil.sysError(response);
}
return response;
}
use of com.tony.billing.entity.Budget in project BillingDubbo by TonyJiangWJ.
the class BudgetController method addBudget.
@RequestMapping("/budget/put")
public BaseResponse addBudget(@ModelAttribute("request") @Validated BudgetPutRequest request) {
BaseResponse response = new BaseResponse();
try {
Budget budget = new Budget();
budget.setBelongMonth(request.getMonth());
budget.setBelongYear(request.getYear());
budget.setBudgetMoney(request.getAmount());
budget.setUserId(request.getUserId());
budget.setBudgetName(request.getName());
if (budgetService.insert(budget) > 0) {
ResponseUtil.success(response);
} else {
ResponseUtil.error(response);
}
} catch (Exception e) {
logger.error("/budget/put error", e);
ResponseUtil.sysError(response);
}
return response;
}
Aggregations