Commit 8003b9368a7850cfcbd0a08bb35e9c693a2bf718

Authored by 云中非
2 parents bfa97591 ff3e0f70

Merge branch 'master' into 20221129

... ... @@ -44,6 +44,7 @@ import org.thingsboard.server.controller.BaseController;
44 44 import org.thingsboard.server.dao.yunteng.service.TkDeviceProfileService;
45 45 import org.thingsboard.server.dao.yunteng.service.TkDeviceScriptService;
46 46
  47 +import java.sql.Timestamp;
47 48 import java.time.LocalDateTime;
48 49 import java.time.ZoneOffset;
49 50 import java.util.*;
... ... @@ -89,6 +90,17 @@ public class TkDeviceScriptController extends BaseController {
89 90 return ResponseEntity.ok(scriptService.insertOrUpdate(scriptDTO));
90 91 }
91 92
  93 + @PostMapping("/{id}/{status}")
  94 + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:js:post','api:yt:js:update:status'})")
  95 + @ApiOperation("修改脚本状态")
  96 + public ResponseEntity<Boolean> updateScriptStatus(
  97 + @PathVariable("id") String id, @PathVariable("status") Integer status)
  98 + throws ThingsboardException {
  99 +
  100 + return ResponseEntity.ok(
  101 + scriptService.updateScriptStatus(getCurrentUser().getCurrentTenantId(), id, status));
  102 + }
  103 +
