Skip to content

Commit f65e5aa

Browse files
author
尘绝
committed
id string->long ,INCREMENT
add mysql 。conf
1 parent 75e6fb5 commit f65e5aa

File tree

12 files changed

+162
-19
lines changed

12 files changed

+162
-19
lines changed

examples/mysql/init/init.sql

+3-3
Large diffs are not rendered by default.

runtime/bootstrap/src/main/resources/config/application.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ spring.datasource.password=root
99
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1010

1111
mybatis-plus.mapper-locations=classpath:mapper/*.xml
12-
mybatis-plus.type-aliases-package=com.alipay.muagent.model
12+
mybatis-plus.type-aliases-package=com.alipay.muagent.model
13+
14+
#配置读取tool的方式:local|mysql
15+
runtime.tool.datatype=mysql

runtime/bootstrap/src/main/resources/tools/system.select_tool.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "system.select_tool",
2+
"id": 2,
33
"gmtCreate": "2024-07-17T07:20:35.000+00:00",
44
"gmtModified": "2024-07-17T07:20:35.000+00:00",
55
"toolKey": "system.select_tool",

runtime/bootstrap/src/main/resources/tools/undercover.dispatch_keyword.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "undercover.dispatch_keyword",
2+
"id": 2,
33
"gmtCreate": "2024-07-17T07:20:35.000+00:00",
44
"gmtModified": "2024-07-17T07:20:35.000+00:00",
55
"toolKey": "undercover.dispatch_keyword",

runtime/bootstrap/src/main/resources/tools/undercover.dispatch_position.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "undercover.dispatch_position",
2+
"id": 2,
33
"gmtCreate": "2024-07-17T07:20:35.000+00:00",
44
"gmtModified": "2024-07-17T07:20:35.000+00:00",
55
"toolKey": "undercover.dispatch_position",

runtime/bootstrap/src/main/resources/tools/undercover.judge.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "undercover.judge",
2+
"id": 2,
33
"gmtCreate": "2024-07-17T07:20:35.000+00:00",
44
"gmtModified": "2024-07-17T07:20:35.000+00:00",
55
"toolKey": "undercover.judge",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Alipay.com Inc.
3+
* Copyright (c) 2004-2024 All Rights Reserved.
4+
*/
5+
package com.alipay.muagent.model.enums.tool;
6+
7+
/**
8+
* @author chenjue.wwp
9+
* @version : ToolDataTypeEnum.java, v 0.1 2024年12月03日 下午17:01 renmao.rm Exp $
10+
*/
11+
public enum ToolDataTypeEnum {
12+
LOCAL("local", "本地数据"), MYSQL("mysql", "MYSQL数据");
13+
14+
private String name;
15+
private String desc;
16+
17+
ToolDataTypeEnum(String name, String desc) {
18+
this.name = name;
19+
this.desc = desc;
20+
}
21+
22+
/**
23+
* Gets get by name.
24+
*
25+
* @param name the name
26+
* @return the get by name
27+
*/
28+
public static ToolDataTypeEnum getByName(String name) {
29+
for (ToolDataTypeEnum toolDataTypeEnum : ToolDataTypeEnum.values()) {
30+
if (toolDataTypeEnum.name.equals(name)) {
31+
return toolDataTypeEnum;
32+
}
33+
}
34+
throw new RuntimeException("ToolDataTypeEnum not found");
35+
}
36+
37+
/**
38+
* Getter method for property <tt>name</tt>.
39+
*
40+
* @return property value of name
41+
*/
42+
public String getName() {
43+
return name;
44+
}
45+
46+
/**
47+
* Setter method for property <tt>name</tt>.
48+
*
49+
* @param name value to be assigned to property name
50+
*/
51+
public void setName(String name) {
52+
this.name = name;
53+
}
54+
55+
/**
56+
* Getter method for property <tt>desc</tt>.
57+
*
58+
* @return property value of desc
59+
*/
60+
public String getDesc() {
61+
return desc;
62+
}
63+
64+
/**
65+
* Setter method for property <tt>desc</tt>.
66+
*
67+
* @param desc value to be assigned to property desc
68+
*/
69+
public void setDesc(String desc) {
70+
this.desc = desc;
71+
}
72+
}

