a337d206e9ab283dba7f0716081b06856316576c.svn-base
3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.thinkgem.jeesite.modules.gen.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.gen.dao.GenSchemeDao;
import com.thinkgem.jeesite.modules.gen.dao.GenTableColumnDao;
import com.thinkgem.jeesite.modules.gen.dao.GenTableDao;
import com.thinkgem.jeesite.modules.gen.entity.GenConfig;
import com.thinkgem.jeesite.modules.gen.entity.GenScheme;
import com.thinkgem.jeesite.modules.gen.entity.GenTable;
import com.thinkgem.jeesite.modules.gen.entity.GenTableColumn;
import com.thinkgem.jeesite.modules.gen.entity.GenTemplate;
import com.thinkgem.jeesite.modules.gen.util.GenUtils;
/**
* 生成方案Service
* @author ThinkGem
* @version 2013-10-15
*/
@Service
@Transactional(readOnly = true)
public class GenSchemeService extends BaseService {
@Autowired
private GenSchemeDao genSchemeDao;
// @Autowired
// private GenTemplateDao genTemplateDao;
@Autowired
private GenTableDao genTableDao;
@Autowired
private GenTableColumnDao genTableColumnDao;
public GenScheme get(String id) {
return genSchemeDao.get(id);
}
public Page<GenScheme> find(Page<GenScheme> page, GenScheme genScheme) {
GenUtils.getTemplatePath();
genScheme.setPage(page);
page.setList(genSchemeDao.findList(genScheme));
return page;
}
@Transactional(readOnly = false)
public String save(GenScheme genScheme) {
if (StringUtils.isBlank(genScheme.getId())){
genScheme.preInsert();
genSchemeDao.insert(genScheme);
}else{
genScheme.preUpdate();
genSchemeDao.update(genScheme);
}
// 生成代码
if ("1".equals(genScheme.getFlag())){
return generateCode(genScheme);
}
return "";
}
@Transactional(readOnly = false)
public void delete(GenScheme genScheme) {
genSchemeDao.delete(genScheme);
}
private String generateCode(GenScheme genScheme){
StringBuilder result = new StringBuilder();
// 查询主表及字段列
GenTable genTable = genTableDao.get(genScheme.getGenTable().getId());
genTable.setColumnList(genTableColumnDao.findList(new GenTableColumn(new GenTable(genTable.getId()))));
// 获取所有代码模板
GenConfig config = GenUtils.getConfig();
// 获取模板列表
List<GenTemplate> templateList = GenUtils.getTemplateList(config, genScheme.getCategory(), false);
List<GenTemplate> childTableTemplateList = GenUtils.getTemplateList(config, genScheme.getCategory(), true);
// 如果有子表模板,则需要获取子表列表
if (childTableTemplateList.size() > 0){
GenTable parentTable = new GenTable();
parentTable.setParentTable(genTable.getName());
genTable.setChildList(genTableDao.findList(parentTable));
}
// 生成子表模板代码
for (GenTable childTable : genTable.getChildList()){
childTable.setParent(genTable);
childTable.setColumnList(genTableColumnDao.findList(new GenTableColumn(new GenTable(childTable.getId()))));
genScheme.setGenTable(childTable);
Map<String, Object> childTableModel = GenUtils.getDataModel(genScheme);
for (GenTemplate tpl : childTableTemplateList){
result.append(GenUtils.generateToFile(tpl, childTableModel, genScheme.getReplaceFile()));
}
}
// 生成主表模板代码
genScheme.setGenTable(genTable);
Map<String, Object> model = GenUtils.getDataModel(genScheme);
for (GenTemplate tpl : templateList){
result.append(GenUtils.generateToFile(tpl, model, genScheme.getReplaceFile()));
}
return result.toString();
}
}