Browse Source

Merge remote-tracking branch 'origin/master'

thing_master
xiachao 1 year ago
parent
commit
c3847d85fe
  1. 12
      modules/thing/src/main/java/com/thing/device/source/controller/IotThingSourceController.java
  2. 4
      modules/thing/src/main/java/com/thing/device/source/dto/IotThingSourceDTO.java
  3. 4
      modules/thing/src/main/java/com/thing/device/source/dto/IotThingSourceListDTO.java
  4. 3
      modules/thing/src/main/java/com/thing/device/source/entity/IotThingSourceEntity.java
  5. 7
      modules/thing/src/main/java/com/thing/device/source/service/IotThingSourceService.java
  6. 65
      modules/thing/src/main/java/com/thing/device/source/service/impl/IotThingSourceServiceImpl.java
  7. 227
      modules/thing/src/main/java/com/thing/listener/QueueDeviceEventListener.java
  8. 33
      modules/thing/src/main/java/com/thing/thing/cache/service/ThingCache.java
  9. 2
      modules/thing/src/main/java/com/thing/thing/dict/service/impl/IotThingDictServiceImpl.java

12
modules/thing/src/main/java/com/thing/device/source/controller/IotThingSourceController.java

@ -142,4 +142,16 @@ public class IotThingSourceController {
return new Result<>();
}
@GetMapping("attrGroup")
@Operation(summary="标签组")
public Result<List<String>> attrGroup() {
return new Result<List<String>>().ok(iotThingSourceService.attrGroup());
}
@PostMapping("attrGroupList")
@Operation(summary="标签组")
public Result<List<IotThingSourceDTO>> attrGroupRootId(@RequestBody IotThingSourceReqDTO iotThingSourceDTO) {
return new Result<List<IotThingSourceDTO>>().ok(iotThingSourceService.attrGroupRootId(iotThingSourceDTO));
}
}

4
modules/thing/src/main/java/com/thing/device/source/dto/IotThingSourceDTO.java

@ -94,6 +94,10 @@ public class IotThingSourceDTO implements Serializable {
private Long updateDate;
@Schema(description = "数据处理规则js")
private String dataRule;
@Schema(description = "物属性名称组(标签组)")
private String thingAttrGroup;
@Schema(description = "启动标志:0启动 1不启动")
private String startStatus;
// @JsonIgnore
// private Long thingTenantId;
// /** 批量新增 同步 或者 更新 功能 **/

4
modules/thing/src/main/java/com/thing/device/source/dto/IotThingSourceListDTO.java

@ -80,5 +80,9 @@ public class IotThingSourceListDTO implements Serializable {
private String extendData;
@Schema(description = "数据处理规则js")
private String dataRule;
@Schema(description = "物属性名称组(标签组)")
private String thingAttrGroup;
@Schema(description = "启动标志:0启动 1不启动")
private String startStatus;
}

3
modules/thing/src/main/java/com/thing/device/source/entity/IotThingSourceEntity.java

@ -81,6 +81,9 @@ public class IotThingSourceEntity extends BaseInfoEntity {
//
// private Long updateDate;
private String thingAttrGroup;
private String startStatus;
}

7
modules/thing/src/main/java/com/thing/device/source/service/IotThingSourceService.java

@ -20,6 +20,9 @@ public interface IotThingSourceService extends IBaseService<IotThingSourceEntity
List<IotThingSourceDTO> findAllByFromIdAndThingIdAndConfigTypeAndAttrCode(Long fromId, List<Long> thingIds, String configType, Long rootId, Collection<String> attrCodes);
List<IotThingSourceDTO> findAllByFromIdAndThingIdAndConfigTypeAndAttrCodeAndAttrGroup(Long fromId, List<Long> thingIds, String configType,
Long rootId, Collection<String> attrCodes,String attrGroup);
List<IotThingSourceDTO> findAllByFromIdAndThingIdAndConfigType(List<Long> fromIds, List<Long> thingIds, String configType, List<Long> rootIds);
IotThingSourceDTO findById(Long id);
@ -43,4 +46,8 @@ public interface IotThingSourceService extends IBaseService<IotThingSourceEntity
List<IotThingSourceAttrRespDTO> getAttrListByThingRelationDTONew(IotThingSourceGetAttrDTO iotThingSourceGetAttrDTO);
void dragAndDrop(Long id,Long toId,Long sort);
List<String> attrGroup();
List<IotThingSourceDTO> attrGroupRootId(IotThingSourceReqDTO iotThingSourceDTO);
}

