Commit 378c34f13e4c956c7ecad0a3349a62b776ce6cc3

Authored by chenjunyu_1481036421
1 parent f8f6e4c2

feat:物模型排序功能代码

@@ -44,6 +44,7 @@ import java.io.InputStream; @@ -44,6 +44,7 @@ import java.io.InputStream;
44 import java.util.HashMap; 44 import java.util.HashMap;
45 import java.util.List; 45 import java.util.List;
46 import java.util.Map; 46 import java.util.Map;
  47 +import java.util.concurrent.atomic.AtomicInteger;
47 import java.util.regex.Pattern; 48 import java.util.regex.Pattern;
48 49
49 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.*; 50 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.*;
@@ -113,6 +114,17 @@ public class ThingsModelController extends BaseController { @@ -113,6 +114,17 @@ public class ThingsModelController extends BaseController {
113 return ResponseEntity.ok(saveOrUpdate(thingsModelDTO)); 114 return ResponseEntity.ok(saveOrUpdate(thingsModelDTO));
114 } 115 }
115 116
  117 + @PostMapping("/updateSort")
  118 + @ApiOperation("修改物模型排序接口")
  119 + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
  120 + public ResponseEntity<Boolean> updateSort(
  121 + @RequestParam(value = "sortType: up向上,down向下,topUp置顶,bottomUp置底" ) String sortType,
  122 + @RequestParam(value = "id") String id,
  123 + @RequestParam (value = "isCategory 是否品类") Boolean isCategory)
  124 + throws ThingsboardException {
  125 + return ResponseEntity.ok(thingsModelService.updateSort(id,sortType,isCategory));
  126 + }
  127 +
