|
@@ -27,8 +27,32 @@ export async function POST(request) {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ // 1. 查找并处理之前的积分历史记录
|
|
|
+ const previousPointHistories = await PointHistory.find({ match: matchId });
|
|
|
+ console.log(111, previousPointHistories);
|
|
|
+ for (const history of previousPointHistories) {
|
|
|
+ // 找到对应的用户并扣除之前加的分数
|
|
|
+ const user = await User.findById(history.user);
|
|
|
+ console.log(222, user);
|
|
|
+ if (user) {
|
|
|
+ user.points -= history.points; // 扣除之前加的分数
|
|
|
+ await user.save();
|
|
|
+ }
|
|
|
+ // 删除积分历史记录
|
|
|
+ await PointHistory.deleteOne({ _id: history._id });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 重置之前的预测结果
|
|
|
const predictions = await Prediction.find({ match: matchId });
|
|
|
+ for (const prediction of predictions) {
|
|
|
+ prediction.whoWillWinResult = null;
|
|
|
+ prediction.firstTeamToScoreResult = null;
|
|
|
+ prediction.totalGoalsResult = null;
|
|
|
+ prediction.pointsEarned = 0;
|
|
|
+ await prediction.save();
|
|
|
+ }
|
|
|
|
|
|
+ // 3. 重新处理预测结果和积分
|
|
|
for (const prediction of predictions) {
|
|
|
let pointsEarned = 0;
|
|
|
let reasons = [];
|
|
@@ -63,20 +87,16 @@ export async function POST(request) {
|
|
|
}
|
|
|
|
|
|
prediction.pointsEarned = pointsEarned;
|
|
|
- // prediction.isCorrect = pointsEarned > 0;
|
|
|
-
|
|
|
await prediction.save();
|
|
|
|
|
|
// 更新用户积分
|
|
|
if (pointsEarned > 0) {
|
|
|
const user = await User.findById(prediction.user);
|
|
|
if (user) {
|
|
|
- const oldPoints = user.points;
|
|
|
- const newPoints = oldPoints + pointsEarned;
|
|
|
- user.points = newPoints;
|
|
|
+ user.points += pointsEarned;
|
|
|
await user.save();
|
|
|
|
|
|
- // 创建积分历史记录
|
|
|
+ // 创建新的积分历史记录
|
|
|
const reason = `${match.homeTeam.name} vs ${
|
|
|
match.awayTeam.name
|
|
|
} 比赛预测: ${reasons.join(", ")}`;
|
|
@@ -92,7 +112,7 @@ export async function POST(request) {
|
|
|
}
|
|
|
|
|
|
const response = NextResponse.json(
|
|
|
- { success: true, message: "预测结果和用户积分已更新" },
|
|
|
+ { success: true, message: "预测结果和用户积分已重新计算更新" },
|
|
|
{ status: 200 }
|
|
|
);
|
|
|
|