添加图表数据展示功能
- 后端新增/chart-data接口用于获取图表所需数据 - 实现近6个月出入库金额统计和药品分类占比功能 - 前端集成echarts图表组件展示数据可视化 - 添加月度出入库柱状图和药品分类饼图 - 优化仪表盘界面布局和样式
This commit is contained in:
@@ -28,4 +28,9 @@ public class DashboardController {
|
||||
@RequestParam(defaultValue = "10") Integer size) {
|
||||
return Result.success(dashboardService.getExpiryWarning(new Page<>(page, size)));
|
||||
}
|
||||
|
||||
@GetMapping("/chart-data")
|
||||
public Result<Map<String, Object>> chartData() {
|
||||
return Result.success(dashboardService.getChartData());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,13 @@ import com.kronecker.backend.entity.Stock;
|
||||
import com.kronecker.backend.entity.StockInRecord;
|
||||
import com.kronecker.backend.entity.StockOutRecord;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface DashboardService {
|
||||
java.util.Map<String, Object> getStatistics();
|
||||
Map<String, Object> getStatistics();
|
||||
Page<Stock> getExpiryWarning(Page<Stock> page);
|
||||
Page<StockInRecord> getRecentStockIn(Page<StockInRecord> page);
|
||||
Page<StockOutRecord> getRecentStockOut(Page<StockOutRecord> page);
|
||||
Map<String, Object> getChartData();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.kronecker.backend.entity.*;
|
||||
import com.kronecker.backend.mapper.*;
|
||||
import com.kronecker.backend.entity.*;
|
||||
import com.kronecker.backend.mapper.*;
|
||||
import com.kronecker.backend.service.DashboardService;
|
||||
import com.kronecker.backend.utils.RedisCacheService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -17,6 +19,7 @@ import java.util.*;
|
||||
public class DashboardServiceImpl implements DashboardService {
|
||||
|
||||
private final MedicineMapper medicineMapper;
|
||||
private final MedicineCategoryMapper medicineCategoryMapper;
|
||||
private final StockMapper stockMapper;
|
||||
private final StockInRecordMapper stockInRecordMapper;
|
||||
private final StockOutRecordMapper stockOutRecordMapper;
|
||||
@@ -97,4 +100,50 @@ public class DashboardServiceImpl implements DashboardService {
|
||||
records.forEach(r -> { if (r.getCustomerId() != null) r.setCustomerName(custMap.getOrDefault(r.getCustomerId(), "")); });
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getChartData() {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
|
||||
// 近6个月出入库金额统计
|
||||
List<Map<String, Object>> monthlyStats = new ArrayList<>();
|
||||
for (int i = 5; i >= 0; i--) {
|
||||
String month = LocalDate.now().minusMonths(i).format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM"));
|
||||
Double inAmount = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
|
||||
.eq(StockInRecord::getStatus, 1)
|
||||
.likeRight(StockInRecord::getInDate, month))
|
||||
.stream().mapToDouble(r -> r.getTotalAmount().doubleValue()).sum();
|
||||
Double outAmount = stockOutRecordMapper.selectList(new LambdaQueryWrapper<StockOutRecord>()
|
||||
.eq(StockOutRecord::getStatus, 1)
|
||||
.likeRight(StockOutRecord::getOutDate, month))
|
||||
.stream().mapToDouble(r -> r.getTotalAmount().doubleValue()).sum();
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("month", month);
|
||||
m.put("inAmount", Math.round(inAmount * 100.0) / 100.0);
|
||||
m.put("outAmount", Math.round(outAmount * 100.0) / 100.0);
|
||||
monthlyStats.add(m);
|
||||
}
|
||||
data.put("monthlyStats", monthlyStats);
|
||||
|
||||
// 药品分类库存占比
|
||||
List<Map<String, Object>> categoryStats = new ArrayList<>();
|
||||
List<MedicineCategory> categories = medicineCategoryMapper.selectList(null);
|
||||
for (MedicineCategory cat : categories) {
|
||||
int count = medicineMapper.selectCount(new LambdaQueryWrapper<Medicine>().eq(Medicine::getCategoryId, cat.getId())).intValue();
|
||||
if (count > 0) {
|
||||
Map<String, Object> cs = new HashMap<>();
|
||||
cs.put("name", cat.getName());
|
||||
cs.put("value", count);
|
||||
categoryStats.add(cs);
|
||||
}
|
||||
}
|
||||
data.put("categoryStats", categoryStats);
|
||||
|
||||
// 库存总价值
|
||||
double totalStockValue = stockMapper.selectList(new LambdaQueryWrapper<Stock>().gt(Stock::getQuantity, 0))
|
||||
.stream().mapToDouble(s -> s.getCostPrice().doubleValue() * s.getQuantity()).sum();
|
||||
data.put("totalStockValue", Math.round(totalStockValue * 100.0) / 100.0);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user