Commit d57bc1487301ccbc57d7dba829b653649db83c9f

Authored by 盛长浩
1 parent bd5ff03d

设备台账完善-添加保存图片及文件接口

... ... @@ -442,8 +442,6 @@ CREATE TABLE "public"."qg_brain_device" (
442 442 "service_dep" varchar(36),
443 443 "contact_info" varchar(50),
444 444 "enable_time" timestamp(6),
445   - "pictures" varchar(500),
446   - "files" varchar(500),
447 445 "tenant_id" varchar(36),
448 446 "create_time" timestamp(6),
449 447 "creator" varchar(36),
... ... @@ -464,6 +462,27 @@ COMMENT ON COLUMN "public"."qg_brain_device"."use_dep" IS '使用部门id';
464 462 COMMENT ON COLUMN "public"."qg_brain_device"."service_dep" IS '维保部门id';
465 463 COMMENT ON COLUMN "public"."qg_brain_device"."contact_info" IS '联系方式';
466 464 COMMENT ON COLUMN "public"."qg_brain_device"."enable_time" IS '启用日期';
467   -COMMENT ON COLUMN "public"."qg_brain_device"."pictures" IS '图片ids';
468   -COMMENT ON COLUMN "public"."qg_brain_device"."files" IS '文件ids';
  465 +
  466 +--图片或者文件资源
  467 +CREATE TABLE "public"."qg_brain_device_source" (
  468 + "id" varchar(36) NOT NULL,
  469 + "source_id" varchar(36),
  470 + "type" varchar(10),
  471 + "name" varchar(100),
  472 + "uid" varchar(100),
  473 + "url" varchar(100),
  474 + "tenant_id" varchar(36),
  475 + "create_time" timestamp(6),
  476 + "creator" varchar(36),
  477 + "updater" varchar(36),
  478 + "update_time" timestamp(6),
  479 + CONSTRAINT "qg_brain_device_source_pkey" PRIMARY KEY ("id")
  480 +);
  481 +
  482 +COMMENT ON TABLE "public"."qg_brain_device_source" IS '设备管理资源表';
  483 +COMMENT ON COLUMN "public"."qg_brain_device_source"."source_id" IS '资源id';
  484 +COMMENT ON COLUMN "public"."qg_brain_device_source"."type" IS '类型-图片/文件(picture/file)';
  485 +COMMENT ON COLUMN "public"."qg_brain_device_source"."name" IS '文件名';
  486 +COMMENT ON COLUMN "public"."qg_brain_device_source"."uid" IS 'uid';
  487 +COMMENT ON COLUMN "public"."qg_brain_device_source"."url" IS '地址';
469 488
... ...
... ... @@ -11,7 +11,6 @@ import org.springframework.validation.annotation.Validated;
11 11 import org.springframework.web.bind.annotation.*;
12 12 import org.thingsboard.server.common.data.exception.ThingsboardException;
13 13 import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
14   -import org.thingsboard.server.common.data.yunteng.dto.BrainDeviceCategoryDTO;
15 14 import org.thingsboard.server.common.data.yunteng.dto.BrainDevicePositionDTO;
16 15 import org.thingsboard.server.common.data.yunteng.utils.tools.ResponseResult;
17 16 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
... ...
... ... @@ -11,6 +11,7 @@ import org.thingsboard.server.common.data.yunteng.enums.BrainDeviceStatusEnum;
11 11
12 12 import javax.validation.constraints.Size;
13 13 import java.time.LocalDateTime;
  14 +import java.util.List;
14 15 import java.util.Set;
15 16
16 17 @EqualsAndHashCode(callSuper = true)
... ... @@ -50,9 +51,9 @@ public class BrainDeviceDTO extends TenantDTO {
50 51 @JsonSerialize(using = LocalDateTimeSerializer.class)
51 52 private LocalDateTime enableTime;
52 53 @ApiModelProperty(value = "图片")
53   - private String pictures;
  54 + private List<BrainDeviceSourceDTO> pictures;
54 55 @ApiModelProperty(value = "文件")
55   - private String files;
  56 + private List<BrainDeviceSourceDTO> files;
56 57 @ApiModelProperty(value = "创建人")
57 58 private String creatorName;
58 59
... ...
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import io.swagger.annotations.ApiModelProperty;
  4 +import lombok.Data;
  5 +import lombok.EqualsAndHashCode;
  6 +import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
  7 +
  8 +import javax.validation.constraints.Size;
  9 +import java.util.Set;
  10 +
  11 +@EqualsAndHashCode(callSuper = true)
  12 +@Data
  13 +public class BrainDeviceSourceDTO extends TenantDTO {
  14 +
  15 + @ApiModelProperty(value = "资源id")
  16 + private String sourceId;
  17 + @ApiModelProperty(value = "资源类型-图片/文件(picture/file)")
  18 + private String type;
  19 + @ApiModelProperty(value = "文件名称")
  20 + private String name;
  21 + @ApiModelProperty(value = "uid")
  22 + private String uid;
  23 + @ApiModelProperty(value = "url地址")
  24 + private String url;
  25 +
  26 + @Size(
  27 + min = 1,
  28 + message = "删除至少需要一个id",
  29 + groups = {DeleteGroup.class})
  30 + @ApiModelProperty(value = "删除ID列表", required = true)
  31 + private Set<String> ids;
  32 +}
... ...
... ... @@ -701,6 +701,7 @@ public class ModelConstants {
701 701 public static final String BRAINDEVICECAGEGORY_TABLE_NAME = "qg_brain_device_category";//设备类型
702 702 public static final String BRAINDEVICEPOSITION_TABLE_NAME = "qg_brain_device_position";//存放位置
703 703 public static final String BRAINDEVICE_TABLE_NAME = "qg_brain_device";//设备台账
  704 + public static final String BRAINDEVICESOURCE_TABLE_NAME = "qg_brain_device_source";//设备资源表
704 705
705 706 protected static final String[] NONE_AGGREGATION_COLUMNS = new String[]{LONG_VALUE_COLUMN, DOUBLE_VALUE_COLUMN, BOOLEAN_VALUE_COLUMN, STRING_VALUE_COLUMN, JSON_VALUE_COLUMN, KEY_COLUMN, TS_COLUMN};
706 707
... ...
... ... @@ -69,16 +69,4 @@ public class BrainDeviceEntity extends TenantBaseEntity {
69 69 * 启用日期
70 70 */
71 71 private LocalDateTime enableTime;
72   -
73   - /**
74   - * 设备图片
75   - */
76   - private String pictures;
77   -
78   - /**
79   - * 关联文档
80   - */
81   - private String files;
82   -
83   -
84 72 }
... ...
  1 +package org.thingsboard.server.dao.yunteng.entities;
  2 +
  3 +import com.baomidou.mybatisplus.annotation.TableName;
  4 +import lombok.Data;
  5 +import lombok.EqualsAndHashCode;
  6 +import org.thingsboard.server.dao.model.ModelConstants;
  7 +
  8 +/**
  9 + * 设备资源图片/文件
  10 + */
  11 +@Data
  12 +@EqualsAndHashCode(callSuper = true)
  13 +@TableName(value = ModelConstants.BRAINDEVICESOURCE_TABLE_NAME, autoResultMap = true)
  14 +public class BrainDeviceSourceEntity extends TenantBaseEntity {
  15 +
  16 + /**
  17 + * 资源id
  18 + */
  19 + private String sourceId;
  20 + /**
  21 + * 资源类型-图片/文件(picture/file)
  22 + */
  23 + private String type;
  24 +
  25 + /**
  26 + * 文件名称
  27 + */
  28 + private String name;
  29 + /**
  30 + * uid
  31 + */
  32 + private String uid;
  33 + /**
  34 + * url地址
  35 + */
  36 + private String url;
  37 +}
... ...
... ... @@ -13,6 +13,7 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
13 13 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
14 14 import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException;
15 15 import org.thingsboard.server.common.data.yunteng.dto.BrainDeviceDTO;
  16 +import org.thingsboard.server.common.data.yunteng.dto.BrainDeviceSourceDTO;
16 17 import org.thingsboard.server.common.data.yunteng.dto.UserDTO;
17 18 import org.thingsboard.server.common.data.yunteng.utils.SpringBeanUtils;
18 19 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
... ... @@ -20,6 +21,7 @@ import org.thingsboard.server.dao.yunteng.entities.BrainDeviceEntity;
20 21 import org.thingsboard.server.dao.yunteng.mapper.BrainDeviceMapper;
21 22 import org.thingsboard.server.dao.yunteng.service.AbstractBaseService;
22 23 import org.thingsboard.server.dao.yunteng.service.BrainDeviceService;
  24 +import org.thingsboard.server.dao.yunteng.service.BrainDeviceSourceService;
23 25 import org.thingsboard.server.dao.yunteng.service.TkUserService;
24 26
25 27 import javax.annotation.Resource;
... ... @@ -36,6 +38,8 @@ public class BrainDeviceServiceImpl extends AbstractBaseService<BrainDeviceMappe
36 38
37 39 @Resource
38 40 private TkUserService tkUserService;
  41 + @Resource
  42 + private BrainDeviceSourceService brainDeviceSourceService;
39 43
40 44 @Override
41 45 public TkPageData<BrainDeviceDTO> page(Map<String, Object> params) throws ThingsboardException {
... ... @@ -66,19 +70,22 @@ public class BrainDeviceServiceImpl extends AbstractBaseService<BrainDeviceMappe
66 70 throw new TkDataValidationException("设备编号重复!");
67 71 }
68 72 BrainDeviceEntity entity = new BrainDeviceEntity();
  73 + String id = "";
69 74 if (StringUtils.isBlank(brainDeviceDTO.getId())) {
70 75 brainDeviceDTO.copyToEntity(entity);
71   - String id = UUID.randomUUID().toString();
  76 + id = UUID.randomUUID().toString();
72 77 entity.setId(id);
73 78 baseMapper.insert(entity);
74 79 } else {
  80 + id = brainDeviceDTO.getId();
75 81 LambdaQueryWrapper<BrainDeviceEntity> filter = new QueryWrapper<BrainDeviceEntity>().lambda()
76   - .eq(BrainDeviceEntity::getId, brainDeviceDTO.getId());
  82 + .eq(BrainDeviceEntity::getId, id);
77 83 entity = brainDeviceDTO.getEntity(BrainDeviceEntity.class);
78 84 baseMapper.update(entity, filter);
79 85 }
80   -
81   -
  86 + //保存文件与图片
  87 + brainDeviceSourceService.batchSave(brainDeviceDTO.getPictures(),id);
  88 + brainDeviceSourceService.batchSave(brainDeviceDTO.getFiles(),id);
82 89 entity.copyToDTO(brainDeviceDTO);
83 90 return brainDeviceDTO;
84 91 }
... ... @@ -122,6 +129,10 @@ public class BrainDeviceServiceImpl extends AbstractBaseService<BrainDeviceMappe
122 129 UserDTO userInfo = tkUserService.findUserInfoById(creatorId);
123 130 result.setCreatorName(userInfo.getRealName());
124 131 }
  132 + List<BrainDeviceSourceDTO> files = brainDeviceSourceService.findDeviceSourceBySourceId(id, "file");
  133 + List<BrainDeviceSourceDTO> pictures = brainDeviceSourceService.findDeviceSourceBySourceId(id, "picture");
  134 + result.setFiles(files);
  135 + result.setPictures(pictures);
125 136 return result;
126 137 }
127 138
... ...
  1 +package org.thingsboard.server.dao.yunteng.impl;
  2 +
  3 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4 +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5 +import com.baomidou.mybatisplus.core.metadata.IPage;
  6 +import lombok.RequiredArgsConstructor;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +import org.apache.commons.collections4.CollectionUtils;
  9 +import org.apache.commons.lang3.StringUtils;
  10 +import org.springframework.stereotype.Service;
  11 +import org.springframework.transaction.annotation.Transactional;
  12 +import org.thingsboard.server.common.data.exception.ThingsboardException;
  13 +import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
  14 +import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException;
  15 +import org.thingsboard.server.common.data.yunteng.dto.BrainDeviceSourceDTO;
  16 +import org.thingsboard.server.common.data.yunteng.dto.UserDTO;
  17 +import org.thingsboard.server.common.data.yunteng.utils.SpringBeanUtils;
  18 +import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
  19 +import org.thingsboard.server.dao.yunteng.entities.BrainDeviceSourceEntity;
  20 +import org.thingsboard.server.dao.yunteng.mapper.BrainDeviceSourceMapper;
  21 +import org.thingsboard.server.dao.yunteng.service.AbstractBaseService;
  22 +import org.thingsboard.server.dao.yunteng.service.BrainDeviceSourceService;
  23 +import org.thingsboard.server.dao.yunteng.service.TkUserService;
  24 +
  25 +import javax.annotation.Resource;
  26 +import java.util.ArrayList;
  27 +import java.util.List;
  28 +import java.util.Map;
  29 +import java.util.UUID;
  30 +import java.util.stream.Collectors;
  31 +
  32 +@Service
  33 +@RequiredArgsConstructor
  34 +@Slf4j
  35 +public class BrainDeviceSourceServiceImpl extends AbstractBaseService<BrainDeviceSourceMapper, BrainDeviceSourceEntity>
  36 + implements BrainDeviceSourceService {
  37 +
  38 +
  39 + @Override
  40 + public void batchSave(List<BrainDeviceSourceDTO> list,String sourceId) throws ThingsboardException {
  41 + if (CollectionUtils.isEmpty(list) || StringUtils.isBlank(sourceId)) {
  42 + return;
  43 + }
  44 + List<BrainDeviceSourceDTO> resultList = findDeviceSourceBySourceId(sourceId,list.get(0).getType());
  45 + if (CollectionUtils.isNotEmpty(resultList)) {
  46 + List<String> ids = resultList.stream().map(item -> item.getId()).collect(Collectors.toList());
  47 + baseMapper.deleteBatchIds(ids);
  48 + }
  49 + list.forEach(item -> {
  50 + BrainDeviceSourceEntity entity = new BrainDeviceSourceEntity();
  51 + item.setSourceId(sourceId);
  52 + try {
  53 + item.setTenantId(SpringBeanUtils.getTenantId().getId().toString());
  54 + } catch (ThingsboardException e) {
  55 + throw new RuntimeException(e);
  56 + }
  57 + item.copyToEntity(entity);
  58 + String id = UUID.randomUUID().toString();
  59 + entity.setId(id);
  60 + baseMapper.insert(entity);
  61 + });
  62 + }
  63 +
  64 + @Override
  65 + public BrainDeviceSourceDTO save(BrainDeviceSourceDTO brainDeviceSourceDTO) throws ThingsboardException {
  66 + checkDto(brainDeviceSourceDTO);
  67 + BrainDeviceSourceEntity entity = new BrainDeviceSourceEntity();
  68 + if (StringUtils.isBlank(brainDeviceSourceDTO.getId())) {
  69 + brainDeviceSourceDTO.copyToEntity(entity);
  70 + String id = UUID.randomUUID().toString();
  71 + entity.setId(id);
  72 + baseMapper.insert(entity);
  73 + } else {
  74 + LambdaQueryWrapper<BrainDeviceSourceEntity> filter = new QueryWrapper<BrainDeviceSourceEntity>().lambda()
  75 + .eq(BrainDeviceSourceEntity::getId, brainDeviceSourceDTO.getId());
  76 + entity = brainDeviceSourceDTO.getEntity(BrainDeviceSourceEntity.class);
  77 + baseMapper.update(entity, filter);
  78 + }
  79 + entity.copyToDTO(brainDeviceSourceDTO);
  80 + return brainDeviceSourceDTO;
  81 + }
  82 +
  83 + private void checkDto(BrainDeviceSourceDTO dto) throws ThingsboardException {
  84 + if (StringUtils.isBlank(dto.getSourceId())) {
  85 + throw new TkDataValidationException("资源id不能为空!");
  86 + }
  87 +
  88 + if (StringUtils.isBlank(dto.getUrl())) {
  89 + throw new TkDataValidationException("文件地址不能为空!");
  90 + }
  91 +
  92 + if (StringUtils.isBlank(dto.getType())) {
  93 + throw new TkDataValidationException("文件按类型不能为空!");
  94 + }
  95 +
  96 + if (StringUtils.isBlank(dto.getTenantId())) {
  97 + dto.setTenantId(SpringBeanUtils.getTenantId().getId().toString());
  98 + }
  99 + }
  100 +
  101 + @Override
  102 + public BrainDeviceSourceDTO detail(String id) throws ThingsboardException {
  103 + BrainDeviceSourceDTO result = null;
  104 + BrainDeviceSourceEntity entity = baseMapper.selectById(id);
  105 + if (entity != null) {
  106 + result = new BrainDeviceSourceDTO();
  107 + entity.copyToDTO(result);
  108 + }
  109 + return result;
  110 + }
  111 +
  112 + @Override
  113 + public boolean delete(String id) throws ThingsboardException {
  114 + int count = baseMapper.deleteById(id);
  115 + return count > 0;
  116 + }
  117 +
  118 + @Override
  119 + public List<BrainDeviceSourceDTO> findDeviceSourceBySourceId(String sourceId,String type) throws ThingsboardException {
  120 + QueryWrapper<BrainDeviceSourceEntity> queryWrapper = new QueryWrapper();
  121 + LambdaQueryWrapper<BrainDeviceSourceEntity> lambda = queryWrapper.lambda();
  122 + lambda.eq(BrainDeviceSourceEntity::getTenantId, SpringBeanUtils.getTenantId().getId().toString());
  123 + if (StringUtils.isEmpty(sourceId)) {
  124 + return new ArrayList<>();
  125 + }
  126 + lambda.eq(BrainDeviceSourceEntity::getSourceId, sourceId);
  127 + if (StringUtils.isNotEmpty(type)) {
  128 + lambda.eq(BrainDeviceSourceEntity::getType, type);
  129 + }
  130 + List<BrainDeviceSourceEntity> entityList = baseMapper.selectList(queryWrapper);
  131 + if (CollectionUtils.isEmpty(entityList)) {
  132 + return new ArrayList<>();
  133 + }
  134 + return entityList.stream().map(item -> {
  135 + BrainDeviceSourceDTO dto = new BrainDeviceSourceDTO();
  136 + item.copyToDTO(dto);
  137 + return dto;
  138 + }).collect(Collectors.toList());
  139 + }
  140 +}
