Commit da9d981f5a28f4ef64a78c29af2442b9697ecadf

Authored by xp.Huang
1 parent d91759bd

fix: 修改使用中的TCP脚本,不能被禁用的提示信息

... ... @@ -33,7 +33,6 @@ import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
33 33 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
34 34 import org.thingsboard.server.common.msg.queue.ServiceQueue;
35 35 import org.thingsboard.server.controller.BaseController;
36   -import org.thingsboard.server.dao.yunteng.service.TkDeviceScriptService;
37 36 import org.thingsboard.server.dao.yunteng.service.TkDeviceProfileService;
38 37 import org.thingsboard.server.service.security.permission.Operation;
39 38
... ... @@ -48,8 +47,7 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.
48 47 @RequestMapping("api/yt/device_profile")
49 48 @Api(tags = {"设备配置管理"})
50 49 public class TkDeviceProfileController extends BaseController {
51   - private final TkDeviceProfileService ytDeviceProfileService;
52   - private final TkDeviceScriptService javaScriptService;
  50 + private final TkDeviceProfileService tkDeviceProfileService;
53 51
54 52 @PostMapping()
55 53 @PreAuthorize(
... ... @@ -67,7 +65,7 @@ public class TkDeviceProfileController extends BaseController {
67 65
68 66 DeviceProfile saveDeviceProfile = updateTbDeviceProfile(tbDeviceProfile, created);
69 67 deviceProfileDTO.setTbProfileId(saveDeviceProfile.getId().toString());
70   - ytDeviceProfileService.insertOrUpdate(deviceProfileDTO);
  68 + tkDeviceProfileService.insertOrUpdate(deviceProfileDTO);
71 69
72 70 return ResponseEntity.ok(deviceProfileDTO);
73 71 }
... ... @@ -131,7 +129,7 @@ public class TkDeviceProfileController extends BaseController {
131 129 public ResponseEntity<DeviceProfileDTO> getDevice(@PathVariable("id") String id)
132 130 throws ThingsboardException {
133 131 return ResponseEntity.ok(
134   - ytDeviceProfileService.getDeviceProfile(getCurrentUser().getCurrentTenantId(), id));
  132 + tkDeviceProfileService.getDeviceProfile(getCurrentUser().getCurrentTenantId(), id));
135 133 }
136 134
137 135 @GetMapping(params = {PAGE_SIZE, PAGE})
... ... @@ -154,7 +152,7 @@ public class TkDeviceProfileController extends BaseController {
154 152 queryMap.put("transportType", transportType);
155 153 queryMap.put(TENANT_ID, getCurrentUser().getCurrentTenantId());
156 154 queryMap.put(CUSTOMER_ID, getCurrentUser().getCustomerId().toString());
157   - return ytDeviceProfileService.page(queryMap, getCurrentUser().isTenantAdmin());
  155 + return tkDeviceProfileService.page(queryMap, getCurrentUser().isTenantAdmin());
158 156 }
159 157
160 158 @GetMapping("/me/list")
... ... @@ -167,10 +165,10 @@ public class TkDeviceProfileController extends BaseController {
167 165 List<DeviceProfileDTO> results;
168 166 String tenantId = getCurrentUser().getCurrentTenantId();
169 167 if (getCurrentUser().isTenantAdmin()) {
170   - results = ytDeviceProfileService.findDeviceProfile(tenantId, null,deviceType);
  168 + results = tkDeviceProfileService.findDeviceProfile(tenantId, null,deviceType);
171 169 } else {
172 170 results =
173   - ytDeviceProfileService.findCustomerDeviceProfiles(
  171 + tkDeviceProfileService.findCustomerDeviceProfiles(
174 172 tenantId, getCurrentUser().getCustomerId(),deviceType);
175 173 }
176 174
... ... @@ -183,18 +181,18 @@ public class TkDeviceProfileController extends BaseController {
183 181 public void deleteDeviceProfiles(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO)
184 182 throws ThingsboardException {
185 183 String tenantId = getCurrentUser().getCurrentTenantId();
186   - ytDeviceProfileService.checkDeviceProfiles(tenantId, deleteDTO.getIds());
  184 + tkDeviceProfileService.checkDeviceProfiles(tenantId, deleteDTO.getIds());
187 185
188 186
189 187 for (String id : deleteDTO.getIds()) {
190 188 deleteTbDeviceProfile(id);
191 189 }
192   - ytDeviceProfileService.deleteDeviceProfiles(tenantId, deleteDTO.getIds());
  190 + tkDeviceProfileService.deleteDeviceProfiles(tenantId, deleteDTO.getIds());
193 191 }
194 192
195 193 private void deleteTbDeviceProfile(String profileId) throws ThingsboardException {
196 194 DeviceProfileDTO dto =
197   - ytDeviceProfileService.findDeviceProfileById(
  195 + tkDeviceProfileService.findDeviceProfileById(
198 196 getCurrentUser().getCurrentTenantId(), profileId);
199 197 if (null != dto) {
200 198 DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(dto.getTbProfileId()));
... ... @@ -248,7 +246,7 @@ public class TkDeviceProfileController extends BaseController {
248 246 DeviceProfile tbDeviceProfile = new DeviceProfile();
249 247 if (StringUtils.isNotBlank(deviceProfileDTO.getId())) {
250 248 DeviceProfileDTO findDeviceProfile =
251   - ytDeviceProfileService.findDeviceProfileById(
  249 + tkDeviceProfileService.findDeviceProfileById(
252 250 getCurrentUser().getCurrentTenantId(), deviceProfileDTO.getId());
253 251 if (StringUtils.isNotEmpty(findDeviceProfile.getTbProfileId())) {
254 252 UUID profileId = UUID.fromString(findDeviceProfile.getTbProfileId());
... ...
... ... @@ -4,12 +4,15 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4 4 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
5 5 import com.baomidou.mybatisplus.core.metadata.IPage;
6 6 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7 +import com.fasterxml.jackson.databind.JsonNode;
7 8 import lombok.RequiredArgsConstructor;
8 9 import lombok.extern.slf4j.Slf4j;
9 10 import org.apache.commons.lang3.StringUtils;
10 11 import org.springframework.stereotype.Service;
11 12 import org.springframework.transaction.annotation.Transactional;
12 13 import org.thingsboard.server.common.data.DeviceProfile;
  14 +import org.thingsboard.server.common.data.DeviceTransportType;
  15 +import org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration;
13 16 import org.thingsboard.server.common.data.id.CustomerId;
14 17 import org.thingsboard.server.common.data.id.TenantId;
15 18 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
... ... @@ -20,6 +23,7 @@ import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
20 23 import org.thingsboard.server.common.data.yunteng.dto.*;
21 24 import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum;
22 25 import org.thingsboard.server.common.data.yunteng.enums.TransportTypeEnum;
  26 +import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
23 27 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
24 28 import org.thingsboard.server.dao.yunteng.entities.*;
25 29 import org.thingsboard.server.dao.yunteng.jpa.dao.TkJpaDeviceProfileDao;
... ... @@ -85,6 +89,19 @@ public class TkDeviceProfileServiceImpl
85 89 }
86 90
87 91 @Override
  92 + public List<DeviceProfileDTO> findDeviceProfileByScriptId(String tenantId, String scriptId) {
  93 + if(StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(scriptId)){
  94 + throw new TkDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  95 + }
  96 + List<TkDeviceProfileEntity> entities = baseMapper.selectList(new LambdaQueryWrapper<TkDeviceProfileEntity>()
  97 + .eq(TkDeviceProfileEntity::getTenantId,tenantId).like(TkDeviceProfileEntity::getScriptId,scriptId));
  98 + if(null != entities && !entities.isEmpty()){
  99 + return entities.stream().map(entity->entity.getDTO(DeviceProfileDTO.class)).collect(Collectors.toList());
  100 + }
  101 + return null;
  102 + }
  103 +
  104 + @Override
88 105 @Transactional
89 106 public DeviceProfileDTO insertOrUpdate(DeviceProfileDTO deviceDTO) {
90 107 if (StringUtils.isBlank(deviceDTO.getId())) {
... ... @@ -97,6 +114,9 @@ public class TkDeviceProfileServiceImpl
97 114 private DeviceProfileDTO insert(DeviceProfileDTO deviceDTO) {
98 115
99 116 TkDeviceProfileEntity profile = new TkDeviceProfileEntity();
  117 + if (Objects.equals(deviceDTO.getTransportType(), DeviceTransportType.TCP.name())) {
  118 + setDeviceScriptId(profile,deviceDTO.getProfileData().getTransportConfiguration());
  119 + }
100 120 deviceDTO.copyToEntity(
101 121 profile,
102 122 ModelConstants.TablePropertyMapping.ACTIVE_TIME,
... ... @@ -113,6 +133,9 @@ public class TkDeviceProfileServiceImpl
113 133
114 134 private DeviceProfileDTO update(DeviceProfileDTO deviceDTO) {
115 135 TkDeviceProfileEntity device = new TkDeviceProfileEntity();
  136 + if (Objects.equals(deviceDTO.getTransportType(), DeviceTransportType.TCP.name())) {
  137 + setDeviceScriptId(device,deviceDTO.getProfileData().getTransportConfiguration());
  138 + }
116 139 deviceDTO.copyToEntity(
117 140 device,
118 141 ModelConstants.TablePropertyMapping.ACTIVE_TIME,
... ... @@ -127,6 +150,23 @@ public class TkDeviceProfileServiceImpl
127 150 baseMapper.updateById(device);
128 151 return device.getDTO(DeviceProfileDTO.class);
129 152 }
  153 + private void setDeviceScriptId(TkDeviceProfileEntity profile, DeviceProfileTransportConfiguration transportConfiguration)
  154 + {
  155 + JsonNode jsonNode = JacksonUtil.convertValue(transportConfiguration, JsonNode.class);
  156 + if(null != jsonNode)
  157 + {
  158 + String authScriptId = String.valueOf(jsonNode.get("authScriptId"));
  159 + String upScriptId = String.valueOf(jsonNode.get("upScriptId"));
  160 + List<String> list = List.of(authScriptId,upScriptId);
  161 + if(null != list && !list.isEmpty()){
  162 + String scriptId = list.get(FastIotConstants.MagicNumber.ZERO);
  163 + if(list.size()>FastIotConstants.MagicNumber.ONE){
  164 + scriptId = StringUtils.join(list,",");
  165 + }
  166 + profile.setScriptId(scriptId);
  167 + }
  168 + }
  169 + }
130 170
131 171 @Override
132 172 @Transactional
... ...
... ... @@ -97,6 +97,14 @@ public class TkDeviceScriptServiceImpl
97 97 if (!entity.getTenantId().equals(tenantId)) {
98 98 throw new TkDataValidationException(ErrorMessage.NOT_BELONG_CURRENT_TENANT.getMessage());
99 99 }
  100 + if(Objects.equals(status,StatusEnum.DISABLE.getIndex())){
  101 + List<DeviceProfileDTO> profileList = tkDeviceProfileService.findDeviceProfileByScriptId(tenantId,id);
  102 + if(null != profileList && !profileList.isEmpty()){
  103 +
  104 + throw new TkDataValidationException(String.format(ErrorMessage.CURRENT_DATA_IN_USE.getMessage(),
  105 + profileList.get(0).getName()));
  106 + }
  107 + }
100 108 entity.setStatus(status);
101 109 return baseMapper.updateById(entity) > FastIotConstants.MagicNumber.ZERO;
102 110 }
... ...
... ... @@ -53,4 +53,12 @@ public interface TkDeviceProfileService extends BaseService<TkDeviceProfileEntit
53 53 * @return 设备配置信息
54 54 */
55 55 DeviceProfileDTO findByTbDeviceProfileId(String tenantId, String tbProfileId);
  56 +
  57 + /**
  58 + * 根据脚本ID查询,使用的产品
  59 + * @param tenantId 租户ID
  60 + * @param scriptId 脚本ID
  61 + * @return 产品列表
  62 + */
  63 + List<DeviceProfileDTO> findDeviceProfileByScriptId(String tenantId,String scriptId);
56 64 }
... ...