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
package com.yanzuoguang.service.Impl;
import com.yanzuoguang.dao.BaseDao;
import com.yanzuoguang.service.BaseService;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.ArrayList;
import java.util.List;
/**
* 基本服务实现
* created by yanzu on 2017/5/31.
*/
public abstract class BaseServiceImpl<T> implements BaseService<T> {
/**
* 数据类的类型
*/
protected Class<T> dataClass;
/**
* 标识
*/
protected String tag;
/**
* 构造函数
*
* @param cls
*/
public BaseServiceImpl(Class<T> cls, String tag) {
this.dataClass = cls;
this.tag = tag;
}
/**
* @param
* @return
* @description 获取Dao类来处理
*/
protected abstract BaseDao getDao();
/**
* 创建数据
*
* @param models 需要创建的数据
*/
public List<String> create(T... models) {
List<String> ids = new ArrayList<String>();
for (T t : models) {
String id = createItem(t);
ids.add(id);
}
return ids;
}
/**
* 创建一项
*
* @param item
* @return
*/
protected String createItem(T item) {
String id = this.getDao().create(item);
check("创建", this.tag, id);
return id;
}
/**
* 修改数据
*
* @param models 需要修改的数据
*/
public List<String> update(T... models) {
List<String> ids = new ArrayList<String>();
for (T t : models) {
String id = updateItem(t);
ids.add(id);
}
return ids;
}
/**
* 修改一项数据
*
* @param item
* @return
*/
protected String updateItem(T item) {
String id = this.getDao().update(item);
check("修改", this.tag, id);
return id;
}
/**
* 保存数据
*
* @param models 需要保存的数据
*/
public List<String> save(T... models) {
List<String> ids = new ArrayList<String>();
for (T item : models) {
String id = saveItem(item);
ids.add(id);
}
return ids;
}
/**
* 保存其中一项
*
* @param item
* @return
*/
protected String saveItem(T item) {
String id = this.getDao().save(item);
check("保存", this.tag, id);
return id;
}
/**
* 删除数据
*
* @param models 需要删除的数据
*/
public int remove(T... models) {
int ret = 0;
for (T item : models) {
ret += removeItem(item);
}
return ret;
}
/**
* 删除其中一项
*
* @param item
* @return
*/
protected int removeItem(T item) {
int id = this.getDao().remove(item);
check("删除", this.tag, id);
return id;
}
/**
* 加载数据
*
* @param models 需要删除的数据
*/
public List<T> load(T... models) {
List<T> tos = new ArrayList<T>();
for (T item : models) {
T to = loadItem(item);
tos.add(to);
}
return tos;
}
/**
* 加载单个数据
*
* @param item
* @return
*/
protected T loadItem(T item) {
T to = this.getDao().load(item, dataClass);
check("加载", this.tag, to);
return to;
}
/**
* 检测编号
*
* @param opTag 操作标识
* @param serviceTag 服务标识
* @param id ID编号
*/
private void check(String opTag, String serviceTag, Object id) {
if (StringHelper.isEmpty(id)) {
throw new CodeException(opTag + serviceTag + "失败,该数据可能已被修改");
}
}
}