peixue-dev/Archive/[一次性]积分功能修复完成-2026-01-31.md

4.5 KiB

积分功能修复完成 - 2026-01-31

问题描述

用户反馈积分页面显示问题:

  1. 累计获得显示为0 - 即使有积分记录,累计获得仍然显示0
  2. 积分明细显示正常 - 能看到"注册赠送"、"签到赠送"等记录

问题原因

根本原因

后端 PointsServiceImpl.javaaddPointsdeductPoints 方法使用了错误的字段:

错误代码:

// 使用了虚拟字段
userPoints.setTotalPoints(userPoints.getTotalPoints() + points);
userPoints.setAvailablePoints(userPoints.getAvailablePoints() + points);

数据库实际字段:

  • balance - 当前余额
  • totalEarned - 累计获得
  • totalSpent - 累计消费

虚拟字段(不存在于数据库):

  • totalPoints - 虚拟字段
  • availablePoints - 虚拟字段
  • usedPoints - 虚拟字段

问题影响

  1. 增加积分时,只更新了虚拟字段,数据库的 totalEarned 字段没有更新
  2. 前端读取 totalEarned 字段,所以显示为0
  3. 积分记录表 points_record 正常,所以明细显示正常

修复方案

1. 修改后端代码

修改 PointsServiceImpl.java 的以下方法:

initUserPoints - 初始化用户积分

// 修改前
userPoints.setTotalPoints(0);
userPoints.setAvailablePoints(0);
userPoints.setUsedPoints(0);

// 修改后
userPoints.setBalance(0);
userPoints.setTotalEarned(0);
userPoints.setTotalSpent(0);

addPoints - 增加积分

// 修改前
userPoints.setTotalPoints(userPoints.getTotalPoints() + points);
userPoints.setAvailablePoints(userPoints.getAvailablePoints() + points);

// 修改后
userPoints.setBalance(userPoints.getBalance() + points);
userPoints.setTotalEarned(userPoints.getTotalEarned() + points);

deductPoints - 扣减积分

// 修改前
userPoints.setAvailablePoints(userPoints.getAvailablePoints() - points);
userPoints.setUsedPoints(userPoints.getUsedPoints() + points);

// 修改后
userPoints.setBalance(userPoints.getBalance() - points);
userPoints.setTotalSpent(userPoints.getTotalSpent() + points);

getPointsStatistics - 获取积分统计

// 修改前
stats.put("totalPoints", userPoints.getTotalPoints());
stats.put("availablePoints", userPoints.getAvailablePoints());
stats.put("usedPoints", userPoints.getUsedPoints());

// 修改后
stats.put("totalPoints", userPoints.getTotalEarned());
stats.put("availablePoints", userPoints.getBalance());
stats.put("usedPoints", userPoints.getTotalSpent());

2. 修复已有数据

执行SQL脚本修复现有用户的积分统计数据:

-- 根据 points_record 表重新计算
UPDATE user_points up
SET totalEarned = (
    SELECT COALESCE(SUM(points), 0)
    FROM points_record pr
    WHERE pr.user_id = up.user_id AND pr.type = 'earn'
);

UPDATE user_points up
SET totalSpent = (
    SELECT COALESCE(SUM(ABS(points)), 0)
    FROM points_record pr
    WHERE pr.user_id = up.user_id AND pr.type IN ('consume', 'spend')
);

UPDATE user_points
SET balance = totalEarned - totalSpent;

执行步骤

1. 修复数据库数据

执行: Archive/[一次性]执行修复积分统计-2026-01-31.bat

2. 重启后端服务

执行: Archive/[一次性]重启后端服务-积分修复-2026-01-31.bat

3. 清除小程序缓存

执行: Archive/[一次性]清除缓存重新编译-积分修复-2026-01-31.bat

验证结果

修复后,积分页面应该正常显示:

  • 累计获得 - 显示正确的累计积分数
  • 累计使用 - 显示正确的消费积分数
  • 当前余额 - 显示正确的可用积分
  • 积分明细 - 继续正常显示

注意事项

  1. 字段映射关系:

    • 数据库 balance = 前端显示的"我的积分"
    • 数据库 totalEarned = 前端显示的"累计获得"
    • 数据库 totalSpent = 前端显示的"累计使用"
  2. 虚拟字段:

    • UserPoints 实体类中的虚拟字段仅用于兼容旧代码
    • 标记为 @TableField(exist = false),不会写入数据库
    • 新代码应该使用真实字段
  3. 数据一致性:

    • 修复脚本会根据 points_record 表重新计算统计数据
    • 确保 balance = totalEarned - totalSpent

修复文件清单

  • peidu/backend/src/main/java/com/peidu/service/impl/PointsServiceImpl.java - 后端代码修复
  • Archive/[一次性]修复积分统计数据-2026-01-31.sql - 数据修复脚本
  • Archive/[一次性]执行修复积分统计-2026-01-31.bat - 执行脚本
  • Archive/[一次性]重启后端服务-积分修复-2026-01-31.bat - 重启脚本

修复完成时间

2026-01-31