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