/**
* 租户登录获取停车场 拿到他当前的停车场
*/
@GetMapping("/v1/getTenantryPark")
public AjaxResult getTenantryPark(String adminAccount) {
List<String> parkIds = new ArrayList<>();
// 判断登录的账号是运营还是租户类型
SysTenantInfo sysTenantInfo =
tenantInfoService.getInfoByAdminAccount(adminAccount);
if (sysTenantInfo == null) {
return AjaxResult.error("没有查到该账号的信息");
}
// 类型:租户,运营 默认租户
String userId = sysUserService.
getUserId(sysTenantInfo.getAdminAccount());
List<ParkLotUserVo> result = new ArrayList<>();
List<ParkLotUser> userList = lotUserService.
getParkNameByUserId(userId);
if (userList.size() <= 0) {
return AjaxResult.error("该账号没有授权车场");
}
// 获取到授权车场的parkId
for (ParkLotUser parkLotUser : userList) {
parkIds.add(parkLotUser.getParkId());
}
List<Passage> parkIdList = passageService.
getPassageListByParkIds(parkIds);
if (parkIdList.size() <= 0) {
return AjaxResult.error("该车场没有关联通道");
}
// 通到 通过parkId分组
Map<String, List<Passage>> passageCollect =
parkIdList.stream().collect(Collectors.
groupingBy(Passage::getParkId));
// 获取到关联了设备的通道id
List<String> passageList = new ArrayList<>();
for (Passage passage : parkIdList) {
passageList.add(passage.getPassageId());
}
// 查询到对应的设备数据
List<ParkDevice> deviceList =
deviceService.getPassageDeviceList(passageList);
if (deviceList.size() <= 0) {
return AjaxResult.error("该通道没有关联设备");
}
// 把查询到的设备数据 通过passageId来分组
Map<String, List<ParkDevice>> deviceCollect =
deviceList.stream().collect(Collectors.
groupingBy(ParkDevice::getPassageId));
// 获取到所有的通道id
Set<String> deviceKey = deviceCollect.keySet();
// 获取到所有的parkId
Set<String> parkIdKey = passageCollect.keySet();
// key:parkId value:设备总数量
Map<String, Integer> numMap = new HashMap<>();
for (String parkId : parkIdKey) {
// 设备数量
int onlineSize = 0;
// 通过map中的parkId 拿到所对应的通道对象集合数据
List<Passage> passages = passageCollect.get(parkId);
for (Passage passage : passages) {
// 遍历所有的通道id
for (String passageId : deviceKey) {
// 判断,要两个map都有同一个passageId才行
if (passage.getPassageId().equals(passageId)) {
// 通过通道id来查询到关联的设备id
List<ParkDevice> devices =
deviceCollect.get(passage.getPassageId());
// 设备的数量
int size = devices.size();
onlineSize += size;
}
}
// 这里时把数据组装,key是parkId,value是parkId对应的passageId,
让后通过passageId找到对应的deviceId的数量
numMap.put(parkId, onlineSize);
}
}
// 拿到所有的parkId
// key:parkId value:在线设备数量
Set<String> parkId = numMap.keySet();
// 遍历获取到的所有的车场 授权了的车场
for (ParkLotUser parkLotUser : userList) {
Integer integers = 0;
ParkLotUserVo parkLot = new ParkLotUserVo();
BeanUtils.copyBeanProp(parkLot, parkLotUser);
for (String ids : parkId) {
// 如果当前车场的id等于当前遍历的车场id,
就从map中拿到对应的value,即设备的数量
if (parkLot.getParkId().equals(ids)) {
// 通过parkId拿到对应map中的value 即设备的数量
integers = numMap.get(ids);
}
}
parkLot.setOnlineDevice(integers);
result.add(parkLot);
}
return AjaxResult.success(result);
}
ParkLotUserVo类
@Data
public class ParkLotUserVo extends ParkLotUser {
/** 管理员账号 */
private String adminAccount;
/** 在线设备数量 */
private Integer onlineDevice;
/** 离线设备数量 */
private Integer offlineDevice;
}
返回的数据
这里因为没有给 离线设备数量 赋值,所以 offlineDevice 为空
{
"msg": "操作成功",
"code": 200,
"data": [
{
"parkId": "1761283754829197314",
"parentParkId": "",
"tenNum": "e382c86767d00e427aa6cd67b6743cff",
"parkName": "莱蒙车场",
"parkType": "0",
"parkTotal": 100,
"personName": "Ltj",
"contactNum": "18541021324",
"projectId": "7740889167ce035d5b1d894acf7b2825",
"latitude": 0.000000,
"longitude": 0.000000,
"address": "江西省南昌市红谷滩区卫东街道红谷中大道商城·世纪村",
"imgUrl": "",
"isMidfield": "0",
"isMainMidfield": "0",
"userId": 50,
"updateId": 0,
"createTime": "2024-02-24 14:55:45",
"updateTime": "2024-02-24T14:55:42.000+0800",
"parkStatus": "1",
"isDelete": "0",
"onlineDevice": 5,
"offlineDevice": null
},
{
"parkId": "1762032549137350658",
"parentParkId": "",
"tenNum": "e382c86767d00e427aa6cd67b6743cff",
"parkName": "梦立方停车场",
"parkType": "0",
"parkTotal": 100,
"personName": "本地测试",
"contactNum": "15874521012",
"projectId": "8764fc4caba344ed9901ffc006d0a1e6",
"latitude": 0.000000,
"longitude": 0.000000,
"address": "江西省南昌市红谷滩区沙井街道季季红(梦立方广场店)梦立方广场",
"imgUrl": "",
"isMidfield": "0",
"isMainMidfield": "0",
"userId": 50,
"updateId": 0,
"createTime": "2024-02-26 16:31:11",
"updateTime": "2024-02-26T16:31:10.000+0800",
"parkStatus": "1",
"isDelete": "0",
"onlineDevice": 0,
"offlineDevice": null
},
{
"parkId": "1762665291856449538",
"parentParkId": "",
"tenNum": "e382c86767d00e427aa6cd67b6743cff",
"parkName": "测试停车场",
"parkType": "0",
"parkTotal": 110,
"personName": "测试人员",
"contactNum": "18574120123",
"projectId": "8764fc4caba344ed9901ffc006d0a1e6",
"latitude": 0.000000,
"longitude": 0.000000,
"address": "江西省南昌市红谷滩区沙井街道季季红(梦立方广场店)梦立方广场",
"imgUrl": "",
"isMidfield": "0",
"isMainMidfield": "0",
"userId": 50,
"updateId": 0,
"createTime": "2024-02-28 10:25:29",
"updateTime": "2024-02-28T10:25:27.000+0800",
"parkStatus": "1",
"isDelete": "0",
"onlineDevice": 0,
"offlineDevice": null
}
]
}