xinyb
4 天以前 73c8452a479ea83d4e9d28a7c66c019d9955d194
提交 | 用户 | age
95fe95 1 package com.yc.crm.mail.service;
X 2
3 import com.yc.action.grid.GridUtils;
4 import com.yc.crm.mail.entity.MailFileEntity;
5 import com.yc.entity.DataSourceEntity;
6 import com.yc.entity.attachment.AttachmentEntity;
7 import com.yc.multiData.MultiDataSource;
8 import com.yc.sdk.shopping.entity.ImageEntity;
9 import com.yc.sdk.shopping.entity.ShoppingImageEntity;
10 import com.yc.sdk.shopping.util.BlobToFile;
11 import com.yc.sdk.shopping.util.SettingKey;
12 import com.yc.service.BaseService;
13 import com.yc.utils.ImageUtils;
14 import org.apache.commons.lang3.StringUtils;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.dao.DataAccessException;
17 import org.springframework.dao.EmptyResultDataAccessException;
18 import org.springframework.jdbc.LobRetrievalFailureException;
19 import org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor;
20 import org.springframework.jdbc.support.lob.DefaultLobHandler;
21 import org.springframework.stereotype.Service;
22 import org.springframework.transaction.annotation.Transactional;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpSession;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37
38 /**
39  * @BelongsProject: eCoWorksV3
40  * @BelongsPackage: com.yc.crm.mail.service
41  * @author: xinyb
42  * @CreateTime: 2024-09-19  10:19
43  * @Description:邮件附件或图片处理
44  */
45 @Service("MailFileImpl")
46 public class MailFileImpl extends BaseService implements MailFileIfc {
67a32b 47     private final ShoppingImageEntity shoppingImage = new ShoppingImageEntity();
95fe95 48     @Autowired
X 49     private DefaultLobHandler defaultLobHandler;
67a32b 50
95fe95 51     @Override
X 52     public void saveAttachment(List<MailFileEntity> mailFile) {
53         try {
54
55         } catch (Exception e) {
56             throw e;
57         }
58     }
59
60
61     @Override
62     public List<AttachmentEntity> getAttachmentEntityList(String unIdSeq) {
63         String unId = null;
64         if (StringUtils.isBlank(unIdSeq)) {
65             return null;
66         }
67         String[] array = unIdSeq.split(";");
68         unId = array[0];//在有值时候第一个必定是unId
69         ArrayList<String> fieldId = new ArrayList<>(Arrays.asList(array));
70         fieldId.remove(0);//去掉第一个元素(UNID)
71         String sql = " set nocount on \n"
72                 + " declare @unid varchar(50) = " + GridUtils.prossSqlParm(unId) +
73                 ",@fieldid varchar(1000) = " + GridUtils.prossSqlParm(StringUtils.join(fieldId, ",")) + " \n";
74
75         sql += " select UNID,seq,DocCode,RowId,FieldId,FormId, \n"
76                 + " PhysicalPath,PhysicalFile,OriginalFileName,FileSize,FileType, \n"
77                 + " AuthorCode,AuthorName,SmallPicPath,UploadTime,OriginalPicture \n"
78                 + " from _sys_Attachment \n"
79                 + " where unid = @unid ";
80         if (fieldId.size() > 0) {
81             if (isNumeric(fieldId.get(0))) {
82                 sql += " and seq in(select list from GetInStr(@fieldid)) \n";
83             } else {
84                 sql += " and fieldid in(select list from GetInStr(@fieldid)) \n";
85             }
86         }
87         List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
88         List<AttachmentEntity> attachmentList = new ArrayList<AttachmentEntity>();
89         for (int i = 0; list != null && i < list.size(); i++) {
90             AttachmentEntity attachment = new AttachmentEntity();
91             attachment.setUnid(unId);
56d9f6 92             attachment.setFileType(list.get(i).get("FileType") == null ? null : (String) list.get(i).get("FileType"));
X 93             attachment.setFileSize(list.get(i).get("FileSize") == null ? 0 : (Long) list.get(i).get("FileSize"));
67a32b 94             attachment.setSeq(list.get(i).get("seq") == null ? 0 : (Integer) list.get(i).get("seq"));
95fe95 95             attachment.setPhysicalFile(list.get(i).get("PhysicalFile") == null ? null : (String) list.get(i).get("PhysicalFile"));
X 96             attachment.setOriginalFileName(list.get(i).get("OriginalFileName") == null ? null : (String) list.get(i).get("OriginalFileName"));
97             attachment.setOriginalPicture(list.get(i).get("OriginalPicture") == null ? null : (byte[]) list.get(i).get("OriginalPicture"));  //附件处理
98             attachmentList.add(attachment);
99         }
100         return attachmentList;
101     }
102
103
104     @Transactional(rollbackFor = Exception.class)
105     @Override
106     public Integer deleteAttachment(List<String> unId) {
107         try {
108             if (unId == null || unId.size() == 0) {
109                 return 0;
110             }
111             String sql = " set nocount on \n"
112                     + " declare @unid varchar(4000) = '" + StringUtils.join(unId, ",") + "'  \n";
113             sql += "delete _sys_Attachment where unid in (select list from GetInStr(@unid)) \n";
114             sql += "select @@ROWCOUNT";
115             return jdbcTemplate.queryForObject(sql, Integer.class);
116         } catch (Exception e) {
117             throw e;
118         }
119     }
120
121     @Override
122     public ImageEntity getImageFile(String unid, Integer width, Integer height, boolean isShowOrgImage, HttpServletRequest request, String attachmentPath, String formidPath) throws Exception {
123         HttpSession session = request.getSession();
124         if (unid == null || "".equals(unid)) {
125             // this.print(response, "获取图片时,必须传递参数 " + SettingKey.UNID);
126             return null;
127         }
128         if (isShowOrgImage) {
129             width = null;
130             height = null;
131         }
67a32b 132         ShoppingImageEntity shoppingImageEntity = null;
95fe95 133         DataSourceEntity dataSourceEntity = null;
X 134         try {
67a32b 135             dataSourceEntity = MultiDataSource.getDataSourceMap(request);
95fe95 136             if (dataSourceEntity == null) {
67a32b 137                 throw new Exception("没有找到数据源,获取图片文件失败!");
95fe95 138             }
X 139
140             String rootPath = session.getServletContext().getRealPath("/");
141             //String uuid = UUID.randomUUID().toString().toUpperCase();
142
67a32b 143             shoppingImageEntity = getImage(unid, rootPath, dataSourceEntity.getDbId(), attachmentPath, formidPath);
95fe95 144
X 145             if (shoppingImageEntity == null || shoppingImageEntity.getImage() == null) {
146                 return null;
147             }
148
149             //取原图路径
67a32b 150             String shoppingImageFileName = SettingKey.getShoppingImageFileName(rootPath, unid, width, height, isShowOrgImage, dataSourceEntity.getDbId() + "", shoppingImageEntity.getFileType(), attachmentPath, formidPath);
95fe95 151             File file = shoppingImageEntity.getImage();
X 152
153             if (file != null) {
154
67a32b 155                 if (width != null && height != null && shoppingImageEntity.getFileType() != null
95fe95 156                         //图片文件才缩放,否则会报错
67a32b 157                         && StringUtils.containsAny(shoppingImageEntity.getFileType().toLowerCase(), "jpg", "png", "gif", "jpeg", "tiff", "bmp", "raw", "tga", "fpx", "webp")) {
95fe95 158                     // 缩放图片
X 159                     ImageUtils.scale(file.getPath(), shoppingImageFileName, width, height);
160                 } else {
161                     shoppingImageFileName = file.getPath(); // 不缩放
162                 }
163                 ImageEntity image = new ImageEntity();
164                 image.setFile(new File(shoppingImageFileName));
165                 image.setFileType(shoppingImageEntity.getFileType());
166                 image.setOriginalFileName(shoppingImageEntity.getOriginalFileName());
167                 return image;
168             }
169         } catch (IOException e) {
67a32b 170             System.out.println("输出缩略图时出错,出错文件名:" + (shoppingImageEntity != null ? shoppingImageEntity.getPhysicalFile() : "") + (dataSourceEntity != null ? "dbid:[" + dataSourceEntity.getDbId() + "],系统名称:[" + dataSourceEntity.getSystemDescribe() + "]" : ""));
95fe95 171             throw e;
67a32b 172         } catch (Exception e) {
X 173             throw e;
95fe95 174         } finally {
X 175             //if (width != null && height != null && file != null && file.exists())
176             //    file.delete();
177         }
178         return null;
179     }
180
181     private ShoppingImageEntity getImage(String unids, String rootPath, int dbId, String attachmentPath, String formidPath) {
67a32b 182         if (unids == null || "".equals(unids)) return null;
X 183         String unid = "", mailSeq = "";
184         String unidStr[] = unids.split(SettingKey.NAVSPLIT);   //
185         unid = unidStr[0];
186         if (unidStr.length > 1) {
187             String seqs[] = unidStr[1].split(";");
188             mailSeq = seqs[seqs.length - 1];   //只取最后一个 seq , 因为有时候传过来的值是:  7394;7382 ,如果不处理会导致语法错误
95fe95 189         }
X 190
191         String sql = " set nocount on ; \n"
192                 + " declare @myrowcount int,@myerror int \n"
67a32b 193                 + " declare @unid varchar(50) = " + GridUtils.prossSqlParm(unid) + " \n"
95fe95 194                 + " declare @FormId int,@DocCode varchar(50),@FieldId varchar(50),@RowId varchar(50) \n"
X 195                 + " declare @OriginalFileName varchar(200),@PhysicalFile varchar(50),@FileType varchar(50),@FileSize bigint \n"
196                 + " declare @AuthorCode varchar(50),@AuthorName varchar(50),@OriginalPicture varbinary(max) \n"
197
198                 + " select top 1 @FormId = a.FormId,@DocCode = a.DocCode,@FieldId = a.FieldId,@RowId = a.RowId, \n"
199                 + "    @OriginalFileName = a.OriginalFileName ,@PhysicalFile = a.PhysicalFile,@FileType = a.FileType,\n"
200                 + "    @FileSize = isnull(a.FileSize,0),@AuthorCode = a.AuthorCode,@AuthorName = a.AuthorName,\n"
201                 + "    @OriginalPicture = a.OriginalPicture \n"
67a32b 202                 + " from " + (formidPath != null && "9747".equals(formidPath) ? "_sys_AttachmentLog" : "_sys_Attachment9") + " a \n"
95fe95 203                 + " where a.UNID = @unid  \n"
67a32b 204                 + (mailSeq != null && !"".equals(mailSeq) ? " and a.fieldid = '" + mailSeq + "' \n" : "")
95fe95 205                 + " select @myrowcount  = @@rowcount,@myerror = @@error \n"
X 206
207                 + " if isnull(@myrowcount,0) = 0 \n"
208                 + " begin \n"
209                 + "    select top 1 @FormId = a.FormId,@DocCode = a.DocCode,@FieldId = a.FieldId,@RowId = a.RowId, \n"
210                 + "       @OriginalFileName = a.OriginalFileName ,@PhysicalFile = a.PhysicalFile,@FileType = a.FileType,\n"
211                 + "       @FileSize = isnull(a.FileSize,0),@AuthorCode = a.AuthorCode,@AuthorName = a.AuthorName,\n"
212                 + "       @OriginalPicture = a.OriginalPicture \n"
67a32b 213                 + "    from " + (formidPath != null && "9747".equals(formidPath) ? "_sys_AttachmentLog" : "_sys_Attachment") + " a \n"
95fe95 214                 + "    where a.UNID = @unid  \n"
67a32b 215                 + (mailSeq != null && !"".equals(mailSeq) ? "    and a.fieldid = '" + mailSeq + "' \n" : "")
95fe95 216                 + " end \n "
X 217                 + " select @FormId as FormId,@DocCode as DocCode,@FieldId as FieldId,@RowId as RowId, \n" +
218                 "       @OriginalFileName as OriginalFileName ,@PhysicalFile as PhysicalFile,@FileType as FileType,\n" +
219                 "       isnull(@FileSize,0) as FileSize,@AuthorCode as AuthorCode,@AuthorName as AuthorName,\n" +
220                 "       @OriginalPicture as OriginalPicture \n";
221         try {
67a32b 222             this.jdbcTemplate.query(sql, new AbstractLobStreamingResultSetExtractor() {
95fe95 223                 protected void handleNoRowFound() throws LobRetrievalFailureException {
X 224                     System.out.println("在数据库id为:" + dbId + "中,导购管理  ShoppingImageImpl 未找到 unid=" + unids + " 的图片文件(或附件)!");
225                     shoppingImage.setFormId(-1);
226                     shoppingImage.setDocCode("");
227                     shoppingImage.setFieldId("");
228                     shoppingImage.setRowId("");
229
230                     shoppingImage.setOriginalFileName("");
231                     shoppingImage.setPhysicalFile(null);
232                     shoppingImage.setFileType("");
233                     shoppingImage.setFileSize(0L);
234                     shoppingImage.setAuthorCode("");
235                     shoppingImage.setAuthorName("");
67a32b 236                     shoppingImage.setImage(null);
95fe95 237                 }
X 238
239                 @Override
240                 protected void streamData(ResultSet rs) throws SQLException, IOException, DataAccessException {
67a32b 241                     shoppingImage.setFormId(rs.getString("FormId") == null ? 0 : rs.getInt("FormId"));
X 242                     shoppingImage.setDocCode(rs.getString("DocCode") == null ? "" : rs.getString("DocCode"));
243                     shoppingImage.setFieldId(rs.getString("FieldId") == null ? "" : rs.getString("FieldId"));
244                     shoppingImage.setRowId(rs.getString("RowId") == null ? "" : rs.getString("RowId"));
95fe95 245
67a32b 246                     shoppingImage.setOriginalFileName(rs.getString("OriginalFileName") == null ? null : rs.getString("OriginalFileName"));
X 247                     shoppingImage.setPhysicalFile(rs.getString("PhysicalFile") == null ? null : rs.getString("PhysicalFile"));
248                     shoppingImage.setFileType(rs.getString("FileType") == null ? null : rs.getString("FileType"));
95fe95 249                     shoppingImage.setFileSize(rs.getLong("FileSize"));
67a32b 250                     shoppingImage.setAuthorCode(rs.getString("AuthorCode") == null ? null : rs.getString("AuthorName"));
X 251                     shoppingImage.setAuthorName(rs.getString("AuthorName") == null ? null : rs.getString("AuthorName"));
95fe95 252
X 253
254                     //取原图路径
67a32b 255                     String shoppingImageFileName = SettingKey.getShoppingImageFileName(rootPath, unids, null, null, true, dbId + "", shoppingImage.getFileType(), attachmentPath, formidPath);
X 256                     InputStream is = null;
257                     byte[] bytes = null;
95fe95 258                     try {
X 259                         is = defaultLobHandler.getBlobAsBinaryStream(rs, "OriginalPicture");
260                         if (is != null) {
67a32b 261                             bytes = ShoppingImageEntity.InputStreamToByte(is); //取二进制图片
95fe95 262
67a32b 263                             File file = BlobToFile.writeBytesToFile(bytes, shoppingImageFileName);  //
95fe95 264                             if (file != null) {
X 265                                 shoppingImage.setImage(file);
266                             }
267                         }
268                     } catch (IOException e) {
269                         e.printStackTrace();
67a32b 270                         throw e;
95fe95 271                     } finally {
67a32b 272                         bytes = null;
95fe95 273                         if (is != null) is.close();
X 274                     }
275
276                 }   // end function
277             });
278
67a32b 279             return shoppingImage;
95fe95 280
67a32b 281         } catch (DataAccessException e) {
X 282             if (e instanceof EmptyResultDataAccessException) {
283                 return null;
284             } else {
285                 System.out.println("dbid:" + dbId + ",error:" + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
95fe95 286                 e.printStackTrace();
X 287                 throw e;
288             }
67a32b 289         } catch (Exception e) {
X 290             System.out.println("dbid:" + dbId + ",error:" + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
95fe95 291             e.printStackTrace();
X 292             throw e;
293         }
294
295     }
67a32b 296
95fe95 297     /**
X 298      * 判断是否为数字
299      *
300      * @param str
301      * @return
302      */
303     private static boolean isNumeric(String str) {
304         Pattern pattern = Pattern.compile("\\d+");
305         Matcher matcher = pattern.matcher(str);
306         return matcher.matches();
307     }
308 }