65
modules/thing/src/main/java/com/thing/device/source/service/impl/IotThingSourceServiceImpl.java

@ -116,6 +116,20 @@ public class IotThingSourceServiceImpl extends BaseServiceImpl<IotThingSourceMap
.eq(IotThingSourceEntity::getTenantCode, UserContext.getRealTenantCode()), IotThingSourceDTO.class);
}
@Override
public List<IotThingSourceDTO> findAllByFromIdAndThingIdAndConfigTypeAndAttrCodeAndAttrGroup(Long fromId, List<Long> thingIds, String configType, Long rootId, Collection<String> attrCodes, String attrGroup) {
return mapper.selectListByQueryAs(
new QueryWrapper()
.eq(IotThingSourceEntity::getFromId, fromId, Objects::nonNull)
.in(IotThingSourceEntity::getThingId, thingIds, CollectionUtil.isNotEmpty(thingIds))
.eq(IotThingSourceEntity::getConfigType, configType, StringUtils::isNotEmpty)
.eq(IotThingSourceEntity::getRootId, rootId, Objects::nonNull)
.in(IotThingSourceEntity::getThingAttrCode, attrCodes, CollectionUtil.isNotEmpty(attrCodes))
.eq(IotThingSourceEntity::getTenantCode, UserContext.getRealTenantCode())
.eq(IotThingSourceEntity::getThingAttrGroup, attrGroup,StringUtils.isNotBlank(attrGroup))
, IotThingSourceDTO.class);
}
@Override
public List<IotThingSourceDTO> findAllByFromIdAndThingIdAndConfigType(List<Long> fromIds, List<Long> thingIds, String configType, List<Long> rootIds) {
return mapper.selectListByQueryAs(
@ -134,9 +148,9 @@ public class IotThingSourceServiceImpl extends BaseServiceImpl<IotThingSourceMap
@Override
public void save(IotThingSourceDTO iotThingSourceDTO) {
List<IotThingSourceDTO> iotThingSourceDTOS = findAllByFromIdAndThingIdAndConfigTypeAndAttrCode(iotThingSourceDTO.getFromId(),
List<IotThingSourceDTO> iotThingSourceDTOS = findAllByFromIdAndThingIdAndConfigTypeAndAttrCodeAndAttrGroup(iotThingSourceDTO.getFromId(),
Collections.singletonList(iotThingSourceDTO.getThingId()), iotThingSourceDTO.getConfigType(), iotThingSourceDTO.getRootId(),
Collections.singletonList(iotThingSourceDTO.getThingAttrCode()));
Collections.singletonList(iotThingSourceDTO.getThingAttrCode()), iotThingSourceDTO.getThingAttrGroup());
if (CollectionUtil.isNotEmpty(iotThingSourceDTOS)) {
throw new SysException("同一个数据源下不允许重复添加相关属性数据:" + iotThingSourceDTO.getThingAttrCode());
}
@ -167,7 +181,10 @@ public class IotThingSourceServiceImpl extends BaseServiceImpl<IotThingSourceMap
.setThingIcon(iotThingSourceDTO.getThingIcon())
.setThingSerial(iotThingSourceDTO.getThingSerial())
.setDataTreatingMark(iotThingSourceDTO.getDataTreatingMark())
.setDataDealConfig(iotThingSourceDTO.getDataDealConfig());
.setDataDealConfig(iotThingSourceDTO.getDataDealConfig())
.setThingAttrGroup(iotThingSourceDTO.getThingAttrGroup())
.setStartStatus(iotThingSourceDTO.getStartStatus())
;
mapper.update(iotThingSourceEntity);
}
@ -280,7 +297,10 @@ public class IotThingSourceServiceImpl extends BaseServiceImpl<IotThingSourceMap
.setFromCode(entityDTO.getCode())
.setFromName(entityDTO.getName())
.setRemark(params.getRemark())
.setExtendData(params.getExtendData());
.setExtendData(params.getExtendData())
// .setThingAttrGroup(params.getThingAttrGroup())
// .setStartStatus(iotThingSourceDTO.getStartStatus())
;
iotThingSourceEntity.setTenantCode(UserContext.getRealTenantCode())
.setCompanyId(UserContext.getRealTenantCode())
.setDeptId(UserContext.getRealTenantCode());
@ -533,6 +553,43 @@ public class IotThingSourceServiceImpl extends BaseServiceImpl<IotThingSourceMap
this.updateBatch(resList);
}
@Override
public List<String> attrGroup() {
List<String> strings = mapper.selectListByQueryAs(QueryWrapper.create()
.select(IOT_THING_SOURCE_ENTITY.THING_ATTR_GROUP).eq(IotThingSourceEntity::getTenantCode, UserContext.getRealTenantCode()), String.class);
if(CollectionUtils.isEmpty(strings)){
return Lists.newArrayList();
}
return strings.stream().filter(s -> !StringUtils.isBlank(s)).distinct().collect(Collectors.toList());
}
@Override
public List<IotThingSourceDTO> attrGroupRootId(IotThingSourceReqDTO iotThingSourceDTO) {
List<IotThingSourceRelationDTO> iotThingSourceRelationDTOList = iotThingSourceDTO.getIotThingSourceRelationDTOList();
if (CollectionUtil.isEmpty(iotThingSourceRelationDTOList)) {
return null;
}
List<Long> thingIds = iotThingSourceRelationDTOList.stream().map(IotThingSourceRelationDTO::getThingId).toList();
List<Long> rootIds = iotThingSourceRelationDTOList.stream().map(IotThingSourceRelationDTO::getRootId).distinct().toList();
List<IotThingSourceDTO> iotThingSourceDTOS = mapper.selectListByQueryAs(QueryWrapper.create()
.in(IotThingSourceEntity::getFromId, thingIds, CollectionUtil.isNotEmpty(thingIds))
.in(IotThingSourceEntity::getRootId, rootIds, CollectionUtil.isNotEmpty(rootIds))
.eq(IotThingSourceEntity::getConfigType, iotThingSourceDTO.getConfigType())
.eq(IotThingSourceEntity::getThingAttrCodeType, iotThingSourceDTO.getThingAttrCodeType(),
StringUtils.isNotBlank(iotThingSourceDTO.getThingAttrCodeType()))
.eq(IotThingSourceEntity::getTenantCode, UserContext.getRealTenantCode())
.eq(IotThingSourceEntity::getStartStatus, "0")
.orderBy(IotThingSourceEntity::getSort,true)
, IotThingSourceDTO.class
);
// 根据 thingAttrGroup 去重
Set<String> seen = new LinkedHashSet<>();
return iotThingSourceDTOS.stream()
.filter(dto -> StringUtils.isNotBlank(dto.getThingAttrGroup()) && seen.add(dto.getThingAttrGroup()))
.toList();
}
/**
* 自定义函数去重
*

227
modules/thing/src/main/java/com/thing/listener/QueueDeviceEventListener.java

@ -1,14 +1,12 @@
package com.thing.listener;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.thing.common.cache.constants.CacheNameEnum;
import com.thing.common.core.enumeration.GateWayStatus;
import com.thing.common.core.enumeration.QueueOriginType;
import com.thing.common.core.enumeration.TemplateMark;
import com.thing.common.core.enumeration.ThingStatus;
import com.thing.common.core.event.AuthParam;
import com.thing.common.core.event.QueueDeviceEvent;
import com.thing.common.core.utils.ConvertUtils;
import com.thing.common.core.utils.DateTimeUtils;
@ -16,9 +14,9 @@ import com.thing.common.core.utils.JacksonUtil;
import com.thing.common.core.utils.TokenGenerator;
import com.thing.common.data.dto.QueueMsgDTO;
import com.thing.thing.cache.service.ThingCache;
import com.thing.thing.context.service.ThingManageContextService;
import com.thing.thing.entity.dto.IotThingViewDTO;
import com.thing.thing.entity.entity.IotThingEntity;
import com.thing.thing.entity.service.IotThingEntityService;
import com.thing.thing.model.dto.IotThingModelDTO;
import com.thing.thing.model.entity.IotThingModelEntity;
import com.thing.thing.model.service.IotThingModelService;
@ -31,6 +29,7 @@ import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
@ -38,10 +37,10 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class QueueDeviceEventListener {
private final ThingManageContextService thingManageContextService;
private final IotThingModelService thingModelService;
private final IotThingEntityService entityService;
private final ThingCache thingCache;
@EventListener(QueueDeviceEvent.class)
@ -55,94 +54,128 @@ public class QueueDeviceEventListener {
if (CollectionUtil.isEmpty(validMsgList)) {
return;
}
//新增和更新物模型信息
Map<String, String> codeOriginMap = validMsgList.parallelStream()
.collect(Collectors.toMap(QueueMsgDTO::getThingCode,
item -> (QueueOriginType.AUTO_DATA_SYNC.name().equals(item.getOrigin())
|| QueueOriginType.MANUAL_DATA_SYNC.name().equals(item.getOrigin()))
? QueueOriginType.TB.name() : item.getOrigin(), (e1, e2) -> e1));
//先根据物模型编码去重
Map<String, Map<Long, List<QueueMsgDTO>>> codeMapList = validMsgList.stream()
.collect(Collectors.groupingBy(QueueMsgDTO::getThingCode, Collectors.groupingBy(QueueMsgDTO::getTenantCode)));
//物模型插入
List<IotThingModelEntity> insertModelList = new ArrayList<>();
//物模型更新
List<IotThingModelEntity> updateModelList = new ArrayList<>();
//筛选物模型的插入和更新
checkAndSaveThingModels(codeOriginMap, insertModelList,updateModelList);
//物实体的插入
List<IotThingEntity> insertEntityList = new ArrayList<>();
//组装物实体和物模型
for (Map.Entry<String, Map<Long, List<QueueMsgDTO>>> entry : codeMapList.entrySet()) {
String thingCode = entry.getKey();
//分用户admin用户不用创建物实体其他用户需要创建物实体
Map<Long, List<QueueMsgDTO>> valueEntry = entry.getValue();
for (Map.Entry<Long, List<QueueMsgDTO>> thingEntry : valueEntry.entrySet()) {
Long tenantCode = thingEntry.getKey();
QueueMsgDTO queueMsgDTO = thingEntry.getValue().get(0);
//物模型缓存查询
ObjectNode jsonObject = thingCache.findAccurateObjectNode(CacheNameEnum.THING_MODEL, thingCode);
//缓存存在更新否则插入
if (null != jsonObject && !jsonObject.isEmpty()) {
IotThingModelEntity modelEntity = JacksonUtil.convertValue(jsonObject, IotThingModelEntity.class);
updateModelList.add(modelEntity);
} else {
String origin = QueueOriginType.AUTO_DATA_SYNC.name().equals(queueMsgDTO.getOrigin())
|| QueueOriginType.MANUAL_DATA_SYNC.name().equals(queueMsgDTO.getOrigin())
? QueueOriginType.TB.name() : queueMsgDTO.getOrigin();
IotThingModelEntity modelEntity = new IotThingModelEntity()
.setCode(thingCode)
.setToken(TokenGenerator.generateValue())
.setGateway(GateWayStatus.NO_GATE_WAY.getValue())
.setStatus(ThingStatus.NOT_CONNECTED.getCode())
.setAuthNum(0L)
.setStatusTs(DateTimeUtils.getCurrentTime())
.setOrigin(origin);
// modelEntity.setId(IdUtil.getSnowflake().nextId())
// .setCreateDate(DateTimeUtils.getCurrentTime())
// .setCreateDate(DateTimeUtils.getCurrentTime());
insertModelList.add(modelEntity);
}
//物实体的构建
if (!Objects.equals(queueMsgDTO.getTenantCode(), 1001L)) {
ObjectNode entityNode = thingCache.findObjectNode(CacheNameEnum.THING_ENTITY, tenantCode + ":" + thingCode);
if (null == entityNode || entityNode.isEmpty()) {
IotThingEntity newThingEntity = createThingEntity(thingCode, queueMsgDTO.getTenantCode(), queueMsgDTO.getCompanyId(), queueMsgDTO.getDeptId());
insertEntityList.add(newThingEntity);
}
}
}
}
//更新老的物模型
if (CollectionUtils.isNotEmpty(updateModelList)) {
insertModelList.addAll(updateModelList);
}
//更新物实体的模型uniqueModelList有值物实体不一定有值
List<IotThingEntity> distinctStudentFile = new ArrayList<>();
if (CollectionUtils.isNotEmpty(insertModelList)) {
// 物实体
Map<AuthParam, Set<String>> tenantThingCodeMap =
validMsgList.stream()
.filter(item -> Objects.nonNull(item.getTenantCode())
|| Objects.nonNull(item.getCompanyId())
|| Objects.nonNull(item.getDeptId()))
.collect(Collectors.groupingBy(
item ->new AuthParam(item.getTenantCode(),item.getCompanyId(),item.getDeptId()),
Collectors.mapping(QueueMsgDTO::getThingCode,Collectors.toSet())));
List<IotThingEntity> insertEntityList = new ArrayList<>();
saveTenantThingList(tenantThingCodeMap, insertEntityList);
insertModelList.forEach(item -> {
long count = insertEntityList.stream().filter(e -> StringUtils.equals(e.getCode(), item.getCode())).count();
List<ObjectNode> keyMap = thingCache.findKeyMap(CacheNameEnum.THING_ENTITY, item.getCode());
item.setAuthNum(count+CollectionUtils.size(keyMap));
});
thingModelService.saveBatch(insertModelList);
//插入物实体
if(CollectionUtils.isNotEmpty(insertEntityList)){
List<IotThingEntity> distinctStudentFile = insertEntityList.stream()
//物实体的插入和缓存更新
if (CollectionUtils.isNotEmpty(insertEntityList)) {
distinctStudentFile.addAll(insertEntityList.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCode() + ";" + o.getTenantCode()))), ArrayList::new));
thingManageContextService.saveEntity(distinctStudentFile);
updateEntityCache(distinctStudentFile);
Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(o -> o.getCode() + ";" + o.getTenantCode()))), ArrayList::new)));
}
}
if(CollectionUtils.isNotEmpty(updateModelList)){
updateModelList.forEach(item -> {
long count = updateModelList.stream().filter(e -> StringUtils.equals(e.getCode(), item.getCode())).count();
List<ObjectNode> keyMap = thingCache.findKeyMap(CacheNameEnum.THING_ENTITY, item.getCode());
item.setAuthNum(count+CollectionUtils.size(keyMap));
});
thingModelService.updateBatch(updateModelList);
insertModelList.addAll(updateModelList);
//更新物模型和物实体的缓存
insertModelList.forEach(item -> {
long count = distinctStudentFile.stream().filter(e -> StringUtils.equals(e.getCode(), item.getCode())).count();
List<ObjectNode> keyMap = thingCache.findKeyMap(CacheNameEnum.THING_ENTITY, item.getCode());
item.setAuthNum(count + CollectionUtils.size(keyMap));
});
if(CollectionUtils.isNotEmpty(insertModelList)){
thingModelService.saveOrUpdateBatch(insertModelList);
updateModelCache(insertModelList);
}
if(CollectionUtils.isNotEmpty(distinctStudentFile)){
entityService.saveOrUpdateBatch(distinctStudentFile);
updateEntityCache(distinctStudentFile);
}
//更新物模板缓存
updateModleCache(insertModelList);
} catch (Exception e) {
log.error("设备保存失败: {}", e.getMessage(), e);
}
}
private void updateModleCache(List<IotThingModelEntity> insertModelList) {
private void updateModelCache(List<IotThingModelEntity> insertModelList) {
List<IotThingModelDTO> iotThingModelDTOS = ConvertUtils.sourceToTarget(insertModelList, IotThingModelDTO.class);
List<ObjectNode> modelList = JsonConverter.convertToJsonObjectListObjectNode(iotThingModelDTOS);
for (ObjectNode item : modelList) {
//更新物模型的缓存
thingCache.updateKeyMap(CacheNameEnum.THING_MODEL
, item.get(CacheNameEnum.ModelField.THING_MODEL_CODE.getField()).asText()
+ ":" + item.get(CacheNameEnum.ModelField.THING_MODEL_ID.getField()).asText(),item);
List<ObjectNode> topicMap = thingCache.getTopicMap(CacheNameEnum.THING_MODEL);
if(CollectionUtils.isNotEmpty(topicMap)){
Map<String, ObjectNode> modelMap = modelList.stream().collect(Collectors.toMap(
model -> model.get(CacheNameEnum.ModelField.THING_MODEL_CODE.getField()).asText()
+ ":" + model.get(CacheNameEnum.ModelField.THING_MODEL_ID.getField()).asText()
, Function.identity()));
thingCache.putMap(CacheNameEnum.THING_MODEL, modelMap);
}else{
for (ObjectNode item : modelList) {
//更新物模型的缓存
thingCache.updateKeyMap(CacheNameEnum.THING_MODEL
, item.get(CacheNameEnum.ModelField.THING_MODEL_CODE.getField()).asText()
+ ":" + item.get(CacheNameEnum.ModelField.THING_MODEL_ID.getField()).asText(), item);
}
}
}
private void updateEntityCache(List<IotThingEntity> distinctStudentFile) {
//更新物实体缓存
List<IotThingViewDTO> entityList = distinctStudentFile.stream().map(e -> {
ObjectNode keyMap = thingCache.getKeyMap(CacheNameEnum.THING_ENTITY, e.getCode());
ObjectNode modelMap = thingCache.findAccurateObjectNode(CacheNameEnum.THING_MODEL, e.getCode());
return new IotThingViewDTO()
.setLat(e.getLat())
.setLon(e.getLon())
.setTags(e.getTags())
.setDeptIds(e.getDeptIds())
.setOrigin(keyMap.get(CacheNameEnum.ModelField.THING_MODEL_ORIGIN.getField()).asText())
.setOrigin(modelMap.get(CacheNameEnum.ModelField.THING_MODEL_ORIGIN.getField()).asText())
.setTemplateMark(TemplateMark.NO.getValue())
.setRealType("1")
.setImg(e.getImg())
.setRemark(e.getRemark())
.setEnableStatus(e.getEnableStatus())
.setStatusTs(keyMap.get(CacheNameEnum.ModelField.THING_MODEL_STATUS_TS.getField()).asLong())
.setStatus(keyMap.get(CacheNameEnum.ModelField.THING_MODEL_STATUS.getField()).asText())
.setStatusTs(modelMap.get(CacheNameEnum.ModelField.THING_MODEL_STATUS_TS.getField()).asLong())
.setStatus(modelMap.get(CacheNameEnum.ModelField.THING_MODEL_STATUS.getField()).asText())
.setTenantCode(e.getTenantCode())
.setCompanyId(e.getTenantCode())
.setDeptId(e.getTenantCode())
@ -150,88 +183,30 @@ public class QueueDeviceEventListener {
.setEntityName(e.getName())
.setEntityCode(e.getCode())
.setEntityId(e.getId())
.setModelId(keyMap.get(CacheNameEnum.ModelField.THING_MODEL_ID.getField()).asLong())
.setModelId(modelMap.get(CacheNameEnum.ModelField.THING_MODEL_ID.getField()).asLong())
.setCreateDate(e.getCreateDate());
}).toList();
List<ObjectNode> entityJsonList = JsonConverter.convertToJsonObjectListObjectNode(entityList);
for (ObjectNode entityNode : entityJsonList) {
thingCache.updateKeyMap(CacheNameEnum.THING_ENTITY
,entityNode.get(CacheNameEnum.EntityField.THING_ENTITY_TENANT_CODE.getField()).asText()
, entityNode.get(CacheNameEnum.EntityField.THING_ENTITY_TENANT_CODE.getField()).asText()
+ ":" + entityNode.get(CacheNameEnum.EntityField.THING_ENTITY_CODE.getField()).asText()
+ ":" + entityNode.get(CacheNameEnum.EntityField.THING_ENTITY_ID.getField()).asText(),entityNode);
}
}
/**
* 物管理表是否存在不存在新增设备
*
* @param codeOriginMap 物编码
*/
private void checkAndSaveThingModels(Map<String, String> codeOriginMap, List<IotThingModelEntity> insertList,List<IotThingModelEntity> updateList) {
// 使用迭代器遍历并删除不符合条件的元素
Iterator<Map.Entry<String, String>> iterator = codeOriginMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
String code = entry.getKey();
ObjectNode jsonObject = thingCache.findObjectNode(CacheNameEnum.THING_MODEL, code);
if (null != jsonObject && !jsonObject.isEmpty()) {
IotThingModelEntity modelEntity = JacksonUtil.convertValue(jsonObject, IotThingModelEntity.class);
updateList.add(modelEntity);
iterator.remove();
}
+ ":" + entityNode.get(CacheNameEnum.EntityField.THING_ENTITY_ID.getField()).asText(), entityNode);
}
codeOriginMap.forEach((thingCode, origin) -> {
IotThingModelEntity modelEntity = new IotThingModelEntity()
.setCode(thingCode)
.setToken(TokenGenerator.generateValue())
.setGateway(GateWayStatus.NO_GATE_WAY.getValue())
.setStatus(ThingStatus.NOT_CONNECTED.getCode())
.setAuthNum(0L)
.setStatusTs(DateTimeUtils.getCurrentTime())
.setOrigin(origin);
modelEntity.setId(IdUtil.getSnowflake().nextId())
.setCreateDate(DateTimeUtils.getCurrentTime())
.setCreateDate(DateTimeUtils.getCurrentTime())
;
insertList.add(modelEntity);
});
}
/**
* 保存物实体
*
* @param tenantThingCodeMap 物实体
*/
private void saveTenantThingList(Map<AuthParam, Set<String>> tenantThingCodeMap, List<IotThingEntity> insertList) {
tenantThingCodeMap.forEach(
(authParam, thingCodes) -> {
if (CollectionUtil.isEmpty(thingCodes)) {
return;
}
thingCodes.removeIf(code -> {
ObjectNode keyMap = thingCache.findObjectNode(CacheNameEnum.THING_ENTITY, authParam.getTenantCode() + ":" + code);
return null != keyMap && !keyMap.isEmpty();
});
if (CollectionUtil.isNotEmpty(thingCodes)) {
for (String newThingCode : thingCodes) {
insertList.add(createThingEntity(newThingCode, authParam));
}
}
});
}
private IotThingEntity createThingEntity(String thingCode, AuthParam authParam) {
private IotThingEntity createThingEntity(String thingCode, Long tenantCode, Long companyId, Long deptId) {
IotThingEntity entity = new IotThingEntity();
entity.setId(IdUtil.getSnowflake().nextId());
// entity.setId(IdUtil.getSnowflake().nextId());
entity.setCode(thingCode);
entity.setName(thingCode);
entity.setEnableStatus("1");
entity.setRealType("1");
entity.setTemplateMark("0");
entity.setType("默认物类型");
entity.setTenantCode(authParam.getTenantCode());
entity.setCompanyId(authParam.getCompanyId());
entity.setDeptIds(authParam.getDeptId().toString());
entity.setTenantCode(tenantCode);
entity.setCompanyId(companyId);
entity.setDeptIds(deptId.toString());
return entity;
}

33
modules/thing/src/main/java/com/thing/thing/cache/service/ThingCache.java

@ -6,10 +6,7 @@ import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@ -90,6 +87,12 @@ public final class ThingCache {
return valMap.entrySet().stream().filter(entry -> entry.getKey().contains(key)).map(Map.Entry::getValue).collect(Collectors.toList());
}
/**
* 模糊匹配
* @param topic
* @param key
* @return
*/
public ObjectNode findObjectNode(String topic, String key) {
ConcurrentHashMap<String, ObjectNode> valMap = thingMap.get(topic);
if(MapUtils.isEmpty(valMap)){
@ -99,6 +102,27 @@ public final class ThingCache {
return first.orElse(null);
}
/**
* 精确匹配
* @param topic
* @param key
* @return
*/
public ObjectNode findAccurateObjectNode(String topic, String key) {
ConcurrentHashMap<String, ObjectNode> valMap = thingMap.get(topic);
if(MapUtils.isEmpty(valMap)){
return null;
}
Optional<ObjectNode> first = valMap.entrySet().stream().filter(entry ->
Arrays.asList(entry.getKey().split(":")).contains(key)
).map(Map.Entry::getValue).findFirst();
return first.orElse(null);
}
public void deleteKeyMap(String topic, String key) {
ConcurrentHashMap<String, ObjectNode> valMap = thingMap.get(topic);
if(MapUtils.isEmpty(valMap)){
@ -132,6 +156,7 @@ public final class ThingCache {
}
//直接利用key覆盖
valMap.put(key,value);
thingMap.put(topic,valMap);
}
public List<ObjectNode> findAllKeyMap(String topic, Collection<String> key) {

2
modules/thing/src/main/java/com/thing/thing/dict/service/impl/IotThingDictServiceImpl.java

@ -251,7 +251,7 @@ public class IotThingDictServiceImpl extends BaseServiceImpl<IotThingDictMapper,
throw new SysException("字典编码和字典类型不能为空,请检查下excel数据");
}
Map<String, Long> codeCountMap = sheetData.stream()
.collect(Collectors.groupingBy(IotThingDictExcel::getCode, Collectors.counting())); // 使用 groupingBy 收集器按照 code 进行分组并计数
.collect(Collectors.groupingBy(s-> s.getCode()+":"+s.getGroupName(), Collectors.counting())); // 使用 groupingBy 收集器按照 code 进行分组并计数
String duplicates = codeCountMap.entrySet().stream()
.filter(entry -> entry.getValue() > 1) // 过滤出计数大于 1 entry
.map(Map.Entry::getKey) // 获取过滤后的 entry key即重复的 code

Loading…
Cancel
Save