Browse Source

物实体更新级联物关系更新

2024年9月12日15:08:47
thing_master
lishuai 1 year ago
parent
commit
05cdd44420
  1. 25
      modules/thing/src/main/java/com/thing/event/ThingRelationDetailUpdateEvent.java
  2. 54
      modules/thing/src/main/java/com/thing/listener/ThingChangedUpdateRelationEventListener.java
  3. 5
      modules/thing/src/main/java/com/thing/thing/entity/service/impl/IotThingEntityServiceImpl.java
  4. 2
      modules/thing/src/main/java/com/thing/thing/relation/detail/service/IotThingRelationDetailService.java
  5. 7
      modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java

25
modules/thing/src/main/java/com/thing/event/ThingRelationDetailUpdateEvent.java

@ -0,0 +1,25 @@
package com.thing.event;
import com.thing.thing.entity.entity.IotThingEntity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;
import java.util.List;
/**
* Author: SiYang
* Date: 2023/12/01 14:39
* Description: 新增物关系子节点事件
*/
@Getter
@Setter
public class ThingRelationDetailUpdateEvent extends ApplicationEvent {
private List<IotThingEntity> list;
public ThingRelationDetailUpdateEvent(Object source, List<IotThingEntity> list) {
super(source);
this.list = list;
}
}

54
modules/thing/src/main/java/com/thing/listener/ThingChangedUpdateRelationEventListener.java

@ -0,0 +1,54 @@
package com.thing.listener;
import com.thing.common.cache.constants.CacheNameEnum;
import com.thing.event.ThingRelationDetailUpdateEvent;
import com.thing.thing.cache.service.ThingCache;
import com.thing.thing.entity.entity.IotThingEntity;
import com.thing.thing.relation.detail.entity.IotThingRelationDetailEntity;
import com.thing.thing.relation.detail.service.IotThingRelationDetailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author siyang
* @date 2024/9/10 10:34
* @description 物变更事件监听器
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class ThingChangedUpdateRelationEventListener {
private final IotThingRelationDetailService thingRelationDetailService;
private final ThingCache cache;
@EventListener(ThingRelationDetailUpdateEvent.class)
public void onThingChangedEvent(ThingRelationDetailUpdateEvent event) {
List<IotThingEntity> list = event.getList();
for (IotThingEntity thing : list) {
List<IotThingRelationDetailEntity> fromEntities = thingRelationDetailService.findByFromIdAndToId(thing.getId(),null);
if(CollectionUtils.isNotEmpty(fromEntities)){
for (IotThingRelationDetailEntity entity : fromEntities) {
entity.setFromName(thing.getName());
}
thingRelationDetailService.updateBatch(fromEntities);
}
List<IotThingRelationDetailEntity> toEntities = thingRelationDetailService.findByFromIdAndToId(null, thing.getId());
if(CollectionUtils.isNotEmpty(toEntities)){
for (IotThingRelationDetailEntity entity : toEntities) {
entity.setToName(thing.getName());
}
thingRelationDetailService.updateBatch(toEntities);
}
}
cache.clearTopic(CacheNameEnum.THING_DETAIL_RELATION);
}
}

5
modules/thing/src/main/java/com/thing/thing/entity/service/impl/IotThingEntityServiceImpl.java

@ -23,6 +23,7 @@ import com.thing.common.data.tskv.TsKvDTO;
import com.thing.common.orm.service.impl.BaseServiceImpl;
import com.thing.common.tskv.service.TsKvService;
import com.thing.event.ThingChangedEvent;
import com.thing.event.ThingRelationDetailUpdateEvent;
import com.thing.sys.biz.entity.SysDeptEntity;
import com.thing.sys.biz.mapper.SysDeptMapper;
import com.thing.sys.oss.cloud.OSSFactory;
@ -531,6 +532,7 @@ public class IotThingEntityServiceImpl extends BaseServiceImpl<IotThingEntityMap
if (nameChanged) {
appEventPublisher.publishEvent(new ThingChangedEvent(this, thingEntity));
appEventPublisher.publishEvent(new ThingRelationDetailUpdateEvent(this, Lists.newArrayList(thingEntity)));
}
}
@ -693,6 +695,9 @@ public class IotThingEntityServiceImpl extends BaseServiceImpl<IotThingEntityMap
}
cache.clearTopic(CacheNameEnum.THING_MODEL);
cache.clearTopic(CacheNameEnum.THING_ENTITY);
appEventPublisher.publishEvent(new ThingRelationDetailUpdateEvent(this, list));
}
}

2
modules/thing/src/main/java/com/thing/thing/relation/detail/service/IotThingRelationDetailService.java

@ -25,6 +25,8 @@ public interface IotThingRelationDetailService extends IBaseService<IotThingRela
IotThingRelationDetailEntity findByRootIdAndRootThingId(Long rootId,Long rootThingId);
List<IotThingRelationDetailEntity> findByFromIdAndToId(Long fromId,Long toId);
List<IotThingRelationDetailDTO> findByRootId(Long rootId);
Long findByMaxSort(Long rootId);

7
modules/thing/src/main/java/com/thing/thing/relation/detail/service/impl/IotThingRelationDetailServiceImpl.java

@ -191,6 +191,13 @@ public class IotThingRelationDetailServiceImpl extends BaseServiceImpl<IotThingR
.eq(IotThingRelationDetailEntity::getFromId, rootId));
}
@Override
public List<IotThingRelationDetailEntity> findByFromIdAndToId(Long fromId, Long toId) {
return mapper.selectListByQuery(QueryWrapper.create()
.eq(IotThingRelationDetailEntity::getToId, toId,!Objects.isNull(toId))
.eq(IotThingRelationDetailEntity::getFromId, fromId,!Objects.isNull(fromId)));
}
@Override
public List<IotThingRelationDetailDTO> findByRootId(Long rootId) {
return mapper.selectListByQueryAs(QueryWrapper.create()

Loading…
Cancel
Save