116 @DeleteMapping 128 @DeleteMapping
117 @ApiOperation("删除") 129 @ApiOperation("删除")
118 @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:things_model:delete'})") 130 @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:things_model:delete'})")
@@ -440,6 +452,8 @@ public class ThingsModelController extends BaseController { @@ -440,6 +452,8 @@ public class ThingsModelController extends BaseController {
440 dto.setExtensionDesc(extensionDesc); 452 dto.setExtensionDesc(extensionDesc);
441 dto.setRemark(row.getCell(10)==null?null:row.getCell(10).toString()); 453 dto.setRemark(row.getCell(10)==null?null:row.getCell(10).toString());
442 dto.setTenantId(tenantId); 454 dto.setTenantId(tenantId);
  455 + AtomicInteger sort = new AtomicInteger(thingsModelService.getCountById(deviceProfileId, false)+1);
  456 + dto.setSort(sort.get());
443 if(dto.getCategoryId()!=null){ 457 if(dto.getCategoryId()!=null){
444 thingsModelService.categorySaveOrUpdate(dto); 458 thingsModelService.categorySaveOrUpdate(dto);
445 }else{ 459 }else{
@@ -555,6 +569,8 @@ public class ThingsModelController extends BaseController { @@ -555,6 +569,8 @@ public class ThingsModelController extends BaseController {
555 dto.setAccessMode(accessMode.equals("读写")?"rw":accessMode.equals("只读")?"r":null); 569 dto.setAccessMode(accessMode.equals("读写")?"rw":accessMode.equals("只读")?"r":null);
556 dto.setRemark(row.getCell(8)==null?null:row.getCell(8).toString()); 570 dto.setRemark(row.getCell(8)==null?null:row.getCell(8).toString());
557 dto.setTenantId(tenantId); 571 dto.setTenantId(tenantId);
  572 + AtomicInteger sort = new AtomicInteger(thingsModelService.getCountById(categoryId, true)+1);
  573 + dto.setSort(sort.get());
558 if(dto.getCategoryId()!=null){ 574 if(dto.getCategoryId()!=null){
559 thingsModelService.categorySaveOrUpdate(dto); 575 thingsModelService.categorySaveOrUpdate(dto);
560 }else{ 576 }else{
@@ -66,4 +66,7 @@ public class ThingsModelDTO extends TenantDTO { @@ -66,4 +66,7 @@ public class ThingsModelDTO extends TenantDTO {
66 66
67 @ApiModelProperty(value = "品类Id") 67 @ApiModelProperty(value = "品类Id")
68 private String categoryId; 68 private String categoryId;
  69 +
  70 + @ApiModelProperty(value = "排序")
  71 + private Integer sort;
69 } 72 }
@@ -38,6 +38,7 @@ import org.thingsboard.server.dao.yunteng.service.ThingsModelService; @@ -38,6 +38,7 @@ import org.thingsboard.server.dao.yunteng.service.ThingsModelService;
38 import org.thingsboard.server.dao.yunteng.service.TkDeviceProfileService; 38 import org.thingsboard.server.dao.yunteng.service.TkDeviceProfileService;
39 39
40 import java.util.*; 40 import java.util.*;
  41 +import java.util.concurrent.atomic.AtomicInteger;
41 import java.util.stream.Collectors; 42 import java.util.stream.Collectors;
42 43
43 @Service 44 @Service
@@ -68,7 +69,7 @@ public class ThingsModelServiceImpl @@ -68,7 +69,7 @@ public class ThingsModelServiceImpl
68 } 69 }
69 IPage<TkThingsModelEntity> iPage = 70 IPage<TkThingsModelEntity> iPage =
70 baseMapper.selectPage( 71 baseMapper.selectPage(
71 - getPage(queryMap, FastIotConstants.DefaultOrder.CREATE_TIME, false), 72 + getPage(queryMap, "sort", true),
72 new LambdaQueryWrapper<TkThingsModelEntity>() 73 new LambdaQueryWrapper<TkThingsModelEntity>()
73 .eq(StringUtils.isNotEmpty(tenantId), TkThingsModelEntity::getTenantId, tenantId) 74 .eq(StringUtils.isNotEmpty(tenantId), TkThingsModelEntity::getTenantId, tenantId)
74 .eq(StringUtils.isNotEmpty(deviceProfileId), TkThingsModelEntity::getDeviceProfileId, deviceProfileId) 75 .eq(StringUtils.isNotEmpty(deviceProfileId), TkThingsModelEntity::getDeviceProfileId, deviceProfileId)
@@ -361,7 +362,7 @@ public class ThingsModelServiceImpl @@ -361,7 +362,7 @@ public class ThingsModelServiceImpl
361 DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(new TenantId(UUID.fromString(tenantId)), new DeviceProfileId(UUID.fromString(profileDTO.getTbProfileId()))); 362 DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(new TenantId(UUID.fromString(tenantId)), new DeviceProfileId(UUID.fromString(profileDTO.getTbProfileId())));
362 DeviceTypeEnum type = profileDTO.getDeviceType(); 363 DeviceTypeEnum type = profileDTO.getDeviceType();
363 String transportType = String.valueOf(deviceProfile.getTransportType()); 364 String transportType = String.valueOf(deviceProfile.getTransportType());
364 - if (transportType.equals("TCP") && type.equals(DeviceTypeEnum.SENSOR) && (null != importThingsModel.getData().getServices() || null != importThingsModel.getData().getEvents())) { 365 + if (transportType.equals("TCP") && type.equals(DeviceTypeEnum.SENSOR) && (!importThingsModel.getData().getServices().isEmpty() || !importThingsModel.getData().getEvents().isEmpty())) {
365 throw new TkDataValidationException(MessageUtils.message(ErrorMessage.INVALID_PARAMETER_IMPORT.getI18nCode())); 366 throw new TkDataValidationException(MessageUtils.message(ErrorMessage.INVALID_PARAMETER_IMPORT.getI18nCode()));
366 } 367 }
367 if (Objects.equals(importThingsModel.getFunctionType(), FunctionTypeEnum.all) 368 if (Objects.equals(importThingsModel.getFunctionType(), FunctionTypeEnum.all)
@@ -406,6 +407,10 @@ public class ThingsModelServiceImpl @@ -406,6 +407,10 @@ public class ThingsModelServiceImpl
406 entities, 407 entities,
407 null); 408 null);
408 } 409 }
  410 + AtomicInteger i = new AtomicInteger(getCountById(tkDeviceProfileId, false)+1);
  411 + entities.stream().forEach(e->{
  412 + e.setSort(i.getAndIncrement());
  413 + });
409 insertBatch(entities, TkThingsModelEntity.class); 414 insertBatch(entities, TkThingsModelEntity.class);
410 entities.forEach(entity -> { 415 entities.forEach(entity -> {
411 //云边同步 416 //云边同步
@@ -458,6 +463,10 @@ public class ThingsModelServiceImpl @@ -458,6 +463,10 @@ public class ThingsModelServiceImpl
458 entities, 463 entities,
459 categoryId); 464 categoryId);
460 } 465 }
  466 + AtomicInteger i = new AtomicInteger(getCountById(categoryId, true)+1);
  467 + entities.stream().forEach(e->{
  468 + e.setSort(i.getAndIncrement());
  469 + });
461 return insertBatch(entities, TkThingsModelEntity.class); 470 return insertBatch(entities, TkThingsModelEntity.class);
462 } 471 }
463 472
@@ -507,6 +516,77 @@ public class ThingsModelServiceImpl @@ -507,6 +516,77 @@ public class ThingsModelServiceImpl
507 return list; 516 return list;
508 } 517 }
509 518
  519 + @Override
  520 + public boolean updateSort(String id, String sortType,Boolean isCategory) {
  521 + TkThingsModelEntity entity = baseMapper.selectById(id);
  522 + TkThingsModelEntity sortEntity ;
  523 + List<TkThingsModelEntity> sortEntityList;
  524 + switch (sortType) {
  525 + case "up":
  526 + sortEntity = getSortEntity(entity,entity.getSort()-1,isCategory);
  527 + swapSortValues(entity,sortEntity);
  528 + break;
  529 + case "down":
  530 + sortEntity = getSortEntity(entity,entity.getSort()+1,isCategory);
  531 + swapSortValues(entity,sortEntity);
  532 + break;
  533 + case "topUp":
  534 + entity.setSort(0);
  535 + baseMapper.updateById(entity);
  536 + getSortEntityList(entity,isCategory);
  537 + break;
  538 + case "bottomUp":
  539 + sortEntityList = getSortEntityList(entity,isCategory);
  540 + entity.setSort(sortEntityList.size()+1);
  541 + baseMapper.updateById(entity);
  542 + break;
  543 + }
  544 + return true;
  545 + }
  546 +
  547 + @Override
  548 + public Integer getCountById(String id, Boolean isCategory) {
  549 + return Math.toIntExact(baseMapper.selectCount(
  550 + new LambdaQueryWrapper<TkThingsModelEntity>()
  551 + .eq(isCategory, TkThingsModelEntity::getCategoryId, id)
  552 + .eq(!isCategory, TkThingsModelEntity::getDeviceProfileId, id)));
  553 + }
  554 +
  555 + private List<TkThingsModelEntity> getSortEntityList(TkThingsModelEntity entity,Boolean isCategory){
  556 + List<TkThingsModelEntity> list = baseMapper.selectList(
  557 + new LambdaQueryWrapper<TkThingsModelEntity>()
  558 + .notIn(TkThingsModelEntity::getId,entity.getId())
  559 + .eq(isCategory, TkThingsModelEntity::getCategoryId, entity.getCategoryId())
  560 + .eq(!isCategory, TkThingsModelEntity::getDeviceProfileId, entity.getDeviceProfileId())
  561 + .orderBy(true, true, TkThingsModelEntity::getSort));
  562 + AtomicInteger i= new AtomicInteger(1);
  563 + list.stream().forEach(e->{
  564 + e.setSort(i.getAndIncrement());
  565 + baseMapper.updateById(e);
  566 + });
  567 + return list;
  568 + }
  569 +
  570 + private TkThingsModelEntity getSortEntity(TkThingsModelEntity entity ,Integer sort ,Boolean isCategory){
  571 + return baseMapper.selectOne(
  572 + new LambdaQueryWrapper<TkThingsModelEntity>()
  573 + .eq(TkThingsModelEntity::getSort,sort)
  574 + .eq(isCategory, TkThingsModelEntity::getCategoryId, entity.getCategoryId())
  575 + .eq(!isCategory, TkThingsModelEntity::getDeviceProfileId, entity.getDeviceProfileId()));
  576 + }
  577 +
  578 + // 交换两个对象的 sort 值
  579 + private void swapSortValues(TkThingsModelEntity entity1, TkThingsModelEntity entity2) {
  580 + if(null!=entity1&&null!=entity2){
  581 + Integer oldSort = entity1.getSort();
  582 + Integer newSort = entity2.getSort();
  583 + entity1.setSort(newSort);
  584 + entity2.setSort(oldSort);
  585 + baseMapper.updateById(entity1);
  586 + baseMapper.updateById(entity2);
  587 + }
  588 + }
  589 +
510 private void checkNameAndIdentifier( 590 private void checkNameAndIdentifier(
511 List<String> existIdentifiers, 591 List<String> existIdentifiers,
512 FunctionTypeEnum functionType, 592 FunctionTypeEnum functionType,
@@ -61,4 +61,9 @@ public interface ThingsModelService extends BaseService<TkThingsModelEntity>{ @@ -61,4 +61,9 @@ public interface ThingsModelService extends BaseService<TkThingsModelEntity>{
61 List<BatchDeviceProfileInfoDTO> getDeviceProfileTSLInfoByIds(String tenantId, List<String> deviceProfileIds, 61 List<BatchDeviceProfileInfoDTO> getDeviceProfileTSLInfoByIds(String tenantId, List<String> deviceProfileIds,
62 FunctionTypeEnum functionTypeEnum); 62 FunctionTypeEnum functionTypeEnum);
63 63
  64 + boolean updateSort(String id,String sortType,Boolean isCategory);
  65 +
  66 + Integer getCountById(String id,Boolean isCategory);
  67 +
  68 +
64 } 69 }