public AjaxResult deviceControl(String userName) {
if (StringUtils.isEmpty(userName)){
return AjaxResult.error("账号不能用空");
}
SysUser sysUser = sysUserService.selectUserByUserName(userName);
if (sysUser == null){
return AjaxResult.error("无效用户");
}
List<ParkLot> parkLots = new ArrayList<>();
//00超管,01运营者,02租户管理员,03租户用户
//01运营者 按权限查询
if ("01".equals(sysUser.getUserType())){
//查询被授权过的车场列表
List<ParkLotUser> parkLotUserList = lotUserService.getParkNameByUserId(sysUser.getUserId().toString());
for (ParkLotUser parkLotUser : parkLotUserList) {
ParkLot parkLot = new ParkLot();
parkLot.setParkId(parkLotUser.getParkId());
parkLot.setParkName(parkLotUser.getParkName());
parkLots.add(parkLot);
}
}
// 02租户管理员,03租户用户 按租户编码查询
else if ("02".equals(sysUser.getUserType()) || "03".equals(sysUser.getUserType())){
// 判断登录的账号是运营还是租户类型
SysTenantInfo sysTenantInfo = tenantInfoService.getInfoByAdminAccount(userName);
if (sysTenantInfo == null) {
return AjaxResult.error("无效租户");
}
parkLots = parkLotService.queryByTenNum(sysTenantInfo.getTenId());
}
if (parkLots.size() == 0){
return AjaxResult.error("没有可用的停车场");
}
//map得到 车场ID列表
List<String> parkIds = parkLots.stream().map(ParkLot::getParkId).collect(Collectors.toList());
//获取通道数据列表
List<Passage> passageList = passageService.getPassageListByParkIds(parkIds);
if (passageList.size() == 0) {
return AjaxResult.error("没有可用的通道");
}
List<String> passageIds = passageList.stream().map(Passage::getPassageId).collect(Collectors.toList());
// 查询到对应的设备数据
List<ParkDevice> deviceList = deviceService.getPassageDeviceList(passageIds);
if (deviceList.size() <= 0) {
return AjaxResult.error("该通道没有关联设备");
}
JSONArray jsonArray = new JSONArray();
// parkId分组 通道
Map<String, List<Passage>> passageCollect = passageList.stream().collect(Collectors.groupingBy(Passage::getParkId));
Iterator<String> iterator = passageCollect.keySet().stream().iterator();
while (iterator.hasNext()){
String parkId = iterator.next();
List<Passage> passages = passageCollect.get(parkId);
//设备总数
long deviceCount = passages.stream().filter(p->!p.getPkDeviceId().equals("")).count();
//在线数量
long onlineCount = 0;
for (Passage passage : passages) {
//获取 关联到通道的设备
ParkDevice parkDevice = deviceList.stream().filter(p-> p.getPkDeviceId().equals(passage.getPkDeviceId())).findFirst().orElse(null);
if (parkDevice == null) {
break;
}
String parkKey = String.format(ParkConstants.parkKeyPrefix, parkDevice.getSerialNum());
String beatValue = redisCache.getCacheObject(parkKey);
if(beatValue != null){
onlineCount ++;
}
}
//离线数量
long outlineCount = deviceCount - onlineCount;
//车场名称
String parkName = parkLots.stream().filter(p -> p.getParkId().equals(parkId)).findFirst().orElse(null).getParkName();
JSONObject jsonObject = new JSONObject();
jsonObject.put("parkId", parkId);
jsonObject.put("parkName", parkName);
jsonObject.put("deviceCount", deviceCount);
jsonObject.put("onlineCount", onlineCount);
jsonObject.put("outlineCount", outlineCount);
jsonArray.add(jsonObject);
}
return AjaxResult.success(jsonArray);
}
三张表关联,使用stream流,避免循环查库
最新推荐文章于 2024-11-13 21:11:52 发布