fix:修改设备台账关联物联设备及物联设备相关接口

main
HuangHuiKang 6 days ago
parent 0764a9508b
commit 6f21d9c1c9

@ -83,4 +83,10 @@ public class DevicePageReqVO extends PageParam {
@Schema(description = "mqtt订阅主题")
private String topic;
@Schema(description = "台账绑定场景当前设备台账ID编辑时用于保留当前已绑定设备")
private Long currentLedgerId;
@Schema(description = "台账绑定场景:是否只查询可绑定的物联设备")
private Boolean onlyAvailableForLedger;
}

@ -129,4 +129,16 @@ public class DeviceRespVO {
@Schema(description = "设备图片", example = "{}")
private String images;
}
@Schema(description = "是否已被设备台账绑定")
private Boolean ledgerBound;
@Schema(description = "当前场景是否可选")
private Boolean selectable;
@Schema(description = "已绑定的设备台账ID")
private Long boundLedgerId;
@Schema(description = "已绑定的设备台账名称")
private String boundLedgerName;
}

@ -15,7 +15,7 @@ public class DeviceSelectRespVO {
@Schema(description = "设备名称(原始)")
private String deviceName;
@Schema(description = "设备名称(展示用,删除状态会拼接“(已删除)”)")
@Schema(description = "设备名称(展示用,删除状态会拼接“已删除”)")
private String displayName;
@Schema(description = "是否已被mes_organization.dv_id选择")

@ -144,6 +144,9 @@ public interface DeviceMapper extends BaseMapperX<DeviceDO> {
List<DeviceSelectRespVO> getAvailableDevicePage(@Param("page") Page<DeviceSelectRespVO> page,@Param("param") DevicePageReqVO pageReqVO);
List<DeviceRespVO> selectDeviceLedgerBoundList(@Param("deviceIds") Collection<Long> deviceIds,
@Param("currentLedgerId") Long currentLedgerId);
List<Date> selectHolidayDaysInRange(@Param("startDay") Date startDay, @Param("endDayExclusive") Date endDayExclusive);

@ -456,6 +456,7 @@ public class DeviceServiceImpl implements DeviceService {
}
fillDeviceLedgerBoundInfo(deviceRespVOPageResult.getList(), pageReqVO.getCurrentLedgerId());
return deviceRespVOPageResult;
}
@ -485,7 +486,37 @@ public class DeviceServiceImpl implements DeviceService {
List<DeviceRespVO> filteredList = deviceRespVOList.stream()
.filter(device -> matchesOperatingStatus(device.getOperatingStatus(), pageReqVO.getOperatingStatus()))
.collect(Collectors.toList());
return new PageResult<>(subPage(filteredList, pageReqVO), (long) filteredList.size());
List<DeviceRespVO> pageList = subPage(filteredList, pageReqVO);
fillDeviceLedgerBoundInfo(pageList, pageReqVO.getCurrentLedgerId());
return new PageResult<>(pageList, (long) filteredList.size());
}
private void fillDeviceLedgerBoundInfo(List<DeviceRespVO> deviceList, Long currentLedgerId) {
if (CollUtil.isEmpty(deviceList)) {
return;
}
List<Long> deviceIds = deviceList.stream()
.map(DeviceRespVO::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (CollUtil.isEmpty(deviceIds)) {
return;
}
Map<Long, DeviceRespVO> boundMap = deviceMapper.selectDeviceLedgerBoundList(deviceIds, currentLedgerId).stream()
.collect(Collectors.toMap(DeviceRespVO::getId, Function.identity(), (a, b) -> a));
for (DeviceRespVO device : deviceList) {
DeviceRespVO bound = boundMap.get(device.getId());
if (bound == null) {
device.setLedgerBound(false);
device.setSelectable(true);
continue;
}
device.setLedgerBound(bound.getLedgerBound());
device.setSelectable(bound.getSelectable());
device.setBoundLedgerId(bound.getBoundLedgerId());
device.setBoundLedgerName(bound.getBoundLedgerName());
}
}
private String resolveDeviceOperatingStatus(DeviceOperationRecordDO record) {

@ -167,29 +167,38 @@
resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.LineCodeAndNameRespVO">
SELECT
iotd.id AS deviceId,
mo.code AS lineCode,
mo.name AS lineName
FROM mes_organization mo
LEFT JOIN iot_device iotd ON mo.dv_id = iotd.id
WHERE iotd.id IN
mdl.dv_id AS deviceId,
top_dl.code AS lineCode,
top_dl.name AS lineName
FROM mes_device_ledger mdl
INNER JOIN mes_device_line dl ON dl.id = mdl.device_line AND dl.deleted = 0
INNER JOIN mes_device_line top_dl ON top_dl.id = CASE
WHEN dl.parent_chain IS NULL OR dl.parent_chain = '' THEN dl.id
ELSE CAST(SUBSTRING_INDEX(dl.parent_chain, ',', 1) AS UNSIGNED)
END AND top_dl.deleted = 0
WHERE mdl.deleted = 0
AND mdl.dv_id IS NOT NULL
AND mdl.dv_id IN
<foreach collection="deviceIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
AND mo.deleted = 0
</select>
<select id="selectDeviceIdsByLine" resultType="java.lang.Long">
SELECT iotd.id
FROM mes_organization mo
LEFT JOIN iot_device iotd ON mo.dv_id = iotd.id
WHERE mo.deleted = 0
and mo.dv_id is not null
SELECT DISTINCT mdl.dv_id
FROM mes_device_ledger mdl
INNER JOIN mes_device_line dl ON dl.id = mdl.device_line AND dl.deleted = 0
INNER JOIN mes_device_line top_dl ON top_dl.id = CASE
WHEN dl.parent_chain IS NULL OR dl.parent_chain = '' THEN dl.id
ELSE CAST(SUBSTRING_INDEX(dl.parent_chain, ',', 1) AS UNSIGNED)
END AND top_dl.deleted = 0
WHERE mdl.deleted = 0
AND mdl.dv_id IS NOT NULL
<if test="lineNode != null and lineNode != ''">
AND mo.code LIKE CONCAT('%', #{lineNode}, '%')
AND top_dl.code LIKE CONCAT('%', #{lineNode}, '%')
</if>
<if test="lineName != null and lineName != ''">
AND mo.name LIKE CONCAT('%', #{lineName}, '%')
AND top_dl.name LIKE CONCAT('%', #{lineName}, '%')
</if>
</select>
<select id="getTotalDeviceCount" resultType="java.lang.Integer">
@ -237,6 +246,7 @@
resultType="cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO">
SELECT
id,
device_code,
device_name,
deleted
FROM iot_device
@ -288,6 +298,9 @@
ELSE FALSE
END AS selected
FROM iot_device d
LEFT JOIN mes_device_ledger mdl
ON mdl.dv_id = d.id
AND mdl.deleted = 0
WHERE d.deleted = 0
<if test="param.deviceCode != null and param.deviceCode != ''">
AND d.device_code LIKE CONCAT('%', #{param.deviceCode}, '%')
@ -296,9 +309,40 @@
<if test="param.deviceName != null and param.deviceName != ''">
AND d.device_name LIKE CONCAT('%', #{param.deviceName}, '%')
</if>
<if test="param.onlyAvailableForLedger != null and param.onlyAvailableForLedger == true">
AND (mdl.id IS NULL OR mdl.id = #{param.currentLedgerId})
</if>
ORDER BY d.id DESC
</select>
<select id="selectDeviceLedgerBoundList"
resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceRespVO">
SELECT
d.id,
mdl.id AS boundLedgerId,
mdl.device_name AS boundLedgerName,
CASE
WHEN mdl.id IS NULL THEN FALSE
ELSE TRUE
END AS ledgerBound,
CASE
WHEN mdl.id IS NULL THEN TRUE
WHEN #{currentLedgerId} IS NOT NULL AND mdl.id = #{currentLedgerId} THEN TRUE
ELSE FALSE
END AS selectable
FROM iot_device d
LEFT JOIN mes_device_ledger mdl
ON mdl.dv_id = d.id
AND mdl.deleted = 0
WHERE d.deleted = 0
<if test="deviceIds != null and deviceIds.size() > 0">
AND d.id IN
<foreach collection="deviceIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</if>
</select>
<select id="selectHolidayDaysInRange" resultType="java.util.Date">
SELECT DISTINCT the_day
FROM mes_cal_holiday

@ -161,6 +161,12 @@ public class DeviceLedgerRespVO extends BaseDO {
@Schema(description = "模具id")
private String moldId;
@Schema(description = "关联采集设备编码")
private String iotDeviceCode;
@Schema(description = "关联采集设备名称")
private String iotDeviceName;
@Schema(description = "关联采集设备id")
private Long dvId;

@ -87,7 +87,7 @@ public class DeviceLedgerSaveReqVO {
private String moldId;
@Schema(description = "关联采集设备id")
private Long dvId;
private Long dvId;
@Schema(description = "是否排产")
private Integer isScheduled;

@ -32,6 +32,7 @@ import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.mes.controller.admin.deviceline.vo.*;
import cn.iocoder.yudao.module.mes.controller.admin.organization.vo.LineAnalysisTreeDTO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceline.DeviceLineDO;
import cn.iocoder.yudao.module.mes.service.deviceline.DeviceLineService;
@ -147,6 +148,15 @@ public class DeviceLineController {
return success(tree);
}
@GetMapping("/deviceParameterAnalysis")
@Operation(summary = "设备运行参数分析")
@PreAuthorize("@ss.hasPermission('iot:device:query')")
public CommonResult<List<LineAnalysisTreeDTO.LineNode>> deviceParameterAnalysis(
@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "showDevices") Integer showDevices) {
return success(deviceLineService.deviceParameterAnalysis(keyword, showDevices));
}
@GetMapping("/children")
@Operation(summary = "获得子设备产线列表")
@Parameter(name = "parentId", description = "父级ID", required = true, example = "0")

@ -199,6 +199,18 @@ public class DeviceLedgerDO extends BaseDO {
*/
private Long dvId;
/**
*
*/
@TableField(exist = false)
private String iotDeviceCode;
/**
*
*/
@TableField(exist = false)
private String iotDeviceName;
/**
*
*/

@ -81,5 +81,7 @@ public interface DeviceLedgerMapper extends BaseMapperX<DeviceLedgerDO> {
DeviceLedgerDO selectByIdWithDeleted(@Param("id") Long id);
DeviceLedgerDO selectByDvIdExcludeId(@Param("dvId") Long dvId, @Param("excludeId") Long excludeId);
String selectPrintTemplate();
}

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.mes.service.deviceledger;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.common.enums.CodeTypeEnum;
@ -84,6 +85,8 @@ import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
@Slf4j
public class DeviceLedgerServiceImpl implements DeviceLedgerService {
private static final ErrorCode DEVICE_LEDGER_DV_EXISTS = new ErrorCode(1002000014, "采集设备已绑定其它设备台账");
@Resource
private DeviceLedgerMapper deviceLedgerMapper;
@ -165,6 +168,8 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
public Long createDeviceLedger(DeviceLedgerSaveReqVO createReqVO) throws UnsupportedEncodingException {
DeviceLedgerDO deviceLedger = BeanUtils.toBean(createReqVO, DeviceLedgerDO.class);
validateDvIdAvailable(createReqVO.getDvId(), null);
//验证是否唯一编码
// validateCodeOnly(createReqVO.getDeviceCode());
if (StringUtils.isBlank(createReqVO.getDeviceCode())) {
@ -215,11 +220,24 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
throw exception(DEVICE_LEDGER_EXISTS);
}
}
private void validateDvIdAvailable(Long dvId, Long excludeId) {
if (dvId == null || dvId == 0) {
return;
}
DeviceLedgerDO usedLedger = deviceLedgerMapper.selectByDvIdExcludeId(dvId, excludeId);
if (usedLedger != null) {
throw exception(DEVICE_LEDGER_DV_EXISTS);
}
}
@Override
public void updateDeviceLedger(DeviceLedgerSaveReqVO updateReqVO) {
// 校验存在
validateDeviceLedgerExists(updateReqVO.getId());
validateDvIdAvailable(updateReqVO.getDvId(), updateReqVO.getId());
//编码重复判断
Long count = deviceLedgerMapper.selectCount(new LambdaQueryWrapper<DeviceLedgerDO>()
.eq(DeviceLedgerDO::getDeviceCode, updateReqVO.getDeviceCode())
@ -392,9 +410,53 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
.map(String::valueOf)
.collect(Collectors.joining(",")));
}
fillIotDeviceInfo(deviceLedgerDO);
return deviceLedgerDO;
}
private void fillIotDeviceInfo(DeviceLedgerDO deviceLedgerDO) {
if (deviceLedgerDO == null || deviceLedgerDO.getDvId() == null) {
return;
}
Map<Long, DeviceDO> deviceMap = deviceService.getMapIncludeDeleted(Collections.singleton(deviceLedgerDO.getDvId()));
DeviceDO deviceDO = deviceMap.get(deviceLedgerDO.getDvId());
if (deviceDO == null) {
return;
}
deviceLedgerDO.setIotDeviceCode(deviceDO.getDeviceCode());
String deviceName = deviceDO.getDeviceName();
if (Boolean.TRUE.equals(deviceDO.getDeleted())) {
deviceName = buildDeletedName(deviceName);
}
deviceLedgerDO.setIotDeviceName(deviceName);
}
private void fillIotDeviceInfo(List<DeviceLedgerDO> deviceLedgerList) {
if (CollUtil.isEmpty(deviceLedgerList)) {
return;
}
Set<Long> dvIds = deviceLedgerList.stream()
.map(DeviceLedgerDO::getDvId)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (CollUtil.isEmpty(dvIds)) {
return;
}
Map<Long, DeviceDO> deviceMap = deviceService.getMapIncludeDeleted(dvIds);
for (DeviceLedgerDO deviceLedgerDO : deviceLedgerList) {
DeviceDO deviceDO = deviceMap.get(deviceLedgerDO.getDvId());
if (deviceDO == null) {
continue;
}
deviceLedgerDO.setIotDeviceCode(deviceDO.getDeviceCode());
String deviceName = deviceDO.getDeviceName();
if (Boolean.TRUE.equals(deviceDO.getDeleted())) {
deviceName = buildDeletedName(deviceName);
}
deviceLedgerDO.setIotDeviceName(deviceName);
}
}
private String buildDeletedName(String snapshotName) {
if (StringUtils.isNotBlank(snapshotName)) {
return snapshotName + "(已删除)";
@ -462,6 +524,7 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
}
fillTopCategoryInfo(deviceLedgerDO, deviceLineMap);
}
fillIotDeviceInfo(deviceLedgerDOPageResult.getList());
return deviceLedgerDOPageResult;
}

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.mes.service.deviceline;
import java.io.UnsupportedEncodingException;
import java.util.*;
import cn.iocoder.yudao.module.mes.controller.admin.organization.vo.LineAnalysisTreeDTO;
import cn.iocoder.yudao.module.mes.controller.admin.deviceline.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceline.DeviceLineDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
@ -35,6 +36,8 @@ public interface DeviceLineService {
List<DeviceLineDO> getAncestorDeviceLines(Long id);
List<LineAnalysisTreeDTO.LineNode> deviceParameterAnalysis(String keyword, Integer showDevices);
void regenerateCode(Long id, String code) throws UnsupportedEncodingException;
String selectPrintTemplate();

@ -5,7 +5,12 @@ import cn.iocoder.yudao.module.common.enums.CodeTypeEnum;
import cn.iocoder.yudao.module.common.enums.QrcodeBizTypeEnum;
import cn.iocoder.yudao.module.common.service.qrcordrecord.QrcodeRecordService;
import cn.iocoder.yudao.module.erp.controller.admin.autocode.util.AutoCodeUtil;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicecontactmodel.DeviceContactModelDO;
import cn.iocoder.yudao.module.iot.dal.mysql.device.DeviceMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.devicecontactmodel.DeviceContactModelMapper;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerDO;
import cn.iocoder.yudao.module.mes.dal.mysql.deviceledger.DeviceLedgerMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@ -18,6 +23,7 @@ import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.stream.Collectors;
import cn.iocoder.yudao.module.mes.controller.admin.deviceline.vo.*;
import cn.iocoder.yudao.module.mes.controller.admin.organization.vo.LineAnalysisTreeDTO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceline.DeviceLineDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -25,6 +31,7 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.dal.mysql.deviceline.DeviceLineMapper;
import javax.annotation.Resource;
import java.util.function.Function;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
@ -48,6 +55,15 @@ public class DeviceLineServiceImpl implements DeviceLineService {
@Resource
private QrcodeRecordService qrcodeService;
@Resource
private DeviceLedgerMapper deviceLedgerMapper;
@Resource
private DeviceMapper deviceMapper;
@Resource
private DeviceContactModelMapper deviceContactModelMapper;
@Override
public Long createDeviceLine(DeviceLineSaveReqVO createReqVO) throws UnsupportedEncodingException {
validateParentDeviceLine(createReqVO.getParentId());
@ -212,6 +228,245 @@ public class DeviceLineServiceImpl implements DeviceLineService {
return ancestors;
}
@Override
public List<LineAnalysisTreeDTO.LineNode> deviceParameterAnalysis(String keyword, Integer showDevices) {
if (showDevices == null || (showDevices != 1 && showDevices != 2)) {
showDevices = 1;
}
boolean needShowDevices = showDevices == 1;
boolean hasKeyword = StringUtils.isNotBlank(keyword);
String lowerKeyword = hasKeyword ? keyword.toLowerCase() : null;
List<DeviceLineDO> deviceLines = deviceLineMapper.selectList();
if (deviceLines == null || deviceLines.isEmpty()) {
return Collections.emptyList();
}
deviceLines.sort(Comparator.comparing(DeviceLineDO::getSort,
Comparator.nullsLast(Integer::compareTo)).thenComparing(DeviceLineDO::getId));
List<DeviceLedgerDO> ledgers = deviceLedgerMapper.selectList(new LambdaQueryWrapper<DeviceLedgerDO>()
.isNotNull(DeviceLedgerDO::getDeviceLine)
.isNotNull(DeviceLedgerDO::getDvId)
.ne(DeviceLedgerDO::getDvId, 0));
List<Long> deviceIds = ledgers.stream()
.map(DeviceLedgerDO::getDvId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
Map<Long, DeviceDO> deviceMap = new HashMap<>();
Map<Long, List<DeviceContactModelDO>> paramsByDeviceId = new HashMap<>();
if (!deviceIds.isEmpty()) {
deviceMap = deviceMapper.selectBatchIds(deviceIds).stream()
.collect(Collectors.toMap(DeviceDO::getId, Function.identity(), (a, b) -> a));
List<DeviceContactModelDO> parameters = deviceContactModelMapper.selectList(
new LambdaQueryWrapper<DeviceContactModelDO>()
.in(DeviceContactModelDO::getDeviceId, deviceIds)
.orderByAsc(DeviceContactModelDO::getSort));
paramsByDeviceId = parameters.stream()
.filter(item -> item.getDeviceId() != null)
.collect(Collectors.groupingBy(DeviceContactModelDO::getDeviceId));
}
Map<Long, List<DeviceLedgerDO>> ledgersByLineId = ledgers.stream()
.filter(item -> item.getDeviceLine() != null)
.collect(Collectors.groupingBy(item -> item.getDeviceLine().longValue()));
Map<Long, List<DeviceLineDO>> childrenByParentId = deviceLines.stream()
.filter(item -> item.getParentId() != null && item.getParentId() != 0L)
.collect(Collectors.groupingBy(DeviceLineDO::getParentId));
Set<Long> matchedNodeIds = new HashSet<>();
if (hasKeyword) {
for (DeviceLineDO line : deviceLines) {
if (isDeviceLineAnalysisMatched(line, ledgersByLineId, deviceMap, paramsByDeviceId, lowerKeyword, needShowDevices)) {
matchedNodeIds.add(line.getId());
addAllDeviceLineDescendants(line.getId(), childrenByParentId, matchedNodeIds);
addDeviceLineAncestors(line, matchedNodeIds);
}
}
if (matchedNodeIds.isEmpty()) {
return Collections.emptyList();
}
} else {
matchedNodeIds.addAll(deviceLines.stream().map(DeviceLineDO::getId).collect(Collectors.toSet()));
}
return buildDeviceLineAnalysisTree(deviceLines, matchedNodeIds, ledgersByLineId, deviceMap,
paramsByDeviceId, needShowDevices, hasKeyword, lowerKeyword);
}
private boolean isDeviceLineAnalysisMatched(DeviceLineDO line,
Map<Long, List<DeviceLedgerDO>> ledgersByLineId,
Map<Long, DeviceDO> deviceMap,
Map<Long, List<DeviceContactModelDO>> paramsByDeviceId,
String lowerKeyword,
boolean needShowDevices) {
if (line.getName() != null && line.getName().toLowerCase().contains(lowerKeyword)) {
return true;
}
if (line.getCode() != null && line.getCode().toLowerCase().contains(lowerKeyword)) {
return true;
}
if (!needShowDevices) {
return false;
}
for (DeviceLedgerDO ledger : ledgersByLineId.getOrDefault(line.getId(), Collections.emptyList())) {
DeviceDO device = deviceMap.get(ledger.getDvId());
if (device == null) {
continue;
}
if (device.getDeviceName() != null && device.getDeviceName().toLowerCase().contains(lowerKeyword)) {
return true;
}
if (device.getDeviceCode() != null && device.getDeviceCode().toLowerCase().contains(lowerKeyword)) {
return true;
}
boolean matchedParam = paramsByDeviceId.getOrDefault(device.getId(), Collections.emptyList()).stream()
.anyMatch(param -> param.getAttributeName() != null
&& param.getAttributeName().toLowerCase().contains(lowerKeyword));
if (matchedParam) {
return true;
}
}
return false;
}
private void addAllDeviceLineDescendants(Long nodeId,
Map<Long, List<DeviceLineDO>> childrenByParentId,
Set<Long> matchedNodeIds) {
Queue<Long> queue = new LinkedList<>();
queue.offer(nodeId);
while (!queue.isEmpty()) {
Long currentId = queue.poll();
for (DeviceLineDO child : childrenByParentId.getOrDefault(currentId, Collections.emptyList())) {
matchedNodeIds.add(child.getId());
queue.offer(child.getId());
}
}
}
private void addDeviceLineAncestors(DeviceLineDO line, Set<Long> matchedNodeIds) {
if (StringUtils.isBlank(line.getParentChain())) {
return;
}
Arrays.stream(line.getParentChain().split(","))
.filter(StringUtils::isNotBlank)
.forEach(id -> {
try {
matchedNodeIds.add(Long.valueOf(id.trim()));
} catch (NumberFormatException ignored) {
}
});
}
private List<LineAnalysisTreeDTO.LineNode> buildDeviceLineAnalysisTree(
List<DeviceLineDO> deviceLines,
Set<Long> matchedNodeIds,
Map<Long, List<DeviceLedgerDO>> ledgersByLineId,
Map<Long, DeviceDO> deviceMap,
Map<Long, List<DeviceContactModelDO>> paramsByDeviceId,
boolean needShowDevices,
boolean hasKeyword,
String lowerKeyword) {
Map<Long, LineAnalysisTreeDTO.LineNode> nodeMap = new LinkedHashMap<>();
for (DeviceLineDO line : deviceLines) {
if (!matchedNodeIds.contains(line.getId())) {
continue;
}
nodeMap.put(line.getId(), LineAnalysisTreeDTO.LineNode.builder()
.id(line.getId())
.name(line.getName())
.parentId(line.getParentId())
.equipments(needShowDevices ? buildEquipmentNodes(line, ledgersByLineId, deviceMap,
paramsByDeviceId, hasKeyword, lowerKeyword) : new ArrayList<>())
.children(new ArrayList<>())
.build());
}
List<LineAnalysisTreeDTO.LineNode> result = new ArrayList<>();
for (LineAnalysisTreeDTO.LineNode node : nodeMap.values()) {
if (node.getParentId() == null || node.getParentId() == 0L) {
result.add(node);
continue;
}
LineAnalysisTreeDTO.LineNode parent = nodeMap.get(node.getParentId());
if (parent != null) {
parent.getChildren().add(node);
} else {
result.add(node);
}
}
return result;
}
private List<LineAnalysisTreeDTO.EquipmentNode> buildEquipmentNodes(
DeviceLineDO line,
Map<Long, List<DeviceLedgerDO>> ledgersByLineId,
Map<Long, DeviceDO> deviceMap,
Map<Long, List<DeviceContactModelDO>> paramsByDeviceId,
boolean hasKeyword,
String lowerKeyword) {
List<LineAnalysisTreeDTO.EquipmentNode> equipmentNodes = new ArrayList<>();
for (DeviceLedgerDO ledger : ledgersByLineId.getOrDefault(line.getId(), Collections.emptyList())) {
DeviceDO device = deviceMap.get(ledger.getDvId());
if (device == null) {
continue;
}
List<DeviceContactModelDO> deviceParams = paramsByDeviceId.getOrDefault(device.getId(), Collections.emptyList());
List<DeviceContactModelDO> filteredParams = filterDeviceLineParams(line, device, deviceParams, hasKeyword, lowerKeyword);
if (hasKeyword && filteredParams.isEmpty()
&& !isDeviceLineNameMatched(line, lowerKeyword)
&& !isDeviceMatched(device, lowerKeyword)) {
continue;
}
equipmentNodes.add(LineAnalysisTreeDTO.EquipmentNode.builder()
.id(device.getId())
.name(device.getDeviceName())
.parameters(filteredParams.stream().map(this::convertToSimpleInfo).collect(Collectors.toList()))
.build());
}
return equipmentNodes;
}
private List<DeviceContactModelDO> filterDeviceLineParams(DeviceLineDO line, DeviceDO device,
List<DeviceContactModelDO> deviceParams,
boolean hasKeyword, String lowerKeyword) {
if (!hasKeyword || isDeviceLineNameMatched(line, lowerKeyword) || isDeviceMatched(device, lowerKeyword)) {
return new ArrayList<>(deviceParams);
}
return deviceParams.stream()
.filter(param -> param.getAttributeName() != null
&& param.getAttributeName().toLowerCase().contains(lowerKeyword))
.collect(Collectors.toList());
}
private boolean isDeviceLineNameMatched(DeviceLineDO line, String lowerKeyword) {
return (line.getName() != null && line.getName().toLowerCase().contains(lowerKeyword))
|| (line.getCode() != null && line.getCode().toLowerCase().contains(lowerKeyword));
}
private boolean isDeviceMatched(DeviceDO device, String lowerKeyword) {
return (device.getDeviceName() != null && device.getDeviceName().toLowerCase().contains(lowerKeyword))
|| (device.getDeviceCode() != null && device.getDeviceCode().toLowerCase().contains(lowerKeyword));
}
private LineAnalysisTreeDTO.ParameterNode convertToSimpleInfo(DeviceContactModelDO param) {
return LineAnalysisTreeDTO.ParameterNode.builder()
.id(param.getId())
.name(param.getAttributeName())
.code(param.getAttributeCode())
.type(param.getAttributeType())
.dataType(param.getDataType())
.unit(param.getDataUnit())
.ratio(param.getRatio())
.build();
}
private List<DeviceLineTreeRespVO> buildDeviceLineTree(List<DeviceLineDO> allDeviceLines) {
Map<Long, List<DeviceLineDO>> childrenMap = CollectionUtils.convertMultiMap(
allDeviceLines, DeviceLineDO::getParentId);

@ -27,6 +27,19 @@
WHERE id = #{id}
LIMIT 1
</select>
<select id="selectByDvIdExcludeId"
resultType="cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerDO">
SELECT id, device_code, device_name, dv_id
FROM mes_device_ledger
WHERE deleted = 0
AND dv_id = #{dvId}
<if test="excludeId != null">
AND id != #{excludeId}
</if>
LIMIT 1
</select>
<select id="selectPrintTemplate" resultType="java.lang.String">
select template_json
from mes_print_template
@ -35,4 +48,4 @@
and template_biz_type = 1
</select>
</mapper>
</mapper>

Loading…
Cancel
Save