ServiceImplTemplate.ftl
8.33 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package ${packageInfo}.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.taover.base.ResultInfo;
import ${poPackageInfo}.${beanClassName}Dao;
import ${poPackageInfo}.${beanClassName}PO;
import ${packageInfo}.${beanClassName}Service;
import ${utilPackageInfo}.ObjectUtil;
@Service
public class ${beanClassName}ServiceImpl implements ${beanClassName}Service{
@Resource
private ${beanClassName}Dao ${beanFieldName}Dao;
@Override
public ResultInfo add(${beanClassName}PO ${beanFieldName}PO){
//创建返回对象
ResultInfo result = new ResultInfo();
//调用Service方法
${beanClassName}PO addResult = this.add${beanClassName}PO(${beanFieldName}PO);
if(addResult == null){
result.setResultId("failure");
result.setResultMessage("添加失败!");
//添加失败,将提交的对象信息放入 data.node
result.addData("node", ${beanFieldName}PO);
}else{
result.setResultId("success");
result.setResultMessage("添加成功!");
//添加成功,将添加成功的对象信息放入 data.node
result.addData("node", addResult);
}
return result;
}
@Override
public ResultInfo delete(Integer id){
//创建返回对象
ResultInfo result = new ResultInfo();
//参数检验
if(id==null || id<0){
result.setResultId("failure");
result.setResultMessage("删除失败,缺少主键信息或者主键错误!");
//添加失败,将提交的对象信息放入 data.node
result.addData("node", id);
return result;
}
//调用Service方法
boolean deleteResult = this.delete${beanClassName}PO(id);
if(!deleteResult){
result.setResultId("failure");
result.setResultMessage("删除失败!");
//添加失败,将id信息放入 data.node
result.addData("node", id);
}else{
result.setResultId("success");
result.setResultMessage("删除成功!");
//添加成功,将id信息放入 data.node
result.addData("node", id);
}
return result;
}
@Override
public ResultInfo update(${beanClassName}PO ${beanFieldName}PO){
//创建返回对象
ResultInfo result = new ResultInfo();
//参数检验
if(${beanFieldName}PO==null || ${beanFieldName}PO.getId()==null){
result.setResultId("failure");
result.setResultMessage("更新失败,缺少主键信息!");
//添加失败,将提交的对象信息放入 data.node
result.addData("node", ${beanFieldName}PO);
return result;
}
//调用Service方法
${beanClassName}PO updateResult = this.update${beanClassName}PO(${beanFieldName}PO.getId(), ${beanFieldName}PO);
if(updateResult == null){
result.setResultId("failure");
result.setResultMessage("更新失败!");
//添加失败,将提交的对象信息放入 data.node
result.addData("node", ${beanFieldName}PO);
}else{
result.setResultId("success");
result.setResultMessage("更新成功!");
//添加成功,将添加成功的对象信息放入 data.node
result.addData("node", updateResult);
}
return result;
}
@Override
public ${beanClassName}PO add${beanClassName}PO(${beanClassName}PO ${beanFieldName}PO) {
//参数检验
if(${beanFieldName}PO == null){
return null;
}
${beanFieldName}PO.setId(null);
//调用DAO方法添加实体
int result = this.${beanFieldName}Dao.add${beanClassName}PO(${beanFieldName}PO);
//依据SQL执行结果返回数据
if(result > 0){
${beanFieldName}PO.setId(result);
return ${beanFieldName}PO;
}else{
return null;
}
}
@Override
public boolean delete${beanClassName}PO(Integer id) {
//参数检验
if(id==null || id<=0){
return false;
}
//执行DAO删除方法
int result = this.${beanFieldName}Dao.delete${beanClassName}POByID(id);
//依据SQL执行结果返回数据
if(result==0 || result==1){
return true;
}else{
return false;
}
}
@Override
public ${beanClassName}PO update${beanClassName}PO(Integer id, ${beanClassName}PO ${beanFieldName}PO) {
//参数检验
if(${beanFieldName}PO==null || id==null){
return null;
}
//获取数据库中的对应记录
${beanClassName}PO dest = this.${beanFieldName}Dao.findPOByID(id);
if(dest == null){
return null;
}
//将erpShopListPO中不为null的字段拷贝到dest中
ObjectUtil.fieldCopy(${beanFieldName}PO, dest, false, new String[]{"id"}, false);
//执行DAO操作方法
int result = this.${beanFieldName}Dao.update${beanClassName}PO(dest);
//依据SQL执行结果返回数据
if(result==1){
return dest;
}else{
return null;
}
}
@Override
public Map<String, Object> query${beanClassName}POList(List<Object[]> arrayCondition, String sort, String order, int page, int pageSize) {
//参数检验
if(page==0 || pageSize<=0){
return new HashMap<String, Object>();
}
//参数解析
if(arrayCondition==null || arrayCondition.size()==0){
arrayCondition = new ArrayList<Object[]>();
arrayCondition.add(new Object[]{"1=1", null});
}
//构造排序内容
String sortCondition = " ";
if(!StringUtils.isEmpty(sort)){
sortCondition = " order by ";
String[] sortArr = sort.split(",");
if(StringUtils.isEmpty(order)){
sortCondition += sortArr[0]+" DESC ";
for(int i=1; i<sortArr.length; ++i){
sortCondition += ","+sortArr[i]+" DESC ";
}
}else{
String[] orderArr = order.split(",");
sortCondition += sortArr[0]+" "+orderArr[0]+" ";
for(int i=1; i<sortArr.length; ++i){
if(i < orderArr.length){
sortCondition = ","+sortArr[i]+" "+orderArr[i]+" ";
}else{
sortCondition = ","+sortArr[i]+" DESC ";
}
}
}
}
//调用DAO查询
Map<String, Object> result = new HashMap<String, Object>();
if(page == -1){
List<${beanClassName}PO> ${beanFieldName}POList = this.${beanFieldName}Dao.findListByCondition(arrayCondition, sortCondition, false);
result.put("page", 1);
result.put("total", ${beanFieldName}POList.size());
result.put("rows", ${beanFieldName}POList);
}else{
result = this.${beanFieldName}Dao.findPageByCondition(arrayCondition, sortCondition, page, pageSize, false);
}
return result;
}
@Override
public Map<String, Object> query${beanClassName}POList(String sqlCondition, String sort, String order, int page, int pageSize) {
//参数检验
if(page==0 || pageSize<=0){
return new HashMap<String, Object>();
}
//参数解析
if(sqlCondition==null || sqlCondition.length()==0){
sqlCondition = " 1=1 ";
}
//构造排序内容
String sortCondition = ErpSQLUtil.getSortCondition(sort, order, null);
sqlCondition += sortCondition;
//调用DAO查询
Map<String, Object> result = new HashMap<String, Object>();
if(page == -1){
List<${beanClassName}PO> ${beanFieldName}POList = this.${beanFieldName}Dao.findListBySql(sqlCondition);
result.put("page", 1);
result.put("total", ${beanFieldName}POList.size());
result.put("rows", ${beanFieldName}POList);
}else{
result = this.${beanFieldName}Dao.findPageBySql(sqlCondition, page, pageSize);
}
return result;
}
@Override
public ${beanClassName}PO query${beanClassName}PO(Integer id) {
//参数检验
if(id == null){
return null;
}
//调用DAO方法查询
return this.${beanFieldName}Dao.findPOByID(id);
}
@Override
public ${beanClassName}PO query${beanClassName}PO(String sqlCondition) {
//参数检验
if(sqlCondition==null || sqlCondition.length()==0){
return null;
}
//调用DAO方法
List<${beanClassName}PO> resultList = this.${beanFieldName}Dao.findListBySql(sqlCondition);
if(resultList==null || resultList.size()==0){
return null;
}else{
return resultList.get(0);
}
}
@Override
public ${beanClassName}PO query${beanClassName}PO(List<Object[]> arrayCondition) {
//参数检验
if(arrayCondition==null || arrayCondition.size()==0){
return null;
}
//调用DAO方法
List<${beanClassName}PO> resultList = this.${beanFieldName}Dao.findListByCondition(arrayCondition);
if(resultList==null || resultList.size()==0){
return null;
}else{
return resultList.get(0);
}
}
}