refactor(oa): 优化日趋势数据处理
- 增加空数据处理:当源数据为空或无记录时,自动生成近7天的默认数据 - 完善数据转换:对value值进行空值处理,若无值则默认为0
This commit is contained in:
@@ -23,90 +23,90 @@ import java.util.Map;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GearDashboardServiceImpl implements IGearDashboardService {
|
||||
|
||||
|
||||
private final GearDashboardMapper dashboardMapper;
|
||||
|
||||
|
||||
@Override
|
||||
public DashboardOverviewVO getDashboardOverview() {
|
||||
DashboardOverviewVO overview = new DashboardOverviewVO();
|
||||
|
||||
|
||||
// 获取订单统计数据
|
||||
overview.setOrderStatistics(getOrderStatistics());
|
||||
|
||||
|
||||
// 获取薪资统计数据
|
||||
overview.setSalaryStatistics(getSalaryStatistics());
|
||||
|
||||
|
||||
// 获取库存排行数据
|
||||
overview.setStockRanking(getStockRanking());
|
||||
|
||||
|
||||
// 获取其他统计数据
|
||||
overview.setOtherStatistics(getOtherStatistics());
|
||||
|
||||
|
||||
return overview;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取订单统计数据
|
||||
*/
|
||||
private OrderStatisticsVO getOrderStatistics() {
|
||||
OrderStatisticsVO statistics = new OrderStatisticsVO();
|
||||
|
||||
|
||||
// 获取今日、本周、本月订单数
|
||||
statistics.setTodayOrderCount(dashboardMapper.getTodayOrderCount());
|
||||
statistics.setWeekOrderCount(dashboardMapper.getWeekOrderCount());
|
||||
statistics.setMonthOrderCount(dashboardMapper.getMonthOrderCount());
|
||||
|
||||
|
||||
// 获取本周订单总金额
|
||||
statistics.setWeekTotalAmount(dashboardMapper.getWeekOrderAmount());
|
||||
|
||||
|
||||
// 获取近一周订单趋势
|
||||
List<Map<String, Object>> weeklyData = dashboardMapper.getWeeklyOrderTrend();
|
||||
statistics.setWeeklyTrend(convertToTrendList(weeklyData));
|
||||
|
||||
|
||||
// 计算增长率
|
||||
Integer lastWeekCount = dashboardMapper.getLastWeekOrderCount();
|
||||
statistics.setGrowthRate(calculateGrowthRate(statistics.getWeekOrderCount(), lastWeekCount));
|
||||
|
||||
|
||||
return statistics;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取薪资统计数据
|
||||
*/
|
||||
private SalaryStatisticsVO getSalaryStatistics() {
|
||||
SalaryStatisticsVO statistics = new SalaryStatisticsVO();
|
||||
|
||||
|
||||
// 获取今日、本周、本月薪资支出
|
||||
statistics.setTodaySalary(dashboardMapper.getTodaySalary());
|
||||
statistics.setWeekSalary(dashboardMapper.getWeekSalary());
|
||||
statistics.setMonthSalary(dashboardMapper.getMonthSalary());
|
||||
|
||||
|
||||
// 获取近一周薪资趋势
|
||||
List<Map<String, Object>> weeklyData = dashboardMapper.getWeeklySalaryTrend();
|
||||
statistics.setWeeklyTrend(convertToTrendList(weeklyData));
|
||||
|
||||
|
||||
// 计算增长率
|
||||
BigDecimal lastWeekSalary = dashboardMapper.getLastWeekSalary();
|
||||
statistics.setGrowthRate(calculateGrowthRate(statistics.getWeekSalary(), lastWeekSalary));
|
||||
|
||||
|
||||
// 计算平均日薪资
|
||||
if (statistics.getWeekSalary() != null) {
|
||||
statistics.setAvgDailySalary(statistics.getWeekSalary().divide(new BigDecimal(7), 2, RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
|
||||
return statistics;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取库存排行数据
|
||||
*/
|
||||
private List<StockRankingVO> getStockRanking() {
|
||||
List<StockRankingVO> rankings = dashboardMapper.getStockRanking();
|
||||
|
||||
|
||||
// 设置排名
|
||||
for (int i = 0; i < rankings.size(); i++) {
|
||||
rankings.get(i).setRank(i + 1);
|
||||
|
||||
|
||||
// 设置物品类型名称
|
||||
String itemType = rankings.get(i).getItemType();
|
||||
if ("product".equals(itemType)) {
|
||||
@@ -115,16 +115,16 @@ public class GearDashboardServiceImpl implements IGearDashboardService {
|
||||
rankings.get(i).setItemTypeName("原材料");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return rankings;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取其他统计数据
|
||||
*/
|
||||
private OtherStatisticsVO getOtherStatistics() {
|
||||
OtherStatisticsVO statistics = new OtherStatisticsVO();
|
||||
|
||||
|
||||
statistics.setActiveCustomerCount(dashboardMapper.getActiveCustomerCount());
|
||||
statistics.setPendingOrderCount(dashboardMapper.getPendingOrderCount());
|
||||
statistics.setLowStockCount(dashboardMapper.getLowStockCount());
|
||||
@@ -133,20 +133,34 @@ public class GearDashboardServiceImpl implements IGearDashboardService {
|
||||
statistics.setTodayAttendanceRate(dashboardMapper.getTodayAttendanceRate());
|
||||
statistics.setTotalProductCount(dashboardMapper.getTotalProductCount());
|
||||
statistics.setTotalSupplierCount(dashboardMapper.getTotalSupplierCount());
|
||||
|
||||
|
||||
return statistics;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换趋势数据
|
||||
*/
|
||||
private List<DailyTrendVO> convertToTrendList(List<Map<String, Object>> data) {
|
||||
List<DailyTrendVO> trends = new ArrayList<>();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
|
||||
|
||||
|
||||
// 如果数据为空或没有元素,则返回包含7天默认数据的列表
|
||||
if (data == null || data.isEmpty()) {
|
||||
// 创建近7天的默认数据
|
||||
for (int i = 6; i >= 0; i--) {
|
||||
DailyTrendVO trend = new DailyTrendVO();
|
||||
LocalDate date = LocalDate.now().minusDays(i);
|
||||
trend.setDate(date.format(formatter));
|
||||
trend.setLabel(trend.getDate());
|
||||
trend.setValue(BigDecimal.ZERO);
|
||||
trends.add(trend);
|
||||
}
|
||||
return trends;
|
||||
}
|
||||
|
||||
for (Map<String, Object> item : data) {
|
||||
DailyTrendVO trend = new DailyTrendVO();
|
||||
|
||||
|
||||
// 处理日期
|
||||
Object dateObj = item.get("date");
|
||||
if (dateObj instanceof LocalDate) {
|
||||
@@ -154,7 +168,7 @@ public class GearDashboardServiceImpl implements IGearDashboardService {
|
||||
} else if (dateObj instanceof String) {
|
||||
trend.setDate((String) dateObj);
|
||||
}
|
||||
|
||||
|
||||
// 处理数值
|
||||
Object valueObj = item.get("value");
|
||||
if (valueObj instanceof BigDecimal) {
|
||||
@@ -163,15 +177,19 @@ public class GearDashboardServiceImpl implements IGearDashboardService {
|
||||
trend.setValue(new BigDecimal((Integer) valueObj));
|
||||
} else if (valueObj instanceof Long) {
|
||||
trend.setValue(new BigDecimal((Long) valueObj));
|
||||
} else {
|
||||
// 如果没有值,则设置为0
|
||||
trend.setValue(BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
|
||||
trend.setLabel(trend.getDate());
|
||||
trends.add(trend);
|
||||
}
|
||||
|
||||
|
||||
return trends;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 计算增长率
|
||||
*/
|
||||
@@ -179,20 +197,20 @@ public class GearDashboardServiceImpl implements IGearDashboardService {
|
||||
if (current == null || previous == null) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
|
||||
BigDecimal currentValue = convertToBigDecimal(current);
|
||||
BigDecimal previousValue = convertToBigDecimal(previous);
|
||||
|
||||
|
||||
if (previousValue.compareTo(BigDecimal.ZERO) == 0) {
|
||||
return currentValue.compareTo(BigDecimal.ZERO) > 0 ? new BigDecimal(100) : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
|
||||
return currentValue.subtract(previousValue)
|
||||
.divide(previousValue, 4, RoundingMode.HALF_UP)
|
||||
.multiply(new BigDecimal(100))
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换为BigDecimal
|
||||
*/
|
||||
@@ -208,4 +226,4 @@ public class GearDashboardServiceImpl implements IGearDashboardService {
|
||||
}
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user