银行回单上传

new
parent fb32fbf24b
commit 54a416f925

@ -0,0 +1 @@
ALTER TABLE archives_bank_slip ADD COLUMN `file_url` varchar(64) NULL DEFAULT NULL COMMENT '';

@ -1,13 +1,17 @@
package cn.iocoder.yudao.module.accounting.controller.admin.bankslip;
import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.*;
import cn.iocoder.yudao.module.accounting.convert.bankslip.BankSlipConvert;
import cn.iocoder.yudao.module.accounting.dal.dataobject.bankslip.BankReceiptDO;
import cn.iocoder.yudao.module.accounting.dal.dataobject.bankslip.BankSlipDO;
import cn.iocoder.yudao.module.accounting.service.bankslip.BankSlipService;
import cn.iocoder.yudao.module.bs.utils.BaiduOcrHandler;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.setting.service.passwords.PasswordsService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
@ -16,13 +20,18 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.DataInput;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
@ -38,6 +47,7 @@ public class BankSlipController {
@Resource
private PasswordsService passwordsService;
@PostMapping("/create")
@Operation(summary = "创建银行回单")
@PreAuthorize("@ss.hasPermission('archives:bank-slip:create')")
@ -118,4 +128,33 @@ public class BankSlipController {
ExcelUtils.write(response, "银行回单.xls", "数据", BankSlipExcelVO.class, datas);
}
@PostMapping("/bankReceipt/identify")
@Operation(summary = "银行回单识别")
public CommonResult identify(@RequestParam("multipartFile") MultipartFile multipartFile) throws Exception {
String imageBase64Param = BaiduOcrHandler.getImageBase64Param(multipartFile);
String originalFileName = multipartFile.getOriginalFilename();
int lastDotIndex = originalFileName.lastIndexOf(".");
String fileExtension = originalFileName.substring(lastDotIndex + 1);
JSONObject ocrResult = null;
//发票识别
switch (fileExtension.toLowerCase()) {
case "pdf":
ocrResult = BaiduOcrHandler.bankReceiptOcr(imageBase64Param, "pdf");
break;
default:
// 获取文件大小(以字节为单位)
long fileSize = multipartFile.getSize();
// 判断文件大小是否小于6MB
if (fileSize < 6 * 1024 * 1024) {
ocrResult = BaiduOcrHandler.bankReceiptOcr(imageBase64Param, "image");
} else {
return error("图像数据base64编码后进行urlencode要求base64编码和urlencode后大小不超过6M最短边至少15px最长边最大4096px支持jpg/jpeg/png/bmp格式");
}
}
System.out.println(ocrResult);
BankReceiptDO wordsResult = JSONObject.toJavaObject( ocrResult, BankReceiptDO.class);
BankSlipDO bankSlipDO = bankSlipService.jsonToJavaObject(wordsResult, multipartFile);
return success(bankSlipDO);
}
}

@ -145,5 +145,8 @@ public class BankSlipBaseVO {
@Schema(description = "认款状态 0:未认款 1 已认款")
private String subscriptionStatus;
@Schema(description = "银行回单地址")
private String fileUrl;
}

@ -0,0 +1,109 @@
package cn.iocoder.yudao.module.accounting.dal.dataobject.bankslip;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.*;
import java.io.DataInput;
import java.util.List;
/**
*
*/
@Data
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class BankReceiptDO {
private String log_id;
private int words_result_num;
private WordsResult words_result;
@Data
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public static class WordsResult {
private List<PaymentAccount> ;
private List<TransactionDate> ;
private List<String> ;
private List<String> ;
private List<String> ;
private List<String> ;
private List<LowerAmount> ;
private List<PaymentAccount> ;
private List<UpperAmount> ;
private List<String> ;
private List<String> ;
private List<String> ;
private List<String> ;
private List<String> ;
// 添加相应的 getter 和 setter 方法
}
// @Data
// @ToString(callSuper = true)
// @NoArgsConstructor
// @AllArgsConstructor
// public static class WordsResultData {
// private PaymentAccount 付款人账号;
// private TransactionDate 交易日期;
// private String 用途;
// private String 流水号;
// private String 摘要;
// private String 付款人户名;
// private LowerAmount 小写金额;
// private PaymentAccount 收款人账号;
// private UpperAmount 大写金额;
// private String 收款人户名;
// private String 标题;
// private String 回单编号;
// private String 付款人开户银行;
// private String 收款人开户银行;
//
// // 添加相应的 getter 和 setter 方法
// }
@Data
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public static class PaymentAccount {
private String word;
// 添加相应的 getter 和 setter 方法
}
@Data
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public static class TransactionDate {
private String word;
// 添加相应的 getter 和 setter 方法
}
@Data
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public static class LowerAmount {
private String word;
// 添加相应的 getter 和 setter 方法
}
@Data
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public static class UpperAmount {
private String word;
}
}

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import lombok.experimental.Accessors;
import java.time.LocalDateTime;
@ -16,6 +17,7 @@ import java.time.LocalDateTime;
@TableName("archives_bank_slip")
@KeySequence("archives_bank_slip_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@ -204,5 +206,10 @@ public class BankSlipDO extends BaseDO {
*/
private String subscriptionStatus;
/**
*
*/
private String fileUrl;
}

