From ee2316e2cb7afde1c54f5e4216a1b0d1e055749e Mon Sep 17 00:00:00 2001 From: xinyb <574600396@qq.com> Date: 星期五, 27 九月 2024 17:33:14 +0800 Subject: [PATCH] CRM邮箱配置调整 --- src/com/yc/crm/mail/service/MailImpl.java | 781 +++++++++++++++++++++++++++++-------------------------- 1 files changed, 410 insertions(+), 371 deletions(-) diff --git a/src/com/yc/crm/mail/service/MailImpl.java b/src/com/yc/crm/mail/service/MailImpl.java index 84dba8f..48a224d 100644 --- a/src/com/yc/crm/mail/service/MailImpl.java +++ b/src/com/yc/crm/mail/service/MailImpl.java @@ -1,34 +1,23 @@ package com.yc.crm.mail.service; -import com.sun.mail.imap.IMAPStore; import com.yc.action.grid.GridUtils; -import com.yc.crm.mail.entity.T482102Entity; -import com.yc.crm.mail.entity.t482101HEntity; +import com.yc.crm.base.entity.T482115Entity; +import com.yc.crm.mail.action.MailFolder; +import com.yc.crm.mail.action.MailTag; +import com.yc.crm.mail.entity.*; import com.yc.entity.AttachmentConfig; -import com.yc.entity.attachment.AttachmentEntity; -import com.yc.entity.attachment.AttachmentWhereEntity; -import com.yc.factory.FactoryBean; import com.yc.sdk.shopping.service.imagedata.ShoppingImageDataIfc; +import com.yc.sdk.weixincp.util.UploadFiles; import com.yc.service.BaseService; -import com.yc.service.upload.AttachmentIfc; -import com.yc.utils.SessionKey; -import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; -import javax.mail.*; -import javax.mail.internet.*; -import javax.mail.search.FlagTerm; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; -import java.util.regex.Pattern; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; /** * @BelongsProject: eCoWorksV3 @@ -39,35 +28,45 @@ */ @Service public class MailImpl extends BaseService implements MailIfc { - @Autowired MailAccountIfc emailAccountIfc; @Autowired ShoppingImageDataIfc imgData; + @Autowired + MailTagIfc mailTagIfc; + @Autowired + MailFolderIfc mailFolderIfc; final static String shoppingImageServer = AttachmentConfig.get("attachment.server"); + @Transactional(rollbackFor = Exception.class) @Override public t482101HEntity saveReceivingMail(t482101HEntity m) { String sql = "set nocount on\n"; try { - if(StringUtils.isBlank(m.getDocCode())) { - sql += "declare @docCode varchar(50) \n"; + if (StringUtils.isBlank(m.getDocCode())) { + sql += "declare @docCode varchar(50) ,@unid varchar(50),@fieldId varchar(100),@originalFileName varchar(100)," + + "@physicalFile varchar(50),@OriginalPicture varbinary(max) \n"; sql += "exec sp_newdoccode 482101," + GridUtils.prossSqlParm(m.getUserCode()) + ",@docCode output \n"; - sql += "insert into t482101H(companyId,companyName,formId,docCode,docdate,PeriodID,postCode,postname,mailType,sender,receiver,cc,bcc,subject,userCode,userName,content,file_unid) values "; + sql += "insert into t482101H(companyId,companyName,formId,docCode,docdate,PeriodID,postCode,postname,mailType,senderName,sender,sender_time,receiver,receiving_time," + + "cc,bcc,subject,userCode,userName,content,plain_text,messageId,read_flag,delete_flag,create_time,update_time," +//receive_time + "attach_flag,attachment_list) values "; sql += "(" + GridUtils.prossSqlParm(m.getCompanyId()) + "," + GridUtils.prossSqlParm(m.getCompanyName()) + ",482101," + "@docCode,convert(varchar(10),getdate(),120),convert(varchar(7),getdate(),120)," + GridUtils.prossSqlParm(m.getUserCode()) + "," + GridUtils.prossSqlParm(m.getUserName()) + "," + - +m.getMailType() + "," + GridUtils.prossSqlParm(m.getSender()) + "," + - GridUtils.prossSqlParm(m.getReceiver()) + "," + GridUtils.prossSqlParm(m.getCc()) + "," + GridUtils.prossSqlParm(m.getBcc()) + "," + + +m.getMailType() + "," + GridUtils.prossSqlParm(m.getSenderName()) + "," + GridUtils.prossSqlParm(m.getSender()) + "," + GridUtils.prossSqlParm(m.getSenderTime()) + "," + + GridUtils.prossSqlParm(StringUtils.join(m.getReceiver(), ",")) + "," + GridUtils.prossSqlParm(m.getReceivingTime()) + "," + GridUtils.prossSqlParm(StringUtils.join(m.getCc(), ",")) + "," + + GridUtils.prossSqlParm(StringUtils.join(m.getBcc(), ",")) + "," + GridUtils.prossSqlParm(m.getSubject()) + "," + GridUtils.prossSqlParm(m.getUserCode()) + "," + GridUtils.prossSqlParm(m.getUserName()) + "," + - GridUtils.prossSqlParm(m.getContent()) + "," + GridUtils.prossSqlParm(m.getFileUNID()) + ") \n"; + GridUtils.prossSqlParm(m.getContent()) + "," + GridUtils.prossSqlParm(m.getPlainText()) + "," + GridUtils.prossSqlParm(m.getMessageId()) + "," + + m.getReadFlag() + "," + m.getDeleteFlag() + ",getdate(),getdate()," + m.getAttachFlag() + "," + GridUtils.prossSqlParm(m.getAttachmentList()) + ") \n" + + mailFileSql(m.getMailFile()); sql += "select @docCode as docCode"; String docCode = jdbcTemplate.queryForObject(sql, String.class); if (StringUtils.isNotBlank(docCode)) { m.setDocCode(docCode); } - }else{ + } else { updateReceivingMail(m);//淇敼 } return m; @@ -76,62 +75,137 @@ } } + @Transactional(rollbackFor = Exception.class) @Override - public void saveReceivingMailList(List<t482101HEntity> mail) { + public String saveReceivingMailList(List<t482101HEntity> mail) { String sql = "set nocount on\n"; try { - sql += "declare @docCode varchar(50) \n"; + sql += "declare @docCode varchar(50) ,@unid varchar(50),@fieldId varchar(100),@originalFileName varchar(100),@docCodeList varchar(3000)=''," + + "@physicalFile varchar(50),@OriginalPicture varbinary(max) \n"; for (t482101HEntity m : mail) {//閬嶅巻鍐呭 sql += "exec sp_newdoccode 482101," + GridUtils.prossSqlParm(m.getUserCode()) + ",@docCode output \n"; - sql += "insert into t482101H(companyId,companyName,formId,docCode,docdate,PeriodID,postCode,postname,mailType,sender,receiver,cc,bcc,subject,userCode,userName,content,file_unid) values "; + sql += " select @docCodeList=@docCodeList+@docCode+';'\n" +//杩斿洖鍗曞彿 + "insert into t482101H(companyId,companyName,formId,docCode,docdate,PeriodID,postCode,postname,mailType,senderName,sender,sender_time,receiver,receiving_time," + + "cc,bcc,subject,userCode,userName,content,plain_text,messageId,read_flag,delete_flag,create_time,update_time," + + "attach_flag,attachment_list) values "; sql += "(" + GridUtils.prossSqlParm(m.getCompanyId()) + "," + GridUtils.prossSqlParm(m.getCompanyName()) + ",482101," + "@docCode,convert(varchar(10),getdate(),120),convert(varchar(7),getdate(),120)," + GridUtils.prossSqlParm(m.getUserCode()) + "," + GridUtils.prossSqlParm(m.getUserName()) + "," + - +m.getMailType() + "," + GridUtils.prossSqlParm(m.getSender()) + "," + - GridUtils.prossSqlParm(m.getReceiver()) + "," + GridUtils.prossSqlParm(m.getCc()) + "," + GridUtils.prossSqlParm(m.getBcc()) + "," + + +m.getMailType() + "," + GridUtils.prossSqlParm(m.getSenderName()) + "," + GridUtils.prossSqlParm(m.getSender()) + "," + GridUtils.prossSqlParm(m.getSenderTime()) + "," + + GridUtils.prossSqlParm(StringUtils.join(m.getReceiver(), ",")) + "," + GridUtils.prossSqlParm(m.getReceivingTime()) + "," + GridUtils.prossSqlParm(StringUtils.join(m.getCc(), ",")) + "," + + GridUtils.prossSqlParm(StringUtils.join(m.getBcc(), ",")) + "," + GridUtils.prossSqlParm(m.getSubject()) + "," + GridUtils.prossSqlParm(m.getUserCode()) + "," + GridUtils.prossSqlParm(m.getUserName()) + "," + - GridUtils.prossSqlParm(m.getContent()) + "," + GridUtils.prossSqlParm(m.getFileUNID()) + ") \n"; -// sql +="exec postt482101V3 '' ,'',''"; + GridUtils.prossSqlParm(m.getContent()) + "," + GridUtils.prossSqlParm(m.getPlainText()) + "," + GridUtils.prossSqlParm(m.getMessageId()) + "," + + m.getReadFlag() + "," + m.getDeleteFlag() + ",getdate(),getdate()," + m.getAttachFlag() + "," + GridUtils.prossSqlParm(m.getAttachmentList()) + ") \n" + + mailFileSql(m.getMailFile()); } - jdbcTemplate.update(sql); + return jdbcTemplate.queryForObject(sql + "\n select @docCodeList", String.class); } catch (Exception e) { throw e; } } + /** + * 閭欢闄勪欢鐨勫鐞� + * + * @param mailFile + * @return + */ + private String mailFileSql(MailFileEntity mailFile) { + if (mailFile == null) {//闄勪欢淇濆瓨 + return ""; + } + String sql = ""; + String originalFileName = ""; + String fileType = ""; + try { + for (MailFileEntity.MailBodyPart file : mailFile.getPart()) { + originalFileName = file.getFileName(); + fileType = file.getFileType(); + sql += "select @unid=" + GridUtils.prossSqlParm(mailFile.getUnId()) + "\n" + + ",@fieldId=" + GridUtils.prossSqlParm(file.getFieldId()) + "\n" + + ",@originalFileName=" + GridUtils.prossSqlParm(originalFileName) + "\n" + + ",@physicalFile=" + GridUtils.prossSqlParm(file.getPhysicalFile()) + "\n" + + ",@OriginalPicture=" + UploadFiles.byteToHexString(file.getByteFile()) + "\n"; + sql += "insert into _sys_Attachment(unId,formId,docCode,fieldId,physicalFile,originalFileName,FileSize,fileType,UploadTime,LastUpdateTime,OriginalPicture) values "; + sql += " (@unid,482101,@docCode,@fieldId,@physicalFile,@originalFileName," + file.getFileSize() + "," + GridUtils.prossSqlParm(fileType) + ",getdate(),getdate(),@OriginalPicture) \n"; + } + return sql; + } catch (Exception e) { + return ""; + } + } + + @Transactional(rollbackFor = Exception.class) @Override public Integer updateReceivingMail(t482101HEntity mail) { String sql = "set nocount on\n"; try { - sql += "update t482101H set sender=" + GridUtils.prossSqlParm(mail.getSender()) + ",receiver=" + GridUtils.prossSqlParm(mail.getReceiver()) + "," + - "cc=" + GridUtils.prossSqlParm(mail.getCc()) + ",bcc=" + GridUtils.prossSqlParm(mail.getBcc()) + ",subject=" + GridUtils.prossSqlParm(mail.getSubject()) + "," + - "content=" + GridUtils.prossSqlParm(mail.getContent()) + ",file_unid=" + GridUtils.prossSqlParm(mail.getFileUNID()) + ",mailType=" + mail.getMailType() + "," + - "updateTime=getdate() \n"; - sql += " where docCode=" + GridUtils.prossSqlParm(mail.getDocCode()); - sql += "select select @@ROWCOUNT"; - return jdbcTemplate.update(sql, Integer.class); + sql += "update t482101H set sender=" + GridUtils.prossSqlParm(mail.getSender()) + ",receiver=" + GridUtils.prossSqlParm(StringUtils.join(mail.getReceiver(), ",")) + "," + + "cc=" + GridUtils.prossSqlParm(StringUtils.join(mail.getCc(), ",")) + ",bcc=" + GridUtils.prossSqlParm(StringUtils.join(mail.getBcc(), ",")) + ",subject=" + GridUtils.prossSqlParm(mail.getSubject()) + "," + + "content=" + GridUtils.prossSqlParm(mail.getContent()) + ",attachment_list=" + GridUtils.prossSqlParm(mail.getAttachmentList()) + ",mailType=" + mail.getMailType() + "," + + "update_time=getdate() \n"; + sql += " where docCode=" + GridUtils.prossSqlParm(mail.getDocCode()) + "\n"; + sql += "select @@ROWCOUNT"; + return jdbcTemplate.queryForObject(sql, Integer.class); + } catch (Exception e) { + throw e; + } + } + + @Transactional(rollbackFor = Exception.class) + @Override + public Integer updateDeleteFlag(String userCode, String docCode) { + String sql = "set nocount on\n"; + try { + sql += "declare @docCode varchar(200) ='" + docCode + "' \n"; + sql += "update t482101H deleteFlag=1 where userCode =" + GridUtils.prossSqlParm(userCode) + + " and docCode in (select list from GetInStr(@docCode))\n";//鏍囪鍒犻櫎 + sql += " select @@ROWCOUNT"; + return jdbcTemplate.queryForObject(sql, Integer.class); + } catch (Exception e) { + throw e; + } + } + + @Transactional(rollbackFor = Exception.class) + @Override + public Integer deleteEmail(String userCode, String docCode) { + String sql = "set nocount on\n"; + try { + sql += "declare @docCode varchar(200) ='" + docCode + "' \n"; + sql += "update t482101H deleteFlag=1 where userCode =" + GridUtils.prossSqlParm(userCode) + + " and docCode in (select list from GetInStr(@docCode))\n";//鏍囪鍒犻櫎 + sql += " select @@ROWCOUNT"; + return jdbcTemplate.queryForObject(sql, Integer.class); } catch (Exception e) { throw e; } } @Override - public Integer deleteReceivingMail(String docCode) { + public Integer deleteEmail(String messageId) { String sql = "set nocount on\n"; try { - sql += "update t482101H deleteFlag=1 where docCode=" + GridUtils.prossSqlParm(docCode) + "\n";//鏍囪涓哄凡鍒犻櫎 - sql += "select select @@ROWCOUNT"; - return jdbcTemplate.update(sql, Integer.class); + sql += "declare @messageId varchar(200) ='" + messageId + "' \n"; + sql += " delete t482101H where messageId in (select list from GetInStr(@messageId))\n";//鍒犻櫎 + sql += " select @@ROWCOUNT"; + return jdbcTemplate.queryForObject(sql, Integer.class); } catch (Exception e) { throw e; } } @Override - public t482101HEntity getReceivingMailInfo(String email, String docCode) { + public t482101HEntity getReceivingMailInfo(String docCode) { String sql = "set nocount on\n"; try { - sql += "select top 1 DocCode,sender,receiver,subject,content,file_unid as fileUNID from t482101H where receiver = '" + email + "' and docCode=" + GridUtils.prossSqlParm(docCode); + sql += "select top 1 DocCode,companyId,companyName,senderName,senderName,sender,receiver,mailType,subject,content,plain_text,file_unid as fileUNID,messageId," + + "read_flag,delete_flag,attach_flag,urgent_flag,receipt_flag,track_flag,userCode,userName,create_time,update_time," + + "send_status,receive_time,sign_id,attachment_list,remark,remark,sender_time,receiving_time," + + "(case when (isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate()) then 1 else 0 end) as handle,handle_time," + + "DocVersion from t482101H " + + "where docCode=" + GridUtils.prossSqlParm(docCode); return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t482101HEntity.class)).get(0); } catch (Exception e) { throw e; @@ -139,361 +213,326 @@ } @Override - public List<t482101HEntity> getReceivingMailList(String email, Integer mailType, boolean isNoRead) { + public List<t482101HList> getReceivingMailList(String email, Integer mailType, boolean isNoRead, String userCode, Integer page, Integer limit) { String sql = "set nocount on\n"; try { - sql += "select DocCode,sender,receiver,subject,content,file_unid as fileUNID " + - "from t482101H "; - if (isNoRead) {//鏈鎵ц - sql += "where receiver ='" + email + "' and isnull(readFlag,0)= 0"; - } else {//宸茶 - sql += "where receiver ='" + email + "' and mailType=" + mailType; + sql += "select companyId,companyName,mailType,DocCode,senderName,sender,receiver,subject,plain_text," + + "receive_time,update_time,create_time,senderName,sender,userCode,userName,folder_id,delete_Flag,attachment_list," + + "read_Flag,(case when (isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate()) then 1 else 0 end) as handle," + + "handle_time,sender_time,receiving_time from t482101H "; + sql += " where userCode=" + GridUtils.prossSqlParm(userCode); + sql += " and mailType=" + mailType;//0锛氳崏绋跨 1锛氭敹浠剁 2锛氬彂浠剁 + if (StringUtils.isNotBlank(email)) { + if (mailType == 1) { + sql += " and receiver like '%" + email + "%'"; + } else if (mailType == 2) { + sql += " and sender =" + GridUtils.prossSqlParm(email); + } } - return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t482101HEntity.class)); + if (isNoRead) {//0琛ㄧず鏈锛�1琛ㄧず宸茶 + sql += " and isnull(read_flag,0) = 0"; + } + sql += " order by docCode asc \n"; + sql += " OFFSET (" + page + " - 1) * " + limit + " ROWS FETCH NEXT " + limit + " ROWS ONLY"; + return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t482101HList.class)); } catch (Exception e) { throw e; - } - } - - - @Override - public List<t482101HEntity> getMailReceiving(T482102Entity emailEntity, HttpServletRequest request) throws MessagingException { - String imapServer = emailEntity.getReceiveHost();//"imap.qq.com"; - String user = emailEntity.getEmail();//"xxx@qq.com"; - String pwd = emailEntity.getPassword();//"xxxx"; - - HashMap IAM = new HashMap(); - //甯︿笂IMAP ID淇℃伅锛岀敱key鍜寁alue缁勬垚锛屼緥濡俷ame锛寁ersion锛寁endor锛宻upport-email绛夈�� - IAM.put("name", emailEntity.getAliasEmail()); - IAM.put("version", emailEntity.getDocVersion() + ""); - IAM.put("vendor", emailEntity.getCompanyName()); - IAM.put("support-email", emailEntity.getEmail()); - //鍒涘缓浼氳瘽 - Session session = Session.getInstance(new Properties(), new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(user, pwd); - } - }); - //瀛樺偍瀵硅薄 - IMAPStore store = (IMAPStore) session.getStore(emailEntity.getReceiveProtocol());//imap鍗忚鎴杙op3鍗忚绫诲瀷锛堟帹鑽愪綘浣跨敤IMAP鍗忚鏉ュ瓨鍙栨湇鍔″櫒涓婄殑閭欢銆傦級 - //杩炴帴 - store.connect(imapServer, user, pwd); - store.id(IAM);//163閭闇�瑕侊紝涓嶇劧浼氭姤锛欰3 NO SELECT Unsafe Login. Please contact kefu@188.com for help - Folder folder = null; - try { - // 鑾峰緱鏀朵欢绠� - folder = store.getFolder("INBOX"); - // 浠ヨ鍐欐ā寮忔墦寮�鏀朵欢绠� - folder.open(Folder.READ_WRITE); - //false 琛ㄧず鏈 - FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), true); - //鑾峰緱鏀朵欢绠辩殑閭欢鍒楄〃 - Message[] messages = folder.search(flagTerm); - //杩斿洖閭灏佽鍐呭 - return setMailContent(messages, request); - } catch (NoSuchProviderException e) { - throw e; - } catch (MessagingException e) { - throw e; - } catch (IOException e) { - throw new RuntimeException(e); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - try { - if (folder != null) { - folder.close(false); - } - if (store != null) { - store.close(); - } - } catch (MessagingException e) { - throw e; - } } } @Override - public void sendingMail(t482101HEntity t482101H, HttpServletRequest request) throws Exception { + public List<t482101HList> getHandleMailList(String email, String userCode, Integer page, Integer limit) { + String sql = "set nocount on\n"; try { - //鏍规嵁褰撳墠鐢ㄦ埛鏌ヨ缁戝畾鐨勯偖绠变俊鎭� - T482102Entity emailEntity = emailAccountIfc.getAccountInfo(t482101H.getUserCode(), t482101H.getSender());//杩斿洖閭鐨勮处鍙蜂俊鎭� - if (emailEntity == null || StringUtils.isBlank(emailEntity.getEmail())) { - throw new Exception("鎵句笉鍒伴偖绠憋細" + t482101H.getSender() + "閰嶇疆淇℃伅锛岃瀹屽杽锛�" + t482101H.getSender() + "閭閰嶇疆鍦ㄥ皾璇曞彂閫侀偖浠�"); - } - //閭鏈嶅姟鍣ㄩ厤缃� - Properties properties = new Properties(); - properties.setProperty("mail.smtp.host", emailEntity.getSmtpHost()); - properties.setProperty("mail.smtp.port", emailEntity.getSmtpPort() + ""); - properties.setProperty("mail.smtp.auth", "true"); - properties.setProperty("mail.smtp.starttls.enable", "true"); - // - String sendEmail = emailEntity.getEmail();//鍙戜欢浜� - String sendPassword = emailEntity.getPassword();//瀵嗙爜 - String recipientEmail = t482101H.getReceiver();//鏀朵欢浜� - Session session = Session.getInstance(properties, new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(sendEmail, sendPassword); - } - }); - - MimeMessage message = new MimeMessage(session); - message.setFrom(new InternetAddress(sendEmail)); - message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail)); - message.setSubject(t482101H.getSubject()); - - if (StringUtils.isNotBlank(t482101H.getCc())) {//鎶勯�� - String[] cc = t482101H.getCc().split(";"); - message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(StringUtils.join(cc, ","))); - } - if (StringUtils.isNotBlank(t482101H.getBcc())) {//瀵嗛�� - String[] bcc = t482101H.getBcc().split(";"); - message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(StringUtils.join(bcc, ","))); - } - - //鍒涘缓澶氶儴鍒嗘秷鎭� - Multipart multipart = new MimeMultipart(); - - MimeBodyPart textPart = new MimeBodyPart(); - textPart.setText(t482101H.getContent());//鍐呭 - multipart.addBodyPart(textPart); - - //涓嬮潰闄勪欢鍐呭 - if (StringUtils.isNotBlank(t482101H.getFileUNID())) {//鏈夐檮浠跺唴瀹� - String[] fileUnId = t482101H.getFileUNID().split(";");//绗竴涓繀瀹氭槸unId鍊� - AttachmentIfc attachmentIfc = (AttachmentIfc) FactoryBean.getBean("AttachmentImpl"); - //鑾峰彇鍒伴檮浠跺唴瀹� - List<AttachmentEntity> attachmentEntities = attachmentIfc.getAttachmentEntityList(fileUnId[0]); - if (attachmentEntities != null && attachmentEntities.size() > 0) { - for (AttachmentEntity a : attachmentEntities) { - if (Arrays.asList(fileUnId).contains(a.getSeq() + "")) {//鏄檮浠跺搴旂殑seq鎵嶅鐞� - InputStream inputStream = new ByteArrayInputStream(a.getOriginalPicture()); - File file = new File(a.getOriginalFileName()); - FileUtils.copyInputStreamToFile(inputStream, file); - MimeBodyPart bodyPart = new MimeBodyPart(); - bodyPart.attachFile(file); - bodyPart.setFileName(a.getOriginalFileName()); - multipart.addBodyPart(bodyPart); - } - } - } - } - message.setContent(multipart); - try { - Transport.send(message);//鍙戦�� - } catch (MessagingException m) {//寮傚父澶勭悊 - t482101H.setMailType(0);//鑽夌绠辩姸鎬� - updateMailDrafts(t482101H);//鏇存柊鎴愯崏绋跨 - throw new Exception("鍙戦�侀偖浠跺紓甯�,閭欢宸蹭繚瀛樺湪鑽夌绠便�傚紓甯稿師鍥狅細" + m.getCause() != null ? m.getCause().getMessage() : m.getMessage()); - } + sql += "select companyId,companyName,mailType,DocCode,senderName,sender,receiver,subject,plain_text," + + "receive_time,update_time,create_time,senderName,sender,userCode,userName,folder_id,delete_Flag, " + + "read_Flag,(case when (isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate()) then 1 else 0 end) as handle," + + "handle_time,sender_time,receiving_time from t482101H "; + sql += " where isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate()"; + sql += " and userCode=" + GridUtils.prossSqlParm(userCode) + " and receiver like '%" + email + "%'"; + sql += " order by docCode asc \n"; + sql += " OFFSET (" + page + " - 1) * " + limit + " ROWS FETCH NEXT " + limit + " ROWS ONLY"; + return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t482101HList.class)); } catch (Exception e) { throw e; } } + @Override + public List<t482101HList> getFolderMailList(String type, String userCode, Integer page, Integer limit) throws Exception { + String sql = "set nocount on\n"; + try { + String[] id = type.split("_"); + if (id.length != 2) { + throw new Exception("閭欢绫诲瀷Id鏍煎紡涓嶆纭�"); + } + sql += "select companyId,companyName,mailType,DocCode,senderName,sender,receiver,subject,plain_text," + + "receive_time,update_time,create_time,senderName,sender,userCode,userName,folder_id,delete_Flag, " + + "read_Flag,(case when (isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate()) then 1 else 0 end) as handle," + + "handle_time,sender_time,receiving_time from t482101H "; + sql += " where userCode=" + GridUtils.prossSqlParm(userCode) + " \n"; + if (id[0].equals("folder")) {//鏄枃浠跺す + sql += " and folder_id=" + id[1] + " \n"; + } else if (id[0].equals("tag")) {//鏄爣绛� + sql += " and tag_list like '%," + id[1] + ",%'"; + } + sql += " order by docCode asc \n"; + sql += " OFFSET (" + page + " - 1) * " + limit + " ROWS FETCH NEXT " + limit + " ROWS ONLY"; + return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t482101HList.class)); + } catch (Exception e) { + throw e; + } + } + + @Override + public List<t482101HList> getUserMailList(String userCode) { + String sql = " set nocount on \n"; + try { + sql = "select a.DocCode,isnull(a.mailType,0) as mailType,isnull(a.read_flag,0) as read_flag,a.sender,a.receiver, " + + "(case when (isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate()) then 1 else 0 end) as handle " + + " from t482101H a where a.userCode=" + GridUtils.prossSqlParm(userCode) + " order by a.mailType"; + return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t482101HList.class)); + } catch (Exception e) { + throw e; + } + } + + @Transactional(rollbackFor = Exception.class) @Override public Integer updateMailDrafts(t482101HEntity mail) { String sql = "set nocount on\n"; try { - sql += "update t482101H set sender=" + GridUtils.prossSqlParm(mail.getSender()) + ",receiver=" + GridUtils.prossSqlParm(mail.getReceiver()) + "," + - "cc=" + GridUtils.prossSqlParm(mail.getCc()) + ",bcc=" + GridUtils.prossSqlParm(mail.getBcc()) + ",subject=" + GridUtils.prossSqlParm(mail.getSubject()) + "," + - "content=" + GridUtils.prossSqlParm(mail.getContent()) + ",file_unid=" + GridUtils.prossSqlParm(mail.getFileUNID()) + ",mailType=" + mail.getMailType() + "," + - "updateTime=getdate() \n"; + sql += "update t482101H set sender=" + GridUtils.prossSqlParm(mail.getSender()) + ",receiver=" + GridUtils.prossSqlParm(StringUtils.join(mail.getReceiver(), ",")) + "," + + "cc=" + GridUtils.prossSqlParm(StringUtils.join(mail.getCc(), ",")) + ",bcc=" + GridUtils.prossSqlParm(StringUtils.join(mail.getBcc(), ",")) + "," + + "subject=" + GridUtils.prossSqlParm(mail.getSubject()) + ",messageId=" + GridUtils.prossSqlParm(mail.getMessageId()) + "," + + "content=" + GridUtils.prossSqlParm(mail.getContent()) + ",attachment_list=" + GridUtils.prossSqlParm(mail.getAttachmentList()) + ",mailType=" + mail.getMailType() + "," + + "update_time=getdate() \n"; sql += " where docCode=" + GridUtils.prossSqlParm(mail.getDocCode()); - sql += "select select @@ROWCOUNT"; - return jdbcTemplate.update(sql, Integer.class); + sql += " select @@ROWCOUNT"; + return jdbcTemplate.queryForObject(sql, Integer.class); } catch (Exception e) { throw e; } } @Override - public Integer deleteMailDrafts(String docCode) { + public List<String> getMessageIdList(String receiver) { + List<String> messageId = null; + String sql = " set nocount on \n"; + try { + sql += "select messageId from t482101H where receiver='" + receiver + "'"; + List<t482101HEntity> message = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(t482101HEntity.class)); + if (message != null && message.size() > 0) { + messageId = new ArrayList<>(); + messageId = message.stream().map(t482101HEntity::getMessageId).distinct().collect(Collectors.toList());//鍘婚噸 + } + return messageId; + } catch (Exception e) { + throw e; + } + } + + @Transactional(rollbackFor = Exception.class) + @Override + public void updateRead(String docCode, boolean status) { + String sql = " set nocount on \n"; + try { + sql += "declare @docCode varchar(200) ='" + docCode + "' \n"; + sql += "update t482101H set read_flag=" + (status ? 1 : 0) + " where docCode in (select list from GetInStr(@docCode))\n"; + sql += "select @@ROWCOUNT"; + jdbcTemplate.queryForObject(sql, Integer.class); + } catch (Exception e) { + throw e; + } + } + + @Override + public void updateMailType(String docCode, Integer folderId, String tagId) { + String sql = " set nocount on \n"; + try { + boolean bol = false; + sql += "declare @docCode varchar(200) ='" + docCode + "' \n"; + sql += "update t482101H set \n"; + if (!folderId.equals(0)) { + sql += " folder_id=" + folderId + "\n"; + bol = true; + } + if (StringUtils.isNotBlank(tagId)) { + sql += (bol ? "," : "") + " tag_list='," + tagId + ",' \n"; + } + sql += " where docCode in (select list from GetInStr(@docCode))\n"; + sql += "select @@ROWCOUNT"; + jdbcTemplate.queryForObject(sql, Integer.class); + } catch (Exception e) { + throw e; + } + } + + @Override + public List<MailModuleEntity> getMailModuleList(String userCode) { + List<MailModuleEntity> module = MailModuleEntity.setMailInitial(); + try { + List<T482102Entity> account = emailAccountIfc.getAccount(userCode);//鑾峰彇杩欎釜璐﹀彿缁戝畾鐨勯偖绠� + List<t482101HList> userMail = getUserMailList(userCode); + for (MailModuleEntity m : module) { + List<t482101HList> mailTypeList = null; + int mailType = m.getMailType();//绫诲瀷 + switch (mailType) { + case 0://鑽夌绠� + case 1://鏀朵欢绠� + case 2://鍙戜欢绠� + if (mailType == 0) { + m.setChildren(new ArrayList<>()); + if (userMail.size() > 0) { + m.setTotal((int) userMail.stream().filter(s -> s.getMailType().equals(mailType)).count()); + } + } else { + List<MailModuleEntity.MailCode> codeList = new ArrayList<>(); + MailModuleEntity.MailCode code = new MailModuleEntity.MailCode(); + code.setKey(mailType == 1 ? "InboxPage1" : "IndexPage1"); + code.setMailType(mailType); + code.setMailName(mailType == 1 ? "鍏ㄩ儴鏀朵欢" : "鍏ㄩ儴鍙戜欢"); + codeList.add(code); + if (account.size() == 0) {//娌℃湁鐩存帴璺宠繃 + m.setChildren(codeList); + continue; + } + int allCount = 0; + int count = 0; + for (T482102Entity a : account) { + MailModuleEntity.MailCode nextCode = new MailModuleEntity.MailCode(); + String eMail = (mailType == 1 ? a.getReceiveEmail() : a.getSmtpEmail());//鑾峰彇閭璐﹀彿 + nextCode.setKey(mailType == 1 ? "receiver" : "sender"); + nextCode.setMailName(eMail); + nextCode.setMailType(m.getMailType()); +// if (userMail.size() > 0) { +// count = (mailType == 1 ? (int) userMail.stream().filter(s -> s.getMailType().equals(mailType) && s.getReceiver().contains(eMail)).count() : +// (int) userMail.stream().filter(s -> s.getMailType().equals(mailType) && s.getSender().contains(eMail)).count()); +// nextCode.setTotal(count); +// } + if (mailType == 1) {//鏄敹浠剁锛岀粺璁℃湭璇绘暟閲� + count = (int) userMail.stream().filter(s -> s.getMailType().equals(mailType) && s.getReceiver().contains(eMail) && s.getReadFlag().equals(0)).count(); + nextCode.setTotal(count); + } + codeList.add(nextCode); + allCount = (allCount + count); + } + codeList.get(0).setTotal(allCount); + m.setTotal(allCount); + m.setChildren(codeList); + } + break; + case 3://寰呭鐞嗛偖浠� + m.setChildren(new ArrayList<>()); + if (userMail.size() > 0) { + m.setTotal((int) userMail.stream().filter(s -> s.getHandle().equals(1)).count()); + } + break; + case 4://鏈閭欢 + m.setChildren(new ArrayList<>()); + if (userMail.size() > 0) { + m.setTotal((int) userMail.stream().filter(s -> s.getReadFlag().equals(0)).count()); + } + break; + case 5://缇ら偖绠� + m.setChildren(new ArrayList<>()); + break; + default: + break; + } + } + return module; + } catch (Exception e) { + throw e; + } + } + + @Override + public List<MailModuleBelowEntity> getMailModuleBelowList(String userCode) { + List<MailModuleBelowEntity> below = MailModuleBelowEntity.setMailInitial(); + try { + for (MailModuleBelowEntity b : below) { + String key = b.getKey(); + if (key.equals("moduleBelowA")) {//鏂囦欢澶� + List<T482107Entity> t482107 = mailFolderIfc.getMailFolder(userCode); + b.setList(MailFolder.getFolderModule(t482107, "")); + } + if (key.equals("moduleBelowB")) {//鏍囩閭欢 + List<T482115Entity> tag = mailTagIfc.getTagList(userCode, "", -1); + b.setList(MailTag.getTagModule(tag)); + } + if (key.equals("moduleBelowC")) {//鏄剧ず鏇村 + List<MailModuleBelowEntity> CList = new ArrayList<>(); + MailModuleBelowEntity delete = new MailModuleBelowEntity(); + delete.setKey("deleteMail"); + delete.setName("宸插垹闄ら偖浠�"); + CList.add(delete); + MailModuleBelowEntity waste = new MailModuleBelowEntity(); + waste.setKey("wasteMail"); + waste.setName("鍨冨溇閭欢"); + CList.add(waste); + MailModuleBelowEntity track = new MailModuleBelowEntity(); + track.setKey("trackMail"); + track.setName("杩借釜淇℃伅"); + CList.add(track); + b.setList(CList); + } + } + return below; + } catch (Exception e) { + throw e; + } + } + + @Transactional(rollbackFor = Exception.class) + @Override + public void updateMailHandle(String handleTime, String docCode) { + String sql = " set nocount on \n"; + try { + sql += "declare @docCode varchar(200) ='" + docCode + "' \n"; + sql += "update t482101H set handle_time=" + (StringUtils.isBlank(handleTime) ? "null" : GridUtils.prossSqlParm(handleTime)) + + " where docCode in (select list from GetInStr(@docCode))\n"; + sql += "select @@ROWCOUNT"; + jdbcTemplate.queryForObject(sql, Integer.class); + } catch (Exception e) { + throw e; + } + } + + @Override + public Integer getMailTotal(String email, Integer mailType, boolean isNoRead, String id, String userCode) { String sql = "set nocount on\n"; try { - sql += "delete t482101H where docCode=" + GridUtils.prossSqlParm(docCode); - sql += "select select @@ROWCOUNT"; - return jdbcTemplate.update(sql, Integer.class); - } catch (Exception e) { - throw e; - } - } - - /** - * 鏀朵欢绠辫繑鍥炵殑淇℃伅杩涜灏佽澶勭悊 - * - * @param messages - * @return - * @throws MessagingException - * @throws IOException - */ - public List<t482101HEntity> setMailContent(Message[] messages, HttpServletRequest request) throws Exception { - try { - List<t482101HEntity> t482101HEntityList = new ArrayList<>(); - HttpSession session = request.getSession(); - Message m = null; - for (int i = 0; i < messages.length; i++) { - m = messages[i]; - String from = MimeUtility.decodeText(m.getFrom()[0].toString()); - InternetAddress internetAddress = new InternetAddress(from); - t482101HEntity mail = new t482101HEntity(); - if (m.isSet(Flags.Flag.SEEN)) { -// System.out.println("閭欢宸叉爣璁颁负宸茶銆�"); - mail.setReadFlag(1);//宸茶 - } else { -// m.setFlag(Flags.Flag.SEEN, true);//鏍囪鎴愬凡璇� - mail.setReadFlag(0);//宸茶 + sql += "select count(*) from t482101H "; + sql += " where userCode=" + GridUtils.prossSqlParm(userCode); + if (mailType < 3) { + sql += " and mailType=" + mailType;//0锛氳崏绋跨 1锛氭敹浠剁 2锛氬彂浠剁 + } + if (StringUtils.isNotBlank(email)) { + if (mailType == 1) { + sql += " and receiver like '%" + email + "%'"; + } else if (mailType == 2) { + sql += " and sender =" + GridUtils.prossSqlParm(email); + } else if (mailType == 3) { + sql += " and isnull(handle_time,'') <> '' and isnull(handle_time,'') < getdate() "; } - if (m.isExpunged()) {//妫�鏌ヤ竴涓秷鎭槸鍚﹀凡琚垹闄ゃ�傗�� - mail.setDeleteFlag(1);//宸插垹闄� - } else { - mail.setDeleteFlag(0);//宸插垹闄� - } - mail.setMailType(1);//鏀朵欢 - mail.setClassType(1); - mail.setUserCode((String) session.getAttribute(SessionKey.USERCODE)); - mail.setUserName((String) session.getAttribute(SessionKey.USERNAME)); - mail.setCompanyId((String) session.getAttribute(SessionKey.COMPANY_ID)); - mail.setCompanyName((String) session.getAttribute(SessionKey.COMPANY_NAME)); - mail.setSubject(m.getSubject());//鏍囬 - String result = ""; - if (m.isMimeType("text/plain")) { - result = m.getContent().toString(); - }else if (m.isMimeType("text/html")) {//html鏍煎紡 - result = m.getContent().toString(); - }else if (m.isMimeType("multipart/*")) { - List<String> fileSeq = new ArrayList<>(); - MimeMultipart mimeMultipart = (MimeMultipart) m.getContent(); - String uuId = UUID.randomUUID().toString().toUpperCase();//鐢熸垚uuid - result = getTextFromMimeMultipart(mimeMultipart, uuId, fileSeq); - if (fileSeq.size() > 0) {//鏈夐檮浠跺唴瀹逛繚瀛� - mail.setFileUNID(uuId + ";" + StringUtils.join(fileSeq, ";")); + } + if (isNoRead) {//0琛ㄧず鏈锛�1琛ㄧず宸茶 + sql += " and isnull(read_flag,0) = 0"; + } + if (StringUtils.isNotBlank(id)) {//鏂囦欢澶� + String[] type = id.split("_"); + if (type.length == 2) { + if (type[0].equals("folder")) { + sql += " and folder_id=" + type[1]; + } else if (type[0].equals("tag")) { + sql += " and tag_list like '%," + type[1] + ",%'"; } - }else { - result = m.getContent().toString(); } - mail.setContent(result);//淇濆瓨鍐呭 - mail.setSender(internetAddress.getAddress());//鍙戜欢浜� - - from = MimeUtility.decodeText(m.getRecipients(Message.RecipientType.TO)[0].toString()); - internetAddress = new InternetAddress(from); - mail.setReceiver(internetAddress.getAddress());//鏀朵欢浜� - - String cc = "";//鎶勯�佷汉 - Address[] ccAddress = m.getRecipients(Message.RecipientType.CC); - if (ccAddress != null && ccAddress.length > 0) { - for (Address c : ccAddress) { - from = MimeUtility.decodeText(c.toString()); - internetAddress = new InternetAddress(from); - cc += internetAddress.getAddress() + ";"; - } - cc = cc.substring(0, cc.lastIndexOf(";")); - } - mail.setCc(cc);//鎶勯�佷汉 - - String bcc = "";//鎶勯�佷汉 - Address[] bccAddress = m.getRecipients(Message.RecipientType.BCC); - if (bccAddress != null && bccAddress.length > 0) { - for (Address c : bccAddress) { - from = MimeUtility.decodeText(c.toString()); - internetAddress = new InternetAddress(from); - bcc += internetAddress.getAddress() + ";"; - } - bcc = bcc.substring(0, bcc.lastIndexOf(";")); - } - mail.setBcc(bcc);//瀵嗛�佷汉 - t482101HEntityList.add(mail); } - return t482101HEntityList; + return jdbcTemplate.queryForObject(sql, Integer.class); } catch (Exception e) { - throw e; - } - } - - // 杈呭姪鏂规硶锛岀敤浜庨�掑綊鑾峰彇绾枃鏈偖浠跺唴瀹� - private String getTextFromMimeMultipart(MimeMultipart mimeMultipart, String uuId, List<String> fileSeq) throws Exception { - int count = mimeMultipart.getCount(); - if (count == 0) { - throw new MessagingException("Multipart with no body parts"); - } - boolean multipartAlternative = isMultipartAlternative(mimeMultipart); - StringBuilder result = new StringBuilder(); - if (multipartAlternative) { - for (int i = 0; i < count; i++) { - BodyPart bodyPart = mimeMultipart.getBodyPart(i); - if (bodyPart.isMimeType("text/plain")) { - result.append(bodyPart.getContent()); - break; // 鍙幏鍙栫涓�涓函鏂囨湰鍐呭 - } - } - } else { - for (int i = 0; i < count; i++) { - BodyPart bodyPart = mimeMultipart.getBodyPart(i); - if (bodyPart.isMimeType("text/plain")) { - result.append(bodyPart.getContent()); - } else if (bodyPart.isMimeType("image/*")) {//鍥剧墖 - result.append(saveFileAndImage(bodyPart, "image", uuId)); - } else if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {//闄勪欢 - fileSeq.add(saveFileAndImage(bodyPart, Part.ATTACHMENT, uuId)); - } else if (bodyPart.isMimeType("multipart/*")) { - result.append(getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent(), uuId, fileSeq)); - } - } - } - return result.toString(); - } - - // 杈呭姪鏂规硶锛屽垽鏂槸鍚︿负multipart/alternative绫诲瀷鐨勯偖浠� - private boolean isMultipartAlternative(MimeMultipart mimeMultipart) throws Exception { - boolean textPlainFound = false; - boolean textHtmlFound = false; - int count = mimeMultipart.getCount(); - for (int i = 0; i < count; i++) { - BodyPart bodyPart = mimeMultipart.getBodyPart(i); - if (bodyPart.isMimeType("text/plain")) { - textPlainFound = true; - } else if (bodyPart.isMimeType("text/html")) { - textHtmlFound = true; - } - } - return textPlainFound && textHtmlFound; - } - - private String saveFileAndImage(BodyPart bodyPart, String type, String unId) throws Exception { - try { - String fileName = MimeUtility.decodeText(bodyPart.getFileName()); - if (type.equals("image")) { - String pattern = ".*\\.(jpg|jpeg|png|gif)$"; - fileName = Pattern.matches(pattern, fileName) ? fileName : fileName + ".jpg"; - } - File file = new File(fileName); - FileUtils.copyInputStreamToFile(bodyPart.getInputStream(), file); - AttachmentEntity attachmentEntity = new AttachmentEntity(); - attachmentEntity.setDoccode(""); - attachmentEntity.setRowId(""); - attachmentEntity.setFormId(482101); - attachmentEntity.setUnid(unId); - attachmentEntity.setSeq(null); - attachmentEntity.setFieldId("file"); - attachmentEntity.setPhysicalFile(file.getName()); - attachmentEntity.setOriginalFileName(file.getName()); - AttachmentIfc attachmentIfc = (AttachmentIfc) FactoryBean.getBean("AttachmentImpl"); - //淇濆瓨闄勪欢 - AttachmentWhereEntity attachmentWhereEntity = attachmentIfc.saveAttachment(attachmentEntity, file, "2"); - if (type.equals("image")) { - //杈撳嚭 url -// return imgData.getImageUrl(attachmentWhereEntity.getUnid() + ";" + attachmentWhereEntity.getSeq(), null, -// null, true, true, null); - return shoppingImageServer + "/uploads/attachment/" + 82 + "/482101/" + attachmentWhereEntity.getUnid() + "@p@" + attachmentWhereEntity.getSeq() + ".jpg"; - } else if (type.equals(Part.ATTACHMENT)) { - return attachmentWhereEntity.getSeq() + ""; - } else { - return null; - } - } catch (Exception e) { - throw e; + return 0; } } } -- Gitblit v1.8.0