更新用户进度

This commit is contained in:
石皮幼鸟 2024-04-14 15:33:52 +08:00
parent 432d0581b7
commit 1b61e9acf6
2 changed files with 19 additions and 3 deletions

View File

@ -11,6 +11,10 @@ import java.io.Serializable;
*/ */
@Data @Data
public class ScoreInfo implements Serializable { public class ScoreInfo implements Serializable {
/**
* ID
*/
private String id;
/** /**
* 用户ID * 用户ID
*/ */

View File

@ -60,12 +60,24 @@ public class LevelServiceImpl implements LevelService {
if (levelRes.getCode() == R.FAIL) { if (levelRes.getCode() == R.FAIL) {
return R.fail("查询关卡失败,请重试"); return R.fail("查询关卡失败,请重试");
} }
if (levelRes.getData().getOrder() > user.getProgress()) { if (user.getProgress() == null || levelRes.getData().getOrder() > user.getProgress()) {
user.setProgress(levelRes.getData().getOrder()); user.setProgress(levelRes.getData().getOrder());
authService.update(user, token); // 这里要直接存因为authService的update方法没有修改progress字段
mongoTemplate.save(user);
} }
ScoreInfo insert = mongoTemplate.insert(scoreInfo); // 先获取数据库中的分数如果有则比较如果没有则直接插入
var score = mongoTemplate.findOne(new Query(Criteria.where("userId").is(scoreInfo.getUserId()).and("levelId").is(scoreInfo.getLevelId())), ScoreInfo.class);
ScoreInfo insert = new ScoreInfo();
if (score != null) {
if (score.getScore() < scoreInfo.getScore()) {
score.setScore(scoreInfo.getScore());
score.setTime(scoreInfo.getTime());
insert = mongoTemplate.save(score);
}
} else {
insert = mongoTemplate.save(scoreInfo);
}
return R.ok(insert); return R.ok(insert);
} }