... ...
  1 +package org.thingsboard.server.dao.yunteng.mapper;
  2 +
  3 +import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4 +import org.apache.ibatis.annotations.Mapper;
  5 +import org.thingsboard.server.dao.yunteng.entities.BrainDeviceSourceEntity;
  6 +
  7 +import java.util.Map;
  8 +
  9 +@Mapper
  10 +public interface BrainDeviceSourceMapper extends BaseMapper<BrainDeviceSourceEntity> {
  11 +
  12 +}
... ...
  1 +package org.thingsboard.server.dao.yunteng.service;
  2 +
  3 +import org.thingsboard.server.common.data.exception.ThingsboardException;
  4 +import org.thingsboard.server.common.data.yunteng.dto.BrainDeviceSourceDTO;
  5 +import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
  6 +import org.thingsboard.server.dao.yunteng.entities.BrainDeviceSourceEntity;
  7 +
  8 +import java.util.List;
  9 +import java.util.Map;
  10 +
  11 +public interface BrainDeviceSourceService extends BaseService<BrainDeviceSourceEntity> {
  12 +
  13 + void batchSave(List<BrainDeviceSourceDTO> list, String sourceId) throws ThingsboardException;
  14 +
  15 + BrainDeviceSourceDTO save(BrainDeviceSourceDTO BrainDeviceSourceDTO) throws ThingsboardException;
  16 +
  17 + BrainDeviceSourceDTO detail(String id) throws ThingsboardException;
  18 +
  19 + boolean delete(String id) throws ThingsboardException;
  20 +
  21 + List<BrainDeviceSourceDTO> findDeviceSourceBySourceId(String sourceId,String type) throws ThingsboardException;
  22 +}
... ...