You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5.0 KiB
5.0 KiB
百色中学订单总金额统计功能实现文档
功能概述
参考ShopOrderController的total方法,完善了BszxOrderController中的订单总金额统计功能,提供REST API接口用于统计百色中学所有捐款记录的总金额。
API接口
统计订单总金额
接口地址: GET /api/bszx/bszx-order/total
接口描述: 统计百色中学所有捐款记录的总金额
请求参数: 无
响应格式:
{
"code": 200,
"message": "操作成功",
"data": 12345.67
}
响应说明:
data
: BigDecimal类型,表示捐款总金额- 统计所有捐款记录(bszx_pay表中的price字段)
- 使用COALESCE函数处理空值,确保返回值不为null
实现细节
1. 接口层 (Controller)
文件: BszxOrderController.java
@Operation(summary = "统计订单总金额")
@GetMapping("/total")
public ApiResult<BigDecimal> total() {
try {
BigDecimal totalAmount = bszxPayService.total();
return success(totalAmount);
} catch (Exception e) {
// 异常时返回0,保持接口稳定性
return success(BigDecimal.ZERO);
}
}
2. 服务层 (Service)
接口定义 (BszxPayService.java
):
/**
* 统计捐款总金额
*
* @return 捐款总金额
*/
BigDecimal total();
实现类 (BszxPayServiceImpl.java
):
@Override
public BigDecimal total() {
try {
// 使用数据库聚合查询统计捐款总金额,性能更高
LambdaQueryWrapper<BszxPay> wrapper = new LambdaQueryWrapper<>();
BigDecimal total = baseMapper.selectSumMoney(wrapper);
if (total == null) {
total = BigDecimal.ZERO;
}
return total;
} catch (Exception e) {
// 异常时返回0,确保接口稳定性
return BigDecimal.ZERO;
}
}
3. 数据访问层 (Mapper)
Mapper接口 (BszxPayMapper.java
):
BigDecimal selectSumMoney(@Param("ew") Wrapper<?> wrapper);
XML映射 (BszxPayMapper.xml
):
<!-- 统计金额总和 -->
<select id="selectSumMoney" resultType="java.math.BigDecimal">
SELECT COALESCE(SUM(price), 0) as total_money
FROM bszx_pay
<if test="ew != null">
${ew.customSqlSegment}
</if>
</select>
与ShopOrderController的对比
特性 | ShopOrderController | BszxOrderController |
---|---|---|
统计字段 | pay_price | price |
过滤条件 | pay_status = 1 AND deleted = 0 | 无特殊过滤 |
数据表 | shop_order | bszx_pay |
业务场景 | 商城已支付订单 | 百色中学捐款记录 |
异常处理 | ✓ | ✓ |
空值处理 | ✓ | ✓ |
统计规则
- 全量统计: 统计bszx_pay表中所有记录的price字段总和
- 空值处理: 使用COALESCE函数,当没有记录时返回0
- 异常处理: 包含完整的异常处理机制,确保接口稳定性
- 性能优化: 使用数据库聚合查询,在数据库层面进行计算
性能优化
- 数据库聚合: 使用SQL的SUM函数在数据库层面进行聚合计算
- 复用现有方法: 复用了已有的selectSumMoney方法,避免重复开发
- 异常处理: 包含完整的异常处理机制,确保接口稳定性
- 索引建议: 如果数据量大,建议在price字段上创建索引
测试用例
创建了测试类 BszxOrderTotalTest.java
用于验证功能:
@Test
void testBszxOrderTotal() {
BigDecimal total = bszxPayService.total();
assertNotNull(total, "百色中学订单总金额不应该为null");
assertTrue(total.compareTo(BigDecimal.ZERO) >= 0, "百色中学订单总金额应该大于等于0");
}
@Test
void testBszxOrderTotalPerformance() {
long startTime = System.currentTimeMillis();
BigDecimal total = bszxPayService.total();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
assertTrue(duration < 5000, "查询时间应该在5秒以内");
}
使用示例
前端调用示例
// 获取百色中学订单总金额
fetch('/api/bszx/bszx-order/total')
.then(response => response.json())
.then(data => {
if (data.code === 200) {
console.log('百色中学订单总金额:', data.data);
}
});
cURL调用示例
curl -X GET "http://localhost:8080/api/bszx/bszx-order/total" \
-H "Content-Type: application/json"
注意事项
- 数据精度: 使用BigDecimal确保金额计算的精度
- 并发安全: 查询操作是只读的,天然支持并发访问
- 业务逻辑: 与商城订单不同,百色中学捐款记录不需要过滤支付状态
- 扩展性: 可以通过传入不同的查询条件实现更复杂的统计需求
扩展功能建议
- 按时间范围统计: 支持指定时间范围的捐款金额统计
- 按项目统计: 支持按form_id进行分组统计
- 按用户统计: 支持统计不同用户的捐款总额
- 缓存机制: 对于大数据量场景,可以添加Redis缓存
- 权限控制: 根据业务需要可以添加相应的权限控制注解