Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-tool
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tool
amos-tool
Commits
2f4e99bd
Commit
2f4e99bd
authored
Jan 02, 2024
by
chenzai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit 导出
parent
f3846066
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
193 additions
and
13 deletions
+193
-13
DataSourceConfig.java
...va/com/yeejoin/amos/api/tool/config/DataSourceConfig.java
+2
-0
DataApiResource.java
...com/yeejoin/amos/api/tool/controller/DataApiResource.java
+1
-2
DatabaseConnectionResource.java
.../amos/api/tool/controller/DatabaseConnectionResource.java
+4
-1
DatabaseConnectionService.java
...amos/api/tool/face/service/DatabaseConnectionService.java
+1
-0
StudioResourceService.java
...oin/amos/api/tool/face/service/StudioResourceService.java
+180
-6
DatabaseUtils.java
...n/java/com/yeejoin/amos/api/tool/utils/DatabaseUtils.java
+3
-4
application-qa.properties
src/main/resources/application-qa.properties
+2
-0
No files found.
src/main/java/com/yeejoin/amos/api/tool/config/DataSourceConfig.java
View file @
2f4e99bd
...
...
@@ -26,4 +26,5 @@ public class DataSourceConfig {
public
DataSource
secondDataSource
()
{
return
DataSourceBuilder
.
create
().
build
();
}
}
\ No newline at end of file
src/main/java/com/yeejoin/amos/api/tool/controller/DataApiResource.java
View file @
2f4e99bd
...
...
@@ -37,8 +37,7 @@ import java.util.Map;
@TycloudResource
(
module
=
"atl"
,
value
=
"dataApi"
)
@RequestMapping
(
value
=
"/v1/dataApi"
)
@Api
(
tags
=
"atl-导出"
)
public
class
DataApiResource
{
public
class
DataApiResource
{
private
final
Logger
logger
=
LogManager
.
getLogger
(
DataApiResource
.
class
);
...
...
src/main/java/com/yeejoin/amos/api/tool/controller/DatabaseConnectionResource.java
View file @
2f4e99bd
...
...
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import
com.yeejoin.amos.api.tool.face.model.AgencyModel
;
import
com.yeejoin.amos.api.tool.face.model.DatabaseConnectionModel
;
import
com.yeejoin.amos.api.tool.face.service.DatabaseConnectionService
;
import
com.yeejoin.amos.api.tool.utils.AESUtils
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -79,7 +80,9 @@ public class DatabaseConnectionResource {
@TycloudOperation
(
ApiLevel
=
UserType
.
AGENCY
,
needAuth
=
false
)
@GetMapping
(
value
=
"/{sequenceNbr}"
)
@ApiOperation
(
httpMethod
=
"GET"
,
value
=
"根据sequenceNbr查询单个"
,
notes
=
"根据sequenceNbr查询单个"
)
public
ResponseModel
<
DatabaseConnectionModel
>
selectOne
(
@PathVariable
Long
sequenceNbr
)
{
public
ResponseModel
<
DatabaseConnectionModel
>
selectOne
(
@PathVariable
Long
sequenceNbr
)
throws
Exception
{
// DatabaseConnectionModel databaseConnectionModel = databaseConnectionService.queryBySeq(sequenceNbr);
// databaseConnectionModel.setPassWord(AESUtils.decrypt(databaseConnectionModel.getPassWord()));
return
ResponseHelper
.
buildResponse
(
databaseConnectionService
.
queryBySeq
(
sequenceNbr
));
}
...
...
src/main/java/com/yeejoin/amos/api/tool/face/service/DatabaseConnectionService.java
View file @
2f4e99bd
...
...
@@ -53,6 +53,7 @@ public class DatabaseConnectionService extends BaseService<DatabaseConnectionMod
*/
public
DatabaseConnectionModel
update
(
DatabaseConnectionModel
model
)
throws
Exception
{
if
(
DatabaseUtils
.
isConnectionValid
(
model
))
{
// return this.updateWithModel(AESUtils.encryptPwd(model));
return
this
.
updateWithModel
(
model
);
}
else
{
return
null
;
...
...
src/main/java/com/yeejoin/amos/api/tool/face/service/StudioResourceService.java
View file @
2f4e99bd
...
...
@@ -10,6 +10,7 @@ import com.yeejoin.amos.api.tool.face.orm.entity.StudioResource;
import
com.yeejoin.amos.api.tool.face.model.StudioResourceModel
;
import
com.yeejoin.amos.api.tool.face.orm.dao.StudioResourceMapper
;
import
com.yeejoin.amos.api.tool.face.orm.entity.TableColumn
;
import
com.yeejoin.amos.api.tool.utils.AESUtils
;
import
com.yeejoin.amos.api.tool.utils.DatabaseUtils
;
import
com.yeejoin.amos.api.tool.utils.DateUtils
;
import
com.yeejoin.amos.api.tool.utils.SqlHelper
;
...
...
@@ -17,6 +18,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
import
org.apache.commons.text.StringSubstitutor
;
import
org.eclipse.paho.client.mqttv3.MqttException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.ObjectUtils
;
...
...
@@ -32,6 +34,8 @@ import java.util.ArrayList;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
/**
...
...
@@ -71,8 +75,24 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
private
static
final
String
ABLEFOREIGN
=
"SET FOREIGN_KEY_CHECKS = 1;\r\n"
;
private
static
final
String
DELIMITER
=
"###################################"
;
/**
* 导出设计器中大屏时分库查询 因为可能加yeeamos,这里不使用多数据源
*/
public
static
final
String
databaseName1
=
"amos_studio"
;
public
static
final
String
databaseName2
=
"amos_project"
;
/**
* amos-tool-library 下的studio_resource
* 中的插入字段已有下列字段,在文本中用其他字符串替换,这里To表示真正要插入的表明
*/
public
static
final
String
column1
=
"morphic_screen"
;
public
static
final
String
column1To
=
"studio_application"
;
public
static
final
String
column2
=
"morphic_screen_scene"
;
public
static
final
String
column2To
=
"morphic_scene"
;
public
static
final
String
appKey
=
"APP_KEY"
;
@Autowired
JdbcTemplate
jdbcTemplate
;
@Autowired
DataSource
datasource
;
...
...
@@ -85,6 +105,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
@Autowired
EmqKeeper
emqKeeper
;
/**
* 列表查询 示例
*/
...
...
@@ -114,13 +135,19 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
String
fileName
=
(
SourceEnum
.
IDX
.
getSource
().
equals
(
resourceCode
.
toLowerCase
())
?
(
resourceCode
.
toLowerCase
()
+
"_"
)
:
"studio_"
)
+
DateFormatUtils
.
format
(
new
Date
(),
"yyyyMMdd"
)
+
"."
+
SUFFIX
;
String
sqlFilePath
=
backPath
+
SLASH
+
fileName
;
File
file
=
new
File
(
sqlFilePath
);
FileOutputStream
out
;
OutputStreamWriter
writer
=
null
;
out
=
new
FileOutputStream
(
file
);
writer
=
new
OutputStreamWriter
(
out
,
"utf8"
);
String
fileName2
=
"project"
+
DateFormatUtils
.
format
(
new
Date
(),
"yyyyMMdd"
)
+
"."
+
SUFFIX
;
String
sqlFilePath2
=
backPath
+
SLASH
+
fileName2
;
File
file2
=
new
File
(
sqlFilePath2
);
FileOutputStream
out2
;
OutputStreamWriter
writer2
=
null
;
out2
=
new
FileOutputStream
(
file2
);
writer2
=
new
OutputStreamWriter
(
out2
,
"utf8"
);
StringBuffer
deleteSql
=
new
StringBuffer
();
StringBuffer
insertSql
=
new
StringBuffer
();
...
...
@@ -131,9 +158,11 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
if
(!
ObjectUtils
.
isEmpty
(
model
.
getResourceDeleteSql
()))
{
String
json
=
format
(
model
.
getResourceDeleteSql
(),
variables
);
JSONObject
dimensionData
=
JSONObject
.
parseObject
(
json
);
String
deleteAllSql
=
dimensionData
.
getString
(
dimension
);
deleteAllSql
.
replaceAll
(
";"
,
";"
+
BR
);
deleteSql
.
append
(
deleteAllSql
);
JSONObject
deleteAllSql
=
dimensionData
.
getJSONObject
(
dimension
);
for
(
String
tableName
:
deleteAllSql
.
keySet
())
{
String
delSql
=(
String
)
deleteAllSql
.
get
(
tableName
);
deleteSql
.
append
(
delSql
.
replace
(
";"
,
";"
+
BR
));
}
}
if
(!
ObjectUtils
.
isEmpty
(
model
.
getResourceInsertSql
()))
{
String
sql
=
buildInsertSql
(
model
,
dimension
,
ipSeq
,
variables
);
...
...
@@ -141,6 +170,43 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
}
}
Connection
connection
=
this
.
getConnection
(
databaseConnectionService
.
queryBySeq
(
ipSeq
),
databaseName1
);
String
sql
=
"select * from studio_application where SEQUENCE_NBR="
+
variables
.
get
(
"appSeq"
);
List
<
Map
<
String
,
Object
>>
maps
=
DatabaseUtils
.
getMaps
(
sql
,
connection
);
String
appKey
=
""
;
String
insertSql2
=
""
;
if
(
maps
!=
null
||
maps
.
size
()!=
0
){
for
(
Map
<
String
,
Object
>
map
:
maps
)
{
// 遍历每一个Map
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
())
{
String
key
=
entry
.
getKey
();
if
(
key
.
equals
(
appKey
)){
appKey
=(
String
)
map
.
get
(
key
);
}
}
}
connection
.
close
();
Connection
conn
=
this
.
getConnection
(
databaseConnectionService
.
queryBySeq
(
ipSeq
),
databaseName2
);
String
projectSql
=
"select * from privilege_permission where APP_CODE = '"
+
appKey
+
"'"
;
List
<
Map
<
String
,
Object
>>
projectMaps
=
DatabaseUtils
.
getMaps
(
projectSql
,
conn
);
if
(
projectMaps
!=
null
||
projectMaps
.
size
()!=
0
){
List
<
TableColumn
>
tableColumn
=
DatabaseUtils
.
getTableColumn
(
"privilege_permission"
,
conn
);
insertSql2
=
DatabaseUtils
.
getInsertSQL
(
projectMaps
,
tableColumn
,
"privilege_permission"
).
toString
();
}
conn
.
close
();
}
if
(
insertSql2
.
length
()
>
0
)
{
print
(
variables
.
get
(
dimension
).
toString
(),
"开始创建insert语句..."
,
"running"
);
writer2
.
write
(
BR
+
DELIMITER
+
BR
);
writer2
.
write
(
"/**"
+
BR
+
"* 资源数据"
+
BR
+
"**/"
+
BR
);
writer2
.
write
(
BR
+
DELIMITER
+
BR
);
writer2
.
write
(
insertSql2
);
writer2
.
write
(
BR
+
BR
+
DELIMITER
+
BR
);
print
(
variables
.
get
(
dimension
).
toString
(),
"insert语句创建完成..."
,
"running"
);
}
if
(
deleteSql
.
length
()
>
0
)
{
print
(
variables
.
get
(
dimension
).
toString
(),
"开始创建delete语句..."
,
"running"
);
writer
.
write
(
BR
+
DELIMITER
+
BR
);
...
...
@@ -160,15 +226,24 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
writer
.
write
(
BR
+
BR
+
DELIMITER
+
BR
);
print
(
variables
.
get
(
dimension
).
toString
(),
"insert语句创建完成..."
,
"running"
);
}
writer2
.
flush
();
writer
.
flush
();
writer
.
close
();
writer2
.
close
();
out
.
close
();
out2
.
close
();
if
(
deleteSql
.
length
()
==
0
&&
insertSql
.
length
()
==
0
)
{
print
(
variables
.
get
(
dimension
).
toString
(),
"该项目下数据路径字段为空,不支持导出..."
,
"start"
);
return
;
}
if
(
insertSql2
.
length
()>
0
){
downloadFilesAsZip
(
new
String
[]{
sqlFilePath
,
sqlFilePath2
},
"output.zip"
,
response
);
}
else
{
download
(
response
,
fileName
,
sqlFilePath
);
}
delExistFile
(
sqlFilePath
,
sqlFilePath2
);
print
(
variables
.
get
(
dimension
).
toString
(),
"执行结束"
,
"end"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
...
...
@@ -177,9 +252,101 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
}
}
private
void
delExistFile
(
String
sqlFilePath
,
String
sqlFilePath2
){
File
delStudio
=
new
File
(
sqlFilePath
);
if
(
delStudio
.
exists
()){
delStudio
.
delete
();
}
File
delProject
=
new
File
(
sqlFilePath2
);
if
(
delProject
.
exists
()){
delProject
.
delete
();
}
}
public
void
downloadFilesAsZip
(
String
[]
filePaths
,
String
zipFileName
,
HttpServletResponse
response
)
{
try
{
// 创建一个临时文件输出流,用于写入压缩文件
FileOutputStream
fos
=
new
FileOutputStream
(
zipFileName
);
ZipOutputStream
zos
=
new
ZipOutputStream
(
fos
);
// 循环遍历要添加到压缩文件中的文件
for
(
String
filePath
:
filePaths
)
{
File
file
=
new
File
(
filePath
);
if
(
file
.
exists
())
{
// 添加文件到压缩文件
ZipEntry
entry
=
new
ZipEntry
(
file
.
getName
());
zos
.
putNextEntry
(
entry
);
FileInputStream
fis
=
new
FileInputStream
(
file
);
byte
[]
buffer
=
new
byte
[
1024
];
int
len
;
while
((
len
=
fis
.
read
(
buffer
))
>
0
)
{
zos
.
write
(
buffer
,
0
,
len
);
}
fis
.
close
();
}
else
{
System
.
out
.
println
(
"File not found: "
+
filePath
);
}
}
zos
.
closeEntry
();
// 关闭压缩文件条目
zos
.
close
();
// 关闭压缩输出流
fos
.
close
();
// 关闭文件输出流
// 将压缩文件发送到浏览器进行下载
sendFileToBrowser
(
zipFileName
,
response
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
void
sendFileToBrowser
(
String
fileName
,
HttpServletResponse
response
)
throws
IOException
{
String
fileExtension
=
""
;
// 如果需要设置特定文件类型,请在此处添加,例如 ".zip"
fileName
=
fileName
+
fileExtension
;
// 例如 "example.zip"
File
downloadFile
=
new
File
(
fileName
);
// 获取要下载的文件路径和名称
response
.
setContentType
(
"application/octet-stream"
);
// 设置响应内容类型为二进制流,适用于下载文件
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename=\""
+
downloadFile
.
getName
()
+
"\""
);
// 设置响应头,指定要下载的文件名
OutputStream
outputStream
=
response
.
getOutputStream
();
// 获取输出流,用于将文件写入响应中
FileInputStream
inputStream
=
new
FileInputStream
(
downloadFile
);
// 打开文件输入流以读取要下载的文件内容
byte
[]
buffer
=
new
byte
[
4096
];
// 创建一个缓冲区来存储从文件中读取的数据块,可以根据需要调整大小
int
bytesRead
;
// 用于存储从文件中读取的字节数
while
((
bytesRead
=
inputStream
.
read
(
buffer
))
!=
-
1
)
{
// 将数据块写入响应输出流中,直到整个文件被读取完为止
outputStream
.
write
(
buffer
,
0
,
bytesRead
);
// 将数据块写入响应输出流中,直到整个文件被读取完为止
}
outputStream
.
flush
();
// 刷新输出流,确保所有数据都被写入响应中,并关闭输出流和输入流。
outputStream
.
close
();
// 关闭输出流和输入流。这将释放与它们关联的所有系统资源。请注意,关闭流是一个好习惯,因为未关闭的流可能导致资源泄漏和其他问题。如果您在使用完流之后不立即关闭它们,您可能会在代码中留下潜在的错误或问题。因此,最好在完成使用流之后立即关闭它们。
}
/**
* 导出机构
* 连接数据库
* @param model DataBaseLinkModel
* @return 数据库连接
*/
private
static
Connection
getConnection
(
DatabaseConnectionModel
model
,
String
databaseName
)
{
try
{
// 加载数据库驱动
Class
.
forName
(
"com.mysql.cj.jdbc.Driver"
);
if
(
model
.
getPrefix
()
==
null
||
model
.
getPrefix
().
isEmpty
()
||
model
.
getPrefix
().
equals
(
"null"
))
{
model
.
setPrefix
(
""
);
}
if
(
model
.
getSuffix
()
==
null
||
model
.
getSuffix
().
isEmpty
()
||
model
.
getSuffix
().
equals
(
"null"
))
{
model
.
setSuffix
(
""
);
}
// 连接新增的数据库
String
dbUrl
=
"jdbc:mysql://"
+
model
.
getIp
()
+
":"
+
model
.
getPort
()
+
"/"
+
model
.
getPrefix
()
+
databaseName
+
model
.
getSuffix
();
return
DriverManager
.
getConnection
(
dbUrl
,
model
.
getUserName
(),
AESUtils
.
decrypt
(
model
.
getPassWord
()));
}
catch
(
ClassNotFoundException
e
)
{
// 处理数据库驱动加载异常
e
.
printStackTrace
();
}
catch
(
SQLException
e
)
{
// 处理数据库连接异常
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
void
generateSQLByDatabaseName
(
String
resourceCode
,
String
dimension
,
String
dbSeqs
,
Long
ipSeq
,
Map
<
String
,
Object
>
variables
,
HttpServletResponse
response
)
{
try
{
DatabaseConnectionModel
dataBaseConnectionModel
=
databaseConnectionService
.
queryBySeq
(
ipSeq
);
...
...
@@ -303,6 +470,7 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
String
json
=
format
(
resource
.
getResourceInsertSql
(),
variables
);
JSONObject
dimensionData
=
JSONObject
.
parseObject
(
json
);
JSONObject
selectAllSql
=
dimensionData
.
getJSONObject
(
dimension
);
DatabaseConnectionModel
dataBaseConnectionModel
=
databaseConnectionService
.
queryBySeq
(
ipSeq
);
for
(
String
tableName
:
selectAllSql
.
keySet
())
{
String
selectsql
=
format
(
selectAllSql
.
getString
(
tableName
),
variables
);
...
...
@@ -315,6 +483,12 @@ public class StudioResourceService extends BaseService<StudioResourceModel, Stud
if
(
ObjectUtils
.
isEmpty
(
selectAllSql
.
keySet
()))
{
return
""
;
}
if
(
tableName
.
equals
(
column1
)){
tableName
=
column1To
;
}
if
(
tableName
.
equals
(
column2
)){
tableName
=
column2To
;
}
List
<
TableColumn
>
columns
=
DatabaseUtils
.
getTableColumn
(
tableName
,
connection
);
List
<
Map
<
String
,
Object
>>
dataList
=
DatabaseUtils
.
getMaps
(
selectsql
,
connection
);
sbsql
.
append
(
DatabaseUtils
.
getInsertSQL
(
dataList
,
columns
,
tableName
));
...
...
src/main/java/com/yeejoin/amos/api/tool/utils/DatabaseUtils.java
View file @
2f4e99bd
...
...
@@ -85,9 +85,9 @@ public class DatabaseUtils {
model
.
setSuffix
(
""
);
}
// 连接新增的数据库
//
String dbUrl = "jdbc:mysql://" + model.getIp() + ":" + model.getPort() + "/" +
//
model.getPrefix() + model.getDbName() + model.getSuffix();
String
dbUrl
=
"jdbc:mysql://"
+
model
.
getIp
()
+
":"
+
model
.
getPort
()
+
"/"
+
model
.
getDbName
();
String
dbUrl
=
"jdbc:mysql://"
+
model
.
getIp
()
+
":"
+
model
.
getPort
()
+
"/"
+
model
.
getPrefix
()
+
model
.
getDbName
()
+
model
.
getSuffix
();
//
String dbUrl = "jdbc:mysql://" + model.getIp() + ":" + model.getPort() + "/" + model.getDbName();
// 解密pwd,并连接
return
DriverManager
.
getConnection
(
dbUrl
,
model
.
getUserName
(),
AESUtils
.
decrypt
(
model
.
getPassWord
()));
...
...
@@ -100,7 +100,6 @@ public class DatabaseUtils {
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
...
...
src/main/resources/application-qa.properties
View file @
2f4e99bd
...
...
@@ -13,6 +13,7 @@ spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime
=
1800000
spring.datasource.hikari.connection-timeout
=
30000
spring.datasource.hikari.connection-test-query
=
SELECT 1
# second-datasource amos_studio
spring.second-datasource.jdbc-url
=
jdbc:mysql://172.16.3.18:3306/amos_studio?allowMultiQueries=true
spring.second-datasource.username
=
root
...
...
@@ -28,6 +29,7 @@ spring.second-datasource.hikari.max-lifetime=1800000
spring.second-datasource.hikari.connection-timeout
=
30000
spring.second-datasource.hikari.connection-test-query
=
SELECT 1
#eureka
eureka.instance.hostname
=
172.16.3.18
eureka.client.serviceUrl.defaultZone
=
http://${eureka.instance.hostname}:10001/eureka/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment