xinyb
2024-09-03 117621d1cbd322f1c5041a76bb380895af7c642f
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
package com.yc.crm.mail.action;
 
import com.yc.crm.mail.entity.T482102Entity;
import com.yc.crm.mail.entity.t482101HEntity;
import com.yc.crm.mail.service.MailAccountIfc;
import com.yc.crm.mail.service.MailIfc;
import com.yc.crm.mail.util.AllBackMsg;
import com.yc.entity.DataSourceEntity;
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.web.bind.annotation.*;
 
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.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 {
    @Autowired
    MailAccountIfc emailAccountIfc;
    @Autowired
    MailIfc emailIfc;
 
    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("/getMailList.do")
    public AllBackMsg getMailList(String mail, @RequestParam(defaultValue = "1") Integer mailType, 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;
                }
            }
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
            List<t482101HEntity> t482101HEntityList = emailIfc.getReceivingMailList(mail, mailType, isNoRead);
            if (t482101HEntityList.size() > 0) {
                msg.setSuccess("执行完成", t482101HEntityList);
            }
        } 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) {
                msg.setSuccess("执行完成", t482101HEntity);
            }
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 删除邮件
     *
     * @return
     */
    @PostMapping("/deleteReceivingMail.do")
    public AllBackMsg deleteReceivingMail(@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.deleteReceivingMail(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("/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);//草稿
            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);
//            msg.setOk("执行成功");
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
    /**
     * 删除草稿箱
     *
     * @return
     */
    @PostMapping("/deleteMailDrafts.do")
    public AllBackMsg deleteMailDrafts(@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());//切换数据源
            int cont = emailIfc.deleteMailDrafts(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;
    }
 
    /**
     * 发送邮件
     *
     * @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);//发件
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
            //发送
            emailIfc.sendingMail(t482101H, request);
            msg.setOk("发送成功");
        } catch (Exception e) {
            String error = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
            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);
            }
        } 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 = emailAccountIfc.getAccount(userCode, mail);//返回邮箱的账号信息
            if (emailEntities.size() > 0) {
                List<t482101HEntity> mails = new ArrayList<>();
                for (T482102Entity e : emailEntities) {//遍历用户绑定邮箱
                    //收邮件信息
                    mails.addAll(emailIfc.getMailReceiving(e, request));
                }
                if (mails.size() > 0) {
                    emailIfc.saveReceivingMailList(mails);//保存
                    msg.setOk("保存成功");
                } else {
                    msg.setOk("未发现新邮件");
                }
            } 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("/updateRead.do")
    public AllBackMsg updateRead(@RequestBody List<String> docCode, 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 (docCode == null && docCode.size() == 0) {
                msg.setFail("请选择标记邮件");
                return msg;
            }
            String arrayCode = StringUtils.join(docCode, ",");
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
            emailIfc.updateRead(arrayCode);
            msg.setOk("标记成功");
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
 
}