Commit 0c75f483 authored by neo's avatar neo

[dev] delete project for generate db

parent ead3f334
......@@ -16,6 +16,7 @@ import pwc.taxtech.atms.dto.ProjectClientDto;
import pwc.taxtech.atms.dto.ServiceTypeDto;
import pwc.taxtech.atms.dto.taxadmin.AddProjectResult;
import pwc.taxtech.atms.service.ProjectService;
import pwc.taxtech.atms.service.impl.ProjectServiceImpl;
import java.util.List;
import java.util.Map;
......@@ -23,15 +24,10 @@ import java.util.Map;
@RestController
@RequestMapping("/api/v1/project")
public class ProjectController {
/**
* @see PwC.Tax.Tech.Atms.WebApi\Controllers\ProjectController.cs
*/
private static Logger logger = LoggerFactory.getLogger(ProjectController.class);
@Autowired
private ProjectService projectService;
private ProjectServiceImpl projectService;
@ApiOperation(value = "listService", notes = "Get service list")
@RequestMapping(value = "listService", method = RequestMethod.GET)
......
......@@ -238,7 +238,6 @@ public class ProjectServiceImpl implements ProjectService {
Message.Project.AddProjectServiceType, pst.getId(), projectDto.getName(), LOG_TYPE);
});
project.setDbName(generateDatabase(project, projectDto.getEnterpriseAccountSetId()));
project.setUpdateTime(new Date());
projectMapper.insert(project);
operationLogService.addDataAddLog(project, OperationModule.Project, userName, Message.Project.AddProject,
......@@ -272,172 +271,6 @@ public class ProjectServiceImpl implements ProjectService {
return result;
}
static class DbName {
String dbName;
public DbName(String dbName) {
this.dbName = dbName;
}
}
private String generateDatabase(Project project, String accsetid) {//TODO: should be impl future(neo)
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(maxDbName, project.getYear() + "");
}
DbName sbDbName = new DbName(dbName);
createDatabaseByName(sbDbName, project);
ShardingContextHolder.setDataSourceKey(sbDbName.dbName);
try (SqlSession sqlSession = dynamicSqlSessionTemplate.getSqlSessionFactory().openSession();
BufferedReader bufferedReader = new BufferedReader(prepareRunScriptReader(DB_MYSQL_PATH, sbDbName.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);
}
initStandardAccount(project.getIndustryId());
return sbDbName.dbName;
}
private void initStandardAccount(String industryId) {
StandardAccountExample example = new StandardAccountExample();
example.createCriteria().andIsActiveEqualTo(ACTIVE).andIndustryIdIn(Lists.newArrayList(industryId, DEFAULT_INDUSTRY_Id));
List<StandardAccount> standardAccountList = standardAccountMapper.selectByExample(example);
List<VatStandardAccount> prepareAccount = new ArrayList<>();
if (industryId.equalsIgnoreCase(DEFAULT_INDUSTRY_Id)) {
prepareAccount = extractFrom(standardAccountList);
} else {
final List<StandardAccount> groupDefault = new ArrayList<>();
final List<StandardAccount> groupInput = new ArrayList<>();
standardAccountList.forEach(m -> {
if (m.getIndustryId().equalsIgnoreCase(industryId)) groupInput.add(m);
else groupDefault.add(m);
});
if (groupInput.isEmpty()) {
if (!groupDefault.isEmpty())
prepareAccount = extractFrom(groupDefault);
} else {
prepareAccount = extractFrom(groupInput);
}
}
prepareAccount.forEach(m -> vatStandardAccountMapper.insert(m));
}
private List<VatStandardAccount> extractFrom(List<StandardAccount> fromSource) {
List<VatStandardAccount> resut = new ArrayList<>(fromSource.size());
fromSource.forEach(m -> {
VatStandardAccount dest = new VatStandardAccount();
try {
FieldsMapper.map(m, dest);
} catch (Exception e) {
LOGGER.warn("some filed can not mapped", e);
}
resut.add(dest);
});
return resut;
}
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_OrganizationId", project.getOrganizationId());
keyValues.put("DBKeyword_EnterpriseAccountSetId", accsetid);
keyValues.put("DBKeyword_AdminDbName", adminDbName);
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 void createDatabaseByName(DbName dbName, Project project) {
int result = databaseAgent.createDatabase(dbName.dbName);
if (result == Constant.CREATE_DB_EXISTS) {
dbName.dbName = generateProjectDbName(dbName.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;
if (post >= 100000) {
Integer pre = Integer.parseInt(String.valueOf(maxName.charAt(0)));
pre2 = ((char) (pre + 1)) + "";
post2 = "00000";
} else if (post >= 10000) {
post2 = post + "";
} else if (post >= 1000) {
post2 = "0" + post;
} else if (post >= 100) {
post2 = "00" + post;
} else if (post >= 10) {
post2 = "000" + post;
} else {
post2 = "0000" + post;
}
return pre2 + year + post2;
}
private List<ProjectDisplayDto> getProjectFromEnterpriseAccountSetOrg(List<ProjectDisplayDto> pList, String orgId,
String serviceId, Integer projectYear) {
......
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