feat(device): 新增用户设备关系管理功能

- 在CsDeviceUserPOService中新增addRelation方法用于新增用户设备关系表数据
- 实现CsDeviceUserPOServiceImpl中的addRelation逻辑,支持新增主用户信息和已关联工程的用户设备关系
- 修改CsEquipmentDeliveryServiceImpl使用DSTransactional注解替代Transactional
- 更新CsEquipmentDeliveryServiceImpl中的saveCld方法,新增设备时同步创建用户设备关系
- 优化CsLedgerServiceImpl中便携式工程显示逻辑,增加空值检查
- 调整RoleEngineerDevServiceImpl中不同角色的设备权限查询逻辑
- 修改CsLinePOMapper.xml中的JOIN方式从LEFT JOIN改为RIGHT JOIN
This commit is contained in:
xy
2026-04-27 13:34:56 +08:00
parent e364ca1cae
commit 13b9981c72
6 changed files with 163 additions and 94 deletions

View File

@@ -28,7 +28,7 @@
t1.*
from
cs_equipment_delivery t0
left join cs_line t1 on
right join cs_line t1 on
t0.id = t1.device_id
where
t0.ndid = #{id}

View File

@@ -55,4 +55,10 @@ public interface CsDeviceUserPOService extends IService<CsDeviceUserPO>{
void channelDevByUserId(UserDevParam param);
List<CsDeviceUserPO> getList(UserDevParam param);
/**
* 新增用户设备关系表数据 新增主用户信息 && 新增已关联工程的用户的设备关系
* @return
*/
Boolean addRelation(UserDevParam param);
}

View File

