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