Browse Source

物实体修改名称时,同步修改到数据源

thing_master
siyang 1 year ago
parent
commit
6e62e95d3d
  1. 29
      modules/thing/src/main/java/com/thing/event/ThingChangedEvent.java
  2. 33
      modules/thing/src/main/java/com/thing/listener/ThingChangedEventListener.java
  3. 13
      modules/thing/src/main/java/com/thing/thing/entity/service/impl/IotThingEntityServiceImpl.java

29
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;
}
}

33
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();
}
}

13
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<IotThingEntityMap
@Resource
private IotGroupRelationService groupRelationService;
@Resource
private ApplicationEventPublisher appEventPublisher;
@Override
public QueryWrapper getWrapper(Map<String, Object> params) {
String name = MapUtil.getStr(params, "name");
@ -497,6 +502,10 @@ public class IotThingEntityServiceImpl extends BaseServiceImpl<IotThingEntityMap
tags = StringUtils.join(dto.getTags(),",");
}
}
// 校验名称是否修改如果修改了则发送事件去修改thingSource中的名称
boolean nameChanged = !ObjectUtil.equals(dto.getName(), thingEntity.getName());
thingEntity.setCode(code)
.setName(BizUtils.trimAll(dto.getName()))
.setRemark(dto.getRemark())
@ -519,6 +528,10 @@ public class IotThingEntityServiceImpl extends BaseServiceImpl<IotThingEntityMap
mapper.update(thingEntity);
//更新物实体缓存
cache.clearTopic(CacheNameEnum.THING_ENTITY);
if (nameChanged) {
appEventPublisher.publishEvent(new ThingChangedEvent(this, thingEntity));
}
}
@Override

Loading…
Cancel
Save