|
@@ -21,6 +21,7 @@ import org.thingsboard.server.common.data.id.EntityId; |
|
@@ -21,6 +21,7 @@ import org.thingsboard.server.common.data.id.EntityId; |
21
|
import org.thingsboard.server.common.data.id.TenantId;
|
21
|
import org.thingsboard.server.common.data.id.TenantId;
|
22
|
import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
|
22
|
import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
|
23
|
import org.thingsboard.server.common.data.yunteng.constant.ModelConstants;
|
23
|
import org.thingsboard.server.common.data.yunteng.constant.ModelConstants;
|
|
|
24
|
+import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils;
|
24
|
import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException;
|
25
|
import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException;
|
25
|
import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
|
26
|
import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
|
26
|
import org.thingsboard.server.common.data.yunteng.dto.*;
|
27
|
import org.thingsboard.server.common.data.yunteng.dto.*;
|
|
@@ -56,9 +57,11 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev |
|
@@ -56,9 +57,11 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev |
56
|
private final ConvertConfigService convertConfigService;
|
57
|
private final ConvertConfigService convertConfigService;
|
57
|
private final TkUserCollectService userCollectService;
|
58
|
private final TkUserCollectService userCollectService;
|
58
|
private final TkDeviceProfileService tkDeviceProfileService;
|
59
|
private final TkDeviceProfileService tkDeviceProfileService;
|
|
|
60
|
+ private final CacheUtils cacheUtils;
|
|
|
61
|
+ private final String cacheName = FastIotConstants.CacheConfigKey.SCENE_REACT;
|
59
|
@Override
|
62
|
@Override
|
60
|
@Transactional
|
63
|
@Transactional
|
61
|
- @CacheEvict(cacheNames = FastIotConstants.CacheConfigKey.SCENE_REACT,key = "{#tenantId, #deviceDTO.profileId}")
|
64
|
+ @CacheEvict(cacheNames = cacheName,key = "{#tenantId, #deviceDTO.profileId}")
|
62
|
public DeviceDTO insertOrUpdate(String tenantId, DeviceDTO deviceDTO) {
|
65
|
public DeviceDTO insertOrUpdate(String tenantId, DeviceDTO deviceDTO) {
|
63
|
|
66
|
|
64
|
if (StringUtils.isBlank(deviceDTO.getId())) {
|
67
|
if (StringUtils.isBlank(deviceDTO.getId())) {
|
|
@@ -71,7 +74,12 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev |
|
@@ -71,7 +74,12 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev |
71
|
private DeviceDTO update(DeviceDTO deviceDTO) {
|
74
|
private DeviceDTO update(DeviceDTO deviceDTO) {
|
72
|
|
75
|
|
73
|
validateUpdate(deviceDTO);
|
76
|
validateUpdate(deviceDTO);
|
74
|
-
|
77
|
+ TkDeviceEntity entity = baseMapper.selectOne(new LambdaQueryWrapper<TkDeviceEntity>().eq(TkDeviceEntity::getTenantId,
|
|
|
78
|
+ deviceDTO.getTenantId()).eq(TkDeviceEntity::getId,deviceDTO.getId()));
|
|
|
79
|
+ if(null != entity && !entity.getProfileId().equals(deviceDTO.getProfileId())){
|
|
|
80
|
+ //更改了产品,需将原产品的缓存置为无效
|
|
|
81
|
+ cacheUtils.invalidate(cacheName,deviceDTO.getTenantId()+","+entity.getProfileId());
|
|
|
82
|
+ }
|
75
|
TkDeviceEntity device = new TkDeviceEntity();
|
83
|
TkDeviceEntity device = new TkDeviceEntity();
|
76
|
deviceDTO.copyToEntity(
|
84
|
deviceDTO.copyToEntity(
|
77
|
device,
|
85
|
device,
|
|
@@ -719,25 +727,53 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev |
|
@@ -719,25 +727,53 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev |
719
|
return baseMapper.findDeviceInfo(tenantId, tbDeviceId);
|
727
|
return baseMapper.findDeviceInfo(tenantId, tbDeviceId);
|
720
|
}
|
728
|
}
|
721
|
|
729
|
|
722
|
- @Cacheable(
|
|
|
723
|
- cacheNames = FastIotConstants.CacheConfigKey.SCENE_REACT, key = "{#tenantId, #tbDeviceProfileId}")
|
|
|
724
|
- @Override
|
|
|
725
|
- public List<String> getDevicesByOrganizationIdAndProjectId(String tenantId, String organizationId, String tbDeviceProfileId) {
|
|
|
726
|
- List<String> orgIds = organizationService.organizationAllIds(tenantId, organizationId);
|
|
|
727
|
|
730
|
|
728
|
- List<TkDeviceEntity> organizationDevices =
|
|
|
729
|
- baseMapper.selectList(
|
|
|
730
|
- new LambdaQueryWrapper<TkDeviceEntity>().eq(TkDeviceEntity::getTenantId,tenantId)
|
|
|
731
|
- .eq(TkDeviceEntity::getProfileId, tbDeviceProfileId)
|
|
|
732
|
- .in(TkDeviceEntity::getOrganizationId, orgIds));
|
731
|
+ @Override
|
|
|
732
|
+ public List<String> getDevicesByOrganizationIdAndProjectId(String tenantId, String organizationId,
|
|
|
733
|
+ Map<String, List<String>> map) {
|
|
|
734
|
+ //不直接查询数据库的目的: 保证获取缓存的设备ID数据是正确的
|
733
|
List<String> allDevices = new ArrayList<>();
|
735
|
List<String> allDevices = new ArrayList<>();
|
734
|
- if (organizationDevices != null && !organizationDevices.isEmpty()) {
|
|
|
735
|
- for (TkDeviceEntity item : organizationDevices) {
|
|
|
736
|
- allDevices.add(item.getTbDeviceId());
|
736
|
+ if(!map.isEmpty()){
|
|
|
737
|
+ List<String> orgIds = organizationService.organizationAllIds(tenantId, organizationId);
|
|
|
738
|
+ if(null !=orgIds && !orgIds.isEmpty()){
|
|
|
739
|
+ orgIds.stream().forEach(orgId->{
|
|
|
740
|
+ map.entrySet().stream().forEach(entry->{
|
|
|
741
|
+ if(entry.getKey().equals(orgId)){
|
|
|
742
|
+ allDevices.addAll(entry.getValue());
|
|
|
743
|
+ }
|
|
|
744
|
+ });
|
|
|
745
|
+ });
|
737
|
}
|
746
|
}
|
738
|
}
|
747
|
}
|
739
|
return allDevices;
|
748
|
return allDevices;
|
740
|
}
|
749
|
}
|
|
|
750
|
+ @Cacheable(
|
|
|
751
|
+ cacheNames = cacheName, key = "{#tenantId, #tbDeviceProfileId}")
|
|
|
752
|
+ @Override
|
|
|
753
|
+ public Map<String, List<String>> getDeviceIdsByDeviceProfileId(String tenantId, String tbDeviceProfileId) {
|
|
|
754
|
+ Map<String, List<String>> result;
|
|
|
755
|
+ List<TkDeviceEntity> organizationDevices =
|
|
|
756
|
+ baseMapper.selectList(
|
|
|
757
|
+ new LambdaQueryWrapper<TkDeviceEntity>().eq(TkDeviceEntity::getTenantId,tenantId)
|
|
|
758
|
+ .eq(TkDeviceEntity::getProfileId, tbDeviceProfileId));
|
|
|
759
|
+ if(null !=organizationDevices && !organizationDevices.isEmpty()){
|
|
|
760
|
+ result = new HashMap<>();
|
|
|
761
|
+ organizationDevices.stream().forEach(entity->{
|
|
|
762
|
+ String organizationId = entity.getOrganizationId();
|
|
|
763
|
+ if(null == result.get(organizationId)){
|
|
|
764
|
+ result.put(organizationId,List.of(entity.getId()));
|
|
|
765
|
+ }else{
|
|
|
766
|
+ ArrayList<String> deviceIds = new ArrayList<>();
|
|
|
767
|
+ deviceIds.add(entity.getId());
|
|
|
768
|
+ deviceIds.addAll(result.get(organizationId));
|
|
|
769
|
+ result.put(organizationId,deviceIds);
|
|
|
770
|
+ }
|
|
|
771
|
+ });
|
|
|
772
|
+ } else {
|
|
|
773
|
+ result = null;
|
|
|
774
|
+ }
|
|
|
775
|
+ return result;
|
|
|
776
|
+ }
|
741
|
|
777
|
|
742
|
@Override
|
778
|
@Override
|
743
|
public ListenableFuture<List<DeviceDTO>> findDeviceListByDeviceProfileId(
|
779
|
public ListenableFuture<List<DeviceDTO>> findDeviceListByDeviceProfileId(
|
|
@@ -818,16 +854,18 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev |
|
@@ -818,16 +854,18 @@ public class TkDeviceServiceImpl extends AbstractBaseService<DeviceMapper, TkDev |
818
|
}
|
854
|
}
|
819
|
@Override
|
855
|
@Override
|
820
|
@Transactional
|
856
|
@Transactional
|
821
|
- @CacheEvict(cacheNames = FastIotConstants.CacheConfigKey.SCENE_REACT,key = "{#tenantId, #tbDeviceProfileId}")
|
857
|
+ @CacheEvict(cacheNames = cacheName,key = "{#tenantId, #tbDeviceProfileId}")
|
822
|
public boolean updateDeviceProfileByTbDeviceId(String tenantId, String tbDeviceId, String tbDeviceProfileId) {
|
858
|
public boolean updateDeviceProfileByTbDeviceId(String tenantId, String tbDeviceId, String tbDeviceProfileId) {
|
823
|
boolean result = false;
|
859
|
boolean result = false;
|
824
|
TkDeviceEntity entity =
|
860
|
TkDeviceEntity entity =
|
825
|
baseMapper.selectOne(new LambdaQueryWrapper<TkDeviceEntity>()
|
861
|
baseMapper.selectOne(new LambdaQueryWrapper<TkDeviceEntity>()
|
826
|
.eq(TkDeviceEntity::getTenantId, tenantId)
|
862
|
.eq(TkDeviceEntity::getTenantId, tenantId)
|
827
|
.eq(TkDeviceEntity::getTbDeviceId, tbDeviceId));
|
863
|
.eq(TkDeviceEntity::getTbDeviceId, tbDeviceId));
|
828
|
- if(null != entity){
|
864
|
+ if(null != entity && !entity.getProfileId().equals(tbDeviceProfileId)){
|
829
|
DeviceProfileDTO deviceProfileDTO = tkDeviceProfileService.findByTbDeviceProfileId(tenantId,tbDeviceProfileId);
|
865
|
DeviceProfileDTO deviceProfileDTO = tkDeviceProfileService.findByTbDeviceProfileId(tenantId,tbDeviceProfileId);
|
830
|
if(null !=deviceProfileDTO){
|
866
|
if(null !=deviceProfileDTO){
|
|
|
867
|
+ //更改了产品,需将原产品的缓存置为无效
|
|
|
868
|
+ cacheUtils.invalidate(cacheName,tenantId+","+entity.getProfileId());
|
831
|
entity.setProfileId(tbDeviceProfileId);
|
869
|
entity.setProfileId(tbDeviceProfileId);
|
832
|
entity.setDeviceProfileId(deviceProfileDTO.getId());
|
870
|
entity.setDeviceProfileId(deviceProfileDTO.getId());
|
833
|
baseMapper.updateById(entity);
|
871
|
baseMapper.updateById(entity);
|