|
@@ -29,6 +29,7 @@ import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
@@ -58,19 +59,33 @@ public class GameLogServiceImpl extends ServiceImpl<GameLogMapper, GameLogEntity
|
|
|
Long roomId = param.getRoomId();
|
|
|
GameLogEntity entity;
|
|
|
Long userId = iBaseService.getUserId();
|
|
|
- // 模式, 活动:activity , 普通:general
|
|
|
- if (roomId == null) {
|
|
|
+ if (roomId == null) { // 普通模式
|
|
|
String type = "general";
|
|
|
entity = new GameLogEntity();
|
|
|
BeanUtils.copyProperties(param, entity);
|
|
|
entity.setType(type);
|
|
|
-
|
|
|
entity.setCreatorId(userId);
|
|
|
- } else {
|
|
|
+ this.save(entity);
|
|
|
+ } else { // 活动模式
|
|
|
+ // 检查房间活动是否结束
|
|
|
+ RoomEntity roomEntity = roomService.getById(roomId);
|
|
|
+ BaseRuntimeException.isNull(roomEntity, ErrorEnum.FAILURE_SYS_2001);
|
|
|
+ LocalDateTime endTime = roomEntity.getEndTime();
|
|
|
+ BaseRuntimeException.isTrue(endTime.isBefore(LocalDateTime.now()) , null, "活动已结束");
|
|
|
+
|
|
|
entity = this.getById(param.getGameLogId());
|
|
|
+ BaseRuntimeException.isNull(entity, ErrorEnum.FAILURE_SYS_2001);
|
|
|
+
|
|
|
+ Integer score = entity.getScore();
|
|
|
+ Integer pScore = param.getScore();
|
|
|
+ // 比较之前得分, 取最大值, DB大-> 不需要更新
|
|
|
+ if (score!=null && score > pScore){
|
|
|
+ log.info("数据库得分高, 不需要更新");
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
BeanUtils.copyProperties(param, entity);
|
|
|
+ this.updateById(entity);
|
|
|
}
|
|
|
- this.saveOrUpdate(entity);
|
|
|
|
|
|
// 更新积分等级
|
|
|
updateUserLevel(userId, roomId);
|
|
@@ -82,21 +97,20 @@ public class GameLogServiceImpl extends ServiceImpl<GameLogMapper, GameLogEntity
|
|
|
private void updateUserLevel(Long userId, Long roomId){
|
|
|
Integer totalScore = this.totalScore(userId, roomId);
|
|
|
int level = 0;
|
|
|
- if (totalScore < 500){
|
|
|
+ if (500 <= totalScore && totalScore < 1000){
|
|
|
level = 1;
|
|
|
- } else if (totalScore < 1000) {
|
|
|
+ } else if (1000 <= totalScore && totalScore < 2000) {
|
|
|
level = 2;
|
|
|
- } else if (totalScore < 2000) {
|
|
|
+ } else if (2000 <= totalScore && totalScore < 5000) {
|
|
|
level = 3;
|
|
|
- } else if (totalScore < 5000) {
|
|
|
+ } else if (totalScore >= 5000) {
|
|
|
level = 4;
|
|
|
}
|
|
|
WxUserEntity entity = wxUserService.getById(userId);
|
|
|
// 更新等级
|
|
|
if (!entity.getLevel().equals(level)){
|
|
|
- LambdaUpdateWrapper<WxUserEntity> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
- updateWrapper.set(WxUserEntity::getLevel, level);
|
|
|
- wxUserService.update(updateWrapper);
|
|
|
+ entity.setLevel(level);
|
|
|
+ wxUserService.updateById(entity);
|
|
|
log.info("更新等级完成:{}", level);
|
|
|
}
|
|
|
|
|
@@ -158,10 +172,10 @@ public class GameLogServiceImpl extends ServiceImpl<GameLogMapper, GameLogEntity
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
sql.append("select sum(score) from tb_game_log where is_delete=0 ");
|
|
|
if (userId != null){
|
|
|
- sql.append("and creator_id=").append(userId);
|
|
|
+ sql.append(" and creator_id=").append(userId);
|
|
|
}
|
|
|
if (roomId != null){
|
|
|
- sql.append("and room_id=").append(roomId);
|
|
|
+ sql.append(" and room_id=").append(roomId);
|
|
|
}
|
|
|
return getBaseMapper().sumSql(sql.toString());
|
|
|
}
|
|
@@ -182,4 +196,15 @@ public class GameLogServiceImpl extends ServiceImpl<GameLogMapper, GameLogEntity
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
+ public GameLogEntity findByRoomIdAndUserId(Long roomId, Long userId, String type){
|
|
|
+ LambdaQueryWrapper<GameLogEntity> wapper = new LambdaQueryWrapper<>();
|
|
|
+ wapper.eq(GameLogEntity::getRoomId, roomId);
|
|
|
+ wapper.eq(GameLogEntity::getCreatorId, userId);
|
|
|
+ wapper.eq(GameLogEntity::getType, type);
|
|
|
+ return this.getOne(wapper);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|