package com.mini.mybigscreen.Job; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.mini.mybigscreen.biz.domain.ErpTransactionFlow; import com.mini.mybigscreen.biz.domain.IndexInfo; import com.mini.mybigscreen.biz.service.ErpTransactionFlowService; import com.mini.mybigscreen.biz.service.IndexInfoService; import com.mini.mybigscreen.utils.DateUtils; import jakarta.annotation.Resource; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Controller; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.List; import java.util.Objects; @Controller public class erpJobs { @Resource private IndexInfoService infoService; @Resource private ErpTransactionFlowService flowService; @Scheduled(cron = "0 0 */2 * * ?") public void updateErpIndex() { try { final BigDecimal BUDGET_AMOUNT = new BigDecimal("3000"); QueryWrapper query = new QueryWrapper<>(); query.eq("year_date", DateUtils.getCurrentYear()) .eq("month_date", DateUtils.getCurrentMonth()); List flowList = flowService.list(query); BigDecimal incomeAmount = flowList.stream() .filter(flow -> flow.getTransactionType().equals("2")) .map(ErpTransactionFlow::getAmount) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal expenseAmount = flowList.stream() .filter(flow -> flow.getTransactionType().equals("1")) .map(ErpTransactionFlow::getAmount) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); //净利润 BigDecimal netProfit = incomeAmount.subtract(expenseAmount); //利润率 BigDecimal profitRate = incomeAmount.equals(BigDecimal.ZERO) ? BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP) : netProfit.divide(incomeAmount, 4, RoundingMode.HALF_UP) .multiply(new BigDecimal("100")) .setScale(2, RoundingMode.HALF_UP); //占比 BigDecimal expenseIncomeRatio = incomeAmount.equals(BigDecimal.ZERO) ? BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP) : expenseAmount.divide(incomeAmount, 4, RoundingMode.HALF_UP) .multiply(new BigDecimal("100")) .setScale(2, RoundingMode.HALF_UP); boolean isOverBudget = expenseAmount.compareTo(BUDGET_AMOUNT) > 0; String budgetStatusName = isOverBudget ? "超支" : "正常"; BigDecimal overBudgetAmount = isOverBudget ? expenseAmount.subtract(BUDGET_AMOUNT) : BigDecimal.ZERO; String[] indexList = { "ERP_001", "ERP_002", "ERP_003", "ERP_004", "ERP_005", "ERP_006", }; for (String index : indexList) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("module_code", index); IndexInfo indexInfo = infoService.getOne(queryWrapper); if (index.equals("ERP_001")) { indexInfo.setValue(incomeAmount); } if (index.equals("ERP_002")) { indexInfo.setValue(expenseAmount); } if (index.equals("ERP_003")) { indexInfo.setValue(profitRate); } if (index.equals("ERP_004")) { indexInfo.setValue(expenseIncomeRatio); } if (index.equals("ERP_005")) { indexInfo.setValue(netProfit); } if (index.equals("ERP_006")) { indexInfo.setValue(overBudgetAmount); indexInfo.setTitle(budgetStatusName); } infoService.update(indexInfo, queryWrapper); } } catch (Exception e) { System.out.println(e.getMessage()); } } }