feature 1.邮箱采集功能完善

release
zhenghaiyang@ele-cloud.com 2 years ago
parent 37d3cf59f6
commit 6e1efb70f2
  1. 16
      dxhy-common/src/main/java/com/dxhy/common/util/MD5.java
  2. 49
      dxhy-core/src/main/java/com/dxhy/core/model/mailGather/OcrResultToyxVo.java
  3. 97
      dxhy-core/src/main/java/com/dxhy/core/task/SnEmailGatherTask.java
  4. 107
      dxhy-core/src/main/java/com/dxhy/core/task/SnEmailGatherTaskTest.java

@ -63,6 +63,22 @@ public class MD5 {
return new String(str);
}
public static String getMd5Encode(String key) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(key.getBytes(StandardCharsets.UTF_8));
byte[] digest = messageDigest.digest();
int j = digest.length;
char[] str = new char[j * 2];
int k = 0;
for (byte byte0 : digest) {
str[k++] = hex[byte0 >>> 4 & 0xf];
str[k++] = hex[byte0 & 0xf];
}
return new String(str);
}
static final char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
static final char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
}

@ -4,41 +4,16 @@ import lombok.Data;
@Data
public class OcrResultToyxVo {
//序号
private String ZNO;
//公司代码
private String ZBUKRS;
//提交人账号
private String ZUSER;
//文件名
private String ZNAME;
//文件内容(非结构化数据)
private String ZCONTENT;
//文件上传日期
private String ZFILEDATE;
//是否为发票
private String ZFLAG;
//发票号码
private String ZFPHM;
//发票代码
private String ZFPDM;
//发票类型
private String ZFPLX;
//发票日期
private String ZKPRQ;
//发票含税金额
private String ZHSJE;
//发票购方名称
private String ZGFMC;
//发票销方名称
private String ZXFMC;
//发票图片内容(非结构化数据)
private String ZFPFILE;
//发票图片URL
private String ZFPURL;
//来源邮箱地址
private String ZFMAIL;
//数据来源系统
private String ZLYXT;
private String companyId; // 公司代码
private String source; // 来源
private String InvoiceCode; // 发票代码
private String InvoiceNumber; // 发票号码
private String totalAmount; // 合计金额
private String buyerName; // 购买方名称
private String sellerName; // 销售方名称
private String fileName; // 文件名称
private String fileType; // 文件类型
private String fileByte; //Byte[]; 文件流
private String email; // 邮箱
private String employeeId; // 员工号
}

