xinyb
2024-09-04 4284431e6755e854a7de7afb2ef09e461d3e72eb
提交 | 用户 | age
385e2d 1 package com.yc.crm.mail.action;
X 2
3 import com.yc.crm.mail.entity.T482102Entity;
4 import com.yc.crm.mail.entity.t482101HEntity;
428443 5 import com.yc.crm.mail.entity.t482101HList;
385e2d 6 import com.yc.crm.mail.service.MailAccountIfc;
X 7 import com.yc.crm.mail.service.MailIfc;
8 import com.yc.crm.mail.util.AllBackMsg;
9 import com.yc.entity.DataSourceEntity;
10 import com.yc.multiData.MultiDataSource;
11 import com.yc.multiData.SpObserver;
12 import com.yc.utils.SessionKey;
13 import org.apache.commons.lang3.StringUtils;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.web.bind.annotation.*;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import javax.servlet.http.HttpSession;
f5cc47 20 import java.util.ArrayList;
X 21 import java.util.HashMap;
385e2d 22 import java.util.List;
f5cc47 23 import java.util.Map;
385e2d 24 import java.util.regex.Pattern;
X 25
26 /**
27  * @BelongsProject: eCoWorksV3
28  * @BelongsPackage: com.yc.crm.mail.action
29  * @author: xinyb
30  * @CreateTime: 2024-08-07  17:30
31  * @Description: 邮箱发送 收件
32  */
33 @CrossOrigin
34 @RestController
f5cc47 35 @RequestMapping("/crm/mail")
385e2d 36 public class MailController {
X 37     @Autowired
38     MailAccountIfc emailAccountIfc;
39     @Autowired
40     MailIfc emailIfc;
41
42     private static final Pattern EMAIL_PATTERN =
43             Pattern.compile("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$");
44
45
46     /**
47      * 获取邮件列表(包括草稿箱,收件箱)
48      *
49      * @return
50      */
51     @GetMapping("/getMailList.do")
52     public AllBackMsg getMailList(String mail, @RequestParam(defaultValue = "1") Integer mailType, boolean isNoRead, HttpServletRequest request, HttpServletResponse response) throws Exception {
53         AllBackMsg msg = new AllBackMsg();
54         try {
55             if (StringUtils.isNotBlank(mail)) {
56                 if (!EMAIL_PATTERN.matcher(mail).matches()) {
57                     msg.setFail("邮箱的各式不争取");
58                     return msg;
59                 }
60             }
61             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
62             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
428443 63             List<t482101HList> t482101HEntityList = emailIfc.getReceivingMailList(mail, mailType, isNoRead);
385e2d 64             if (t482101HEntityList.size() > 0) {
f5cc47 65                 msg.setSuccess("执行完成", t482101HEntityList);
385e2d 66             }
X 67         } catch (Exception e) {
f5cc47 68             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 69         } finally {
X 70             SpObserver.setDBtoInstance();
71         }
72         return msg;
73     }
74
75     /**
76      * 获取邮件详情
77      *
78      * @return
79      */
80     @GetMapping("/getMailInfo.do")
f4c162 81     public AllBackMsg getMailInfo(String docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
385e2d 82         AllBackMsg msg = new AllBackMsg();
X 83         try {
84             if (StringUtils.isBlank(docCode)) {//获取不到当前用户直接结束
85                 msg.setFail("缺少邮箱唯一码docCode,请检查");
86                 return msg;
87             }
88             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
89             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
f4c162 90             t482101HEntity t482101HEntity = emailIfc.getReceivingMailInfo(docCode);
385e2d 91             if (t482101HEntity != null) {
f5cc47 92                 msg.setSuccess("执行完成", t482101HEntity);
385e2d 93             }
X 94         } catch (Exception e) {
f5cc47 95             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 96         } finally {
X 97             SpObserver.setDBtoInstance();
98         }
99         return msg;
100     }
101
102     /**
f4c162 103      * 删除邮件
385e2d 104      *
X 105      * @return
106      */
f4c162 107     @PostMapping("/deleteReceivingMail.do")
X 108     public AllBackMsg deleteReceivingMail(@RequestBody List<String> docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
385e2d 109         AllBackMsg msg = new AllBackMsg();
X 110         try {
f4c162 111             if (docCode == null && docCode.size() == 0) {//获取不到当前用户直接结束
X 112                 msg.setFail("请选中删除的邮件");
385e2d 113                 return msg;
X 114             }
f4c162 115             String arrayCode = StringUtils.join(docCode, ",");
f5cc47 116             HttpSession session = request.getSession();
X 117             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
118             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
119                 msg.setFail("获取不到用户信息");
120                 return msg;
121             }
385e2d 122             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
X 123             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
f4c162 124             Integer cont = emailIfc.deleteReceivingMail(userCode, arrayCode);
385e2d 125             if (cont > 0) {
f5cc47 126                 msg.setOk("邮件已删除");
385e2d 127             }
X 128         } catch (Exception e) {
f5cc47 129             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 130         } finally {
X 131             SpObserver.setDBtoInstance();
132         }
133         return msg;
134     }
135
136     /**
137      * 保存草稿箱(保存或修改)
138      *
139      * @return
140      */
141     @PostMapping("/saveMailDrafts.do")
142     public AllBackMsg saveMailDrafts(@RequestBody t482101HEntity t482101H, HttpServletRequest request, HttpServletResponse response) throws Exception {
143         AllBackMsg msg = new AllBackMsg();
144         try {
145             HttpSession session = request.getSession();
146             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
147             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
148                 msg.setFail("获取不到用户信息");
149                 return msg;
150             }
151             if (StringUtils.isNotBlank(t482101H.getSender())) {
152                 if (!EMAIL_PATTERN.matcher(t482101H.getSender()).matches()) {
153                     msg.setFail("发送人邮箱:" + t482101H.getSender() + "的格式不正确");
154                     return msg;
155                 }
156             }
f4c162 157             if (t482101H.getReceiver()!=null && t482101H.getReceiver().size()>0) {
X 158                 for (String mail : t482101H.getReceiver()) {
159                     if (!EMAIL_PATTERN.matcher(mail).matches()) {
160                         msg.setFail("收件人邮箱:" + mail + "的格式不正确");
161                         return msg;
162                     }
385e2d 163                 }
f4c162 164
385e2d 165             }
X 166             String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID);
167             String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME);
168             String userName = (String) session.getAttribute(SessionKey.USER_NAME);
169             t482101H.setCompanyId(companyId);
170             t482101H.setCompanyName(companyName);
171             t482101H.setUserCode(userCode);
172             t482101H.setUserName(userName);
173             t482101H.setMailType(0);//草稿
174             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
175             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
176             t482101H = emailIfc.saveReceivingMail(t482101H);
f5cc47 177             Map<String, Object> map = new HashMap<>();
X 178             map.put("docCode", t482101H.getDocCode());
179             msg.setSuccess("执行成功", map);
385e2d 180         } catch (Exception e) {
f5cc47 181             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 182         } finally {
X 183             SpObserver.setDBtoInstance();
184         }
185         return msg;
186     }
187
188     /**
189      * 删除草稿箱
190      *
191      * @return
192      */
f4c162 193     @PostMapping("/deleteMailDrafts.do")
X 194     public AllBackMsg deleteMailDrafts(@RequestBody List<String> docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
385e2d 195         AllBackMsg msg = new AllBackMsg();
X 196         try {
f4c162 197             if (docCode == null && docCode.size() == 0) {//获取不到当前用户直接结束
X 198                 msg.setFail("请选中删除的邮件");
199                 return msg;
200             }
201             String arrayCode=StringUtils.join(docCode,",");
202             HttpSession session = request.getSession();
203             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
204             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
205                 msg.setFail("获取不到用户信息");
385e2d 206                 return msg;
X 207             }
208             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
209             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
f4c162 210             int cont = emailIfc.deleteMailDrafts(userCode,arrayCode);
385e2d 211             if (cont > 0) {
f5cc47 212                 msg.setOk("删除成功");
385e2d 213             }
X 214         } catch (Exception e) {
f5cc47 215             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 216         } finally {
X 217             SpObserver.setDBtoInstance();
218         }
219         return msg;
220     }
221
222     /**
223      * 发送邮件
224      *
225      * @param request
226      * @param response
227      * @return
228      */
229     @PostMapping("/sendingMail.do")
230     public AllBackMsg sendingMail(@RequestBody t482101HEntity t482101H, HttpServletRequest request, HttpServletResponse response) throws Exception {
231         AllBackMsg msg = new AllBackMsg();
232         try {
233             HttpSession session = request.getSession();
234             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
235             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
236                 msg.setFail("获取不到用户信息");
237                 return msg;
238             }
239             if (StringUtils.isNotBlank(t482101H.getSender())) {
240                 if (!EMAIL_PATTERN.matcher(t482101H.getSender()).matches()) {
241                     msg.setFail("发送人邮箱:" + t482101H.getSender() + "的格式不正确");
242                     return msg;
243                 }
244             }
f4c162 245             if (t482101H.getReceiver()!=null && t482101H.getReceiver().size()>0) {
X 246                 for (String mail : t482101H.getReceiver()) {
247                     if (!EMAIL_PATTERN.matcher(mail).matches()) {
248                         msg.setFail("收件人邮箱:" + mail + "的格式不正确");
249                         return msg;
250                     }
385e2d 251                 }
f4c162 252
385e2d 253             }
X 254             String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID);
255             String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME);
256             String userName = (String) session.getAttribute(SessionKey.USER_NAME);
257             t482101H.setCompanyId(companyId);
258             t482101H.setCompanyName(companyName);
259             t482101H.setUserCode(userCode);
260             t482101H.setUserName(userName);
261             t482101H.setMailType(2);//发件
262             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
263             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
264             //发送
265             emailIfc.sendingMail(t482101H, request);
f5cc47 266             msg.setOk("发送成功");
385e2d 267         } catch (Exception e) {
f5cc47 268             String error = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
X 269             String[] arrayError = error.split("#");
270             if (arrayError.length == 2) {
271                 Map<String, Object> map = new HashMap<>();
272                 map.put("docCode", arrayError[1]);
273                 msg.setError(arrayError[0], map);
274             } else {
275                 msg.setFail(error);
276             }
385e2d 277         } finally {
X 278             SpObserver.setDBtoInstance();
279         }
280         return msg;
281     }
282
283
284     /**
285      * 接收邮件内容
286      *
287      * @param request
288      * @param response
289      * @return
290      */
291     @GetMapping("/receive.do")
292     public AllBackMsg getReceiveMail(String mail, HttpServletRequest request, HttpServletResponse response) throws Exception {
293         AllBackMsg msg = new AllBackMsg();
294         try {
295             HttpSession session = request.getSession();
296             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
297             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
298                 msg.setFail("获取不到用户信息");
299                 return msg;
300             }
301             if (StringUtils.isNotBlank(mail)) {
302                 if (!EMAIL_PATTERN.matcher(mail).matches()) {
303                     msg.setFail("请输入有效的邮箱");
304                     return msg;
305                 }
306             }
307             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
308             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
309             //根据当前用户查询绑定的邮箱信息
310             List<T482102Entity> emailEntities = emailAccountIfc.getAccount(userCode, mail);//返回邮箱的账号信息
311             if (emailEntities.size() > 0) {
f5cc47 312                 List<t482101HEntity> mails = new ArrayList<>();
385e2d 313                 for (T482102Entity e : emailEntities) {//遍历用户绑定邮箱
f5cc47 314                     //收邮件信息
X 315                     mails.addAll(emailIfc.getMailReceiving(e, request));
385e2d 316                 }
f5cc47 317                 if (mails.size() > 0) {
117621 318                     emailIfc.saveReceivingMailList(mails);//保存
f5cc47 319                     msg.setOk("保存成功");
X 320                 } else {
321                     msg.setOk("未发现新邮件");
322                 }
385e2d 323             } else {
X 324                 msg.setFail("没有找到绑定的邮箱信息");
325             }
326         } catch (Exception e) {
f5cc47 327             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
385e2d 328         } finally {
X 329             SpObserver.setDBtoInstance();
330         }
331         return msg;
332     }
f4c162 333
X 334     /**
335      * 标记已读
336      *
337      * @param request
338      * @param response
339      * @return
340      */
341     @PostMapping("/updateRead.do")
342     public AllBackMsg updateRead(@RequestBody List<String> docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
343         AllBackMsg msg = new AllBackMsg();
344         try {
345             HttpSession session = request.getSession();
346             String userCode = (String) session.getAttribute(SessionKey.USERCODE);//当前登录用户
347             if (StringUtils.isBlank(userCode)) {//获取不到当前用户直接结束
348                 msg.setFail("获取不到用户信息");
349                 return msg;
350             }
351             if (docCode == null && docCode.size() == 0) {
352                 msg.setFail("请选择标记邮件");
353                 return msg;
354             }
355             String arrayCode = StringUtils.join(docCode, ",");
356             DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
357             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
358             emailIfc.updateRead(arrayCode);
359             msg.setOk("标记成功");
360         } catch (Exception e) {
361             msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
362         } finally {
363             SpObserver.setDBtoInstance();
364         }
365         return msg;
366     }
367
385e2d 368 }