Showing
10 changed files
with
220 additions
and
1 deletions
... | ... | @@ -471,3 +471,20 @@ COMMENT ON COLUMN qg_db_connect_ssl.ca_cert IS 'CA证书'; |
471 | 471 | COMMENT ON COLUMN qg_db_connect_ssl.verify_ca_cert IS '验证CA证书'; |
472 | 472 | COMMENT ON COLUMN qg_db_connect_ssl.client_key IS '客户端秘钥'; |
473 | 473 | COMMENT ON COLUMN qg_db_connect_ssl.client_cert IS '客户端证书'; |
474 | + | |
475 | +CREATE TABLE "qg_db_data_set" ( | |
476 | + "id" varchar(36) PRIMARY KEY, | |
477 | + "component_code" varchar(36) NOT NULL, | |
478 | + "connect_id" varchar(36) NOT NULL, | |
479 | + "sql" text NOT NULL, | |
480 | + "tenant_id" varchar(36), | |
481 | + "create_time" timestamp(6), | |
482 | + "creator" varchar(36) NULL, | |
483 | + "updater" varchar(36) NULL, | |
484 | + "update_time" timestamp(6) | |
485 | +); | |
486 | +COMMENT ON TABLE qg_db_data_set IS '数据集配置'; | |
487 | +COMMENT ON COLUMN qg_db_data_set.tenant_id IS '租户ID'; | |
488 | +COMMENT ON COLUMN qg_db_data_set.component_code IS '大屏组件code'; | |
489 | +COMMENT ON COLUMN qg_db_data_set.connect_id IS '数据库链接信息id'; | |
490 | +COMMENT ON COLUMN qg_db_data_set.sql IS 'sql内容'; | |
\ No newline at end of file | ... | ... |
... | ... | @@ -10,10 +10,12 @@ import org.springframework.security.access.prepost.PreAuthorize; |
10 | 10 | import org.springframework.web.bind.annotation.*; |
11 | 11 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
12 | 12 | import org.thingsboard.server.common.data.yunteng.dto.TkDbConnectDTO; |
13 | +import org.thingsboard.server.common.data.yunteng.dto.TkDbDataSetDTO; | |
13 | 14 | import org.thingsboard.server.common.data.yunteng.enums.DbConnectTypeEnum; |
14 | 15 | import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; |
15 | 16 | import org.thingsboard.server.controller.BaseController; |
16 | 17 | import org.thingsboard.server.dao.yunteng.service.TkDbConnectService; |
18 | +import org.thingsboard.server.dao.yunteng.service.TkDbDataSetService; | |
17 | 19 | import org.thingsboard.server.queue.util.TbCoreComponent; |
18 | 20 | |
19 | 21 | import java.util.HashMap; |
... | ... | @@ -31,6 +33,7 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. |
31 | 33 | @Slf4j |
32 | 34 | public class TkDbConnectController extends BaseController { |
33 | 35 | private final TkDbConnectService tkDbConnectService; |
36 | + private final TkDbDataSetService tkDbDataSetService; | |
34 | 37 | |
35 | 38 | @GetMapping(params = {PAGE_SIZE, PAGE}) |
36 | 39 | @ApiOperation("分页查询") |
... | ... | @@ -80,4 +83,23 @@ public class TkDbConnectController extends BaseController { |
80 | 83 | public ResponseEntity<Object> dataView(@RequestBody TkDbConnectDTO tkDbConnectDTO) throws ThingsboardException { |
81 | 84 | return ResponseEntity.ok(tkDbConnectService.connectResult(tkDbConnectDTO)); |
82 | 85 | } |
86 | + | |
87 | + @PostMapping("/saveDataSet") | |
88 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')") | |
89 | + public ResponseEntity<TkDbDataSetDTO> dataView(@RequestBody TkDbDataSetDTO tkDbDataSetDTO) throws ThingsboardException { | |
90 | + tkDbDataSetDTO.setTenantId(getCurrentUser().getCurrentTenantId()); | |
91 | + return ResponseEntity.ok(tkDbDataSetService.save(tkDbDataSetDTO)); | |
92 | + } | |
93 | + | |
94 | + @GetMapping("/getDataSet") | |
95 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')") | |
96 | + public ResponseEntity<TkDbDataSetDTO> getDataSet(@RequestParam("id") String id) throws ThingsboardException { | |
97 | + return ResponseEntity.ok(tkDbDataSetService.get(id)); | |
98 | + } | |
99 | + | |
100 | + @PostMapping("/getDataView") | |
101 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN','CUSTOMER_USER')") | |
102 | + public ResponseEntity<Object> getDataView(@RequestBody TkDbDataSetDTO tkDbDataSetDTO) throws ThingsboardException { | |
103 | + return ResponseEntity.ok(tkDbDataSetService.getDataView(tkDbDataSetDTO)); | |
104 | + } | |
83 | 105 | } | ... | ... |
... | ... | @@ -5,6 +5,8 @@ import lombok.Data; |
5 | 5 | import lombok.EqualsAndHashCode; |
6 | 6 | import org.thingsboard.server.common.data.yunteng.enums.DbConnectTypeEnum; |
7 | 7 | |
8 | +import java.util.Map; | |
9 | + | |
8 | 10 | /** |
9 | 11 | * 数据库链接信息 |
10 | 12 | */ |
... | ... | @@ -62,4 +64,7 @@ public class TkDbConnectDTO extends TenantDTO { |
62 | 64 | |
63 | 65 | @ApiModelProperty("预览sql") |
64 | 66 | private String sql; |
67 | + | |
68 | + @ApiModelProperty("SQL传参") | |
69 | + private Map<String, Object> paramMap; | |
65 | 70 | } | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/dto/TkDbDataSetDTO.java
0 → 100644
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 | + | |
7 | +import java.util.Map; | |
8 | + | |
9 | +/** | |
10 | + * SSL通道配置信息 | |
11 | + */ | |
12 | +@EqualsAndHashCode(callSuper = true) | |
13 | +@Data | |
14 | +public class TkDbDataSetDTO extends TenantDTO { | |
15 | + | |
16 | + @ApiModelProperty("大屏组件code") | |
17 | + private String componentCode; | |
18 | + | |
19 | + @ApiModelProperty("数据库链接信息id") | |
20 | + private String connectId; | |
21 | + | |
22 | + @ApiModelProperty("sql内容") | |
23 | + private String sql; | |
24 | + | |
25 | + @ApiModelProperty("数据库链接信息") | |
26 | + private TkDbConnectDTO tkDbConnectDTO; | |
27 | + | |
28 | + @ApiModelProperty("SQL传参") | |
29 | + private Map<String, Object> paramMap; | |
30 | +} | ... | ... |
... | ... | @@ -704,6 +704,7 @@ public class ModelConstants { |
704 | 704 | public static final String TKDBCONNECT_TABLE_NAME = "qg_db_connect"; // 数据库链接信息 |
705 | 705 | public static final String TKDBCONNECTSSH_TABLE_NAME = "qg_db_connect_ssh"; // SSH通道配置信息 |
706 | 706 | public static final String TKDBCONNECTSSL_TABLE_NAME = "qg_db_connect_ssl"; // SSL通道配置信息 |
707 | + public static final String TKDBDATESET_TABLE_NAME = "qg_db_data_set"; // 数据集配置 | |
707 | 708 | |
708 | 709 | 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}; |
709 | 710 | ... | ... |
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.TKDBDATESET_TABLE_NAME, autoResultMap = true) | |
14 | +public class TkDbDataSetEntity extends TenantBaseEntity { | |
15 | + | |
16 | + private String componentCode; | |
17 | + | |
18 | + private String connectId; | |
19 | + | |
20 | + private String sql; | |
21 | +} | ... | ... |
... | ... | @@ -5,7 +5,9 @@ import com.jcraft.jsch.Session; |
5 | 5 | import com.zaxxer.hikari.HikariConfig; |
6 | 6 | import com.zaxxer.hikari.HikariDataSource; |
7 | 7 | import lombok.extern.slf4j.Slf4j; |
8 | +import org.apache.commons.collections4.MapUtils; | |
8 | 9 | import org.apache.commons.lang3.BooleanUtils; |
10 | +import org.apache.commons.lang3.ObjectUtils; | |
9 | 11 | import org.apache.commons.lang3.StringUtils; |
10 | 12 | import org.springframework.stereotype.Service; |
11 | 13 | import org.thingsboard.server.common.data.yunteng.dto.TkDbConnectDTO; |
... | ... | @@ -21,6 +23,7 @@ import java.security.KeyStore; |
21 | 23 | import java.sql.*; |
22 | 24 | import java.util.ArrayList; |
23 | 25 | import java.util.List; |
26 | +import java.util.Map; | |
24 | 27 | import java.util.Properties; |
25 | 28 | |
26 | 29 | /** |
... | ... | @@ -181,7 +184,7 @@ public class BaseDbConnectServiceImpl implements BaseDbConnectService { |
181 | 184 | List<Object> resultList = new ArrayList<>(); |
182 | 185 | try { |
183 | 186 | HikariConfig config = new HikariConfig(); |
184 | - config.setJdbcUrl(tkDbConnectDTO.getUrl()); | |
187 | + config.setJdbcUrl(replaceParam(tkDbConnectDTO.getUrl(), tkDbConnectDTO.getParamMap())); | |
185 | 188 | config.setUsername(tkDbConnectDTO.getUserName()); |
186 | 189 | config.setPassword(tkDbConnectDTO.getPassword()); |
187 | 190 | if (DbConnectTypeEnum.MySql.equals(tkDbConnectDTO.getType())) { |
... | ... | @@ -254,6 +257,24 @@ public class BaseDbConnectServiceImpl implements BaseDbConnectService { |
254 | 257 | return resultList; |
255 | 258 | } |
256 | 259 | |
260 | + private String replaceParam(String url, Map<String, Object> paramMap) { | |
261 | + if (MapUtils.isEmpty(paramMap)) { | |
262 | + return url; | |
263 | + } | |
264 | + | |
265 | + for (Map.Entry<String, Object> objectEntry : paramMap.entrySet()) { | |
266 | + String key = objectEntry.getKey(); | |
267 | + Object value = objectEntry.getValue(); | |
268 | + if (StringUtils.isBlank(key) || ObjectUtils.isEmpty(value)) { | |
269 | + continue; | |
270 | + } | |
271 | + | |
272 | + url = url.replaceAll("${" + key + "}", String.valueOf(value)); | |
273 | + } | |
274 | + | |
275 | + return url; | |
276 | + } | |
277 | + | |
257 | 278 | private Object getTypedValue(ResultSet rs, int index, int sqlType) throws SQLException { |
258 | 279 | Object value; |
259 | 280 | ... | ... |
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 lombok.RequiredArgsConstructor; | |
6 | +import lombok.extern.slf4j.Slf4j; | |
7 | +import org.apache.commons.lang3.StringUtils; | |
8 | +import org.springframework.stereotype.Service; | |
9 | +import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException; | |
10 | +import org.thingsboard.server.common.data.yunteng.dto.TkDbConnectDTO; | |
11 | +import org.thingsboard.server.common.data.yunteng.dto.TkDbDataSetDTO; | |
12 | +import org.thingsboard.server.dao.yunteng.entities.TkDbDataSetEntity; | |
13 | +import org.thingsboard.server.dao.yunteng.factory.DbConnectServiceFactory; | |
14 | +import org.thingsboard.server.dao.yunteng.mapper.TkDbDataSetMapper; | |
15 | +import org.thingsboard.server.dao.yunteng.service.AbstractBaseService; | |
16 | +import org.thingsboard.server.dao.yunteng.service.TkDbConnectService; | |
17 | +import org.thingsboard.server.dao.yunteng.service.TkDbDataSetService; | |
18 | + | |
19 | +import java.util.Map; | |
20 | + | |
21 | +@Service | |
22 | +@RequiredArgsConstructor | |
23 | +@Slf4j | |
24 | +public class TkDbDataSetServiceImpl extends AbstractBaseService<TkDbDataSetMapper, TkDbDataSetEntity> | |
25 | + implements TkDbDataSetService { | |
26 | + | |
27 | + private final TkDbConnectService tkDbConnectService; | |
28 | + private final DbConnectServiceFactory connectServiceFactory; | |
29 | + | |
30 | + @Override | |
31 | + public TkDbDataSetDTO save(TkDbDataSetDTO dto) { | |
32 | + checkDto(dto); | |
33 | + TkDbDataSetEntity entity = new TkDbDataSetEntity(); | |
34 | + if (StringUtils.isBlank(dto.getId())) { | |
35 | + dto.copyToEntity(entity); | |
36 | + baseMapper.insert(entity); | |
37 | + } else { | |
38 | + LambdaQueryWrapper<TkDbDataSetEntity> filter = new QueryWrapper<TkDbDataSetEntity>().lambda() | |
39 | + .eq(TkDbDataSetEntity::getId, dto.getId()); | |
40 | + entity = dto.getEntity(TkDbDataSetEntity.class); | |
41 | + baseMapper.update(entity, filter); | |
42 | + } | |
43 | + | |
44 | + return dto; | |
45 | + } | |
46 | + | |
47 | + @Override | |
48 | + public TkDbDataSetDTO get(String id) { | |
49 | + if (StringUtils.isBlank(id)) { | |
50 | + return new TkDbDataSetDTO(); | |
51 | + } | |
52 | + | |
53 | + TkDbDataSetEntity entity = baseMapper.selectById(id); | |
54 | + TkDbDataSetDTO tkDbDataSetDTO = new TkDbDataSetDTO(); | |
55 | + entity.copyToDTO(tkDbDataSetDTO); | |
56 | + if (StringUtils.isNotBlank(tkDbDataSetDTO.getConnectId())) { | |
57 | + tkDbDataSetDTO.setTkDbConnectDTO(tkDbConnectService.get(tkDbDataSetDTO.getConnectId())); | |
58 | + } | |
59 | + | |
60 | + return tkDbDataSetDTO; | |
61 | + } | |
62 | + | |
63 | + @Override | |
64 | + public Object getDataView(TkDbDataSetDTO dto) { | |
65 | + Map<String, Object> paramMap = dto.getParamMap(); | |
66 | + dto = get(dto.getId()); | |
67 | + TkDbConnectDTO dbConnectDTO = dto.getTkDbConnectDTO(); | |
68 | + dbConnectDTO.setParamMap(paramMap); | |
69 | + return connectServiceFactory.getService("baseConnect").connect(dbConnectDTO); | |
70 | + } | |
71 | + | |
72 | + private void checkDto(TkDbDataSetDTO dto) { | |
73 | + if (StringUtils.isBlank(dto.getTenantId())) { | |
74 | + throw new TkDataValidationException("租户id为空!"); | |
75 | + } | |
76 | + } | |
77 | +} | ... | ... |
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.TkDbDataSetEntity; | |
6 | + | |
7 | +/** | |
8 | + * 数据集配置 | |
9 | + */ | |
10 | +@Mapper | |
11 | +public interface TkDbDataSetMapper extends BaseMapper<TkDbDataSetEntity> { | |
12 | +} | ... | ... |
1 | +package org.thingsboard.server.dao.yunteng.service; | |
2 | + | |
3 | +import org.thingsboard.server.common.data.yunteng.dto.TkDbDataSetDTO; | |
4 | +import org.thingsboard.server.dao.yunteng.entities.TkDbDataSetEntity; | |
5 | + | |
6 | +public interface TkDbDataSetService extends BaseService<TkDbDataSetEntity> { | |
7 | + | |
8 | + TkDbDataSetDTO save(TkDbDataSetDTO dto); | |
9 | + | |
10 | + TkDbDataSetDTO get(String id); | |
11 | + | |
12 | + Object getDataView(TkDbDataSetDTO dto); | |
13 | +} | ... | ... |