runtime/model/src/main/java/com/alipay/muagent/model/tool/meta/Tool.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@Data
1717
public class Tool {
1818

19-
private String id;
19+
private Long id;
2020

2121
/**
2222
* 创建时间

runtime/service/src/main/java/com/alipay/muagent/service/mybatisplus/dto/ToolDO.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@Data
1616
@TableName(value = "tool")
1717
public class ToolDO {
18-
private String id;
18+
private Long id;
1919

2020
/**
2121
* 创建时间

runtime/service/src/main/java/com/alipay/muagent/service/tool/loader/ToolLoader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
*/
1515
public interface ToolLoader {
1616

17-
Tool queryToolById(String id);
17+
Tool queryToolById(Long id);
1818

19-
List<Tool> queryToolsByIdList(List<String> id);
19+
List<Tool> queryToolsByIdList(List<Long> id);
2020

2121
Tool queryToolByKey(String toolKey);
2222

runtime/service/src/main/java/com/alipay/muagent/service/tool/loader/impl/LocalToolLoader.java

+74-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44
*/
55
package com.alipay.muagent.service.tool.loader.impl;
66

7+
import com.alipay.muagent.model.enums.tool.ToolDataTypeEnum;
78
import com.alipay.muagent.model.tool.meta.Tool;
89
import com.alipay.muagent.service.mybatisplus.dto.ToolConverter;
910
import com.alipay.muagent.service.mybatisplus.dto.ToolDO;
1011
import com.alipay.muagent.service.mybatisplus.mapper.TooDOlMapper;
1112
import com.alipay.muagent.service.tool.loader.ToolLoader;
13+
import com.alipay.muagent.util.GsonUtils;
14+
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
1215
import com.google.common.collect.Lists;
1316
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.beans.factory.annotation.Value;
18+
import org.springframework.core.io.Resource;
1419
import org.springframework.core.io.ResourceLoader;
1520
import org.springframework.stereotype.Service;
1621
import org.springframework.util.CollectionUtils;
22+
import java.io.BufferedInputStream;
1723
import java.util.List;
1824

1925
/**
@@ -26,32 +32,94 @@ public class LocalToolLoader implements ToolLoader {
2632
@Autowired
2733
private ResourceLoader resourceLoader;
2834

35+
@Value("${runtime.tool.datatype:local}")
36+
private String toolType;
37+
2938
@Autowired
3039
private TooDOlMapper tooDOlMapper;
3140

41+
/**
42+
* Query tool by id tool.
43+
*
44+
* @param id the id
45+
* @return the tool
46+
*/
3247
@Override
33-
public Tool queryToolById(String id) {
34-
return queryToolByKey(id);
48+
public Tool queryToolById(Long id) {
49+
ToolDataTypeEnum toolDataTypeEnum = ToolDataTypeEnum.getByName(toolType);
50+
return switch (toolDataTypeEnum) {
51+
case LOCAL -> queryLocalToolByKey(String.valueOf(id));
52+
case MYSQL -> queryMysqlToolById(id);
53+
};
3554
}
3655

56+
/**
57+
* Query tools by id list list.
58+
*
59+
* @param ids the ids
60+
* @return the list
61+
*/
3762
@Override
38-
public List<Tool> queryToolsByIdList(List<String> ids) {
63+
public List<Tool> queryToolsByIdList(List<Long> ids) {
3964
if (CollectionUtils.isEmpty(ids)) {
4065
return Lists.newArrayList();
4166
}
4267
return ids.stream().map(this::queryToolById).toList();
4368
}
4469

70+
/**
71+
* Query tool by key tool.
72+
*
73+
* @param id the id
74+
* @return the tool
75+
*/
4576
@Override
4677
public Tool queryToolByKey(String id) {
78+
ToolDataTypeEnum toolDataTypeEnum = ToolDataTypeEnum.getByName(toolType);
79+
return switch (toolDataTypeEnum) {
80+
case LOCAL -> queryLocalToolByKey(id);
81+
case MYSQL -> queryMysqlToolByKey(id);
82+
};
83+
}
84+
85+
private Tool queryMysqlToolById(Long id) {
86+
ToolDO toolDO = tooDOlMapper.selectById(id);
87+
return new ToolConverter().convertFromDto(toolDO);
88+
}
89+
90+
private Tool queryMysqlToolByKey(String key) {
91+
QueryWrapper<ToolDO> queryWrapper = new QueryWrapper<>();
92+
queryWrapper.eq("tool_key", key);
93+
ToolDO toolDO = tooDOlMapper.selectOne(queryWrapper);
94+
return new ToolConverter().convertFromDto(toolDO);
95+
}
96+
97+
private Tool queryLocalToolByKey(String key) {
98+
String fileName = String.format("tools/%s.json", key);
4799
try {
48-
ToolDO toolDO = tooDOlMapper.selectById(id);
49-
return new ToolConverter().convertFromDto(toolDO);
100+
Resource resource = resourceLoader.getResource("classpath:" + fileName);
101+
BufferedInputStream bufferedReader = new BufferedInputStream(resource.getInputStream());
102+
byte[] buffer = new byte[1024]; // 设置缓冲区大小
103+
int bytesRead = 0;
104+
StringBuffer sBuffer = new StringBuffer();
105+
while ((bytesRead = bufferedReader.read(buffer)) != -1) {
106+
sBuffer.append(new String(buffer, 0, bytesRead));
107+
}
108+
bufferedReader.close();
109+
110+
Tool tool = GsonUtils.fromString(Tool.class, sBuffer.toString());
111+
return tool;
50112
} catch (Exception e) {
51-
throw new RuntimeException(String.format("loadToolFailed:%s", id), e);
113+
throw new RuntimeException(String.format("loadToolFailed:%s", key), e);
52114
}
53115
}
54116

117+
/**
118+
* Query tools by key list list.
119+
*
120+
* @param keys the keys
121+
* @return the list
122+
*/
55123
@Override
56124
public List<Tool> queryToolsByKeyList(List<String> keys) {
57125
if (CollectionUtils.isEmpty(keys)) {

runtime/web/src/main/java/com/alipay/muagent/web/ToolController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ToolController {
3535

3636
@RequestMapping(method = RequestMethod.GET)
3737
@ResponseBody
38-
public Tool queryToolById(@RequestParam String id) {
38+
public Tool queryToolById(@RequestParam Long id) {
3939
return toolLoader.queryToolById(id);
4040
}
4141

0 commit comments

Comments
 (0)