|
@@ -1,24 +1,30 @@
|
|
|
package com.gis.cms.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.gis.cms.entity.dto.RoomDto;
|
|
|
import com.gis.cms.entity.dto.RoomListDto;
|
|
|
import com.gis.cms.entity.po.GameLogEntity;
|
|
|
import com.gis.cms.entity.po.RoomEntity;
|
|
|
+import com.gis.cms.entity.vo.RoomVo;
|
|
|
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.constant.ErrorEnum;
|
|
|
import com.gis.common.util.BaseUtil;
|
|
|
import com.gis.common.util.RedisUtil;
|
|
|
import com.gis.common.util.Result;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -26,10 +32,13 @@ import org.springframework.stereotype.Service;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -70,9 +79,12 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
Long userId = iBaseService.getUserId();
|
|
|
entity.setCreatorId(userId);
|
|
|
|
|
|
- //检查房间号唯一性
|
|
|
- RoomEntity room = baseMapper.findByCode(param.getCode(), userId);
|
|
|
- BaseRuntimeException.isTrue(room != null, null, "房间口令已存在");
|
|
|
+ //检查房间号唯一性, 允许为空
|
|
|
+ String code = param.getCode();
|
|
|
+ if (StringUtils.isNotBlank(code)){
|
|
|
+ RoomEntity room = baseMapper.findByCode(param.getCode(), userId);
|
|
|
+ BaseRuntimeException.isTrue(room != null, null, "房间口令已存在");
|
|
|
+ }
|
|
|
|
|
|
this.save(entity);
|
|
|
return Result.success(entity);
|
|
@@ -80,18 +92,55 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
|
|
|
|
|
|
|
|
|
+// @Override
|
|
|
+// public Result join(String code, Long creatorId) {
|
|
|
+// RoomEntity roomEntity = baseMapper.findByCode(code, creatorId);
|
|
|
+// if (roomEntity==null){
|
|
|
+// log.error("该房间号不存在: {}", code);
|
|
|
+// return Result.failure("该房间不存在: " + code );
|
|
|
+// }
|
|
|
+//
|
|
|
+// LocalDateTime endTime = roomEntity.getEndTime();
|
|
|
+// BaseRuntimeException.isTrue(endTime.isBefore(LocalDateTime.now()) , null, "活动已结束");
|
|
|
+//
|
|
|
+// Long roomId = roomEntity.getId();
|
|
|
+// Long userId = iBaseService.getUserId();
|
|
|
+// String type = "activity";
|
|
|
+// // 加入房间记录
|
|
|
+// GameLogEntity entity = gameLogService.findByRoomIdAndUserId(roomId, userId, type);
|
|
|
+// if (entity == null){
|
|
|
+// log.info("需要创建该房间的游戏记录");
|
|
|
+// // 用户加入房间
|
|
|
+// entity = new GameLogEntity();
|
|
|
+// entity.setCreatorId(userId);
|
|
|
+// entity.setType(type);
|
|
|
+// entity.setScore(0);
|
|
|
+// entity.setRoomId(roomId);
|
|
|
+// gameLogService.save(entity);
|
|
|
+// }
|
|
|
+//
|
|
|
+// return Result.success(entity);
|
|
|
+// }
|
|
|
+
|
|
|
@Override
|
|
|
- public Result join(String code, Long creatorId) {
|
|
|
- RoomEntity roomEntity = baseMapper.findByCode(code, creatorId);
|
|
|
- if (roomEntity==null){
|
|
|
- log.error("该房间号不存在: {}", code);
|
|
|
- return Result.failure("该房间不存在: " + code );
|
|
|
- }
|
|
|
+ public Result join(String code, Long roomId) {
|
|
|
+// RoomEntity roomEntity = baseMapper.findByCode(code, creatorId);
|
|
|
+// if (roomEntity==null){
|
|
|
+// log.error("该房间号不存在: {}", code);
|
|
|
+// return Result.failure("该房间不存在: " + code );
|
|
|
+// }
|
|
|
+
|
|
|
+ RoomEntity roomEntity = this.getById(roomId);
|
|
|
+ BaseRuntimeException.isNull(roomEntity, ErrorEnum.FAILURE_SYS_2001);
|
|
|
|
|
|
LocalDateTime endTime = roomEntity.getEndTime();
|
|
|
BaseRuntimeException.isTrue(endTime.isBefore(LocalDateTime.now()) , null, "活动已结束");
|
|
|
|
|
|
- Long roomId = roomEntity.getId();
|
|
|
+ // 验证口令
|
|
|
+ if (StringUtils.isNotBlank(code)){
|
|
|
+ BaseRuntimeException.isTrue(!StringUtils.equals(code, roomEntity.getCode()), null, "口令有误");
|
|
|
+ }
|
|
|
+
|
|
|
Long userId = iBaseService.getUserId();
|
|
|
String type = "activity";
|
|
|
// 加入房间记录
|
|
@@ -118,7 +167,57 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
// 每次请求接口前更新房间状态,使用redis, 缓存30分钟更新一次房间状态
|
|
|
checkRoomStatus();
|
|
|
// 获取用户房间列表
|
|
|
- return Result.success(countByCreatorIdAndDate(param));
|
|
|
+ List<RoomEntity> rooms = countByCreatorIdAndDate(param);
|
|
|
+ // 获取房间用户头像
|
|
|
+ handleAvatarUrlByRoom(rooms);
|
|
|
+
|
|
|
+ return Result.success(rooms);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 封装房间头像
|
|
|
+ * @param rooms
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private void handleAvatarUrlByRoom(List<RoomEntity> rooms) {
|
|
|
+ // 房间ids
|
|
|
+ List<Long> collect = rooms.stream().map(RoomEntity::getId).collect(Collectors.toList());
|
|
|
+ List<RoomVo> avatars = getAvatarUrlByRoomId(collect);
|
|
|
+ // 按房间分组
|
|
|
+ Map<Long, List<RoomVo>> roomMap = avatars.stream().collect(Collectors.groupingBy(RoomVo::getRoomId));
|
|
|
+ // 同一房间下的头像
|
|
|
+ List<String> roomImg;
|
|
|
+ for (RoomEntity room : rooms) {
|
|
|
+ roomImg = new ArrayList<>();
|
|
|
+ List<RoomVo> roomVos = roomMap.get(room.getId());
|
|
|
+ if (CollectionUtil.isNotEmpty(roomVos)){
|
|
|
+ for (RoomVo roomVo : roomVos) {
|
|
|
+ roomImg.add(roomVo.getAvatarUrl());
|
|
|
+ }
|
|
|
+ // 更新头像组合
|
|
|
+ room.setAvatarUrls(roomImg);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 获取该房间的用户头像
|
|
|
+ * @param roomIds
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<RoomVo> getAvatarUrlByRoomId(List<Long> roomIds){
|
|
|
+
|
|
|
+ System.out.println("11111: " + roomIds.toArray());
|
|
|
+ String roomIdsStr = CollectionUtil.join(roomIds, ",");
|
|
|
+ StringBuffer sql = new StringBuffer();
|
|
|
+ sql.append("select b.avatar_url, a.room_id, b.id as userId from tb_game_log a left join wx_user b on b.id=a.creator_id where a.is_delete=0 and a.type='activity' ");
|
|
|
+ sql.append(" and a.room_id in (").append(roomIdsStr).append(")");
|
|
|
+ // 该房间的用户头像
|
|
|
+ List<RoomVo> avatarUrls = baseMapper.selectVoList(sql.toString());
|
|
|
+ return avatarUrls;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -163,6 +262,9 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
private List<RoomEntity> countByCreatorIdAndDate(RoomListDto param){
|
|
|
Long creatorId = iBaseService.getUserId();
|
|
|
StringBuffer sql = new StringBuffer();
|
|
@@ -170,7 +272,9 @@ public class RoomServiceImpl extends ServiceImpl<RoomMapper, RoomEntity> impleme
|
|
|
|
|
|
sql.append("(select *, IF(end_time < NOW(),1,0) statusVo from tb_room where is_delete=0");
|
|
|
if (param.getIsClose() == 1){
|
|
|
- sql.append(" and status=1 ");
|
|
|
+ sql.append(" and status=1");
|
|
|
+ } else {
|
|
|
+ sql.append(" and status=0");
|
|
|
}
|
|
|
|
|
|
if (param.getIsJoin() == 1){
|