1 package gboat2.attachment.service.impl;
2
3 import gboat2.base.core.dao.QuerySupport;
4 import gboat2.base.core.service.BaseService;
5 import gboat2.attachment.business.IAttachConfigBusiness;
6 import gboat2.attachment.model.AttachConfig;
7 import gboat2.attachment.model.Attachment;
8 import gboat2.attachment.model.AttachmentOperEnum;
9 import gboat2.attachment.model.AttachmentVO;
10 import gboat2.attachment.model.DownloadRecord;
11 import gboat2.attachment.service.IAttachmentService;
12 import gboat2.attachment.util.ConfigUtil;
13 import gboat2.attachment.util.MD5Util;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.io.FileUtils;
25 import org.apache.commons.lang3.StringUtils;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.transaction.annotation.Transactional;
28
29
30
31
32
33
34
35
36 @Transactional
37 public class AttachmentServiceImpl extends BaseService implements IAttachmentService {
38
39 @Autowired
40 private IAttachConfigBusiness attachConfigBusiness;
41
42 @SuppressWarnings("deprecation")
43 @Override
44 public String[][] getAvailableAttachTypeByCode(String attachCode) {
45 return attachConfigBusiness.getAvailableAttachTypeByCode(attachCode);
46 }
47
48 @Override
49 public AttachConfig getAttachConfigBy(String attachCode, String attachType) {
50 return attachConfigBusiness.getAttachConfigBy(attachCode, attachType);
51 }
52
53 @Override
54 public String getAttachConfigIdBy(String attachCode, String attachType) {
55 return attachConfigBusiness.getAttachConfigIdBy(attachCode, attachType);
56 }
57
58 public void deleteAttachments(String[] attachIds) {
59 if (null == attachIds) {
60 return;
61 }
62 for (String s : attachIds) {
63 String fileid = s.trim();
64 Attachment attach = (Attachment) this.get(Attachment.class, fileid);
65 if (null != attach) {
66
67
68
69 this.delete(attach);
70 this.deleteRealFile(attach.getSavePath());
71 }
72 }
73 }
74
75 private void deleteRealFile(String path) {
76 if (StringUtils.isNotBlank(path)) {
77 File file = new File(ConfigUtil.getSavepathRoot() + path);
78 if (file.exists()) {
79 file.delete();
80 }
81 }
82 }
83
84 public void deleteAttachment(String attachId) {
85 Attachment attach = (Attachment) this.get(Attachment.class, attachId);
86 if (null != attach) {
87
88
89
90 this.delete(attach);
91 this.deleteRealFile(attach.getSavePath());
92 }
93 }
94
95
96
97
98
99 public String saveAttachment(Attachment attach) {
100 return baseDAO.save(attach);
101 }
102
103 @SuppressWarnings("unchecked")
104 public List<Attachment> getAttachmentsByBelongId(String belongId) {
105 HashMap<String, Object> params = new HashMap<String, Object>();
106 params.put(QuerySupport.PARAM_TABLENAME, Attachment.class);
107 params.put("_belongId", belongId);
108 params.put(QuerySupport.PARAM_ORDERBY, "uploadDate desc");
109 List<Attachment> list = (List<Attachment>) baseDAO.queryList(params);
110 return list;
111 }
112
113 @SuppressWarnings("unchecked")
114 public List<AttachmentVO> getAttachmentVOsByBelongId(String belongId) {
115 HashMap<String, Object> params = new HashMap<String, Object>();
116 params.put(QuerySupport.PARAM_TABLENAME, AttachmentVO.class);
117 params.put("_belongId", belongId);
118
119 params.put(QuerySupport.PARAM_ORDERBY, "attachOrder");
120
121 List<AttachmentVO> list = (List<AttachmentVO>) baseDAO.queryList(params);
122
123 for (Iterator<AttachmentVO> iterator = list.iterator(); iterator.hasNext();) {
124 AttachmentVO attachmentVO = (AttachmentVO) iterator.next();
125 if (StringUtils.isNotEmpty(attachmentVO.getConfigId())) {
126 attachmentVO.setAttachName(attachmentVO.getConfigAttachType());
127 }
128 }
129
130 return list;
131 }
132
133 @SuppressWarnings("unchecked")
134 @Override
135 public List<Attachment> getAttachmentsBy(String belongId, String[] attachNames) {
136 if (attachNames == null) {
137 return null;
138 }
139 Map<String, Object> params = new HashMap<String, Object>();
140 params.put(QuerySupport.PARAM_TABLENAME, Attachment.class);
141 params.put("_belongId", belongId);
142 params.put("_attachName_in", convertAttachNameArrayToString(attachNames));
143 List<Attachment> atts = (List<Attachment>) super.query(params);
144 return atts;
145 }
146
147 @Override
148 public List<Attachment> getAttachmentsByAttachIds(String attachIds) {
149 if (StringUtils.isEmpty(attachIds)) {
150 return Collections.emptyList();
151 }
152 Map<String, Object> params = new HashMap<String, Object>();
153 params.put(QuerySupport.PARAM_TABLENAME, Attachment.class);
154 params.put("_attachId_in", attachIds);
155 List<Attachment> atts = (List<Attachment>) super.query(params);
156 return atts;
157 }
158
159
160 private String convertAttachNameArrayToString(String[] attachNames) {
161 StringBuilder builder = new StringBuilder();
162 for (String attachName : attachNames) {
163 builder.append("'").append(attachName).append("',");
164 }
165 builder.deleteCharAt(builder.length() - 1);
166 return builder.toString();
167 }
168
169 @SuppressWarnings("unchecked")
170 @Override
171 public List<Attachment> getAttachmentsBy(String belongId, String attachType) {
172 if (StringUtils.isEmpty(belongId) || StringUtils.isEmpty(attachType)) {
173 return null;
174 }
175 Map<String, Object> params = new HashMap<String, Object>();
176 params.put(QuerySupport.PARAM_TABLENAME, Attachment.class);
177 params.put("_belongId", belongId);
178 params.put("_attachType", attachType);
179 List<Attachment> atts = (List<Attachment>) super.query(params);
180 return atts;
181 }
182
183
184
185
186 @SuppressWarnings("unchecked")
187 @Override
188 public List<AttachmentVO> getAttachmentVOBy(String belongId, String attachType) {
189 if (StringUtils.isEmpty(belongId) || StringUtils.isEmpty(attachType)) {
190 return null;
191 }
192 Map<String, Object> params = new HashMap<String, Object>();
193 params.put(QuerySupport.PARAM_TABLENAME, AttachmentVO.class);
194 params.put("_belongId", belongId);
195 params.put("_configAttachType", attachType);
196 List<AttachmentVO> atts = (List<AttachmentVO>) super.query(params);
197 return atts;
198 }
199
200
201
202
203 @SuppressWarnings("unchecked")
204 @Override
205 public List<Attachment> getAttachmentsByBelongIdAndConfigId(String belongId, String configId) {
206 if (StringUtils.isEmpty(belongId)) {
207 return null;
208 }
209 Map<String, Object> params = new HashMap<String, Object>();
210 params.put(QuerySupport.PARAM_TABLENAME, Attachment.class);
211 params.put("_belongId", belongId);
212 if (StringUtils.isEmpty(configId)) {
213 params.put("_configId_null", null);
214 } else {
215 params.put("_configId", configId);
216 }
217 List<Attachment> atts = (List<Attachment>) super.query(params);
218 return atts;
219 }
220
221 public void updateAttachsBelongId(String[] attachIds, String belongId) {
222 if (null == attachIds || attachIds.length <= 0) {
223 return;
224 }
225
226 StringBuilder builder = new StringBuilder();
227 for (String attachId : attachIds) {
228 builder.append("'").append(attachId).append("',");
229 }
230 builder.deleteCharAt(builder.length() - 1);
231
232 Map<String, Object> params = new HashMap<String, Object>();
233 params.put("belongId", belongId);
234 baseDAO.updateByQuery("update Attachment set belongId=:belongId where attachId in (" + builder.toString() + ")", params);
235 }
236
237 @SuppressWarnings("unchecked")
238 @Override
239 public DownloadRecord getDownLoad(String attachmentId) {
240 Map<String, Object> params = new HashMap<String, Object>();
241 params.put(QuerySupport.PARAM_TABLENAME, DownloadRecord.class);
242 params.put("_fileId", attachmentId);
243 List<DownloadRecord> recordList = (List<DownloadRecord>) super.query(params);
244 if (null != recordList && recordList.size() > 0) {
245 return recordList.get(0);
246 }
247 return null;
248 }
249
250
251
252
253
254
255 @SuppressWarnings("unchecked")
256 @Override
257 public void deleteAttachmentByBelongId(String belongId) {
258 if (StringUtils.isEmpty(belongId)) {
259 return;
260 }
261 Map<String, Object> params = new HashMap<String, Object>();
262 params.put(QuerySupport.PARAM_TABLENAME, Attachment.class);
263 params.put("_belongId", belongId);
264 List<Attachment> list = (List<Attachment>) baseDAO.queryList(params);
265 if (list != null) {
266 for (Attachment attachment : list) {
267 if (attachment == null) {
268 continue;
269 }
270 this.deleteAttachment(attachment.getAttachId());
271 }
272 }
273 }
274
275 @Override
276 public void virtualDelete(String attachId) {
277 Attachment attach = (Attachment) this.get(Attachment.class, attachId);
278 if (null != attach) {
279 this.delete(attach);
280 }
281
282 }
283
284
285
286
287 @Override
288 public Attachment generateAttachment(byte[] fileContent, String belongId, String attachName, String attachType, String configId,
289 AttachmentOperEnum attOperEnum) throws IOException {
290 String relativeDir = ConfigUtil.generateRelativeSaveDir();
291 String targetDir = ConfigUtil.getSavepathRoot() + relativeDir;
292 String targetFileName = ConfigUtil.genearteNewFilename(attachName);
293 String targetFullFileName = targetDir + targetFileName;
294
295 String savepath = relativeDir + targetFileName;
296 String downloadurl = ConfigUtil.getDownloadContextPath() + savepath;
297
298
299 File path = new File(targetDir);
300 if (!path.exists() && !path.mkdirs()) {
301 String msg = "文件夹创建失败!:" + targetDir;
302
303 throw new IOException(msg);
304 }
305
306 File file = null;
307 try {
308 file = new File(targetFullFileName);
309 FileUtils.writeByteArrayToFile(file, fileContent);
310 } catch (IOException e1) {
311 String msg = "服务器保存文件失败";
312
313 throw new IOException(msg, e1);
314 }
315
316 String md5 = "";
317 try {
318 md5 = MD5Util.getFileMD5String(file);
319 } catch (IOException e) {
320 String msg = "生产MD5校验串失败";
321 throw new IOException(msg, e);
322 }
323
324
325
326
327
328
329
330 Attachment att = null;
331 List<Attachment> atts = null;
332 if (null == attOperEnum) {
333 att = new Attachment();
334 } else if (attOperEnum.equals(AttachmentOperEnum.SAVE)) {
335 att = new Attachment();
336 } else if (attOperEnum.equals(AttachmentOperEnum.UPDATE)) {
337 atts = (List<Attachment>) this.getAttachmentsByBelongIdAndConfigId(belongId, configId);
338 if (atts != null && atts.size() > 0) {
339 att = atts.get(0);
340 } else {
341 att = new Attachment();
342 }
343 } else if (attOperEnum.equals(AttachmentOperEnum.SAVEORUPDATE)) {
344 atts = (List<Attachment>) this.getAttachmentsByBelongIdAndConfigId(belongId, configId);
345 if (atts != null && atts.size() > 0) {
346 att = atts.get(0);
347 } else {
348 att = new Attachment();
349 }
350 } else {
351 att = new Attachment();
352 }
353
354 att.setBelongId(belongId);
355 att.setAttachName(attachType);
356 att.setAttachType(attachType);
357 att.setConfigId(configId);
358 att.setDownloadUrl(downloadurl);
359 att.setOriginalName(attachName);
360 att.setSavePath(savepath);
361 att.setValidateCode(md5);
362 att.setUploadDate(new Date());
363 att.setSize(file.length());
364
365 if (att != null && StringUtils.isNotEmpty(att.getAttachId())) {
366
367 this.update(att);
368 } else {
369 this.saveAttachment(att);
370 }
371 return att;
372 }
373
374 @SuppressWarnings("unchecked")
375 public Boolean isDownLoadFile(String organId, String belongId, String attachType) {
376 Map<String, Object> params = new HashMap<String, Object>();
377
378 params.put(QuerySupport.PARAM_TABLENAME, DownloadRecord.class);
379 params.put("_organId", organId);
380 params.put("_belongId", belongId);
381 params.put("_attachType_like", attachType);
382
383 List<Attachment> list = (List<Attachment>) baseDAO.queryList(params);
384
385 return list.size() > 0 ? Boolean.TRUE : Boolean.FALSE;
386 }
387
388 @SuppressWarnings("unchecked")
389 public DownloadRecord getLastDownloadRecord(String organId, String belongId, String attachType) {
390 Map<String, Object> params = new HashMap<String, Object>();
391
392 params.put(QuerySupport.PARAM_TABLENAME, DownloadRecord.class);
393 params.put("_organId", organId);
394 params.put("_belongId", belongId);
395 params.put("_attachType_like", attachType);
396
397 params.put(QuerySupport.PARAM_ORDERBY, "downloadDate desc");
398
399 List<DownloadRecord> list = (List<DownloadRecord>) baseDAO.queryList(params);
400
401 if (list.size() > 0)
402 return list.get(0);
403
404 return null;
405 }
406
407 @Override
408 public List<AttachConfig> getAvailableAttachTypesByCode(String attachCode) {
409 return attachConfigBusiness.getAvailableAttachTypesByCode(attachCode);
410 }
411
412 @Override
413 public Attachment getAttachment(String attachId) {
414 return (Attachment) this.get(Attachment.class, attachId);
415 }
416 }