12-04-人员定位-终版-优化版
This commit is contained in:
@ -60,6 +60,8 @@ public class RydwWebSocketServer {
|
|||||||
log.info("✅ 人员定位WebSocket 服务端已随项目启动初始化!端点路径:/websocket/rydw");
|
log.info("✅ 人员定位WebSocket 服务端已随项目启动初始化!端点路径:/websocket/rydw");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final String[] TIME_SLOTS = {"06:00", "08:00", "10:00", "12:00", "14:00", "16:00", "18:00"};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 客户端连接时触发(无需手动启动,有客户端连接时自动调用)
|
* 客户端连接时触发(无需手动启动,有客户端连接时自动调用)
|
||||||
*/
|
*/
|
||||||
@ -329,124 +331,34 @@ public class RydwWebSocketServer {
|
|||||||
/**
|
/**
|
||||||
* 柱状图数据初始化
|
* 柱状图数据初始化
|
||||||
*/
|
*/
|
||||||
public void zztcsh (List<Map<String, Map>> allValue,List<BusProjectPunchrange> busProjectPunchranges,List<SubConstructionUser> persons,long expireSeconds){
|
public void zztcsh(List<Map<String, Map>> allValue, List<BusProjectPunchrange> busProjectPunchranges,
|
||||||
|
List<SubConstructionUser> persons, long expireSeconds) {
|
||||||
for (BusProjectPunchrange busProjectPunchrange : busProjectPunchranges) {
|
for (BusProjectPunchrange busProjectPunchrange : busProjectPunchranges) {
|
||||||
Map<String,Object> map2 = new HashMap<>();
|
Map<String, Object> map2 = new HashMap<>();
|
||||||
|
|
||||||
boolean existsObject = RedisUtils.isExistsObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":06:00");
|
for (String timeSlot : TIME_SLOTS) {
|
||||||
if (!existsObject) {
|
String redisKey = busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":" + timeSlot;
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":06:00", 0, Duration.ofSeconds(expireSeconds));
|
boolean existsObject = RedisUtils.isExistsObject(redisKey);
|
||||||
map2.put("06:00",0);
|
|
||||||
}else {
|
if (!existsObject) {
|
||||||
//如果存在 循环找到打卡的人
|
RedisUtils.setCacheObject(redisKey, 0, Duration.ofSeconds(expireSeconds));
|
||||||
Integer i = 0;
|
map2.put(timeSlot, 0);
|
||||||
for (SubConstructionUser person : persons) {
|
} else {
|
||||||
//这个人 在6点 是否活跃过 "rydw_tj_"+busProjectPunchrange.getId() + "_" + "06:00" + "_" + constructionUser.getSysUserId()
|
int count = 0;
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "06:00" + "_" + person.getSysUserId());
|
String timeSlotKey = "rydw_tj_" + busProjectPunchrange.getId() + "_" + timeSlot + "_";
|
||||||
if ( b){
|
int timeSlotKeyLength = timeSlotKey.length();
|
||||||
i++;
|
|
||||||
|
for (SubConstructionUser person : persons) {
|
||||||
|
if (RedisUtils.isExistsObject(timeSlotKey + person.getSysUserId())) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
map2.put(timeSlot, count);
|
||||||
}
|
}
|
||||||
map2.put("06:00",i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
existsObject = RedisUtils.isExistsObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":08:00");
|
Map<String, Map> map1 = new HashMap<>();
|
||||||
if (!existsObject) {
|
map1.put(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName(), map2);
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":08:00", 0, Duration.ofSeconds(expireSeconds));
|
|
||||||
map2.put("08:00",0);
|
|
||||||
}else {
|
|
||||||
//如果存在 循环找到打卡的人
|
|
||||||
Integer i = 0;
|
|
||||||
for (SubConstructionUser person : persons) {
|
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "08:00" + "_" + person.getSysUserId());
|
|
||||||
if ( b){
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map2.put("08:00",i);
|
|
||||||
}
|
|
||||||
|
|
||||||
existsObject = RedisUtils.isExistsObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":10:00");
|
|
||||||
if (!existsObject) {
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":10:00", 0, Duration.ofSeconds(expireSeconds));
|
|
||||||
map2.put("10:00",0);
|
|
||||||
}else {
|
|
||||||
//如果存在 循环找到打卡的人
|
|
||||||
Integer i = 0;
|
|
||||||
for (SubConstructionUser person : persons) {
|
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "10:00" + "_" + person.getSysUserId());
|
|
||||||
if ( b){
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map2.put("10:00",i);
|
|
||||||
}
|
|
||||||
|
|
||||||
existsObject = RedisUtils.isExistsObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":12:00");
|
|
||||||
if (!existsObject) {
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":12:00", 0, Duration.ofSeconds(expireSeconds));
|
|
||||||
map2.put("12:00",0);
|
|
||||||
}else {
|
|
||||||
//如果存在 循环找到打卡的人
|
|
||||||
Integer i = 0;
|
|
||||||
for (SubConstructionUser person : persons) {
|
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "12:00" + "_" + person.getSysUserId());
|
|
||||||
if ( b){
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map2.put("12:00",i);
|
|
||||||
}
|
|
||||||
|
|
||||||
existsObject = RedisUtils.isExistsObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":14:00");
|
|
||||||
if (!existsObject) {
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":14:00", 0, Duration.ofSeconds(expireSeconds));
|
|
||||||
map2.put("14:00",0);
|
|
||||||
}else {
|
|
||||||
//如果存在 循环找到打卡的人
|
|
||||||
Integer i = 0;
|
|
||||||
for (SubConstructionUser person : persons) {
|
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "14:00" + "_" + person.getSysUserId());
|
|
||||||
if ( b){
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map2.put("14:00",i);
|
|
||||||
}
|
|
||||||
|
|
||||||
existsObject = RedisUtils.isExistsObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":16:00");
|
|
||||||
if (!existsObject) {
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":16:00", 0, Duration.ofSeconds(expireSeconds));
|
|
||||||
map2.put("16:00",0);
|
|
||||||
}else {
|
|
||||||
//如果存在 循环找到打卡的人
|
|
||||||
Integer i = 0;
|
|
||||||
for (SubConstructionUser person : persons) {
|
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "16:00" + "_" + person.getSysUserId());
|
|
||||||
if ( b){
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map2.put("16:00",i);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!existsObject) {
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":18:00", 0, Duration.ofSeconds(expireSeconds));
|
|
||||||
map2.put("18:00",0);
|
|
||||||
}else {
|
|
||||||
//如果存在 循环找到打卡的人
|
|
||||||
Integer i = 0;
|
|
||||||
for (SubConstructionUser person : persons) {
|
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "18:00" + "_" + person.getSysUserId());
|
|
||||||
if ( b){
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
map2.put("18:00",i);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String,Map> map1 = new HashMap<>();
|
|
||||||
map1.put(busProjectPunchrange.getId()+":"+busProjectPunchrange.getPunchName(),map2);
|
|
||||||
allValue.add(map1);
|
allValue.add(map1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -454,100 +366,53 @@ public class RydwWebSocketServer {
|
|||||||
/**
|
/**
|
||||||
* 柱状图 数据
|
* 柱状图 数据
|
||||||
*/
|
*/
|
||||||
public void setZztData(String key,List<Map<String, Map>> allValue,GpsEquipmentSon one,BusProjectPunchrange busProjectPunchrange,SubConstructionUser constructionUser,long expireSeconds){
|
public void setZztData(String key, List<Map<String, Map>> allValue, GpsEquipmentSon one,
|
||||||
|
BusProjectPunchrange busProjectPunchrange, SubConstructionUser constructionUser, long expireSeconds) {
|
||||||
|
|
||||||
|
// 先找到对应的map,避免在循环内部多次判断
|
||||||
|
Map<String, Object> timeSlotMap = null;
|
||||||
for (Map<String, Map> mapMap : allValue) {
|
for (Map<String, Map> mapMap : allValue) {
|
||||||
//这个map为 id+名字 : 时间段 MAP
|
if (mapMap.containsKey(key)) {
|
||||||
if (mapMap.containsKey(key)){
|
timeSlotMap = mapMap.get(key);
|
||||||
//找到了时间段map
|
break;
|
||||||
LocalDateTime dateTime = one.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
}
|
||||||
// 获取小时数
|
}
|
||||||
int hour = dateTime.getHour();
|
|
||||||
// 根据小时数判断所属区间并增加计数 一个人在一个时间段只能增加一次 设计一个新的redis存储 Key = 范围ID + 时间段 + 用户ID value = 0(未统计)1(已统计) 过期时间 = 今天为止
|
|
||||||
if (hour >= 6 && hour < 8) {
|
|
||||||
//先从redis判断此人在该时间段是否已经活动过
|
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "06:00" + "_" + constructionUser.getSysUserId());
|
|
||||||
//如果不存在
|
|
||||||
if (!b) {
|
|
||||||
mapMap.get(key).put("06:00", (int) mapMap.get(key).get("06:00") + 1);
|
|
||||||
//将redis中该时间段人数+1 map中已经加过
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":06:00", (int) mapMap.get(key).get("06:00"), Duration.ofSeconds(expireSeconds));
|
|
||||||
//判断数据添加到redis
|
|
||||||
RedisUtils.setCacheObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "06:00" + "_" + constructionUser.getSysUserId(), 1, Duration.ofSeconds(expireSeconds));
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (hour >= 8 && hour < 10) {
|
if (timeSlotMap == null) return;
|
||||||
//先从redis判断此人在该时间段是否已经活动过
|
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "08:00" + "_" + constructionUser.getSysUserId());
|
|
||||||
//如果不存在
|
|
||||||
if (!b) {
|
|
||||||
mapMap.get(key).put("08:00", (int) mapMap.get(key).get("08:00") + 1);
|
|
||||||
//将redis中该时间段人数+1
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":08:00", (int) mapMap.get(key).get("08:00"), Duration.ofSeconds(expireSeconds));
|
|
||||||
//判断数据添加到redis
|
|
||||||
RedisUtils.setCacheObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "08:00" + "_" + constructionUser.getSysUserId(), 1, Duration.ofSeconds(expireSeconds));
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (hour >= 10 && hour < 12) {
|
LocalDateTime dateTime = one.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||||
//先从redis判断此人在该时间段是否已经活动过
|
int hour = dateTime.getHour();
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "10:00" + "_" + constructionUser.getSysUserId());
|
|
||||||
//如果不存在
|
|
||||||
if (!b) {
|
|
||||||
mapMap.get(key).put("10:00", (int) mapMap.get(key).get("10:00") + 1);
|
|
||||||
//将redis中该时间段人数+1
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":10:00", (int) mapMap.get(key).get("10:00"), Duration.ofSeconds(expireSeconds));
|
|
||||||
//判断数据添加到redis
|
|
||||||
RedisUtils.setCacheObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "10:00" + "_" + constructionUser.getSysUserId(), 1, Duration.ofSeconds(expireSeconds));
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (hour >= 12 && hour < 14) {
|
// 使用映射表减少if-else判断
|
||||||
//先从redis判断此人在该时间段是否已经活动过
|
Map<Integer, String> hourToTimeSlot = new HashMap<>();
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "12:00" + "_" + constructionUser.getSysUserId());
|
hourToTimeSlot.put(6, "06:00");
|
||||||
//如果不存在
|
hourToTimeSlot.put(8, "08:00");
|
||||||
if (!b) {
|
hourToTimeSlot.put(10, "10:00");
|
||||||
mapMap.get(key).put("12:00", (int) mapMap.get(key).get("12:00") + 1);
|
hourToTimeSlot.put(12, "12:00");
|
||||||
//将redis中该时间段人数+1
|
hourToTimeSlot.put(14, "14:00");
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":12:00", (int) mapMap.get(key).get("12:00"), Duration.ofSeconds(expireSeconds));
|
hourToTimeSlot.put(16, "16:00");
|
||||||
//判断数据添加到redis
|
hourToTimeSlot.put(18, "18:00");
|
||||||
RedisUtils.setCacheObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "12:00" + "_" + constructionUser.getSysUserId(), 1, Duration.ofSeconds(expireSeconds));
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (hour >= 14 && hour < 16) {
|
for (Map.Entry<Integer, String> entry : hourToTimeSlot.entrySet()) {
|
||||||
//先从redis判断此人在该时间段是否已经活动过
|
int startHour = entry.getKey();
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "14:00" + "_" + constructionUser.getSysUserId());
|
if (hour >= startHour && hour < startHour + 2) {
|
||||||
//如果不存在
|
String timeSlot = entry.getValue();
|
||||||
if (!b) {
|
String redisCheckKey = "rydw_tj_" + busProjectPunchrange.getId() + "_" + timeSlot + "_" + constructionUser.getSysUserId();
|
||||||
mapMap.get(key).put("14:00", (int) mapMap.get(key).get("14:00") + 1);
|
|
||||||
//将redis中该时间段人数+1
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":14:00", (int) mapMap.get(key).get("14:00"), Duration.ofSeconds(expireSeconds));
|
|
||||||
//判断数据添加到redis
|
|
||||||
RedisUtils.setCacheObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "14:00" + "_" + constructionUser.getSysUserId(), 1, Duration.ofSeconds(expireSeconds));
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (hour >= 16 && hour < 18) {
|
if (!RedisUtils.isExistsObject(redisCheckKey)) {
|
||||||
//先从redis判断此人在该时间段是否已经活动过
|
// 原子性更新,避免并发问题
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "16:00" + "_" + constructionUser.getSysUserId());
|
synchronized (this) {
|
||||||
//如果不存在
|
if (!RedisUtils.isExistsObject(redisCheckKey)) {
|
||||||
if (!b) {
|
int currentCount = (int) timeSlotMap.get(timeSlot);
|
||||||
mapMap.get(key).put("16:00", (int) mapMap.get(key).get("16:00") + 1);
|
timeSlotMap.put(timeSlot, currentCount + 1);
|
||||||
//将redis中该时间段人数+1
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":16:00", (int) mapMap.get(key).get("16:00"), Duration.ofSeconds(expireSeconds));
|
|
||||||
//判断数据添加到redis
|
|
||||||
RedisUtils.setCacheObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "16:00" + "_" + constructionUser.getSysUserId(), 1, Duration.ofSeconds(expireSeconds));
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (hour >= 18 && hour < 20) {
|
String redisCountKey = busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":" + timeSlot;
|
||||||
//先从redis判断此人在该时间段是否已经活动过
|
RedisUtils.setCacheObject(redisCountKey, currentCount + 1, Duration.ofSeconds(expireSeconds));
|
||||||
boolean b = RedisUtils.isExistsObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "18:00" + "_" + constructionUser.getSysUserId());
|
RedisUtils.setCacheObject(redisCheckKey, 1, Duration.ofSeconds(expireSeconds));
|
||||||
//如果不存在
|
}
|
||||||
if (!b) {
|
|
||||||
mapMap.get(key).put("18:00", (int) mapMap.get(key).get("18:00") + 1);
|
|
||||||
//将redis中该时间段人数+1
|
|
||||||
RedisUtils.setCacheObject(busProjectPunchrange.getId() + ":" + busProjectPunchrange.getPunchName() + ":18:00", (int) mapMap.get(key).get("18:00"), Duration.ofSeconds(expireSeconds));
|
|
||||||
//判断数据添加到redis
|
|
||||||
RedisUtils.setCacheObject("rydw_tj_"+busProjectPunchrange.getId() + "_" + "18:00" + "_" + constructionUser.getSysUserId(), 1, Duration.ofSeconds(expireSeconds));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
break; // 找到对应时段后退出循环
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -555,62 +420,66 @@ public class RydwWebSocketServer {
|
|||||||
/**
|
/**
|
||||||
* 统计饼状图 数据
|
* 统计饼状图 数据
|
||||||
*/
|
*/
|
||||||
public List<Map.Entry<String, Integer>> getPieData(List<Map<String, Map>> value,List<SubConstructionUser> persons,List<BusProjectPunchrange> dkfw) {
|
public List<Map.Entry<String, Integer>> getPieData(List<Map<String, Map>> value,
|
||||||
//redis中有判断标识数据: "rydw_tj_"+busProjectPunchrange.getId() + "_" + "06:00" + "_" + constructionUser.getSysUserId()
|
List<SubConstructionUser> persons,
|
||||||
|
List<BusProjectPunchrange> dkfw) {
|
||||||
|
|
||||||
//饼状图数据Map
|
// 使用Set去重,提高查找效率
|
||||||
Map<String, Object> map = new HashMap<>();
|
Set<String> userRangeSet = new HashSet<>();
|
||||||
|
|
||||||
|
// 预构建打卡范围ID到名称的映射
|
||||||
|
Map<String, String> rangeIdToName = new HashMap<>();
|
||||||
|
for (BusProjectPunchrange fw : dkfw) {
|
||||||
|
rangeIdToName.put(fw.getId().toString(), fw.getPunchName());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历所有数据
|
||||||
for (Map<String, Map> mapMap : value) {
|
for (Map<String, Map> mapMap : value) {
|
||||||
//打卡范围ID : 打卡范围名称
|
for (Map.Entry<String, Map> entry : mapMap.entrySet()) {
|
||||||
Set<String> keys = mapMap.keySet();
|
String fwIdMc = entry.getKey();
|
||||||
for (String fwIdMc : keys) {
|
String[] split = fwIdMc.split(":");
|
||||||
//时间段
|
if (split.length < 2) continue;
|
||||||
Set keySet = mapMap.get(fwIdMc).keySet();
|
|
||||||
for (Object time : keySet) {
|
String fwId = split[0];
|
||||||
//组合key值进行查询redis: 打卡范围ID + "_" + 时间段 + "_" + 用户ID
|
String fwName = rangeIdToName.get(fwId);
|
||||||
String[] split = fwIdMc.split(":");
|
if (fwName == null) continue;
|
||||||
//分离出范围ID
|
|
||||||
String fwId = split[0];
|
Map<String, Object> timeMap = entry.getValue();
|
||||||
//范围名称
|
for (String timeSlot : timeMap.keySet()) {
|
||||||
String fwMc = split[1];
|
// 批量构建redis key
|
||||||
//组合用户id进行循环查询
|
List<String> redisKeys = new ArrayList<>();
|
||||||
for (SubConstructionUser person : persons) {
|
for (SubConstructionUser person : persons) {
|
||||||
//在此范围 此时间 循环用户 -> 在此范围 下一个时间 循环用户 -> 在下一个范围 此时间 循环用户
|
redisKeys.add("rydw_tj_" + fwId + "_" + timeSlot + "_" + person.getSysUserId());
|
||||||
String key = "rydw_tj_"+fwId + "_" + time + "_" + person.getSysUserId();
|
}
|
||||||
//针对时间去重 一个范围内只需要有一个时间段在范围内 其余不统计
|
|
||||||
if (RedisUtils.isExistsObject( key)){
|
// 批量查询redis(假设RedisUtils支持批量操作)
|
||||||
//在map中判断 如果有 则不进行添加
|
// 如果RedisUtils不支持批量,可以批量获取以减少网络开销
|
||||||
if (!map.containsKey(fwId + "_" + person.getSysUserId())) {
|
for (int i = 0; i < redisKeys.size(); i++) {
|
||||||
//如果没有 则说明该用户在 这个范围 这个时间段 有数据 可以添加
|
if (RedisUtils.isExistsObject(redisKeys.get(i))) {
|
||||||
map.put(fwId + "_" + person.getSysUserId(), 1);
|
String uniqueKey = fwId + "_" + persons.get(i).getSysUserId();
|
||||||
}
|
userRangeSet.add(uniqueKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//循环完成后 可以统计出 在此范围 此用户 是否活跃过
|
// 统计结果
|
||||||
Map<String, Integer> countMap = new HashMap<>();
|
Map<String, Integer> countMap = new HashMap<>();
|
||||||
//再次进行循环 统计出 一个范围内 多少活跃 用一个新map进行计数
|
for (String userRangeKey : userRangeSet) {
|
||||||
map.forEach((key, num) -> {
|
String[] parts = userRangeKey.split("_");
|
||||||
for (BusProjectPunchrange fw : dkfw) {
|
if (parts.length < 2) continue;
|
||||||
//提取出范围ID
|
|
||||||
String fwId = key.split("_")[0];
|
String fwId = parts[0];
|
||||||
if (fw.getId().toString().equals(fwId)) {
|
String fwName = rangeIdToName.get(fwId);
|
||||||
if (!countMap.containsKey(fw.getId() + ":" + fw.getPunchName())){
|
if (fwName != null) {
|
||||||
//如果没有则初始化为1人
|
String rangeKey = fwId + ":" + fwName;
|
||||||
countMap.put(fw.getId() + ":" + fw.getPunchName(), 1);
|
countMap.put(rangeKey, countMap.getOrDefault(rangeKey, 0) + 1);
|
||||||
}else {
|
|
||||||
//如果有则进行累加
|
|
||||||
countMap.put(fw.getId() + ":" + fw.getPunchName(), countMap.get(fw.getId() + ":" + fw.getPunchName()) + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//放一条测试数据
|
}
|
||||||
countMap.put("1000000:测试数据", 8);
|
|
||||||
});
|
// 测试数据
|
||||||
|
countMap.put("1000000:测试数据", 8);
|
||||||
|
|
||||||
return new ArrayList<>(countMap.entrySet());
|
return new ArrayList<>(countMap.entrySet());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user