From 6e62e95d3dc94cb20e1fb1284370b6302fb9523c Mon Sep 17 00:00:00 2001 From: siyang <2337720667@qq.com> Date: Tue, 10 Sep 2024 10:50:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=A9=E5=AE=9E=E4=BD=93=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E6=97=B6=EF=BC=8C=E5=90=8C=E6=AD=A5=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=88=B0=E6=95=B0=E6=8D=AE=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/thing/event/ThingChangedEvent.java | 29 ++++++++++++++++ .../listener/ThingChangedEventListener.java | 33 +++++++++++++++++++ .../impl/IotThingEntityServiceImpl.java | 13 ++++++++ 3 files changed, 75 insertions(+) create mode 100644 modules/thing/src/main/java/com/thing/event/ThingChangedEvent.java create mode 100644 modules/thing/src/main/java/com/thing/listener/ThingChangedEventListener.java diff --git a/modules/thing/src/main/java/com/thing/event/ThingChangedEvent.java b/modules/thing/src/main/java/com/thing/event/ThingChangedEvent.java new file mode 100644 index 0000000..8fb1508 --- /dev/null +++ b/modules/thing/src/main/java/com/thing/event/ThingChangedEvent.java @@ -0,0 +1,29 @@ +package com.thing.event; + +import com.thing.thing.entity.entity.IotThingEntity; + +import lombok.Getter; +import lombok.Setter; + +import org.springframework.context.ApplicationEvent; + +import java.io.Serial; + +/** + * @author siyang + * @date 2024/9/10 10:28 + * @description 物变更事件 + */ +@Getter +@Setter +public class ThingChangedEvent extends ApplicationEvent { + + @Serial private static final long serialVersionUID = 6767577249634996931L; + + private IotThingEntity thing; + + public ThingChangedEvent(Object source, IotThingEntity thing) { + super(source); + this.thing = thing; + } +} diff --git a/modules/thing/src/main/java/com/thing/listener/ThingChangedEventListener.java b/modules/thing/src/main/java/com/thing/listener/ThingChangedEventListener.java new file mode 100644 index 0000000..22b275c --- /dev/null +++ b/modules/thing/src/main/java/com/thing/listener/ThingChangedEventListener.java @@ -0,0 +1,33 @@ +package com.thing.listener; + +import com.mybatisflex.core.update.UpdateChain; +import com.thing.device.source.entity.IotThingSourceEntity; +import com.thing.event.ThingChangedEvent; +import com.thing.thing.entity.entity.IotThingEntity; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +/** + * @author siyang + * @date 2024/9/10 10:34 + * @description 物变更事件监听器 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class ThingChangedEventListener { + + @EventListener(ThingChangedEvent.class) + public void onThingChangedEvent(ThingChangedEvent event) { + IotThingEntity thing = event.getThing(); + UpdateChain.of(IotThingSourceEntity.class) + .set(IotThingSourceEntity::getThingName, thing.getName()) + .where(IotThingSourceEntity::getThingId).eq(thing.getId()) + .and(IotThingSourceEntity::getTenantCode).eq(thing.getTenantCode()) + .update(); + } +} diff --git a/modules/thing/src/main/java/com/thing/thing/entity/service/impl/IotThingEntityServiceImpl.java b/modules/thing/src/main/java/com/thing/thing/entity/service/impl/IotThingEntityServiceImpl.java index 32baf16..3f0b166 100644 --- a/modules/thing/src/main/java/com/thing/thing/entity/service/impl/IotThingEntityServiceImpl.java +++ b/modules/thing/src/main/java/com/thing/thing/entity/service/impl/IotThingEntityServiceImpl.java @@ -22,6 +22,7 @@ import com.thing.common.core.web.response.PageData; 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.sys.biz.entity.SysDeptEntity; import com.thing.sys.biz.mapper.SysDeptMapper; import com.thing.sys.oss.cloud.OSSFactory; @@ -60,6 +61,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -103,6 +105,9 @@ public class IotThingEntityServiceImpl extends BaseServiceImpl params) { String name = MapUtil.getStr(params, "name"); @@ -497,6 +502,10 @@ public class IotThingEntityServiceImpl extends BaseServiceImpl