Commit 329be79c authored by frank.xa.zhang's avatar frank.xa.zhang

Merge remote-tracking branch 'origin/dev' into dev

parents b5dd8e9c e0f53b11
......@@ -366,6 +366,7 @@
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.sql</include>
</includes>
<filtering>true</filtering><!-- replace variable attribute or not -->
</resource>
......
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;
}
}
}
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;
}
......@@ -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
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;
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment