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.List;
|
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 {
|
// 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<t482101HEntity> t482101HEntityList = emailIfc.getReceivingMailList(mail, mailType, isNoRead);
|
if (t482101HEntityList.size() > 0) {
|
msg.setSuccess("操作完成", t482101HEntityList);
|
}
|
} catch (Exception e) {
|
msg.setError("执行出错", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
|
} finally {
|
SpObserver.setDBtoInstance();
|
}
|
return msg;
|
}
|
|
/**
|
* 获取邮件详情
|
*
|
* @return
|
*/
|
@GetMapping("/getMailInfo.do")
|
public AllBackMsg getMailInfo(String mail, 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;
|
}
|
if (StringUtils.isNotBlank(mail)) {
|
if (!EMAIL_PATTERN.matcher(mail).matches()) {
|
msg.setFail("邮箱的格式不正确");
|
return msg;
|
}
|
}
|
DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request);//获取数据源信息
|
SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源
|
t482101HEntity t482101HEntity = emailIfc.getReceivingMailInfo(mail, docCode);
|
if (t482101HEntity != null) {
|
msg.setSuccess("操作完成", t482101HEntity);
|
}
|
} catch (Exception e) {
|
msg.setError("执行出错", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
|
} finally {
|
SpObserver.setDBtoInstance();
|
}
|
return msg;
|
}
|
|
/**
|
* 获取邮件详情
|
*
|
* @return
|
*/
|
@GetMapping("/deleteReceivingMail.do")
|
public AllBackMsg deleteReceivingMail(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());//切换数据源
|
Integer cont = emailIfc.deleteReceivingMail(docCode);
|
if (cont > 0) {
|
msg.setSuccess("操作完成", "邮件已删除");
|
}
|
} catch (Exception e) {
|
msg.setError("执行出错", 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 (StringUtils.isNotBlank(t482101H.getReceiver())) {
|
if (!EMAIL_PATTERN.matcher(t482101H.getReceiver()).matches()) {
|
msg.setFail("收件人邮箱:" + t482101H.getReceiver() + "的格式不正确");
|
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);
|
msg.setSuccess("操作完成", "执行成功");
|
} catch (Exception e) {
|
msg.setError("执行出错", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
|
} finally {
|
SpObserver.setDBtoInstance();
|
}
|
return msg;
|
}
|
|
/**
|
* 删除草稿箱
|
*
|
* @return
|
*/
|
@GetMapping("/deleteMailDrafts.do")
|
public AllBackMsg deleteMailDrafts(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());//切换数据源
|
int cont = emailIfc.deleteMailDrafts(docCode);
|
if (cont > 0) {
|
msg.setSuccess("操作完成", "删除成功");
|
}
|
} catch (Exception e) {
|
msg.setError("执行出错", 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 (StringUtils.isNotBlank(t482101H.getReceiver())) {
|
if (!EMAIL_PATTERN.matcher(t482101H.getReceiver()).matches()) {
|
msg.setFail("收件人邮箱:" + t482101H.getReceiver() + "的格式不正确");
|
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());//切换数据源
|
//有发送的邮件保存到后台数据库
|
t482101H = emailIfc.saveReceivingMail(t482101H);
|
//发送
|
emailIfc.sendingMail(t482101H, request);
|
msg.setSuccess("执行成功", "发送成功");
|
} catch (Exception e) {
|
msg.setError("执行出错", 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 = emailAccountIfc.getAccount(userCode, mail);//返回邮箱的账号信息
|
if (emailEntities.size() > 0) {
|
for (T482102Entity e : emailEntities) {//遍历用户绑定邮箱
|
List<t482101HEntity> mails = emailIfc.getMailReceiving(e, request);
|
emailIfc.saveReceivingMailList(mails);//保存
|
}
|
msg.setSuccess("操作完成", "保存成功");
|
} else {
|
msg.setFail("没有找到绑定的邮箱信息");
|
}
|
} catch (Exception e) {
|
msg.setError("执行出错", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
|
} finally {
|
SpObserver.setDBtoInstance();
|
}
|
return msg;
|
}
|
}
|