Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
T
traffic-front
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangxiaoming
traffic-front
Commits
329be79c
Commit
329be79c
authored
Jun 13, 2018
by
frank.xa.zhang
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/dev' into dev
parents
b5dd8e9c
e0f53b11
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
176 additions
and
7 deletions
+176
-7
pom.xml
atms-api/pom.xml
+1
-0
DatabaseAgent.java
...i/src/main/java/pwc/taxtech/atms/agent/DatabaseAgent.java
+42
-0
Constant.java
...api/src/main/java/pwc/taxtech/atms/constant/Constant.java
+11
-0
ProjectMapper.java
...api/src/main/java/pwc/taxtech/atms/dao/ProjectMapper.java
+4
-0
ProjectInfoServiceImpl.java
...pwc/taxtech/atms/service/impl/ProjectInfoServiceImpl.java
+0
-2
ProjectServiceImpl.java
...ava/pwc/taxtech/atms/service/impl/ProjectServiceImpl.java
+118
-5
CreateProjectDB.sql
...main/resources/RuntimeDbScripts/MSSql/CreateProjectDB.sql
+0
-0
CreateProjectDB.sql
...main/resources/RuntimeDbScripts/MYSQL/CreateProjectDB.sql
+0
-0
No files found.
atms-api/pom.xml
View file @
329be79c
...
...
@@ -366,6 +366,7 @@
<includes>
<include>
**/*.properties
</include>
<include>
**/*.xml
</include>
<include>
**/*.sql
</include>
</includes>
<filtering>
true
</filtering>
<!-- replace variable attribute or not -->
</resource>
...
...
atms-api/src/main/java/pwc/taxtech/atms/agent/DatabaseAgent.java
0 → 100644
View file @
329be79c
package
pwc
.
taxtech
.
atms
.
agent
;
import
com.mysql.jdbc.MysqlErrorNumbers
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Service
;
import
java.sql.SQLException
;
import
static
pwc
.
taxtech
.
atms
.
constant
.
Constant
.
CREATE_DB_EXISTS
;
import
static
pwc
.
taxtech
.
atms
.
constant
.
Constant
.
CREATE_DB_FAILED
;
import
static
pwc
.
taxtech
.
atms
.
constant
.
Constant
.
CREATE_DB_SUCCESS
;
@Service
public
class
DatabaseAgent
{
private
static
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
DatabaseAgent
.
class
);
@Autowired
private
JdbcTemplate
jdbcTemplate
;
public
int
createDatabase
(
String
dbName
)
{
try
{
jdbcTemplate
.
execute
(
"CREATE DATABASE "
+
dbName
+
" default charset utf8 COLLATE utf8_general_ci"
);
return
CREATE_DB_SUCCESS
;
}
catch
(
Exception
exception
)
{
if
(
exception
.
getCause
()
instanceof
SQLException
)
{
SQLException
ex
=
(
SQLException
)
exception
.
getCause
();
if
(
ex
.
getErrorCode
()
==
MysqlErrorNumbers
.
ER_DB_CREATE_EXISTS
)
{
LOGGER
.
warn
(
"db is exists {}"
,
dbName
);
return
CREATE_DB_EXISTS
;
}
}
LOGGER
.
warn
(
"Unknown Exception {}"
,
exception
.
getMessage
(),
exception
);
return
CREATE_DB_FAILED
;
}
}
}
atms-api/src/main/java/pwc/taxtech/atms/constant/Constant.java
View file @
329be79c
package
pwc
.
taxtech
.
atms
.
constant
;
import
java.io.File
;
public
final
class
Constant
{
public
static
final
String
Comma
=
","
;
public
static
final
String
Other
=
"其他"
;
public
static
final
int
WholeYear
=
-
1
;
public
static
final
int
CREATE_DB_SUCCESS
=
1
;
public
static
final
int
CREATE_DB_EXISTS
=
0
;
public
static
final
int
CREATE_DB_FAILED
=
-
1
;
public
static
final
String
DB_SCRIPT_FOLDER
=
"RuntimeDbScripts"
;
public
static
final
String
DB_SCRIPT_SQL
=
"CreateProjectDB.sql"
;
public
static
final
String
DB_MYSQL_TYPE
=
"MYSQL"
;
public
static
final
String
DB_MYSQL_PATH
=
DB_SCRIPT_FOLDER
+
File
.
separator
+
DB_MYSQL_TYPE
+
File
.
separator
+
DB_SCRIPT_SQL
;
}
atms-api/src/main/java/pwc/taxtech/atms/dao/ProjectMapper.java
View file @
329be79c
...
...
@@ -231,4 +231,7 @@ public interface ProjectMapper extends MyMapper {
" p.DbName = #{dbName} "
+
"ORDER BY PeriodId , Status"
)
List
<
ProjectStatusManage
>
selectProjectAllStatus
(
String
dbName
);
@Select
(
"SELECT max(DbName) as DbName FROM Project WHERE Year=#{year}"
)
String
maxDbName
(
Integer
year
);
}
\ No newline at end of file
atms-api/src/main/java/pwc/taxtech/atms/service/impl/ProjectInfoServiceImpl.java
View file @
329be79c
package
pwc
.
taxtech
.
atms
.
service
.
impl
;
import
org.apache.log4j.lf5.LogLevel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -11,7 +10,6 @@ import pwc.taxtech.atms.dto.taxadmin.PeriodInfoDto;
import
pwc.taxtech.atms.entitiy.PeriodInfo
;
import
pwc.taxtech.atms.entitiy.PeriodInfoExample
;
import
pwc.taxtech.atms.service.ProjectInfoService
;
import
sun.rmi.runtime.Log
;
import
java.util.List
;
...
...
atms-api/src/main/java/pwc/taxtech/atms/service/impl/ProjectServiceImpl.java
View file @
329be79c
...
...
@@ -2,13 +2,21 @@ package pwc.taxtech.atms.service.impl;
import
com.beust.jcommander.internal.Lists
;
import
com.google.common.base.Predicate
;
import
org.apache.ibatis.io.Resources
;
import
org.apache.ibatis.jdbc.ScriptRunner
;
import
org.apache.ibatis.session.SqlSession
;
import
org.mybatis.spring.SqlSessionTemplate
;
import
org.reflections.util.Utils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
pwc.taxtech.atms.agent.DatabaseAgent
;
import
pwc.taxtech.atms.common.CommonUtils
;
import
pwc.taxtech.atms.common.OperateLogType
;
import
pwc.taxtech.atms.common.OperationModule
;
import
pwc.taxtech.atms.common.datasource.ShardingContextHolder
;
import
pwc.taxtech.atms.constant.Constant
;
import
pwc.taxtech.atms.dao.OrganizationServiceTemplateGroupMapper
;
import
pwc.taxtech.atms.dao.ProjectClientMapper
;
import
pwc.taxtech.atms.dao.ProjectMapper
;
...
...
@@ -27,15 +35,27 @@ import pwc.taxtech.atms.entitiy.ProjectClientExample.Criteria;
import
pwc.taxtech.atms.service.OperationLogService
;
import
pwc.taxtech.atms.service.ProjectService
;
import
java.io.BufferedReader
;
import
java.io.BufferedWriter
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.Reader
;
import
java.io.StringReader
;
import
java.io.StringWriter
;
import
java.nio.charset.Charset
;
import
java.util.ArrayList
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
import
static
pwc
.
taxtech
.
atms
.
constant
.
Constant
.*;
@Service
public
class
ProjectServiceImpl
implements
ProjectService
{
/**
...
...
@@ -65,6 +85,12 @@ public class ProjectServiceImpl implements ProjectService {
@Autowired
private
OperationLogService
operationLogService
;
@Autowired
private
DatabaseAgent
databaseAgent
;
@Autowired
private
SqlSessionTemplate
dynamicSqlSessionTemplate
;
private
final
OperateLogType
LOG_TYPE
=
OperateLogType
.
OperationLogProject
;
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ProjectServiceImpl
.
class
);
...
...
@@ -125,9 +151,11 @@ public class ProjectServiceImpl implements ProjectService {
List
<
ProjectDisplayDto
>
data
=
projectMapper
.
getProjectList
(
orgID
,
serviceID
,
projectYear
);
//获取每个项目的项目状态
data
.
forEach
(
p
->
{
List
<
ProjectStatusManage
>
manageStatus
=
projectMapper
.
getProjectSatusListByDbName
(
p
.
getDbName
());
Map
<
Integer
,
Integer
>
dic
=
new
HashMap
<>();
manageStatus
.
forEach
(
m
->{
dic
.
put
(
m
.
getPeriodId
(),
m
.
getStatus
());});
List
<
ProjectStatusManage
>
manageStatus
=
projectMapper
.
getProjectSatusListByDbName
(
p
.
getDbName
());
Map
<
Integer
,
Integer
>
dic
=
new
HashMap
<>();
manageStatus
.
forEach
(
m
->
{
dic
.
put
(
m
.
getPeriodId
(),
m
.
getStatus
());
});
p
.
setProjectStatusList
(
dic
);
});
...
...
@@ -238,10 +266,95 @@ public class ProjectServiceImpl implements ProjectService {
}
private
String
generateDatabase
(
Project
project
,
String
accsetid
)
{
//TODO: should be impl future(neo)
return
"db_test"
;
String
maxDbName
=
projectMapper
.
maxDbName
(
project
.
getYear
());
String
dbName
=
""
;
if
(!
Utils
.
isEmpty
(
maxDbName
))
{
dbName
=
String
.
format
(
"%s%s%s"
,
"a"
,
(
project
.
getYear
()
+
""
).
substring
(
2
),
"00000"
);
}
else
{
dbName
=
generateProjectDbName
(
dbName
,
project
.
getYear
()
+
""
);
}
createDatabaseByName
(
dbName
,
project
);
ShardingContextHolder
.
setDataSourceKey
(
dbName
);
try
(
SqlSession
sqlSession
=
dynamicSqlSessionTemplate
.
getSqlSessionFactory
().
openSession
();
BufferedReader
bufferedReader
=
new
BufferedReader
(
prepareRunScriptReader
(
DB_MYSQL_PATH
,
dbName
,
project
,
accsetid
));)
{
ScriptRunner
scriptRunner
=
new
ScriptRunner
(
sqlSession
.
getConnection
());
//only support mysql
scriptRunner
.
setAutoCommit
(
true
);
scriptRunner
.
runScript
(
bufferedReader
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
LOGGER
.
error
(
"generate data base error"
,
e
);
}
return
dbName
;
}
private
Reader
prepareRunScriptReader
(
String
sqlPath
,
String
dbName
,
Project
project
,
String
accsetid
)
{
Map
<
String
,
String
>
keyValues
=
new
HashMap
<>();
keyValues
.
put
(
"DBKeyword_ProjectDbName"
,
dbName
);
keyValues
.
put
(
"DBKeyword_IndustryId"
,
project
.
getIndustryID
());
keyValues
.
put
(
"DBKeyword_OrganizationID"
,
project
.
getOrganizationID
());
keyValues
.
put
(
"DBKeyword_EnterpriseAccountSetID"
,
accsetid
);
BufferedReader
bufferedReader
=
null
;
String
line
=
null
;
StringWriter
sw
=
new
StringWriter
();
BufferedWriter
writer
=
new
BufferedWriter
(
sw
);
String
resultContent
=
null
;
try
{
Resources
.
setCharset
(
Charset
.
forName
(
"UTF-8"
));
bufferedReader
=
new
BufferedReader
(
Resources
.
getResourceAsReader
(
sqlPath
));
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
Iterator
<
Map
.
Entry
<
String
,
String
>>
kv
=
keyValues
.
entrySet
().
iterator
();
while
(
kv
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
entry
=
kv
.
next
();
line
=
line
.
replaceAll
(
entry
.
getKey
(),
entry
.
getValue
());
}
writer
.
write
(
line
);
writer
.
newLine
();
}
writer
.
flush
();
resultContent
=
sw
.
getBuffer
().
toString
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
LOGGER
.
warn
(
"prepare run reader some error"
,
e
);
}
finally
{
try
{
if
(
writer
!=
null
)
writer
.
close
();
if
(
sw
!=
null
)
sw
.
close
();
if
(
bufferedReader
!=
null
)
bufferedReader
.
close
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"io resource close some error"
,
e
);
}
}
return
new
StringReader
(
resultContent
);
}
private
String
GenerateProjectDbName
(
String
maxName
,
String
year
)
{
private
void
createDatabaseByName
(
String
dbName
,
Project
project
)
{
int
result
=
databaseAgent
.
createDatabase
(
dbName
);
if
(
result
==
Constant
.
CREATE_DB_EXISTS
)
{
dbName
=
generateProjectDbName
(
dbName
,
project
.
getYear
()
+
""
);
createDatabaseByName
(
dbName
,
project
);
}
else
if
(
result
==
Constant
.
CREATE_DB_FAILED
)
{
LOGGER
.
debug
(
"should throws biz exception"
);
//TODO: should shrow biz exception in futrue(neo)
}
}
private
String
generateProjectDbName
(
String
maxName
,
String
year
)
{
year
=
year
.
substring
(
2
);
String
pre2
=
maxName
.
charAt
(
0
)
+
""
,
post2
=
""
;
int
post
=
Integer
.
parseInt
(
maxName
.
substring
(
3
))
+
1
;
...
...
atms-api/src/main/resources/RuntimeDbScripts/MSSql/CreateProjectDB.sql
0 → 100644
View file @
329be79c
This diff is collapsed.
Click to expand it.
atms-api/src/main/resources/RuntimeDbScripts/MYSQL/CreateProjectDB.sql
0 → 100644
View file @
329be79c
This diff is collapsed.
Click to expand it.
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