|
|
|
|
package com.ruoyi.websocket.service;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 白板房间服务:按房间维度将白板数据存储于 Redis
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
public class WhiteboardRoomService {
|
|
|
|
|
|
|
|
|
|
private static final String ROOM_WHITEBOARDS_PREFIX = "room:";
|
|
|
|
|
private static final String ROOM_WHITEBOARDS_SUFFIX = ":whiteboards";
|
|
|
|
|
private static final int EXPIRE_DAYS = 60;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
@Qualifier("stringObjectRedisTemplate")
|
|
|
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
|
|
|
|
private String whiteboardsKey(Long roomId) {
|
|
|
|
|
return ROOM_WHITEBOARDS_PREFIX + roomId + ROOM_WHITEBOARDS_SUFFIX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 获取房间下所有白板列表 */
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public List<Object> listWhiteboards(Long roomId) {
|
|
|
|
|
if (roomId == null) return new ArrayList<>();
|
|
|
|
|
String key = whiteboardsKey(roomId);
|
|
|
|
|
Object raw = redisTemplate.opsForValue().get(key);
|
|
|
|
|
if (raw == null) return new ArrayList<>();
|
|
|
|
|
if (raw instanceof List) return (List<Object>) raw;
|
|
|
|
|
if (raw instanceof String) {
|
|
|
|
|
try {
|
|
|
|
|
return JSON.parseArray((String) raw);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 获取单个白板详情 */
|
|
|
|
|
public Object getWhiteboard(Long roomId, String whiteboardId) {
|
|
|
|
|
List<Object> list = listWhiteboards(roomId);
|
|
|
|
|
for (Object item : list) {
|
|
|
|
|
if (item instanceof java.util.Map) {
|
|
|
|
|
Object id = ((java.util.Map<?, ?>) item).get("id");
|
|
|
|
|
if (whiteboardId.equals(String.valueOf(id))) return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 创建白板 */
|
|
|
|
|
public Object createWhiteboard(Long roomId, Object whiteboard) {
|
|
|
|
|
if (roomId == null || whiteboard == null) return null;
|
|
|
|
|
List<Object> list = listWhiteboards(roomId);
|
|
|
|
|
String id = UUID.randomUUID().toString();
|
|
|
|
|
Object wb = whiteboard;
|
|
|
|
|
if (wb instanceof java.util.Map) {
|
|
|
|
|
((java.util.Map<String, Object>) wb).put("id", id);
|
|
|
|
|
if (!((java.util.Map<String, Object>) wb).containsKey("name")) {
|
|
|
|
|
((java.util.Map<String, Object>) wb).put("name", "草稿");
|
|
|
|
|
}
|
|
|
|
|
if (!((java.util.Map<String, Object>) wb).containsKey("timeBlocks")) {
|
|
|
|
|
((java.util.Map<String, Object>) wb).put("timeBlocks", new ArrayList<String>());
|
|
|
|
|
}
|
|
|
|
|
if (!((java.util.Map<String, Object>) wb).containsKey("contentByTime")) {
|
|
|
|
|
((java.util.Map<String, Object>) wb).put("contentByTime", new java.util.HashMap<String, Object>());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
list.add(wb);
|
|
|
|
|
saveWhiteboards(roomId, list);
|
|
|
|
|
return wb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 更新白板 */
|
|
|
|
|
public boolean updateWhiteboard(Long roomId, String whiteboardId, Object whiteboard) {
|
|
|
|
|
if (roomId == null || whiteboardId == null || whiteboard == null) return false;
|
|
|
|
|
List<Object> list = listWhiteboards(roomId);
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
Object item = list.get(i);
|
|
|
|
|
if (item instanceof java.util.Map) {
|
|
|
|
|
Object id = ((java.util.Map<?, ?>) item).get("id");
|
|
|
|
|
if (whiteboardId.equals(String.valueOf(id))) {
|
|
|
|
|
if (whiteboard instanceof java.util.Map) {
|
|
|
|
|
((java.util.Map<String, Object>) whiteboard).put("id", whiteboardId);
|
|
|
|
|
}
|
|
|
|
|
list.set(i, whiteboard);
|
|
|
|
|
saveWhiteboards(roomId, list);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 删除白板 */
|
|
|
|
|
public boolean deleteWhiteboard(Long roomId, String whiteboardId) {
|
|
|
|
|
if (roomId == null || whiteboardId == null) return false;
|
|
|
|
|
List<Object> list = listWhiteboards(roomId);
|
|
|
|
|
boolean removed = list.removeIf(item -> {
|
|
|
|
|
if (item instanceof java.util.Map) {
|
|
|
|
|
Object id = ((java.util.Map<?, ?>) item).get("id");
|
|
|
|
|
return whiteboardId.equals(String.valueOf(id));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
if (removed) saveWhiteboards(roomId, list);
|
|
|
|
|
return removed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveWhiteboards(Long roomId, List<Object> list) {
|
|
|
|
|
String key = whiteboardsKey(roomId);
|
|
|
|
|
redisTemplate.opsForValue().set(key, list, EXPIRE_DAYS, TimeUnit.DAYS);
|
|
|
|
|
}
|
|
|
|
|
}
|