@ -5,9 +5,13 @@ import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.BankSlipC
import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.BankSlipExportReqVO;
import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.BankSlipPageReqVO;
import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.BankSlipUpdateReqVO;
import cn.iocoder.yudao.module.accounting.dal.dataobject.bankslip.BankReceiptDO;
import cn.iocoder.yudao.module.accounting.dal.dataobject.bankslip.BankSlipDO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
@ -79,4 +83,10 @@ public interface BankSlipService {
* @return
*/
PageResult<BankSlipDO> getBankSlipPageTime(Long months);
/**
* json
* @param ocrResult
*/
BankSlipDO jsonToJavaObject(BankReceiptDO ocrResult, MultipartFile multipartFile) throws IOException;
}

@ -1,18 +1,29 @@
package cn.iocoder.yudao.module.accounting.service.bankslip;
import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.BankSlipCreateReqVO;
import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.BankSlipExportReqVO;
import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.BankSlipPageReqVO;
import cn.iocoder.yudao.module.accounting.controller.admin.bankslip.vo.BankSlipUpdateReqVO;
import cn.iocoder.yudao.module.accounting.convert.bankslip.BankSlipConvert;
import cn.iocoder.yudao.module.accounting.dal.dataobject.bankslip.BankReceiptDO;
import cn.iocoder.yudao.module.accounting.dal.dataobject.bankslip.BankSlipDO;
import cn.iocoder.yudao.module.accounting.dal.mysql.bankslip.BankSlipMapper;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.DataInput;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;
@ -32,6 +43,10 @@ public class BankSlipServiceImpl implements BankSlipService {
@Resource
private BankSlipMapper bankSlipMapper;
@Resource
private FileService fileService;
@Override
public Long createBankSlip(BankSlipCreateReqVO createReqVO) {
// 插入
@ -101,4 +116,36 @@ public class BankSlipServiceImpl implements BankSlipService {
pageResult.setTotal(size);
return pageResult;
}
/**
* json
* @param bankReceiptDO
*/
@Override
public BankSlipDO jsonToJavaObject(BankReceiptDO bankReceiptDO, MultipartFile multipartFile) throws IOException {
BankSlipDO bankSlipDO = new BankSlipDO();
String invoiceDateStr = bankReceiptDO.getWords_result().get().get(0).getWord().replace("年", "-").replaceAll("月", "-").replaceAll("日", "").trim();
LocalDate parse = LocalDate.parse(invoiceDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDateTime date = parse.atStartOfDay();;
bankSlipDO.setTradTime(date);
// 将日期字符串解析为 LocalDate 对象
//这里用中文是因为调用百度ocr银行回单的接口返回的数据json都是中文的
bankSlipDO
.setBank(bankReceiptDO.getWords_result().get().get(0))
.setMyCompany(bankReceiptDO.getWords_result().get().get(0))
.setMyNum(bankReceiptDO.getWords_result().get().get(0).getWord())
.setAdverseCompany(bankReceiptDO.getWords_result().get().get(0))
.setAdverseNum(bankReceiptDO.getWords_result().get().get(0).getWord())
.setSerialNum(bankReceiptDO.getWords_result().get().get(0))
.setTradMoney(bankReceiptDO.getWords_result().get().get(0).getWord())
.setReceiptNum(bankReceiptDO.getWords_result().get().get(0))
.setDigest(bankReceiptDO.getWords_result().get().get(0))
.setPurpose(bankReceiptDO.getWords_result().get().get(0))
;
bankSlipDO.setFileUrl(
fileService.createFile(null, null, IoUtil.readBytes(multipartFile.getInputStream())));
return bankSlipDO;
}
}

@ -55,4 +55,9 @@ public interface BaiduOcrConstant {
* ofdpdf
*/
String INVOICEURL_VERIFICATION = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice_verification";
/**
* img,pdf
*/
String BANK_RECEIPT = "https://aip.baidubce.com/rest/2.0/ocr/v1/bank_receipt_new";
}

@ -333,6 +333,34 @@ public class BaiduOcrHandler {
return null;
}
/**
*
* @param baseStr
* @param fileType
* @return
* @throws Exception
*/
public static JSONObject bankReceiptOcr(String baseStr, String fileType) throws Exception {
String accessToken = getAccessToken();
if (StringUtils.isEmpty(accessToken)) {
throw new RuntimeException("获取token失败");
}
String image = "image";
if (fileType.equals("pdf")) {
image = "pdf_file";
}
String param = image + "=" +baseStr;
String jsonStr = HttpUtil.post(BaiduOcrConstant.BANK_RECEIPT, accessToken, param);
JSONObject jsonObject = JSON.parseObject(jsonStr);
if (!jsonObject.containsKey("words_result")) {
log.error("银行回单识别失败"+jsonObject.toString());
throw exception("银行回单识别失败"+jsonObject.toString());
}
return jsonObject;
}
/**
*
* @param baseStr

@ -45,13 +45,13 @@ spring:
datasource:
master:
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3307/lyr-one?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
url: jdbc:mysql://127.0.0.1:3306/lyr-one?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
# url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT # MySQL Connector/J 5.X 连接的示例
# url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.slave.name} # PostgreSQL 连接的示例
# url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
# url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.master.name} # SQLServer 连接的示例
username: root
password: 123456
password: root
# username: sa
# password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
# slave: # 模拟从库,可根据自己需要修改

Loading…
Cancel
Save