fs-danaus
2024-09-13 d4a21ed564d51bbcf00543b8d7c3eedb77ff28f4
提交 | 用户 | age
385e2d 1 package com.yc.crm.mail.action;
X 2
7caeae 3 import com.yc.crm.mail.entity.*;
385e2d 4 import com.yc.crm.mail.service.MailAccountIfc;
X 5 import com.yc.crm.mail.service.MailIfc;
7caeae 6 import com.yc.crm.mail.service.MailServiceIfc;
385e2d 7 import com.yc.crm.mail.util.AllBackMsg;
7cf738 8 import com.yc.entity.AttachmentConfig;
385e2d 9 import com.yc.entity.DataSourceEntity;
7caeae 10 import com.yc.factory.FactoryBean;
385e2d 11 import com.yc.multiData.MultiDataSource;
X 12 import com.yc.multiData.SpObserver;
13 import com.yc.utils.SessionKey;
14 import org.apache.commons.lang3.StringUtils;
15 import org.springframework.beans.factory.annotation.Autowired;
7caeae 16 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
385e2d 17 import org.springframework.web.bind.annotation.*;
X 18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21 import javax.servlet.http.HttpSession;
f5cc47 22 import java.util.ArrayList;
X 23 import java.util.HashMap;
385e2d 24 import java.util.List;
f5cc47 25 import java.util.Map;
385e2d 26 import java.util.regex.Pattern;
X 27
28 /**
29  * @BelongsProject: eCoWorksV3
30  * @BelongsPackage: com.yc.crm.mail.action
31  * @author: xinyb
32  * @CreateTime: 2024-08-07  17:30
33  * @Description: 邮箱发送 收件
34  */
35 @CrossOrigin
36 @RestController
f5cc47 37 @RequestMapping("/crm/mail")
385e2d 38 public class MailController {
7caeae 39     ThreadPoolTaskExecutor threadPoolExecutor = (ThreadPoolTaskExecutor) FactoryBean.getBean("threadPoolExecutor");//线程池
7cf738 40
X 41     final static String shoppingImageServer = AttachmentConfig.get("attachment.server");
385e2d 42     @Autowired
X 43     MailAccountIfc emailAccountIfc;
44     @Autowired
45     MailIfc emailIfc;
7caeae 46     @Autowired
X 47     MailServiceIfc mailServiceIfc;
385e2d 48
X 49     private static final Pattern EMAIL_PATTERN =
50             Pattern.compile("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$");
51
d8f4b9 52
X 53     /**
6a63e2 54      * 左边结构层
d8f4b9 55      *
X 56      * @return
57      */
58     @GetMapping("/getEmailModule.do")
59     public AllBackMsg getEmailModule(HttpServletRequest request, HttpServletResponse response) throws Exception {
60         AllBackMsg msg = new AllBackMsg();
61         try {
62             HttpSession session = request.getSession();
63             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
64             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
65                 msg.setFail("获取不到用户信息");
66                 return msg;
67             }
68             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
69             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
70             List<MailModuleEntity> list = emailIfc.getMailModuleList(userCode);
71             msg.setSuccess("执行成功", list);
72         } catch (Exception e) {
73             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
74         } finally {
75             SpObserver.setDBtoInstance();
76         }
77         return msg;
78     }
385e2d 79
X 80     /**
81      * 获取邮件列表(包括草稿箱,收件箱)
82      *
83      * @return
84      */
85     @GetMapping("/getMailList.do")
86     public AllBackMsg getMailList(String mail, @RequestParam(defaultValue = "1") Integer mailType, boolean isNoRead, HttpServletRequest request, HttpServletResponse response) throws Exception {
87         AllBackMsg msg = new AllBackMsg();
88         try {
89             if (StringUtils.isNotBlank(mail)) {
90                 if (!EMAIL_PATTERN.matcher(mail).matches()) {
91                     msg.setFail("邮箱的各式不争取");
92                     return msg;
93                 }
94             }
5b1a4a 95             HttpSession session = request.getSession();
X 96             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
97             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
98                 msg.setFail("获取不到用户信息");
99                 return msg;
100             }
385e2d 101             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 102             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
5b1a4a 103             List<t482101HList> t482101HEntityList = emailIfc.getReceivingMailList(mail, mailType, isNoRead, userCode);
385e2d 104             if (t482101HEntityList.size() > 0) {
f5cc47 105                 msg.setSuccess("执行完成", t482101HEntityList);
385e2d 106             }
X 107         } catch (Exception e) {
f5cc47 108             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 109         } finally {
X 110             SpObserver.setDBtoInstance();
111         }
112         return msg;
113     }
114
115     /**
116      * 获取邮件详情
117      *
118      * @return
119      */
120     @GetMapping("/getMailInfo.do")
f4c162 121     public AllBackMsg getMailInfo(String docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
385e2d 122         AllBackMsg msg = new AllBackMsg();
X 123         try {
124             if (StringUtils.isBlank(docCode)) {//获取不到当前用户直接结束
125                 msg.setFail("缺少邮箱唯一码docCode,请检查");
126                 return msg;
127             }
128             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
129             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
f4c162 130             t482101HEntity t482101HEntity = emailIfc.getReceivingMailInfo(docCode);
385e2d 131             if (t482101HEntity != null) {
7cf738 132                 if (StringUtils.isNotBlank(t482101HEntity.getAttachmentList())) {//附件的处理
X 133                     List<String> list = new ArrayList<>();
134                     String[] path = t482101HEntity.getAttachmentList().split(";");
135                     if (path.length > 1) {
136                         String unId = path[0];//第一个必定是unid;
137                         for (int p = 1; p < path.length; p++) {
138                             list.add(shoppingImageServer + "/uploads/attachment/82/482101/" + unId + "@p@" + path[p]);
139                         }
140                         t482101HEntity.setAttachmentPath(list);
141                     }
142                 }
f5cc47 143                 msg.setSuccess("执行完成", t482101HEntity);
385e2d 144             }
X 145         } catch (Exception e) {
f5cc47 146             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 147         } finally {
X 148             SpObserver.setDBtoInstance();
149         }
150         return msg;
151     }
152
153     /**
f4c162 154      * 删除邮件
385e2d 155      *
X 156      * @return
157      */
5b1a4a 158     @PostMapping("/deleteEmail.do")
X 159     public AllBackMsg deleteEmail(@RequestBody List<String> docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
385e2d 160         AllBackMsg msg = new AllBackMsg();
X 161         try {
f4c162 162             if (docCode == null && docCode.size() == 0) {//获取不到当前用户直接结束
X 163                 msg.setFail("请选中删除的邮件");
385e2d 164                 return msg;
X 165             }
f4c162 166             String arrayCode = StringUtils.join(docCode, ",");
f5cc47 167             HttpSession session = request.getSession();
X 168             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
169             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
170                 msg.setFail("获取不到用户信息");
171                 return msg;
172             }
385e2d 173             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 174             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
5b1a4a 175             Integer cont = emailIfc.deleteEmail(userCode, arrayCode);
385e2d 176             if (cont > 0) {
5b1a4a 177                 msg.setOk("已删除");
385e2d 178             }
X 179         } catch (Exception e) {
f5cc47 180             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 181         } finally {
X 182             SpObserver.setDBtoInstance();
183         }
184         return msg;
185     }
186
187     /**
188      * 保存草稿箱(保存或修改)
189      *
190      * @return
191      */
192     @PostMapping("/saveMailDrafts.do")
193     public AllBackMsg saveMailDrafts(@RequestBody t482101HEntity t482101H, HttpServletRequest request, HttpServletResponse response) throws Exception {
194         AllBackMsg msg = new AllBackMsg();
195         try {
196             HttpSession session = request.getSession();
197             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
198             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
199                 msg.setFail("获取不到用户信息");
200                 return msg;
201             }
202             if (StringUtils.isNotBlank(t482101H.getSender())) {
203                 if (!EMAIL_PATTERN.matcher(t482101H.getSender()).matches()) {
204                     msg.setFail("发送人邮箱:" + t482101H.getSender() + "的格式不正确");
205                     return msg;
206                 }
207             }
5b1a4a 208             if (t482101H.getReceiver() != null && t482101H.getReceiver().size() > 0) {
f4c162 209                 for (String mail : t482101H.getReceiver()) {
X 210                     if (!EMAIL_PATTERN.matcher(mail).matches()) {
211                         msg.setFail("收件人邮箱:" + mail + "的格式不正确");
212                         return msg;
213                     }
385e2d 214                 }
f4c162 215
385e2d 216             }
X 217             String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID);
218             String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME);
219             String userName = (String) session.getAttribute(SessionKey.USER_NAME);
220             t482101H.setCompanyId(companyId);
221             t482101H.setCompanyName(companyName);
222             t482101H.setUserCode(userCode);
223             t482101H.setUserName(userName);
224             t482101H.setMailType(0);//草稿
5b1a4a 225             t482101H.setReadFlag(1);//已读
7caeae 226             t482101H.setAttachmentList(t482101H.getAttachmentList());
385e2d 227             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 228             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
229             t482101H = emailIfc.saveReceivingMail(t482101H);
f5cc47 230             Map<String, Object> map = new HashMap<>();
X 231             map.put("docCode", t482101H.getDocCode());
232             msg.setSuccess("执行成功", map);
385e2d 233         } catch (Exception e) {
f5cc47 234             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 235         } finally {
X 236             SpObserver.setDBtoInstance();
237         }
238         return msg;
239     }
240
241
242     /**
7caeae 243      * 收邮件
X 244      *
245      * @param request
246      * @param response
247      * @return
248      */
249     @GetMapping("/receive.do")
250     public AllBackMsg getReceiveMail(String mail, HttpServletRequest request, HttpServletResponse response) throws Exception {
251         AllBackMsg msg = new AllBackMsg();
252         try {
253             HttpSession session = request.getSession();
254             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
255             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
256                 msg.setFail("获取不到用户信息");
257                 return msg;
258             }
259             if (StringUtils.isNotBlank(mail)) {
260                 if (!EMAIL_PATTERN.matcher(mail).matches()) {
261                     msg.setFail("请输入有效的邮箱");
262                     return msg;
263                 }
264             }
265             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
266             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
267             //根据当前用户查询绑定的邮箱信息
268             List<T482102Entity> emailEntities = new ArrayList<>();
269             if (StringUtils.isBlank(mail)) {
270                 emailEntities = emailAccountIfc.getAccount(userCode);//返回邮箱的账号信息
271             } else {
7cf738 272                 T482102Entity T482102 = emailAccountIfc.getAccountInfo(userCode, mail);
X 273                 if (T482102 != null) {
274                     emailEntities.add(T482102);
275                 }
7caeae 276             }
X 277             if (emailEntities.size() > 0) {
278                 List<t482101HEntity> mails = new ArrayList<>();
279                 Map<String, Object> map = new HashMap<>();
280                 for (T482102Entity e : emailEntities) {//遍历用户绑定邮箱
281                     //多线程收邮件
282                     threadPoolExecutor.execute(new ReceivingEmailsSave(mailServiceIfc, e, new FoundationEntity(request)));
283                 }
7cf738 284                 map.put("code", 0);
X 285                 map.put("msg", "请销等,邮件正在系统保存");
7caeae 286                 msg.setSuccess("执行成功", map);
X 287             } else {
288                 msg.setFail("没有找到绑定的邮箱信息");
289             }
290         } catch (Exception e) {
291             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
292         } finally {
293             SpObserver.setDBtoInstance();
294         }
295         return msg;
296     }
297
298
299     /**
300      * 发邮件
385e2d 301      *
X 302      * @param request
303      * @param response
304      * @return
305      */
306     @PostMapping("/sendingMail.do")
307     public AllBackMsg sendingMail(@RequestBody t482101HEntity t482101H, HttpServletRequest request, HttpServletResponse response) throws Exception {
308         AllBackMsg msg = new AllBackMsg();
309         try {
310             HttpSession session = request.getSession();
311             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
312             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
313                 msg.setFail("获取不到用户信息");
314                 return msg;
315             }
316             if (StringUtils.isNotBlank(t482101H.getSender())) {
317                 if (!EMAIL_PATTERN.matcher(t482101H.getSender()).matches()) {
318                     msg.setFail("发送人邮箱:" + t482101H.getSender() + "的格式不正确");
319                     return msg;
320                 }
321             }
5b1a4a 322             if (t482101H.getReceiver() != null && t482101H.getReceiver().size() > 0) {
f4c162 323                 for (String mail : t482101H.getReceiver()) {
X 324                     if (!EMAIL_PATTERN.matcher(mail).matches()) {
325                         msg.setFail("收件人邮箱:" + mail + "的格式不正确");
326                         return msg;
327                     }
385e2d 328                 }
f4c162 329
385e2d 330             }
X 331             String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID);
332             String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME);
333             String userName = (String) session.getAttribute(SessionKey.USER_NAME);
334             t482101H.setCompanyId(companyId);
335             t482101H.setCompanyName(companyName);
336             t482101H.setUserCode(userCode);
337             t482101H.setUserName(userName);
338             t482101H.setMailType(2);//发件
7caeae 339             t482101H.setAttachmentList(t482101H.getAttachmentList());
385e2d 340             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 341             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
342             //发送
7caeae 343             mailServiceIfc.sendEmails(t482101H, request);
f5cc47 344             msg.setOk("发送成功");
385e2d 345         } catch (Exception e) {
f5cc47 346             String error = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
X 347             String[] arrayError = error.split("#");
348             if (arrayError.length == 2) {
349                 Map<String, Object> map = new HashMap<>();
350                 map.put("docCode", arrayError[1]);
351                 msg.setError(arrayError[0], map);
352             } else {
353                 msg.setFail(error);
354             }
385e2d 355         } finally {
X 356             SpObserver.setDBtoInstance();
357         }
358         return msg;
359     }
f4c162 360
X 361     /**
362      * 标记已读
363      *
364      * @param request
365      * @param response
366      * @return
367      */
368     @PostMapping("/updateRead.do")
7cf738 369     public AllBackMsg updateRead(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws Exception {
f4c162 370         AllBackMsg msg = new AllBackMsg();
X 371         try {
372             HttpSession session = request.getSession();
373             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
374             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
375                 msg.setFail("获取不到用户信息");
376                 return msg;
377             }
7cf738 378             List<String> docCode = (List<String>) map.get("list");
f4c162 379             if (docCode == null && docCode.size() == 0) {
X 380                 msg.setFail("请选择标记邮件");
381                 return msg;
382             }
7cf738 383             if (map.get("status") == null) {
X 384                 msg.setFail("请传入状态值");
385                 return msg;
386             }
387             boolean read = (Boolean) map.get("status");
f4c162 388             String arrayCode = StringUtils.join(docCode, ",");
X 389             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
390             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
7cf738 391             emailIfc.updateRead(arrayCode, read);
f4c162 392             msg.setOk("标记成功");
X 393         } catch (Exception e) {
394             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
395         } finally {
396             SpObserver.setDBtoInstance();
397         }
398         return msg;
399     }
400
7cf738 401     /**
X 402      * 待处理邮件
403      *
404      * @param request
405      * @param response
406      * @return
407      */
408     @GetMapping("/updateHandle.do")
409     public AllBackMsg updateHandle(String handleTime, String docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
410         AllBackMsg msg = new AllBackMsg();
411         try {
412             HttpSession session = request.getSession();
413             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
414             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
415                 msg.setFail("获取不到用户信息");
416                 return msg;
417             }
418
419             if (StringUtils.isBlank(docCode)) {
420                 msg.setFail("邮件唯一标识docCode不能空");
421                 return msg;
422             }
423             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
424             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
425             emailIfc.updateMailHandle(handleTime, docCode);
426             msg.setOk("成功标记为待处理邮件");
427         } catch (Exception e) {
428             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
429         } finally {
430             SpObserver.setDBtoInstance();
431         }
432         return msg;
433     }
434
7caeae 435     private class ReceivingEmailsSave implements Runnable {
X 436         final MailServiceIfc mailServiceIfc;
437         final T482102Entity t482102;
438         final FoundationEntity foundation;
439
440         public ReceivingEmailsSave(MailServiceIfc mailServiceIfc, T482102Entity t482102, FoundationEntity foundation) {
441             this.mailServiceIfc = mailServiceIfc;
442             this.t482102 = t482102;
443             this.foundation = foundation;
444         }
445
446         @Override
447         public void run() {
448             try {
449                 SpObserver.setDBtoInstance("_" + foundation.getDbId());
450                 //收邮件
451                 mailServiceIfc.receivingEmails(t482102, foundation);
452             } catch (Exception e) {
453                 e.printStackTrace();
454             } finally {
455                 SpObserver.setDBtoInstance();
456             }
457         }
458     }
459
385e2d 460 }