xinyb
4 天以前 73c8452a479ea83d4e9d28a7c66c019d9955d194
提交 | 用户 | 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;
195685 5 import com.yc.crm.mail.service.MailFileIfc;
385e2d 6 import com.yc.crm.mail.service.MailIfc;
7caeae 7 import com.yc.crm.mail.service.MailServiceIfc;
385e2d 8 import com.yc.crm.mail.util.AllBackMsg;
7cf738 9 import com.yc.entity.AttachmentConfig;
385e2d 10 import com.yc.entity.DataSourceEntity;
ad7c8d 11 import com.yc.entity.attachment.AttachmentEntity;
7caeae 12 import com.yc.factory.FactoryBean;
385e2d 13 import com.yc.multiData.MultiDataSource;
X 14 import com.yc.multiData.SpObserver;
15 import com.yc.utils.SessionKey;
16 import org.apache.commons.lang3.StringUtils;
17 import org.springframework.beans.factory.annotation.Autowired;
7caeae 18 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
385e2d 19 import org.springframework.web.bind.annotation.*;
X 20
f6ae0b 21 import javax.mail.MessagingException;
385e2d 22 import javax.servlet.http.HttpServletRequest;
X 23 import javax.servlet.http.HttpServletResponse;
24 import javax.servlet.http.HttpSession;
f5cc47 25 import java.util.ArrayList;
X 26 import java.util.HashMap;
385e2d 27 import java.util.List;
f5cc47 28 import java.util.Map;
562e20 29 import java.util.regex.Matcher;
385e2d 30 import java.util.regex.Pattern;
X 31
32 /**
33  * @BelongsProject: eCoWorksV3
34  * @BelongsPackage: com.yc.crm.mail.action
35  * @author: xinyb
36  * @CreateTime: 2024-08-07  17:30
37  * @Description: 邮箱发送 收件
38  */
39 @CrossOrigin
40 @RestController
f5cc47 41 @RequestMapping("/crm/mail")
385e2d 42 public class MailController {
7caeae 43     ThreadPoolTaskExecutor threadPoolExecutor = (ThreadPoolTaskExecutor) FactoryBean.getBean("threadPoolExecutor");//线程池
7cf738 44
X 45     final static String shoppingImageServer = AttachmentConfig.get("attachment.server");
385e2d 46     @Autowired
X 47     MailAccountIfc emailAccountIfc;
48     @Autowired
49     MailIfc emailIfc;
7caeae 50     @Autowired
X 51     MailServiceIfc mailServiceIfc;
195685 52     @Autowired
X 53     MailFileIfc mailFileIfc;
385e2d 54
X 55     private static final Pattern EMAIL_PATTERN =
56             Pattern.compile("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$");
57
d8f4b9 58
X 59     /**
658898 60      * 左边结构上层
d8f4b9 61      *
X 62      * @return
63      */
64     @GetMapping("/getEmailModule.do")
65     public AllBackMsg getEmailModule(HttpServletRequest request, HttpServletResponse response) throws Exception {
66         AllBackMsg msg = new AllBackMsg();
67         try {
68             HttpSession session = request.getSession();
69             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
70             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
71                 msg.setFail("获取不到用户信息");
72                 return msg;
73             }
74             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
75             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
76             List<MailModuleEntity> list = emailIfc.getMailModuleList(userCode);
77             msg.setSuccess("执行成功", list);
78         } catch (Exception e) {
79             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
80         } finally {
81             SpObserver.setDBtoInstance();
82         }
83         return msg;
84     }
aad276 85
658898 86     /**
X 87      * 左边结构下层
88      *
89      * @return
90      */
91     @GetMapping("/getEmailModuleBelow.do")
92     public AllBackMsg getEmailModuleBelow(HttpServletRequest request, HttpServletResponse response) throws Exception {
93         AllBackMsg msg = new AllBackMsg();
94         try {
95             HttpSession session = request.getSession();
96             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
97             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
98                 msg.setFail("获取不到用户信息");
99                 return msg;
100             }
101             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
102             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
aad276 103             List<MailModuleBelowEntity> list = emailIfc.getMailModuleBelowList(userCode);
658898 104             msg.setSuccess("执行成功", list);
X 105         } catch (Exception e) {
106             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
107         } finally {
108             SpObserver.setDBtoInstance();
109         }
110         return msg;
111     }
385e2d 112
X 113     /**
114      * 获取邮件列表(包括草稿箱,收件箱)
115      *
116      * @return
117      */
118     @GetMapping("/getMailList.do")
195685 119     public AllBackMsg getMailList(String mail, @RequestParam(defaultValue = "1") Integer mailType,
562e20 120                                   @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit, boolean isNoRead, HttpServletRequest request, HttpServletResponse response) throws Exception {
385e2d 121         AllBackMsg msg = new AllBackMsg();
X 122         try {
123             if (StringUtils.isNotBlank(mail)) {
124                 if (!EMAIL_PATTERN.matcher(mail).matches()) {
125                     msg.setFail("邮箱的各式不争取");
126                     return msg;
127                 }
128             }
5b1a4a 129             HttpSession session = request.getSession();
X 130             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
131             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
132                 msg.setFail("获取不到用户信息");
133                 return msg;
134             }
385e2d 135             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 136             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
562e20 137             List<t482101HList> t482101HEntityList = emailIfc.getReceivingMailList(mail, mailType, isNoRead, userCode, page, limit);
aad276 138             Map<String, Object> map = new HashMap<>();
X 139             map.put("page", page);
140             map.put("limit", limit);
385e2d 141             if (t482101HEntityList.size() > 0) {
aad276 142                 map.put("total", emailIfc.getMailTotal(mail, mailType, isNoRead, null, userCode));
X 143                 map.put("list", t482101HEntityList);
144             } else {
145                 map.put("list", new ArrayList<>());
385e2d 146             }
aad276 147             msg.setSuccess("执行完成", map);
385e2d 148         } catch (Exception e) {
f5cc47 149             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 150         } finally {
X 151             SpObserver.setDBtoInstance();
152         }
153         return msg;
154     }
155
156     /**
57c8bf 157      * 获取待处理邮件列表
X 158      *
159      * @return
160      */
161     @GetMapping("/getHandleMailList.do")
562e20 162     public AllBackMsg getHandleMailList(String mail, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit,
195685 163                                         HttpServletRequest request, HttpServletResponse response) throws Exception {
57c8bf 164         AllBackMsg msg = new AllBackMsg();
X 165         try {
166             if (StringUtils.isNotBlank(mail)) {
167                 if (!EMAIL_PATTERN.matcher(mail).matches()) {
168                     msg.setFail("邮箱的各式不争取");
169                     return msg;
170                 }
171             }
172             HttpSession session = request.getSession();
173             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
174             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
175                 msg.setFail("获取不到用户信息");
176                 return msg;
177             }
178             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
179             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
562e20 180             List<t482101HList> t482101HEntityList = emailIfc.getHandleMailList(mail, userCode, page, limit);
aad276 181             Map<String, Object> map = new HashMap<>();
X 182             map.put("page", page);
183             map.put("limit", limit);
57c8bf 184             if (t482101HEntityList.size() > 0) {
aad276 185                 map.put("total", emailIfc.getMailTotal(mail, 3, false, null, userCode));
X 186                 map.put("list", t482101HEntityList);
187             } else {
188                 map.put("list", new ArrayList<>());
57c8bf 189             }
aad276 190             msg.setSuccess("执行完成", map);
57c8bf 191         } catch (Exception e) {
X 192             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
193         } finally {
194             SpObserver.setDBtoInstance();
195         }
196         return msg;
197     }
198
199     /**
385e2d 200      * 获取邮件详情
X 201      *
202      * @return
203      */
204     @GetMapping("/getMailInfo.do")
f4c162 205     public AllBackMsg getMailInfo(String docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
385e2d 206         AllBackMsg msg = new AllBackMsg();
X 207         try {
208             if (StringUtils.isBlank(docCode)) {//获取不到当前用户直接结束
209                 msg.setFail("缺少邮箱唯一码docCode,请检查");
210                 return msg;
211             }
212             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
213             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
f4c162 214             t482101HEntity t482101HEntity = emailIfc.getReceivingMailInfo(docCode);
385e2d 215             if (t482101HEntity != null) {
7cf738 216                 if (StringUtils.isNotBlank(t482101HEntity.getAttachmentList())) {//附件的处理
ad7c8d 217                     //获取到附件内容
195685 218                     List<AttachmentEntity> attachmentEntities = mailFileIfc.getAttachmentEntityList(t482101HEntity.getAttachmentList());
67a32b 219                     List<Map<String, Object>> list = new ArrayList<>();
ad7c8d 220                     if (attachmentEntities.size() > 0) {
X 221                         for (AttachmentEntity a : attachmentEntities) {
67a32b 222                             Map<String, Object> map = new HashMap<>();
73c845 223                             map.put("url", "http://yingchen.onbus.cn:9010" + "/uploads/email/" + dataSourceEntity.getDbId() + "/482101/" + a.getUnid() + "@p@" + a.getPhysicalFile());
67a32b 224                             if (!t482101HEntity.getMailType().equals(1)) {//不是收件类型(1)
X 225                                 map.put("url", shoppingImageServer + "/uploads/attachment/" + dataSourceEntity.getDbId() + "/482101/" + a.getUnid() + "@p@" + a.getSeq() + "." + a.getFileType());
226                             }
227                             map.put("name", a.getOriginalFileName());
228                             map.put("size", a.getFileSize());
229                             map.put("fileType", a.getFileType());
56d9f6 230                             list.add(map);
7cf738 231                         }
X 232                         t482101HEntity.setAttachmentPath(list);
233                     }
234                 }
f5cc47 235                 msg.setSuccess("执行完成", t482101HEntity);
385e2d 236             }
X 237         } catch (Exception e) {
f5cc47 238             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 239         } finally {
X 240             SpObserver.setDBtoInstance();
241         }
242         return msg;
243     }
244
245     /**
f4c162 246      * 删除邮件
385e2d 247      *
X 248      * @return
249      */
5b1a4a 250     @PostMapping("/deleteEmail.do")
X 251     public AllBackMsg deleteEmail(@RequestBody List<String> docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
385e2d 252         AllBackMsg msg = new AllBackMsg();
X 253         try {
f4c162 254             if (docCode == null && docCode.size() == 0) {//获取不到当前用户直接结束
X 255                 msg.setFail("请选中删除的邮件");
385e2d 256                 return msg;
X 257             }
f4c162 258             String arrayCode = StringUtils.join(docCode, ",");
f5cc47 259             HttpSession session = request.getSession();
X 260             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
261             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
262                 msg.setFail("获取不到用户信息");
263                 return msg;
264             }
385e2d 265             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 266             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
5b1a4a 267             Integer cont = emailIfc.deleteEmail(userCode, arrayCode);
385e2d 268             if (cont > 0) {
5b1a4a 269                 msg.setOk("已删除");
385e2d 270             }
X 271         } catch (Exception e) {
f5cc47 272             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 273         } finally {
X 274             SpObserver.setDBtoInstance();
275         }
276         return msg;
277     }
278
279     /**
57c8bf 280      * 快速回复
X 281      *
282      * @return
283      */
284     @PostMapping("/setQuickReply.do")
285     public AllBackMsg setQuickReply(@RequestBody Map<String, Object> reply, HttpServletRequest request, HttpServletResponse response) throws Exception {
286         AllBackMsg msg = new AllBackMsg();
287         try {
288             if (reply.get("docCode") == null) {
289                 msg.setFail("获取不到回复邮件的唯一docCode值");
290                 return msg;
291             }
292             if (reply.get("content") == null) {
293                 msg.setFail("请输入回复内容");
294                 return msg;
295             }
296             HttpSession session = request.getSession();
297             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
298             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
299                 msg.setFail("获取不到用户信息");
300                 return msg;
301             }
302             String docCode = (String) reply.get("docCode");//编号
303             String content = (String) reply.get("content");//内容
304             Integer count = mailServiceIfc.setQuickReply(docCode, content);
305             Map<String, Object> map = new HashMap<>();
306             if (count.equals(1)) {
307                 map.put("code", 0);
308                 map.put("docCode", docCode);
309                 map.put("msg", "邮件已回复");
310                 msg.setSuccess("执行成功", map);
311             } else {
312                 map.put("code", -1);
313                 map.put("docCode", docCode);
314                 map.put("msg", "邮件回复失败");
315                 msg.setSuccess("执行成功", map);
316             }
317         } catch (Exception e) {
318             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
319         } finally {
320             SpObserver.setDBtoInstance();
321         }
322         return msg;
323     }
324
325     /**
385e2d 326      * 保存草稿箱(保存或修改)
X 327      *
328      * @return
329      */
330     @PostMapping("/saveMailDrafts.do")
331     public AllBackMsg saveMailDrafts(@RequestBody t482101HEntity t482101H, HttpServletRequest request, HttpServletResponse response) throws Exception {
332         AllBackMsg msg = new AllBackMsg();
333         try {
334             HttpSession session = request.getSession();
335             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
336             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
337                 msg.setFail("获取不到用户信息");
338                 return msg;
339             }
340             if (StringUtils.isNotBlank(t482101H.getSender())) {
341                 if (!EMAIL_PATTERN.matcher(t482101H.getSender()).matches()) {
342                     msg.setFail("发送人邮箱:" + t482101H.getSender() + "的格式不正确");
343                     return msg;
344                 }
345             }
5b1a4a 346             if (t482101H.getReceiver() != null && t482101H.getReceiver().size() > 0) {
f4c162 347                 for (String mail : t482101H.getReceiver()) {
X 348                     if (!EMAIL_PATTERN.matcher(mail).matches()) {
349                         msg.setFail("收件人邮箱:" + mail + "的格式不正确");
350                         return msg;
351                     }
385e2d 352                 }
f4c162 353
385e2d 354             }
X 355             String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID);
356             String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME);
357             String userName = (String) session.getAttribute(SessionKey.USER_NAME);
358             t482101H.setCompanyId(companyId);
359             t482101H.setCompanyName(companyName);
360             t482101H.setUserCode(userCode);
361             t482101H.setUserName(userName);
362             t482101H.setMailType(0);//草稿
5b1a4a 363             t482101H.setReadFlag(1);//已读
7caeae 364             t482101H.setAttachmentList(t482101H.getAttachmentList());
385e2d 365             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 366             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
367             t482101H = emailIfc.saveReceivingMail(t482101H);
f5cc47 368             Map<String, Object> map = new HashMap<>();
X 369             map.put("docCode", t482101H.getDocCode());
370             msg.setSuccess("执行成功", map);
385e2d 371         } catch (Exception e) {
f5cc47 372             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 373         } finally {
X 374             SpObserver.setDBtoInstance();
375         }
376         return msg;
377     }
378
379
380     /**
7caeae 381      * 收邮件
X 382      *
383      * @param request
384      * @param response
385      * @return
386      */
387     @GetMapping("/receive.do")
388     public AllBackMsg getReceiveMail(String mail, HttpServletRequest request, HttpServletResponse response) throws Exception {
389         AllBackMsg msg = new AllBackMsg();
390         try {
391             HttpSession session = request.getSession();
392             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
393             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
394                 msg.setFail("获取不到用户信息");
395                 return msg;
396             }
397             if (StringUtils.isNotBlank(mail)) {
398                 if (!EMAIL_PATTERN.matcher(mail).matches()) {
399                     msg.setFail("请输入有效的邮箱");
400                     return msg;
401                 }
402             }
403             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
404             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
405             //根据当前用户查询绑定的邮箱信息
406             List<T482102Entity> emailEntities = new ArrayList<>();
407             if (StringUtils.isBlank(mail)) {
408                 emailEntities = emailAccountIfc.getAccount(userCode);//返回邮箱的账号信息
409             } else {
7cf738 410                 T482102Entity T482102 = emailAccountIfc.getAccountInfo(userCode, mail);
X 411                 if (T482102 != null) {
412                     emailEntities.add(T482102);
413                 }
7caeae 414             }
X 415             if (emailEntities.size() > 0) {
416                 List<t482101HEntity> mails = new ArrayList<>();
417                 Map<String, Object> map = new HashMap<>();
418                 for (T482102Entity e : emailEntities) {//遍历用户绑定邮箱
419                     //多线程收邮件
420                     threadPoolExecutor.execute(new ReceivingEmailsSave(mailServiceIfc, e, new FoundationEntity(request)));
421                 }
7cf738 422                 map.put("code", 0);
X 423                 map.put("msg", "请销等,邮件正在系统保存");
7caeae 424                 msg.setSuccess("执行成功", map);
X 425             } else {
426                 msg.setFail("没有找到绑定的邮箱信息");
427             }
428         } catch (Exception e) {
429             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
430         } finally {
431             SpObserver.setDBtoInstance();
432         }
433         return msg;
434     }
435
436
437     /**
438      * 发邮件
385e2d 439      *
X 440      * @param request
441      * @param response
442      * @return
443      */
444     @PostMapping("/sendingMail.do")
445     public AllBackMsg sendingMail(@RequestBody t482101HEntity t482101H, HttpServletRequest request, HttpServletResponse response) throws Exception {
446         AllBackMsg msg = new AllBackMsg();
447         try {
448             HttpSession session = request.getSession();
449             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
450             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
451                 msg.setFail("获取不到用户信息");
452                 return msg;
453             }
454             if (StringUtils.isNotBlank(t482101H.getSender())) {
455                 if (!EMAIL_PATTERN.matcher(t482101H.getSender()).matches()) {
456                     msg.setFail("发送人邮箱:" + t482101H.getSender() + "的格式不正确");
457                     return msg;
458                 }
459             }
5b1a4a 460             if (t482101H.getReceiver() != null && t482101H.getReceiver().size() > 0) {
f4c162 461                 for (String mail : t482101H.getReceiver()) {
X 462                     if (!EMAIL_PATTERN.matcher(mail).matches()) {
463                         msg.setFail("收件人邮箱:" + mail + "的格式不正确");
464                         return msg;
465                     }
385e2d 466                 }
f4c162 467
385e2d 468             }
X 469             String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID);
470             String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME);
471             String userName = (String) session.getAttribute(SessionKey.USER_NAME);
472             t482101H.setCompanyId(companyId);
473             t482101H.setCompanyName(companyName);
474             t482101H.setUserCode(userCode);
475             t482101H.setUserName(userName);
476             t482101H.setMailType(2);//发件
7caeae 477             t482101H.setAttachmentList(t482101H.getAttachmentList());
385e2d 478             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 479             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
480             //发送
7caeae 481             mailServiceIfc.sendEmails(t482101H, request);
f5cc47 482             msg.setOk("发送成功");
f6ae0b 483         } catch (MessagingException e) {
f5cc47 484             String error = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
f6ae0b 485             if (StringUtils.isNotBlank(error)) {
X 486                 String[] arrayError = error.split("#");
487                 if (arrayError.length == 2) {
488                     Map<String, Object> map = new HashMap<>();
489                     map.put("docCode", arrayError[1]);
490                     msg.setError(arrayError[0], map);
491                 } else {
492                     msg.setFail(error);
493                 }
f5cc47 494             } else {
X 495                 msg.setFail(error);
496             }
f6ae0b 497         } catch (Exception e) {
X 498             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 499         } finally {
X 500             SpObserver.setDBtoInstance();
501         }
502         return msg;
503     }
f4c162 504
X 505     /**
506      * 标记已读
507      *
508      * @param request
509      * @param response
510      * @return
511      */
512     @PostMapping("/updateRead.do")
7cf738 513     public AllBackMsg updateRead(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws Exception {
f4c162 514         AllBackMsg msg = new AllBackMsg();
X 515         try {
516             HttpSession session = request.getSession();
517             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
518             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
519                 msg.setFail("获取不到用户信息");
520                 return msg;
521             }
7cf738 522             List<String> docCode = (List<String>) map.get("list");
f4c162 523             if (docCode == null && docCode.size() == 0) {
X 524                 msg.setFail("请选择标记邮件");
525                 return msg;
526             }
7cf738 527             if (map.get("status") == null) {
X 528                 msg.setFail("请传入状态值");
529                 return msg;
530             }
531             boolean read = (Boolean) map.get("status");
f4c162 532             String arrayCode = StringUtils.join(docCode, ",");
X 533             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
534             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
7cf738 535             emailIfc.updateRead(arrayCode, read);
f4c162 536             msg.setOk("标记成功");
X 537         } catch (Exception e) {
538             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
539         } finally {
540             SpObserver.setDBtoInstance();
541         }
542         return msg;
543     }
544
7cf738 545     /**
aad276 546      * 移动邮件分类
X 547      *
548      * @param request
549      * @param response
550      * @return
551      */
552     @PostMapping("/updateMailType.do")
553     public AllBackMsg updateMailType(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws Exception {
554         AllBackMsg msg = new AllBackMsg();
555         try {
556             HttpSession session = request.getSession();
557             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
558             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
559                 msg.setFail("获取不到用户信息");
560                 return msg;
561             }
562             if (map.get("docCode") == null) {
563                 msg.setFail("获取不到移动邮件编号docCode");
564                 return msg;
565             }
566             String docCode = (String) map.get("docCode");
567             Integer folderId = map.get("folderId") == null ? 0 : (Integer) map.get("folderId");
568             List<String> tagArray = (List<String>) map.get("tagId");
569             if (folderId.equals(0) && (tagArray == null || tagArray.size() == 0)) {
570                 msg.setFail("移动邮件分类不能为空");
571                 return msg;
572             }
573             String tagId = "";
574             if (tagArray != null && tagArray.size() > 0) {
575                 tagId = StringUtils.join(tagArray, ",");
576             }
577             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
578             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
579             emailIfc.updateMailType(docCode, folderId, tagId);
580             msg.setOk("移动成功");
581         } catch (Exception e) {
582             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
583         } finally {
584             SpObserver.setDBtoInstance();
585         }
586         return msg;
587     }
588
589     /**
590      * 加载不同目录里的邮件
591      *
592      * @param request
593      * @param response
594      * @return
595      */
596     @GetMapping("/classificationMail.do")
597     public AllBackMsg classificationMail(@RequestParam(defaultValue = "-1") Integer id, String type, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit,
598                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
599         AllBackMsg msg = new AllBackMsg();
600         try {
601             HttpSession session = request.getSession();
602             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
603             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
604                 msg.setFail("获取不到用户信息");
605                 return msg;
606             }
607             if (id == null || id.equals(-1)) {
608                 msg.setFail("获取不到文件Id");
609                 return msg;
610             }
611             if (StringUtils.isBlank(type)) {
612                 msg.setFail("获取不到文件类型");
613                 return msg;
614             }
615             type = type + "_" + id;//组装下
616             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
617             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
618             List<t482101HList> t482101 = emailIfc.getFolderMailList(type, userCode, page, limit);
619             Map<String, Object> map = new HashMap<>();
620             map.put("page", page);
621             map.put("limit", limit);
622             if (t482101.size() > 0) {
623                 map.put("total", emailIfc.getMailTotal(null, 6, false, type, userCode));
624                 map.put("list", t482101);
625             } else {
626                 map.put("list", new ArrayList<>());
627             }
628             msg.setSuccess("执行完成", map);
629         } catch (Exception e) {
630             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
631         } finally {
632             SpObserver.setDBtoInstance();
633         }
634         return msg;
635     }
636
637     /**
7cf738 638      * 待处理邮件
X 639      *
640      * @param request
641      * @param response
642      * @return
643      */
562e20 644     @PostMapping("/updateHandle.do")
X 645     public AllBackMsg updateHandle(@RequestBody Map<String, Object> param, HttpServletRequest request, HttpServletResponse response) throws Exception {
7cf738 646         AllBackMsg msg = new AllBackMsg();
X 647         try {
648             HttpSession session = request.getSession();
649             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
650             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
651                 msg.setFail("获取不到用户信息");
652                 return msg;
653             }
562e20 654             if (param.get("docCode") == null) {
7cf738 655                 msg.setFail("邮件唯一标识docCode不能空");
562e20 656                 return msg;
X 657             }
73c845 658             List<String> docCode = (List<String>) param.get("docCode");//待处理的单号
X 659             String handleTime = "";//待处理时间
660             if (param.get("handleTime") != null && !param.get("handleTime").equals("")) {
661                 handleTime = (String) param.get("handleTime");
562e20 662             }
73c845 663             if (StringUtils.isNotBlank(handleTime)) {
X 664                 // 正则表达式,用于匹配日期格式
665                 String regex = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}";
666                 Pattern pattern = Pattern.compile(regex);
667                 Matcher matcher = pattern.matcher(handleTime);
668                 if (!matcher.matches()) {
669                     msg.setFail("待处理日期格式为:yyyy-MM-dd HH:mm(如:1900-08-01 08:00)。请检查");
670                     return msg;
671                 }
7cf738 672             }
X 673             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
674             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
562e20 675             emailIfc.updateMailHandle(handleTime, StringUtils.join(docCode, ","));
73c845 676             if(StringUtils.isBlank(handleTime)){//没值就是完成
X 677                 msg.setOk("待处理邮件已完成");
678             }else{
679                 msg.setOk("标记为待处理邮件");
680             }
7cf738 681         } catch (Exception e) {
X 682             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
683         } finally {
684             SpObserver.setDBtoInstance();
685         }
686         return msg;
687     }
688
7caeae 689     private class ReceivingEmailsSave implements Runnable {
X 690         final MailServiceIfc mailServiceIfc;
691         final T482102Entity t482102;
692         final FoundationEntity foundation;
693
694         public ReceivingEmailsSave(MailServiceIfc mailServiceIfc, T482102Entity t482102, FoundationEntity foundation) {
695             this.mailServiceIfc = mailServiceIfc;
696             this.t482102 = t482102;
697             this.foundation = foundation;
698         }
699
700         @Override
701         public void run() {
702             try {
703                 SpObserver.setDBtoInstance("_" + foundation.getDbId());
704                 //收邮件
705                 mailServiceIfc.receivingEmails(t482102, foundation);
706             } catch (Exception e) {
707                 e.printStackTrace();
708             } finally {
709                 SpObserver.setDBtoInstance();
710             }
711         }
712     }
713
385e2d 714 }