@@ -35,6 +35,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import static java.util.Objects.isNull;
@@ -545,4 +546,59 @@ public class CsDeviceUserPOServiceImpl extends ServiceImpl<CsDeviceUserPOMapper,
queryWrapper.eq(CsDeviceUserPO::getStatus, "1");
return this.list(queryWrapper);
}
/**
*
* @param userId 这边放设备id
* @param list 这边放用户id集合
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean addRelation(UserDevParam param) {
String userIndex;
CsDeviceUserPO existingRecord = this.lambdaQuery()
.eq(CsDeviceUserPO::getDeviceId, param.getUserId())
.one();
if (existingRecord != null && Objects.equals(existingRecord.getPrimaryUserId(), existingRecord.getSubUserId())) {
userIndex = existingRecord.getPrimaryUserId();
} else {
userIndex = RequestUtil.getUserIndex();
if (userIndex == null) {
throw new IllegalArgumentException("当前用户信息获取失败");
}
CsDeviceUserPO mainDeviceRelation = new CsDeviceUserPO();
mainDeviceRelation.setDeviceId(param.getUserId());
mainDeviceRelation.setPrimaryUserId(userIndex);
mainDeviceRelation.setSubUserId(userIndex);
mainDeviceRelation.setStatus("1");
boolean mainSaveSuccess = this.save(mainDeviceRelation);
if (!mainSaveSuccess) {
return false;
}
}
if (CollectionUtil.isNotEmpty(param.getList())) {
List<CsDeviceUserPO> batchRecords = param.getList().stream()
.filter(Objects::nonNull)
.filter(item -> !Objects.equals(item, userIndex))
.map(item -> {
CsDeviceUserPO relation = new CsDeviceUserPO();
relation.setDeviceId(param.getUserId());
relation.setPrimaryUserId(userIndex);
relation.setSubUserId(item);
relation.setStatus("1");
return relation;
})
.collect(Collectors.toList());
if (!batchRecords.isEmpty()) {
return this.saveBatch(batchRecords);
}
}
return true;
}
}

View File

@@ -15,6 +15,7 @@ import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -25,14 +26,15 @@ import com.njcn.access.api.AskDeviceDataFeignClient;
import com.njcn.access.utils.MqttUtil;
import com.njcn.common.pojo.dto.DeviceLogDTO;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.api.CsLedgerFeignClient;
import com.njcn.csdevice.api.CsLogsFeignClient;
import com.njcn.csdevice.api.EngineeringFeignClient;
import com.njcn.csdevice.constant.DataParam;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.mapper.CsEquipmentDeliveryMapper;
import com.njcn.csdevice.mapper.CsLedgerMapper;
import com.njcn.csdevice.mapper.CsSoftInfoMapper;
import com.njcn.csdevice.mapper.CsTerminalLogsMapper;
import com.njcn.csdevice.pojo.dto.DevDetailDTO;
import com.njcn.csdevice.pojo.dto.PqsCommunicateDto;
import com.njcn.csdevice.pojo.param.*;
import com.njcn.csdevice.pojo.po.*;
@@ -122,10 +124,10 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
private final AppProjectService appProjectService;
private final CsEdDataService csEdDataService;
private final ICsSoftInfoService csSoftInfoService;
private final EngineeringFeignClient engineeringFeignClient;
private final EventUserFeignClient eventUserFeignClient;
private final UserFeignClient userFeignClient;
private final OverLimitWlMapper overLimitWlMapper;
private final CsLedgerFeignClient csLedgerFeignClient;
@Override
public void refreshDeviceDataCache() {
@@ -192,7 +194,7 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
}
@Override
@Transactional(rollbackFor = {Exception.class})
@DSTransactional
public Boolean AuditEquipmentDelivery(String id) {
//物理删除
boolean update = false;
@@ -690,7 +692,7 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
* 6.删除cs_device_user
* */
@Override
@Transactional(rollbackFor = Exception.class)
@DSTransactional
public void delete(String devId) {
QueryWrapper<CsLedger> csLedgerQueryWrapper = new QueryWrapper<>();
/**
@@ -829,11 +831,6 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
@Transactional(rollbackFor = Exception.class)
public CsEquipmentDeliveryPO saveCld(CsEquipmentDeliveryAddParm param) {
boolean result;
//设备名称可以重复
//CsEquipmentDeliveryPO one = this.lambdaQuery().eq(CsEquipmentDeliveryPO::getName, param.getName()).ne(CsEquipmentDeliveryPO::getRunStatus, 0).one();
//if(Objects.nonNull (one)){
// throw new BusinessException ("设备名称不能重复");
//}
StringUtil.containsSpecialCharacters(param.getNdid());
CsEquipmentDeliveryPO po = this.queryEquipmentPOByndid(param.getNdid());
if (!Objects.isNull(po)) {
@@ -879,8 +876,28 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
csLedger.setSort(0);
int addLedger = csLedgerMapper.insert(csLedger);
//谁新建的设备,则认为是该设备的主用户,新建用户设备关系表数据
boolean addUser = csDeviceUserPOService.add(csEquipmentDeliveryPo.getId());
//1.谁新建的设备,则认为是该设备的主用户,新建用户设备关系表数据
//2.获取当前工程已经关注的用户列表,新增用户和设备的关系
boolean addUser = false;
List<DevDetailDTO> devDetailList = csLedgerFeignClient.getDevInfoByEngineerIds(Collections.singletonList(param.getEngineeringId())).getData();
if (!CollectionUtils.isEmpty(devDetailList)) {
List<String> devIds = devDetailList.stream().map(DevDetailDTO::getEquipmentId).collect(Collectors.toList());
LambdaQueryWrapper<CsDeviceUserPO> wrapper = new LambdaQueryWrapper<>();
wrapper.in(CsDeviceUserPO::getDeviceId, devIds);
List<CsDeviceUserPO> csDeviceUserPOList = csDeviceUserPOService.list(wrapper);
if (!CollectionUtils.isEmpty(csDeviceUserPOList)) {
List<String> userIds = csDeviceUserPOList.stream().map(CsDeviceUserPO::getSubUserId).distinct().collect(Collectors.toList());
UserDevParam param1 = new UserDevParam();
param1.setUserId(csEquipmentDeliveryPo.getId());
param1.setList(userIds);
addUser = csDeviceUserPOService.addRelation(param1);
}
} else {
UserDevParam param1 = new UserDevParam();
param1.setUserId(csEquipmentDeliveryPo.getId());
param1.setList(null);
addUser = csDeviceUserPOService.addRelation(param1);
}
if (result && ObjectUtil.isNotNull(relation) && addLedger > 0 && addUser) {
refreshDeviceDataCache();
@@ -889,7 +906,7 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
}
@Override
@Transactional(rollbackFor = Exception.class)
@DSTransactional
public Boolean delCldDev(String id) {
CsEquipmentDeliveryPO one = this.lambdaQuery().eq(CsEquipmentDeliveryPO::getId, id).one();
LambdaQueryWrapper<CsEquipmentDeliveryPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
@@ -915,6 +932,10 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
if (update) {
refreshDeviceDataCache();
}
//删除用户设备关系表数据
LambdaQueryWrapper<CsDeviceUserPO> csDeviceUserPOLambdaQueryWrapper = new LambdaQueryWrapper<>();
csDeviceUserPOLambdaQueryWrapper.eq(CsDeviceUserPO::getDeviceId, id);
csDeviceUserPOService.remove(csDeviceUserPOLambdaQueryWrapper);
//新增操作日志
CsTerminalLogs csTerminalLogs = new CsTerminalLogs();
csTerminalLogs.setDeviceId(id);

View File

@@ -71,7 +71,6 @@ public class CsLedgerServiceImpl extends ServiceImpl<CsLedgerMapper, CsLedger> i
private final UserFeignClient userFeignClient;
private final ICsUserPinsService csUserPinsService;
private final CsCommTerminalFeignClient csCommTerminalFeignClient;
private final CsMarketDataFeignClient csMarketDataFeignClient;
@Override
@@ -291,22 +290,24 @@ public class CsLedgerServiceImpl extends ServiceImpl<CsLedgerMapper, CsLedger> i
tree.addAll(new ArrayList<>(engineeringMap.values()));
String id = IdUtil.simpleUUID();
CsLedgerVO portable1 = new CsLedgerVO();
portable1.setLevel(0);
portable1.setName("便携式工程");
portable1.setPid("0");
portable1.setId(id);
if (CollUtil.isNotEmpty(portables)) {
String id = IdUtil.simpleUUID();
CsLedgerVO portable1 = new CsLedgerVO();
portable1.setLevel(0);
portable1.setName("便携式工程");
portable1.setPid("0");
portable1.setId(id);
CsLedgerVO portable2 = new CsLedgerVO();
portable2.setLevel(1);
portable2.setName("便携式项目");
portable2.setPid(id);
portable2.setId(IdUtil.simpleUUID());
portable2.setChildren(portables);
CsLedgerVO portable2 = new CsLedgerVO();
portable2.setLevel(1);
portable2.setName("便携式项目");
portable2.setPid(id);
portable2.setId(IdUtil.simpleUUID());
portable2.setChildren(portables);
portable1.setChildren(Collections.singletonList(portable2));
tree.add(portable1);
portable1.setChildren(Collections.singletonList(portable2));
tree.add(portable1);
}
if (CollectionUtil.isNotEmpty(list)) {
csUserPinsService.channelTree(list, tree, 4);
@@ -703,24 +704,26 @@ public class CsLedgerServiceImpl extends ServiceImpl<CsLedgerMapper, CsLedger> i
tree.addAll(new ArrayList<>(engineeringMap.values()));
String id = IdUtil.simpleUUID();
CsLedgerVO portable1 = new CsLedgerVO();
portable1.setLevel(0);
portable1.setName("便携式工程");
portable1.setPid("0");
portable1.setId(id);
if (CollUtil.isNotEmpty(portables)) {
String id = IdUtil.simpleUUID();
CsLedgerVO portable1 = new CsLedgerVO();
portable1.setLevel(0);
portable1.setName("便携式工程");
portable1.setPid("0");
portable1.setId(id);
CsLedgerVO portable2 = new CsLedgerVO();
portable2.setLevel(1);
portable2.setName("便携式项目");
portable2.setPid(id);
portable2.setId(IdUtil.simpleUUID());
portable2.setChildren(portables);
CsLedgerVO portable2 = new CsLedgerVO();
portable2.setLevel(1);
portable2.setName("便携式项目");
portable2.setPid(id);
portable2.setId(IdUtil.simpleUUID());
portable2.setChildren(portables);
List<CsLedgerVO> portable2List = new ArrayList<>();
portable2List.add(portable2);
portable1.setChildren(portable2List);
tree.add(portable1);
List<CsLedgerVO> portable2List = new ArrayList<>();
portable2List.add(portable2);
portable1.setChildren(portable2List);
tree.add(portable1);
}
if (CollectionUtil.isNotEmpty(list)) {
csUserPinsService.channelTree(list, tree, 4);

View File

@@ -47,7 +47,24 @@ public class RoleEngineerDevServiceImpl implements RoleEngineerDevService {
QueryWrapper<CsLedger> csLedgerQueryWrapper = new QueryWrapper<>();
List<String> collect = new ArrayList<>();
if(Objects.equals(role,AppRoleEnum.APP_VIP_USER.getCode()) || Objects.equals(role,AppRoleEnum.REGULAR_USER_8000.getCode())){
if ( Objects.equals(role,AppRoleEnum.MARKET_USER.getCode())||
Objects.equals(role, AppRoleEnum.ENGINEERING_USER.getCode())) {
QueryWrapper<CsMarketData> csMarketDataQueryWrapper = new QueryWrapper<>();
csMarketDataQueryWrapper.eq("user_id", userIndex);
List<CsMarketData> csMarketData = csMarketDataMapper.selectList(csMarketDataQueryWrapper);
collect = csMarketData.stream().map(CsMarketData::getEngineerId).collect(Collectors.toList());
} else if (Objects.equals(role,AppRoleEnum.TOURIST.getCode())) {
//todo查询配置的游客工程
List<CsTouristDataPO> csTouristDataPOS = csTouristDataPOMapper.selectList(null);
collect = csTouristDataPOS.stream().map(CsTouristDataPO::getEnginerId).distinct().collect(Collectors.toList());
}
// ||Objects.equals(role,"bxs_user")
else if(Objects.equals(role,AppRoleEnum.ROOT.getCode()) || Objects.equals(role,AppRoleEnum.OPERATION_MANAGER.getCode())){
List<CsEngineeringPO> csEngineeringPOS = csEngineeringMapper.selectList(null);
collect =csEngineeringPOS.stream().filter(temp->Objects.equals(temp.getStatus(),"1")).map(CsEngineeringPO::getId).collect(Collectors.toList());
} else {
csDeviceUserPOQueryWrapper.clear();
csEngineeringUserPOQueryWrapper.clear();
csLedgerQueryWrapper.clear();
@@ -79,24 +96,6 @@ public class RoleEngineerDevServiceImpl implements RoleEngineerDevService {
collect = collect.stream().distinct().collect(Collectors.toList());
}
return collect;
} else if ( Objects.equals(role,AppRoleEnum.MARKET_USER.getCode())||
Objects.equals(role, AppRoleEnum.ENGINEERING_USER.getCode())) {
QueryWrapper<CsMarketData> csMarketDataQueryWrapper = new QueryWrapper<>();
csMarketDataQueryWrapper.eq("user_id", userIndex);
List<CsMarketData> csMarketData = csMarketDataMapper.selectList(csMarketDataQueryWrapper);
collect = csMarketData.stream().map(CsMarketData::getEngineerId).collect(Collectors.toList());
} else if (Objects.equals(role,AppRoleEnum.TOURIST.getCode())) {
//todo查询配置的游客工程
List<CsTouristDataPO> csTouristDataPOS = csTouristDataPOMapper.selectList(null);
collect = csTouristDataPOS.stream().map(CsTouristDataPO::getEnginerId).distinct().collect(Collectors.toList());
}
// ||Objects.equals(role,"bxs_user")
else if(Objects.equals(role,AppRoleEnum.ROOT.getCode()) || Objects.equals(role,AppRoleEnum.OPERATION_MANAGER.getCode()) || Objects.equals(role,AppRoleEnum.REGULAR_USER.getCode()) ){
List<CsEngineeringPO> csEngineeringPOS = csEngineeringMapper.selectList(null);
collect =csEngineeringPOS.stream().filter(temp->Objects.equals(temp.getStatus(),"1")).map(CsEngineeringPO::getId).collect(Collectors.toList());
}
return collect;
@@ -115,37 +114,11 @@ public class RoleEngineerDevServiceImpl implements RoleEngineerDevService {
QueryWrapper<CsLedger> csLedgerQueryWrapper = new QueryWrapper<>();
List<String> collect = new ArrayList<>();
if(
Objects.equals(role,AppRoleEnum.APP_VIP_USER.getCode()) || Objects.equals(role,AppRoleEnum.REGULAR_USER_8000.getCode()) || Objects.equals(role,"bxs_user")){
csDeviceUserPOQueryWrapper.clear();
csDeviceUserPOQueryWrapper.eq("status","1").and(wq -> {
wq.eq("primary_user_id", userIndex)
.or()
.eq("sub_user_id",userIndex);
});
List<CsDeviceUserPO> csDeviceUserPOS = csDeviceUserPOMapper.selectList(csDeviceUserPOQueryWrapper);
if(CollectionUtils.isEmpty(csDeviceUserPOS)){
return new ArrayList<>();
}
List<String> collect1 = csDeviceUserPOS.stream().map(CsDeviceUserPO::getDeviceId).distinct().collect(Collectors.toList());
return collect1;
}
//fix 新需求 工程用户可以看到关注工程的设备和对应的主用户和子用户的设备
//note 这边工程用户不查询主设备,只看关注的工程,做下调整,不然会出现工程用户未关注任何工程,但是有一台主设备,导致界面有数据
else if (Objects.equals(role, AppRoleEnum.ENGINEERING_USER.getCode())) {
if (Objects.equals(role, AppRoleEnum.ENGINEERING_USER.getCode())) {
List<String> sumDevId = new ArrayList<>();
csDeviceUserPOQueryWrapper.clear();
// csDeviceUserPOQueryWrapper.eq("status","1").and(wq -> {
// wq.eq("primary_user_id", userIndex)
// .or()
// .eq("sub_user_id",userIndex);
// });
// List<CsDeviceUserPO> csDeviceUserPOS = csDeviceUserPOMapper.selectList(csDeviceUserPOQueryWrapper);
// if(!CollectionUtils.isEmpty(csDeviceUserPOS)){
// sumDevId = csDeviceUserPOS.stream().map(CsDeviceUserPO::getDeviceId).distinct().collect(Collectors.toList());
// }
QueryWrapper<CsMarketData> csMarketDataQueryWrapper = new QueryWrapper<>();
csMarketDataQueryWrapper.eq("user_id", userIndex);
List<CsMarketData> csMarketData = csMarketDataMapper.selectList(csMarketDataQueryWrapper);
@@ -198,15 +171,25 @@ public class RoleEngineerDevServiceImpl implements RoleEngineerDevService {
collect = csTouristDataPOS.stream().map(CsTouristDataPO::getDeviceId).distinct().collect(Collectors.toList());
}
// ||Objects.equals(role,"bxs_user")
else if(Objects.equals(role,AppRoleEnum.ROOT.getCode())||Objects.equals(role,AppRoleEnum.OPERATION_MANAGER.getCode()) || Objects.equals(role,AppRoleEnum.REGULAR_USER.getCode())){
else if(Objects.equals(role,AppRoleEnum.ROOT.getCode())||Objects.equals(role,AppRoleEnum.OPERATION_MANAGER.getCode())){
csLedgerQueryWrapper.clear();
csLedgerQueryWrapper.eq("level",2).eq("state",1);
List<CsLedger> csLedgers = csLedgerMapper.selectList(csLedgerQueryWrapper);
collect = csLedgers.stream().map(CsLedger::getId).distinct().collect(Collectors.toList());
} else {
csDeviceUserPOQueryWrapper.clear();
csDeviceUserPOQueryWrapper.eq("status","1").and(wq -> {
wq.eq("primary_user_id", userIndex)
.or()
.eq("sub_user_id",userIndex);
});
List<CsDeviceUserPO> csDeviceUserPOS = csDeviceUserPOMapper.selectList(csDeviceUserPOQueryWrapper);
if(CollectionUtils.isEmpty(csDeviceUserPOS)){
return new ArrayList<>();
}
List<String> collect1 = csDeviceUserPOS.stream().map(CsDeviceUserPO::getDeviceId).distinct().collect(Collectors.toList());
return collect1;
}
return collect;
}