92 104 /**
93 105 * 更新thingsboard的设备配置信息
94 106 *
... ... @@ -131,8 +143,7 @@ public class TkDeviceScriptController extends BaseController {
131 143
132 144 @GetMapping("{id}")
133 145 @ApiOperation("详情")
134   - @PreAuthorize(
135   - "@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{'api:yt:js:get'})")
  146 + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN','CUSTOMER_USER'},{'api:yt:js:get'})")
136 147 public ResponseEntity<TkDeviceScriptDTO> getDevice(@PathVariable("id") String id)
137 148 throws ThingsboardException {
138 149 return ResponseEntity.of(
... ... @@ -146,7 +157,10 @@ public class TkDeviceScriptController extends BaseController {
146 157 @RequestParam(PAGE_SIZE) int pageSize,
147 158 @RequestParam(PAGE) int page,
148 159 @RequestParam(value = ORDER_FILED, required = false) String orderFiled,
149   - @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType)
  160 + @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType,
  161 + @RequestParam(value = "name", required = false) String name,
  162 + @RequestParam(value = "startTime", required = false) Long startTime,
  163 + @RequestParam(value = "endTime", required = false) Long endTime)
150 164 throws ThingsboardException {
151 165 Map<String, Object> queryMap = new HashMap<>();
152 166 queryMap.put(PAGE_SIZE, pageSize);
... ... @@ -155,6 +169,19 @@ public class TkDeviceScriptController extends BaseController {
155 169 queryMap.put(ORDER_TYPE, orderType);
156 170 queryMap.put(CUSTOMER_ID, getCurrentUser().getCustomerId().toString());
157 171 queryMap.put(TENANT_ID, getCurrentUser().getCurrentTenantId());
  172 +
  173 + if(!StringUtils.isEmpty(name)){
  174 + queryMap.put("name", name);
  175 + }
  176 + if(null != startTime && null!=endTime){
  177 + if (startTime > endTime) {
  178 + throw new YtDataValidationException(
  179 + ErrorMessage.START_TIME_NOT_MORE_THAN_END_TIME.getMessage());
  180 + }
  181 + queryMap.put("startTime", new Timestamp(startTime).toLocalDateTime());
  182 + queryMap.put("endTime", new Timestamp(endTime).toLocalDateTime());
  183 + }
  184 +
158 185 return scriptService.page(queryMap, getCurrentUser().isTenantAdmin());
159 186 }
160 187
... ...
... ... @@ -24,6 +24,7 @@ import org.thingsboard.server.dao.yunteng.mapper.TkDeviceProfileMapper;
24 24 import org.thingsboard.server.dao.yunteng.mapper.TkDeviceScriptMapper;
25 25 import org.thingsboard.server.dao.yunteng.service.*;
26 26
  27 +import java.time.LocalDateTime;
27 28 import java.util.*;
28 29 import java.util.stream.Collectors;
29 30
... ... @@ -80,6 +81,23 @@ public class TkDeviceScriptServiceImpl
80 81 }
81 82
82 83 @Override
  84 + @Transactional
  85 + public boolean updateScriptStatus(String tenantId, String id, Integer status) {
  86 + if (StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(id) || null != status) {
  87 + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  88 + }
  89 + TkDeviceScriptEntity entity = baseMapper.selectById(id);
  90 + if (null == entity) {
  91 + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  92 + }
  93 + if (!entity.getTenantId().equals(tenantId)) {
  94 + throw new YtDataValidationException(ErrorMessage.NOT_BELONG_CURRENT_TENANT.getMessage());
  95 + }
  96 + entity.setStatus(status);
  97 + return baseMapper.updateById(entity) > FastIotConstants.MagicNumber.ZERO;
  98 + }
  99 +
  100 + @Override
83 101 public String getScriptText(String tenantId, String scriptId) {
84 102 LambdaQueryWrapper<TkDeviceScriptEntity> queryWrapper =
85 103 new QueryWrapper<TkDeviceScriptEntity>()
... ... @@ -108,7 +126,7 @@ public class TkDeviceScriptServiceImpl
108 126 .lambda()
109 127 .in(TkDeviceProfileEntity::getScriptId, ids));
110 128 if (usedList != null && usedList.size() > 0) {
111   - List<String> names = usedList.stream().map(i -> i.getName()).collect(Collectors.toList());
  129 + List<String> names = usedList.stream().map(TkDeviceProfileEntity::getName).collect(Collectors.toList());
112 130 throw new YtDataValidationException(
113 131 String.format(ErrorMessage.PROJECT_USED_SCRIPT.getMessage(), names));
114 132 }
... ... @@ -169,10 +187,7 @@ public class TkDeviceScriptServiceImpl
169 187 TkDeviceScriptEntity profile = baseMapper.selectOne(queryWrapper);
170 188 return Optional.ofNullable(profile)
171 189 .map(
172   - entity -> {
173   - TkDeviceScriptDTO result = entity.getDTO(TkDeviceScriptDTO.class);
174   - return result;
175   - });
  190 + entity -> entity.getDTO(TkDeviceScriptDTO.class));
176 191 }
177 192
178 193 @Override
... ... @@ -188,6 +203,11 @@ public class TkDeviceScriptServiceImpl
188 203 Optional.ofNullable(queryMap.get(QueryConstant.CUSTOMER_ID))
189 204 .map(Object::toString)
190 205 .orElse(null);
  206 + String name = Optional.ofNullable(queryMap.get("name")).map(Object::toString).orElse(null);
  207 + LocalDateTime startTime =
  208 + (LocalDateTime) Optional.ofNullable(queryMap.get("startTime")).orElse(null);
  209 + LocalDateTime endTime =
  210 + (LocalDateTime) Optional.ofNullable(queryMap.get("endTime")).orElse(null);
191 211 Set<String> scriptIds = null;
192 212 if (!isTenantAdmin && StringUtils.isNotEmpty(customerId)) {
193 213 scriptIds = getCustomerScripts(customerId, tenantId);
... ... @@ -199,6 +219,12 @@ public class TkDeviceScriptServiceImpl
199 219 new QueryWrapper<TkDeviceScriptEntity>()
200 220 .lambda()
201 221 .eq(TkDeviceScriptEntity::getTenantId, tenantId)
  222 + .like(StringUtils.isNotEmpty(name), TkDeviceScriptEntity::getName, name)
  223 + .and(
  224 + null != startTime && null != endTime,
  225 + qr ->
  226 + qr.ge(TkDeviceScriptEntity::getCreateTime, startTime)
  227 + .le(TkDeviceScriptEntity::getCreateTime, endTime))
202 228 .in(!isTenantAdmin, TkDeviceScriptEntity::getId, scriptIds);
203 229
204 230 IPage<TkDeviceScriptEntity> scripts = baseMapper.selectPage(currentPage, queryWrapper);
... ... @@ -215,11 +241,9 @@ public class TkDeviceScriptServiceImpl
215 241 new QueryWrapper<TkDeviceScriptEntity>()
216 242 .lambda()
217 243 .eq(TkDeviceScriptEntity::getTenantId, tenantId);
218   - List<TkDeviceScriptDTO> results =
219   - baseMapper.selectList(queryWrapper).stream()
220   - .map(item -> item.getDTO(TkDeviceScriptDTO.class))
221   - .collect(Collectors.toList());
222   - return results;
  244 + return baseMapper.selectList(queryWrapper).stream()
  245 + .map(item -> item.getDTO(TkDeviceScriptDTO.class))
  246 + .collect(Collectors.toList());
223 247 }
224 248
225 249 private Set<String> getCustomerScripts(String customerId, String tenantId) {
... ... @@ -227,9 +251,9 @@ public class TkDeviceScriptServiceImpl
227 251 List<TkCustomerDeviceDTO> list = tkCustomerDevice.getMappingByCustomerId(customerId);
228 252 if (null != list && !list.isEmpty()) {
229 253 Set<String> deviceIds =
230   - list.stream().map(obj -> obj.getDeviceId()).collect(Collectors.toSet());
  254 + list.stream().map(TkCustomerDeviceDTO::getDeviceId).collect(Collectors.toSet());
231 255 scriptIds =
232   - Optional.ofNullable(deviceIds)
  256 + Optional.of(deviceIds)
233 257 .map(
234 258 ids -> {
235 259 List<DeviceDTO> deviceDTOS =
... ... @@ -237,7 +261,7 @@ public class TkDeviceScriptServiceImpl
237 261 if (null != deviceDTOS && !deviceDTOS.isEmpty()) {
238 262 List<String> deviceProfiles =
239 263 deviceDTOS.stream()
240   - .map(obj -> obj.getDeviceProfileId())
  264 + .map(DeviceDTO::getDeviceProfileId)
241 265 .collect(Collectors.toList());
242 266 List<DeviceProfileDTO> deviceProfileDTOList =
243 267 tkDeviceProfileService.findDeviceProfileByIds(tenantId, deviceProfiles);
... ... @@ -245,7 +269,7 @@ public class TkDeviceScriptServiceImpl
245 269 .map(
246 270 deviceProfileDTOS ->
247 271 deviceProfileDTOS.stream()
248   - .map(obj -> obj.getScriptId())
  272 + .map(DeviceProfileDTO::getScriptId)
249 273 .collect(Collectors.toSet()))
250 274 .orElse(null);
251 275 }
... ...
... ... @@ -40,4 +40,6 @@ public interface TkDeviceScriptService extends BaseService<TkDeviceScriptEntity>
40 40 * @return
41 41 */
42 42 boolean validateFormdata(TkDeviceScriptDTO scriptDTO, boolean created);
  43 +
  44 + boolean updateScriptStatus(String tenantId,String id,Integer status);
43 45 }
... ...