xinyb
2024-09-19 1956855b43808d81f246d9570d477ac1cbc404b7
src/com/yc/crm/mail/action/MailController.java
@@ -1,20 +1,24 @@
package com.yc.crm.mail.action;
import com.yc.crm.mail.entity.MailModuleEntity;
import com.yc.crm.mail.entity.T482102Entity;
import com.yc.crm.mail.entity.t482101HEntity;
import com.yc.crm.mail.entity.t482101HList;
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;
@@ -35,10 +39,17 @@
@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}$");
@@ -77,7 +88,8 @@
     * @return
     */
    @GetMapping("/getMailList.do")
    public AllBackMsg getMailList(String mail, @RequestParam(defaultValue = "1") Integer mailType, boolean isNoRead, HttpServletRequest request, HttpServletResponse response) throws Exception {
    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)) {
@@ -94,7 +106,43 @@
            }
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
            List<t482101HList> t482101HEntityList = emailIfc.getReceivingMailList(mail, mailType, isNoRead, userCode);
            List<t482101HList> t482101HEntityList = emailIfc.getReceivingMailList(mail, mailType, isNoRead, userCode,page,limit);
            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("/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);
            if (t482101HEntityList.size() > 0) {
                msg.setSuccess("执行完成", t482101HEntityList);
            }
@@ -123,6 +171,17 @@
            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) {
@@ -158,6 +217,52 @@
            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());
@@ -206,7 +311,7 @@
            t482101H.setUserName(userName);
            t482101H.setMailType(0);//草稿
            t482101H.setReadFlag(1);//已读
            t482101H.setAttachmentList(t482101H.getFileUNID());
            t482101H.setAttachmentList(t482101H.getAttachmentList());
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
            t482101H = emailIfc.saveReceivingMail(t482101H);
@@ -221,42 +326,66 @@
        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.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;
//    }
    /**
     * 发送邮件
     * 收邮件
     *
     * @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
@@ -295,70 +424,25 @@
            t482101H.setUserCode(userCode);
            t482101H.setUserName(userName);
            t482101H.setMailType(2);//发件
            t482101H.setAttachmentList(t482101H.getFileUNID());
            t482101H.setAttachmentList(t482101H.getAttachmentList());
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
            //发送
            emailIfc.sendingMail(t482101H, request);
            mailServiceIfc.sendEmails(t482101H, request);
            msg.setOk("发送成功");
        } catch (Exception e) {
        } catch (MessagingException 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);
            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);
            }
        } 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);//返回邮箱的账号信息
            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());
@@ -376,7 +460,7 @@
     * @return
     */
    @PostMapping("/updateRead.do")
    public AllBackMsg updateRead(@RequestBody List<String> docCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
    public AllBackMsg updateRead(@RequestBody Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws Exception {
        AllBackMsg msg = new AllBackMsg();
        try {
            HttpSession session = request.getSession();
@@ -385,14 +469,20 @@
                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);
            emailIfc.updateRead(arrayCode, read);
            msg.setOk("标记成功");
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
@@ -402,4 +492,63 @@
        return msg;
    }
    /**
     * 待处理邮件
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/updateHandle.do")
    public AllBackMsg updateHandle(String handleTime, 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 (StringUtils.isBlank(docCode)) {
                msg.setFail("邮件唯一标识docCode不能空");
                return msg;
            }
            DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
            SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
            emailIfc.updateMailHandle(handleTime, 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();
            }
        }
    }
}