@ -3,20 +3,28 @@ package com.dxhy.core.task;
import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.util.MD5;
import com.dxhy.core.job.entity.ScheduleJobEntity;
import com.dxhy.core.job.service.ScheduleJobService;
import com.dxhy.core.model.mailGather.EmailMaintainVo;
import com.dxhy.core.model.mailGather.MailGatherLogVo;
import com.dxhy.core.model.mailGather.OcrResultToyxVo;
import com.dxhy.core.model.openservice.OpenServiceOcr;
import com.dxhy.core.service.mailGather.EmailMaintainService;
import com.dxhy.core.service.mailGather.MailGatherLogService;
import com.dxhy.core.service.openservice.IOpenServicePlatformService;
import com.dxhy.core.service.openservice.impl.OpenServicePlatformServiceImpl;
import com.dxhy.core.util.EmailParseUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sun.mail.pop3.POP3Folder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -24,6 +32,7 @@ import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Map;
@ -42,6 +51,10 @@ public class SnEmailGatherTask {
private MailGatherLogService mailGatherLogService;
@Resource
private IOpenServicePlatformService openServicePlatformService;
@Value("${snyx.salt:}")
private String snyxSalt;
@Value("${snyx.pjurl:}")
private String snyxPjurl;
public void mailGatherTask(){
DynamicContextHolder.push(DbConstant.BASICS_READ);
@ -49,6 +62,7 @@ public class SnEmailGatherTask {
if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
try {
log.info("邮箱采集接口开始--");
scheduleJobEntity.setJobStatus("1");
DynamicContextHolder.push(DbConstant.BASICS_WRITE);
scheduleJobService.updateById(scheduleJobEntity);
@ -136,29 +150,43 @@ public class SnEmailGatherTask {
if(isContainerAttachment){
List<Map<String, String>> mapList = Lists.newArrayList();
EmailParseUtils.saveAttachment(msg, mapList);
for (Map<String, String> map : mapList) {
map.put("emailName",emailName);
String pdfStream = map.get("pdfStream");
if(StringUtils.isNotBlank(pdfStream)){
List<OpenServiceOcr> openServiceOcrs = OpenServicePlatformServiceImpl.OcrToSnTest(pdfStream);
if(openServiceOcrs != null && !openServiceOcrs.isEmpty()){
Map<String, Object> ocrResult = convertToOcrResult(openServiceOcrs, map, maintainVo);
try {
String md5Encode = MD5.getMd5Encode(ocrResult.get("batchId") + snyxSalt);
Map<String, String> hashMap = Maps.newHashMap();
hashMap.put("Authorization",md5Encode);
log.info("调用影像票夹接口,调用地址:{},请求头:{},请求参数:{}",snyxPjurl,snyxPjurl,JSONObject.toJSONString(ocrResult));
String body = HttpRequest.post(snyxPjurl).addHeaders(hashMap).body(JSONObject.toJSONString(ocrResult)).timeout(300000).execute().body();
log.info("d调用影像票夹接口返回参数:{}",body);
Map resultMap = JSONObject.parseObject(body, Map.class);
Object status = resultMap.get("status");
if(status != null && "200".equals(String.valueOf(status))){
}else {
gatherLogVo.setFphm("");
gatherLogVo.setOcrType("0");
gatherLogVo.setErrorMsg("推送影像票夹失败");
mailGatherLogService.insert(gatherLogVo);
break;
}
System.out.println(body);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
}
// gatherLogVo.setFileType(map.get("contentType"));
// gatherLogVo.setFileName(map.get("fileName"));
// String pdfStream = map.get("pdfStream");
// if(StringUtils.isNotBlank(pdfStream)){
// List<OpenServiceOcr> openServiceOcrs = openServicePlatformService.ocrInvoice(pdfStream);
// if(openServiceOcrs != null && !openServiceOcrs.isEmpty()){
// openServiceOcrs.get(0).getData().getFPDM();
// gatherLogVo.setFpdm( openServiceOcrs.get(0).getData().getFPDM());
// gatherLogVo.setFphm( openServiceOcrs.get(0).getData().getFPHM());
// } else {
//
// }
// }
}else { //无附件的不处理
continue;
}
// Flags flags = msg.getFlags();
// if (!flags.contains(Flags.Flag.SEEN)) {
// //设置为已读
// msg.setFlag(Flags.Flag.SEEN, true);
// }
//进行数据插入
if(StringUtils.isNotBlank(gatherLogVo.getFpdm()) && StringUtils.isNotBlank(gatherLogVo.getFphm())){
gatherLogVo.setOcrType("0");
@ -179,6 +207,37 @@ public class SnEmailGatherTask {
}
}
public static Map<String, Object> convertToOcrResult(List<OpenServiceOcr> openServiceOcrs, Map<String, String> map,EmailMaintainVo maintainVo){
List<OcrResultToyxVo> resultToyxVoList = Lists.newArrayList();
Map<String, Object> hashMap = Maps.newHashMap();
String batchId = String.valueOf(System.currentTimeMillis());
hashMap.put("batchId",batchId);
for (int i = 0; i < openServiceOcrs.size(); i++) {
OpenServiceOcr openServiceOcr = openServiceOcrs.get(i);
OpenServiceOcr.InvoiceOcrData invoiceOcrData = openServiceOcr.getData();
OcrResultToyxVo resultToyxVo = new OcrResultToyxVo();
resultToyxVo.setCompanyId(maintainVo.getCompanyCode());
resultToyxVo.setSource("01");
resultToyxVo.setInvoiceCode(invoiceOcrData.getFPDM());
resultToyxVo.setInvoiceNumber(invoiceOcrData.getFPHM());
resultToyxVo.setTotalAmount(invoiceOcrData.getJSHJ());
resultToyxVo.setBuyerName(invoiceOcrData.getGMFMC());
resultToyxVo.setSellerName(invoiceOcrData.getXHFMC());
resultToyxVo.setFileName(map.get("fileName"));
resultToyxVo.setFileType("102");
resultToyxVo.setFileByte(map.get("pdfStream"));
resultToyxVo.setEmail(map.get("emailName"));
resultToyxVo.setEmployeeId(maintainVo.getUserId());
resultToyxVoList.add(resultToyxVo);
}
hashMap.put("list",resultToyxVoList);
return hashMap;
}
public Store mailAuth(String emailName, String password){
//判断是否为QQ还是163
//邮件接收协议

@ -1,21 +1,26 @@
package com.dxhy.core.task;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.util.MD5;
import com.dxhy.core.model.mailGather.OcrResultToyxVo;
import com.dxhy.core.model.openservice.OpenServiceOcr;
import com.dxhy.core.service.openservice.impl.OpenServicePlatformServiceImpl;
import com.dxhy.core.util.EmailParseUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sun.mail.pop3.POP3Folder;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
class SnEmailGatherTaskTest {
@ -96,21 +101,40 @@ class SnEmailGatherTaskTest {
if(StringUtils.isNotBlank(pdfStream)){
List<OpenServiceOcr> openServiceOcrs = OpenServicePlatformServiceImpl.OcrToSnTest(pdfStream);
if(openServiceOcrs != null && !openServiceOcrs.isEmpty()){
convertToOcrResult(openServiceOcrs,map);
Map<String, Object> ocrResult = convertToOcrResult(openServiceOcrs, map);
String salt = "e4b0190b2fadc0adbe54471ffd79a729";
try {
String md5Encode = MD5.getMd5Encode(ocrResult.get("batchId") + salt);
Map<String, String> hashMap = Maps.newHashMap();
hashMap.put("Authorization",md5Encode);
String body = HttpRequest.post("http://192.168.33.105:2333/ticket/receive").addHeaders(hashMap).body(JSONObject.toJSONString(ocrResult)).timeout(300000).execute().body();
Map resultMap = JSONObject.parseObject(body, Map.class);
Object status = resultMap.get("status");
if(status != null && "200".equals(String.valueOf(status))){
}
System.out.println(body);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
System.out.println("");
}
}
POP3Folder inbox = (POP3Folder) folder;
String uid = inbox.getUID(msg);
System.out.println("uid: ------------------" + uid);
Flags flags = msg.getFlags();
if (!flags.contains(Flags.Flag.SEEN)) {
//设置为已读
msg.setFlag(Flags.Flag.SEEN, true);
}
// POP3Folder inbox = (POP3Folder) folder;
// String uid = inbox.getUID(msg);
// System.out.println("uid: ------------------" + uid);
// Flags flags = msg.getFlags();
// if (!flags.contains(Flags.Flag.SEEN)) {
// //设置为已读
// msg.setFlag(Flags.Flag.SEEN, true);
// }
System.out.println("");
}
System.out.println("");
@ -131,36 +155,35 @@ class SnEmailGatherTaskTest {
}
}
public static List<OcrResultToyxVo> convertToOcrResult(List<OpenServiceOcr> openServiceOcrs, Map<String, String> map){
public static Map<String, Object> convertToOcrResult(List<OpenServiceOcr> openServiceOcrs, Map<String, String> map){
List<OcrResultToyxVo> resultToyxVoList = Lists.newArrayList();
for (int i = 0; i < openServiceOcrs.size(); i++) {
Map<String, Object> hashMap = Maps.newHashMap();
String batchId = String.valueOf(System.currentTimeMillis());
hashMap.put("batchId",batchId);
for (int i = 0; i < openServiceOcrs.size(); i++) {
OpenServiceOcr openServiceOcr = openServiceOcrs.get(i);
OpenServiceOcr.InvoiceOcrData invoiceOcrData = openServiceOcr.getData();
OcrResultToyxVo resultToyxVo = new OcrResultToyxVo();
resultToyxVo.setZNO(i+1+"");
resultToyxVo.setZBUKRS("");
resultToyxVo.setZUSER("");
resultToyxVo.setZNAME(map.get("fileName"));
resultToyxVo.setZCONTENT(openServiceOcr.getIMAGE());
resultToyxVo.setZFILEDATE(new DateTime().toString());
if(StringUtils.isNotBlank(invoiceOcrData.getFPHM())){
resultToyxVo.setZFLAG("X");
}
resultToyxVo.setZFPHM(invoiceOcrData.getFPHM());
resultToyxVo.setZFPDM(invoiceOcrData.getFPDM());
Map<String, String> invoiceType = convertInvoiceType();
resultToyxVo.setZFPLX(invoiceType.get(""));
resultToyxVo.setZKPRQ(invoiceOcrData.getKPRQ());
resultToyxVo.setZHSJE(invoiceOcrData.getJSHJ());
resultToyxVo.setZGFMC(invoiceOcrData.getGMFMC());
resultToyxVo.setZXFMC(invoiceOcrData.getXHFMC());
resultToyxVo.setZFPFILE("");
resultToyxVo.setZFPURL("");
resultToyxVo.setZFMAIL(map.get("emailName"));
resultToyxVo.setZLYXT("");
resultToyxVo.setCompanyId("");
resultToyxVo.setSource("01");
resultToyxVo.setInvoiceCode(invoiceOcrData.getFPDM());
resultToyxVo.setInvoiceNumber(invoiceOcrData.getFPHM());
resultToyxVo.setTotalAmount(invoiceOcrData.getJSHJ());
resultToyxVo.setBuyerName(invoiceOcrData.getGMFMC());
resultToyxVo.setSellerName(invoiceOcrData.getXHFMC());
resultToyxVo.setFileName(map.get("fileName"));
resultToyxVo.setFileType("102");
resultToyxVo.setFileByte(map.get("pdfStream"));
resultToyxVo.setEmail(map.get("emailName"));
resultToyxVo.setEmployeeId("");
resultToyxVoList.add(resultToyxVo);
}
return resultToyxVoList;
hashMap.put("list",resultToyxVoList);
return hashMap;
}
public static Map<String, String> convertInvoiceType(){
@ -198,4 +221,16 @@ class SnEmailGatherTaskTest {
@Test
void mailAuth() {
}
public static void main(String[] args) throws NoSuchAlgorithmException {
String value = String.valueOf(System.currentTimeMillis());
System.out.println(value);
String str = "1680510212905" + "e4b0190b2fadc0adbe54471ffd79a729";
String encode = MD5.getMd5Encode(str);
if("acec779e6689227e4ede6d1a513d94b0".equals(encode)){
System.out.println("");
}
cn.hutool.crypto.digest.MD5 md5 = cn.hutool.crypto.digest.MD5.create();
System.out.println();
}
}
Loading…
Cancel
Save