|
@@ -1,7 +1,7 @@
|
|
|
package com.gis.cms.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.gis.cms.entity.dto.RoomDto;
|
|
@@ -12,7 +12,9 @@ import com.gis.cms.entity.vo.WxUserVo;
|
|
|
import com.gis.cms.mapper.RoomMapper;
|
|
|
import com.gis.cms.service.GameLogService;
|
|
|
import com.gis.cms.service.RoomService;
|
|
|
+import com.gis.common.base.exception.BaseRuntimeException;
|
|
|
import com.gis.common.base.service.IBaseService;
|
|
|
+import com.gis.common.util.BaseUtil;
|
|
|
import com.gis.common.util.RedisUtil;
|
|
|
import com.gis.common.util.Result;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -22,9 +24,11 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -37,7 +41,7 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
- IBaseService baseUtil;
|
|
|
+ IBaseService iBaseService;
|
|
|
|
|
|
@Autowired
|
|
|
RedisUtil redisUtil;
|
|
@@ -47,6 +51,10 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
|
|
|
|
|
|
|
|
|
+ // 房间序号, 当大于99时从1开始
|
|
|
+ private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
|
|
|
+
|
|
|
+
|
|
|
/**redis 房间key*/
|
|
|
final static String ROOM_STATUS_KEY = "room:status";
|
|
|
|
|
@@ -56,27 +64,54 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
RoomEntity entity = new RoomEntity();
|
|
|
BeanUtils.copyProperties(param, entity);
|
|
|
entity.setStatus(0); // 新建默认进行中, 0:进行中
|
|
|
- entity.setCreatorId(baseUtil.getUserId());
|
|
|
+ entity.setName(getAtomicNum());
|
|
|
+ entity.setEndTime(LocalDateTime.parse(param.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
|
|
|
+ Long userId = iBaseService.getUserId();
|
|
|
+ entity.setCreatorId(userId);
|
|
|
+
|
|
|
+ //检查房间号唯一性
|
|
|
+ RoomEntity room = baseMapper.findByCode(param.getCode(), userId);
|
|
|
+ BaseRuntimeException.isTrue(room != null, null, "房间口令已存在");
|
|
|
+
|
|
|
+ this.save(entity);
|
|
|
+ return Result.success(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result saveEntity1(RoomDto param) {
|
|
|
+ RoomEntity entity = new RoomEntity();
|
|
|
+ BeanUtils.copyProperties(param, entity);
|
|
|
+ entity.setStatus(0); // 新建默认进行中, 0:进行中
|
|
|
+ entity.setName(getAtomicNum());
|
|
|
+ entity.setEndTime(LocalDateTime.parse(param.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
|
|
|
+ Long userId = iBaseService.getUserId();
|
|
|
+ entity.setCreatorId(userId);
|
|
|
+
|
|
|
+ //检查房间号唯一性
|
|
|
+ RoomEntity room = baseMapper.findByCode(param.getCode(), userId);
|
|
|
+ BaseRuntimeException.isTrue(room != null, null, "房间口令已存在");
|
|
|
+
|
|
|
this.save(entity);
|
|
|
- return Result.success();
|
|
|
+ return Result.success(entity);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Result join(String code, Long creatorId) {
|
|
|
- RoomEntity roomEntity = findByCode(code, creatorId);
|
|
|
+ RoomEntity roomEntity = baseMapper.findByCode(code, creatorId);
|
|
|
if (roomEntity==null){
|
|
|
log.error("该房间号不存在: {}", code);
|
|
|
return Result.failure("该房间不存在: " + code );
|
|
|
}
|
|
|
|
|
|
-// // 加入房间记录
|
|
|
-// GameLogEntity entity = new GameLogEntity();
|
|
|
-// entity.setRoomId(roomEntity.getId());
|
|
|
-// entity.setType("activity");
|
|
|
-// entity.setUserId(baseUtil.getUserId());
|
|
|
-// // 需要知道谁创建
|
|
|
-// entity.setCreatorId(creatorId);
|
|
|
-// gameLogService.save(entity);
|
|
|
+ // 加入房间记录
|
|
|
+ GameLogEntity entity = new GameLogEntity();
|
|
|
+ entity.setRoomId(roomEntity.getId());
|
|
|
+ entity.setType("activity");
|
|
|
+ // 用户加入房间
|
|
|
+ entity.setCreatorId(iBaseService.getUserId());
|
|
|
+ entity.setScore(0);
|
|
|
+ gameLogService.save(entity);
|
|
|
return Result.success(roomEntity);
|
|
|
}
|
|
|
|
|
@@ -105,7 +140,7 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
@Override
|
|
|
public List<WxUserVo> ranking(Long roomId) {
|
|
|
StringBuffer sql = new StringBuffer();
|
|
|
- sql.append("select a.score, a.creator_id ,a.time, b.nick_name from tb_game_log a left join wx_user b on b.id=a.creator_id");
|
|
|
+ sql.append("select a.score, a.creator_id ,a.time, b.nick_name,b.id from tb_game_log a left join wx_user b on b.id=a.creator_id");
|
|
|
sql.append(" where a.is_delete=0 and a.type='activity' ");
|
|
|
|
|
|
if (roomId != null){
|
|
@@ -129,22 +164,9 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
}
|
|
|
|
|
|
|
|
|
-// private List<RoomEntity> countByCreatorIdAndDate(RoomListDto param){
|
|
|
-// Long creatorId = baseUtil.getUserId();
|
|
|
-// StringBuffer sql = new StringBuffer();
|
|
|
-// sql.append("select a.id, a.create_time, a.name, a.creator_id, a.end_time, a.code, b.count, IF(end_time < NOW(),0,1) as status from tb_room a LEFT JOIN ");
|
|
|
-// sql.append(StrUtil.format("(select COUNT(id) as count, room_id from tb_game_log WHERE is_delete=0 " +
|
|
|
-// "and type='activity' and creator_id={} GROUP BY room_id) b ", creatorId));
|
|
|
-// sql.append(StrUtil.format("on b.room_id=a.id WHERE a.is_delete=0 and a.creator_id={} ", creatorId));
|
|
|
-// // 格式化日期需要使用yyyy-mm-dd , 没有日会异常
|
|
|
-// sql.append(StrUtil.format(" and a.create_time > {}", beforeDate(param.getDate())));
|
|
|
-// sql.append(" ORDER BY a.create_time desc");
|
|
|
-//
|
|
|
-// return getBaseMapper().selectEntitys(sql.toString());
|
|
|
-// }
|
|
|
|
|
|
private List<RoomEntity> countByCreatorIdAndDate(RoomListDto param){
|
|
|
- Long creatorId = baseUtil.getUserId();
|
|
|
+ Long creatorId = iBaseService.getUserId();
|
|
|
StringBuffer sql = new StringBuffer();
|
|
|
sql.append("select a.*,b.count from ");
|
|
|
|
|
@@ -167,13 +189,6 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
return getBaseMapper().selectList(sql.toString());
|
|
|
}
|
|
|
|
|
|
- private RoomEntity findByCode(String code, Long creatorId){
|
|
|
- LambdaQueryWrapper<RoomEntity> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(RoomEntity::getCode, code);
|
|
|
- wrapper.eq(RoomEntity::getCreatorId, creatorId);
|
|
|
- return getBaseMapper().selectOne(wrapper);
|
|
|
-
|
|
|
- }
|
|
|
|
|
|
// 获取最近时间日期
|
|
|
private LocalDate beforeDate(Integer num){
|
|
@@ -208,4 +223,37 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
this.update(ew);
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ // 房间id, 规则年月日-数字
|
|
|
+ private String getAtomicNum(){
|
|
|
+ int num = ATOMIC_INTEGER.incrementAndGet();
|
|
|
+ if (num>=9999){
|
|
|
+ ATOMIC_INTEGER.set(0);
|
|
|
+ num = ATOMIC_INTEGER.incrementAndGet();
|
|
|
+ }
|
|
|
+ String yyyymmdd = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
|
+ String code = yyyymmdd + "-" + num;
|
|
|
+ // 检查房间id是否重复, 从100
|
|
|
+ if (num == 1){
|
|
|
+ Integer id = baseMapper.findByName(code);
|
|
|
+ if (id != null){
|
|
|
+ num = 100;
|
|
|
+ while (true){
|
|
|
+ num = num + 10;
|
|
|
+ code = yyyymmdd + "-" + num;
|
|
|
+ id = baseMapper.findByName(code);
|
|
|
+ if (id == null){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ATOMIC_INTEGER.set(num);
|
|
|
+ log.info("重置ATOMIC num: {}", num);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return yyyymmdd + "-" + num;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|