image发票识别,发票批量导入

new
JilingLee 1 year ago
parent 040512413e
commit d615ea9aef

@ -3,13 +3,18 @@ package cn.iocoder.yudao.module.accounting.controller.admin.invoices;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.util.FileUtils; import cn.iocoder.yudao.framework.common.util.FileUtils;
import cn.iocoder.yudao.framework.common.util.barcode.BarcodeUtil; import cn.iocoder.yudao.framework.common.util.barcode.BarcodeUtil;
import cn.iocoder.yudao.module.accounting.controller.admin.listener.DemoDataListener;
import cn.iocoder.yudao.module.accounting.controller.admin.listener.InvoicesImportListener;
import cn.iocoder.yudao.module.accounting.dal.dataobject.invoices.BaiduInvoicesDO; import cn.iocoder.yudao.module.accounting.dal.dataobject.invoices.BaiduInvoicesDO;
import cn.iocoder.yudao.module.accounting.dal.dataobject.invoices.Converter; import cn.iocoder.yudao.module.accounting.dal.dataobject.invoices.Converter;
import cn.iocoder.yudao.module.accounting.enums.AccountingStatusEnum; import cn.iocoder.yudao.module.accounting.enums.AccountingStatusEnum;
import cn.iocoder.yudao.module.bs.utils.BaiduOcrHandler; import cn.iocoder.yudao.module.bs.utils.BaiduOcrHandler;
import cn.iocoder.yudao.module.infra.service.file.FileService; import cn.iocoder.yudao.module.infra.service.file.FileService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import liquibase.pro.packaged.A;
import me.zhyd.oauth.log.Log; import me.zhyd.oauth.log.Log;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -23,12 +28,14 @@ import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*; import javax.validation.*;
import javax.servlet.http.*; import javax.servlet.http.*;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.util.*; import java.util.*;
import java.io.IOException; import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
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.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
@ -125,11 +132,23 @@ public class InvoicesController {
String fileExtension = originalFileName.substring(lastDotIndex + 1); String fileExtension = originalFileName.substring(lastDotIndex + 1);
JSONObject ocrResult = null; JSONObject ocrResult = null;
//发票识别 //发票识别
if (fileExtension.equalsIgnoreCase("ofd")) { switch (fileExtension.toLowerCase()) {
case "ofd":
ocrResult = BaiduOcrHandler.invoiceOCR(imageBase64Param, "ofd"); ocrResult = BaiduOcrHandler.invoiceOCR(imageBase64Param, "ofd");
} break;
if (fileExtension.equalsIgnoreCase("pdf")) { case "pdf":
ocrResult = BaiduOcrHandler.invoiceOCR(imageBase64Param, "pdf"); ocrResult = BaiduOcrHandler.invoiceOCR(imageBase64Param, "pdf");
break;
default:
// 获取文件大小(以字节为单位)
long fileSize = multipartFile.getSize();
// 判断文件大小是否小于6MB
if (fileSize < 6 * 1024 * 1024) {
ocrResult = BaiduOcrHandler.invoiceOCR(imageBase64Param, "image");
} else {
return error("图像数据base64编码后进行urlencode要求base64编码和urlencode后大小不超过6M最短边至少15px最长边最大4096px支持jpg/jpeg/png/bmp格式");
}
} }
BaiduInvoicesDO parse = JSONObject.toJavaObject(ocrResult, BaiduInvoicesDO.class); BaiduInvoicesDO parse = JSONObject.toJavaObject(ocrResult, BaiduInvoicesDO.class);
InvoicesCreateReqVO invoicesDO = Converter.convertInvoiceToInvoicesDO(parse); InvoicesCreateReqVO invoicesDO = Converter.convertInvoiceToInvoicesDO(parse);
@ -159,19 +178,18 @@ public class InvoicesController {
queryIn.setInvoiceCode(invoicesDO.getInvoiceCode()); queryIn.setInvoiceCode(invoicesDO.getInvoiceCode());
queryIn.setInvoiceNum(invoicesDO.getInvoiceNum()); queryIn.setInvoiceNum(invoicesDO.getInvoiceNum());
List<InvoicesDO> invoicesList = invoicesService.getInvoicesList(queryIn); List<InvoicesDO> invoicesList = invoicesService.getInvoicesList(queryIn);
if (invoicesList.size()>0){ if (invoicesList.size() > 0) {
invoicesDO.setDuplicateMark(Byte.valueOf(AccountingStatusEnum.DUPLICATE_INVOICE.getValue())); invoicesDO.setDuplicateMark(Byte.valueOf(AccountingStatusEnum.DUPLICATE_INVOICE.getValue()));
}else { } else {
invoicesDO.setDuplicateMark(Byte.valueOf(AccountingStatusEnum.UN_DUPLICATE_INVOICE.getValue())); invoicesDO.setDuplicateMark(Byte.valueOf(AccountingStatusEnum.UN_DUPLICATE_INVOICE.getValue()));
} }
String url=""; String url = "";
if (fileExtension.equalsIgnoreCase("ofd")) { if (fileExtension.equalsIgnoreCase("ofd")) {
url = getUrl(multipartFile, UUID.randomUUID()+".ofd", "ofd"); url = getUrl(multipartFile, UUID.randomUUID() + ".ofd", "ofd");
}else { } else {
url = getUrl(multipartFile, "other", "other"); url = getUrl(multipartFile, "other", "other");
} }
invoicesDO.setFileUrl(url); invoicesDO.setFileUrl(url);
invoicesService.createInvoices(invoicesDO);
return success(invoicesDO); return success(invoicesDO);
} }
@ -189,15 +207,53 @@ public class InvoicesController {
return success(jsonObjectVer); return success(jsonObjectVer);
} }
@GetMapping("/outTemplate")
@Operation(summary = "导出发票 Excel模板")
@PreAuthorize("@ss.hasPermission('accounting:invoices:export')")
@OperateLog(type = EXPORT)
public void outTemplate(HttpServletResponse response) throws IOException {
// 导出 Excel
ExcelUtils.write(response, "发票.xls", "数据", InvoicesExcelVO.class, new ArrayList<InvoicesExcelVO>());
}
@GetMapping("/inTemplate")
@Operation(summary = "导入发票 Excel模板")
@PreAuthorize("@ss.hasPermission('accounting:invoices:export')")
@OperateLog(type = IMPORT)
@Transactional
public CommonResult inTemplate(@RequestParam("multipartFile") MultipartFile multipartFile) throws IOException {
// 导入 Excel
int i = 0;
InputStream inputStream = multipartFile.getInputStream();
try {
List<InvoicesExcelVO> invoicesExcelVOList = EasyExcel.read(inputStream)
// 注册监听器,可以在这里校验字段
.registerReadListener(new InvoicesImportListener())
.head(InvoicesExcelVO.class)
// 设置sheet,默认读取第一个
.sheet()
// 设置标题所在行数
.headRowNumber(1)
.doReadSync();
i = invoicesService.batchInsert(invoicesExcelVOList);
} catch (Exception e) {
return error(e.getMessage());
}
return success(i);
}
/** /**
* *
*
* @param file * @param file
* @return * @return
*/ */
private String getUrl(MultipartFile file,String name, String path) throws Exception{ private String getUrl(MultipartFile file, String name, String path) throws Exception {
String url=""; String url = "";
if (path.equals("ofd")){ if (path.equals("ofd")) {
url = fileService.createFile(name, path, IoUtil.readBytes(file.getInputStream())); url = fileService.createFile(name, path, IoUtil.readBytes(file.getInputStream()));
return url; return url;
} }

@ -9,205 +9,856 @@ import java.time.LocalDateTime;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import lombok.experimental.Accessors;
/** /**
* Excel VO * Excel VO
* *
* @author * @author
*/ */
@Data
public class InvoicesExcelVO { public class InvoicesExcelVO {
@ExcelProperty("发票id") @ExcelProperty(value = "发票id", index = 0)
private Long id; private Long id;
@ExcelProperty("凭证id一个凭证对应多个电子发票") @ExcelProperty(value = "凭证id一个凭证对应多个电子发票",index = 1)
private Long voucherId; private Long voucherId;
@ExcelProperty("发票代码") @ExcelProperty(value = "发票代码",index = 2)
private String invoiceCode; private String invoiceCode;
@ExcelProperty("发票号码") @ExcelProperty(value = "发票号码", index = 3)
private String invoiceNum; private String invoiceNum;
@ExcelProperty("大写金额") @ExcelProperty(value = "大写金额", index = 4)
private String amountinWords; private String amountinWords;
@ExcelProperty("单价") @ExcelProperty(value = "单价", index = 5)
private String price; private String price;
@ExcelProperty("合计税额") @ExcelProperty(value = "合计税额", index = 6)
private String totalTax; private String totalTax;
@ExcelProperty("税率") @ExcelProperty(value = "税率", index = 7)
private String taxRate; private String taxRate;
@ExcelProperty("金额") @ExcelProperty(value = "金额", index = 8)
private String totalAmount; private String totalAmount;
@ExcelProperty("税额") @ExcelProperty(value = "税额", index = 9)
private String commodityTax; private String commodityTax;
@ExcelProperty("价税合计") @ExcelProperty(value = "价税合计", index = 10)
private String commodityAmount; private String commodityAmount;
@ExcelProperty("小写价税合计") @ExcelProperty(value = "小写价税合计", index = 11)
private String amountinFiguers; private String amountinFiguers;
@ExcelProperty("开票人") @ExcelProperty(value = "开票人", index = 12)
private String foteDrawer; private String foteDrawer;
@ExcelProperty("销方地址电话") @ExcelProperty(value = "销方地址电话", index = 13)
private String sellerAddress; private String sellerAddress;
@ExcelProperty("服务条数") @ExcelProperty(value = "服务条数", index = 14)
private String commodityNum; private String commodityNum;
@ExcelProperty("销方纳税识别号") @ExcelProperty(value = "销方纳税识别号", index = 15)
private String sellerRegisterNum; private String sellerRegisterNum;
@ExcelProperty("报送状态") @ExcelProperty(value = "报送状态", index = 16)
private String machineCode; private String machineCode;
@ExcelProperty("备注") @ExcelProperty(value = "备注", index = 17)
private String remarks; private String remarks;
@ExcelProperty("销方开户行及账号") @ExcelProperty(value = "销方开户行及账号", index = 18)
private String sellerBank; private String sellerBank;
@ExcelProperty("校验码") @ExcelProperty(value = "校验码", index = 19)
private String checkCode; private String checkCode;
@ExcelProperty("开票日期") @ExcelProperty(value = "开票日期", index = 20)
private LocalDateTime invoiceDate; private LocalDateTime invoiceDate;
@ExcelProperty("购方税号") @ExcelProperty(value = "购方税号", index = 21)
private String purchaserRegisterNum; private String purchaserRegisterNum;
@ExcelProperty("清单标志") @ExcelProperty(value = "清单标志", index = 22)
private String invoiceTypeOrg; private String invoiceTypeOrg;
@ExcelProperty("密码区") @ExcelProperty(value = "密码区", index = 23)
private String password; private String password;
@ExcelProperty("打印标志") @ExcelProperty(value = "打印标志", index = 24)
private String agent; private String agent;
@ExcelProperty("购方开户行及账号") @ExcelProperty(value = "购方开户行及账号", index = 25)
private String purchaserBank; private String purchaserBank;
@ExcelProperty("复核人") @ExcelProperty(value = "复核人", index = 26)
private String checker; private String checker;
@ExcelProperty("城市") @ExcelProperty(value = "城市", index = 27)
private String city; private String city;
@ExcelProperty("购方公司名") @ExcelProperty(value = "购方公司名", index = 28)
private String purchaserName; private String purchaserName;
@ExcelProperty("规格型号") @ExcelProperty(value = "规格型号", index = 29)
private String commodityType; private String commodityType;
@ExcelProperty("报送日志") @ExcelProperty(value = "报送日志", index = 30)
private String province; private String province;
@ExcelProperty("发票种类") @ExcelProperty(value = "发票种类", index = 31)
private String invoiceType; private String invoiceType;
@ExcelProperty("发票联") @ExcelProperty(value = "发票联", index = 32)
private String sheetNum; private String sheetNum;
@ExcelProperty("购方地址电话") @ExcelProperty(value = "购方地址电话", index = 33)
private String purchaserAddress; private String purchaserAddress;
@ExcelProperty("部门") @ExcelProperty(value = "部门", index = 34)
private String commodityUnit; private String commodityUnit;
@ExcelProperty("收款人") @ExcelProperty(value = "收款人", index = 35)
private String payee; private String payee;
@ExcelProperty("主要商品名称") @ExcelProperty(value = "主要商品名称", index = 36)
private String commodityName; private String commodityName;
@ExcelProperty("销方名称") @ExcelProperty(value = "销方名称", index = 37)
private String sellerName; private String sellerName;
@ExcelProperty("审核状态0待审核1已审核2审核退回3未提交") @ExcelProperty(value = "审核状态0待审核1已审核2审核退回3未提交", index = 38)
private Byte invoiceCheck; private Byte invoiceCheck;
@ExcelProperty("是否印章01") @ExcelProperty(value = "是否印章01", index = 39)
private Byte invoiceSeal; private Byte invoiceSeal;
@ExcelProperty("发票综合代码") @ExcelProperty(value = "发票综合代码", index = 40)
private String invoiceQrcode; private String invoiceQrcode;
@ExcelProperty("发票综合号码") @ExcelProperty(value = "发票综合号码", index = 41)
private String invoiceQrnum; private String invoiceQrnum;
@ExcelProperty("二维码查验1查询相符,0查验不符") @ExcelProperty(value = "二维码查验1查询相符,0查验不符", index = 42)
private Byte qrCheckCode; private Byte qrCheckCode;
@ExcelProperty("发票状态1正常0作废") @ExcelProperty(value = "发票状态1正常0作废", index = 43)
private Byte invoiceState; private Byte invoiceState;
@ExcelProperty("作废日期") @ExcelProperty(value = "作废日期", index = 44)
private LocalDateTime printNum; private LocalDateTime printNum;
@ExcelProperty("进销项标识0进项1销项") @ExcelProperty(value = "进销项标识0进项1销项", index = 45)
private Byte inoutMark; private Byte inoutMark;
@ExcelProperty("作废标志0作废1启用") @ExcelProperty(value = "作废标志0作废1启用", index = 46)
private String invalidMark; private String invalidMark;
@ExcelProperty("重复标识0已查重1有重复") @ExcelProperty(value = "重复标识0已查重1有重复", index = 47)
private Byte duplicateMark; private Byte duplicateMark;
@ExcelProperty("验真0未验真1已验证") @ExcelProperty(value = "验真0未验真1已验证", index = 48)
private Byte checkTrue; private Byte checkTrue;
@ExcelProperty("加密") @ExcelProperty(value = "加密", index = 49)
private String encrypt; private String encrypt;
@ExcelProperty("制单人") @ExcelProperty(value = "制单人", index = 50)
private String createBy; private String createBy;
@ExcelProperty("创建时间") @ExcelProperty(value = "创建时间", index = 51)
private LocalDateTime createTime; private LocalDateTime createTime;
@ExcelProperty("归档id一个归档id对应多个发票") @ExcelProperty(value = "归档id一个归档id对应多个发票", index = 52)
private Long recordId; private Long recordId;
@ExcelProperty("业务实体id") @ExcelProperty(value = "业务实体id", index = 53)
private Long companyId; private Long companyId;
@ExcelProperty("业务实体") @ExcelProperty(value = "业务实体", index = 54)
private String company; private String company;
@ExcelProperty("所属部门id") @ExcelProperty(value = "所属部门id", index = 55)
private Long deptId; private Long deptId;
@ExcelProperty("所属部门") @ExcelProperty(value = "所属部门", index = 56)
private String deptName; private String deptName;
@ExcelProperty("用户id") @ExcelProperty(value = "用户id", index = 57)
private Long userId; private Long userId;
@ExcelProperty("备注") @ExcelProperty(value = "备注", index = 58)
private String remark; private String remark;
@ExcelProperty("预留字段1") @ExcelProperty(value = "预留字段1", index = 59)
private String attr1; private String attr1;
@ExcelProperty("预留字段2") @ExcelProperty(value = "预留字段2", index = 60)
private String attr2; private String attr2;
@ExcelProperty("预留字段3") @ExcelProperty(value = "预留字段3", index = 61)
private Integer attr3; private Integer attr3;
@ExcelProperty("预留字段4") @ExcelProperty(value = "预留字段4", index = 62)
private Integer attr4; private Integer attr4;
@ExcelProperty("归档状态") @ExcelProperty(value = "归档状态", index = 63)
private String archiveState; private String archiveState;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVoucherId() {
return voucherId;
}
public void setVoucherId(Long voucherId) {
this.voucherId = voucherId;
}
public String getInvoiceCode() {
return invoiceCode;
}
public void setInvoiceCode(String invoiceCode) {
this.invoiceCode = invoiceCode;
}
public String getInvoiceNum() {
return invoiceNum;
}
public void setInvoiceNum(String invoiceNum) {
this.invoiceNum = invoiceNum;
}
public String getAmountinWords() {
return amountinWords;
}
public void setAmountinWords(String amountinWords) {
this.amountinWords = amountinWords;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getTotalTax() {
return totalTax;
}
public void setTotalTax(String totalTax) {
this.totalTax = totalTax;
}
public String getTaxRate() {
return taxRate;
}
public void setTaxRate(String taxRate) {
this.taxRate = taxRate;
}
public String getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(String totalAmount) {
this.totalAmount = totalAmount;
}
public String getCommodityTax() {
return commodityTax;
}
public void setCommodityTax(String commodityTax) {
this.commodityTax = commodityTax;
}
public String getCommodityAmount() {
return commodityAmount;
}
public void setCommodityAmount(String commodityAmount) {
this.commodityAmount = commodityAmount;
}
public String getAmountinFiguers() {
return amountinFiguers;
}
public void setAmountinFiguers(String amountinFiguers) {
this.amountinFiguers = amountinFiguers;
}
public String getFoteDrawer() {
return foteDrawer;
}
public void setFoteDrawer(String foteDrawer) {
this.foteDrawer = foteDrawer;
}
public String getSellerAddress() {
return sellerAddress;
}
public void setSellerAddress(String sellerAddress) {
this.sellerAddress = sellerAddress;
}
public String getCommodityNum() {
return commodityNum;
}
public void setCommodityNum(String commodityNum) {
this.commodityNum = commodityNum;
}
public String getSellerRegisterNum() {
return sellerRegisterNum;
}
public void setSellerRegisterNum(String sellerRegisterNum) {
this.sellerRegisterNum = sellerRegisterNum;
}
public String getMachineCode() {
return machineCode;
}
public void setMachineCode(String machineCode) {
this.machineCode = machineCode;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getSellerBank() {
return sellerBank;
}
public void setSellerBank(String sellerBank) {
this.sellerBank = sellerBank;
}
public String getCheckCode() {
return checkCode;
}
public void setCheckCode(String checkCode) {
this.checkCode = checkCode;
}
public LocalDateTime getInvoiceDate() {
return invoiceDate;
}
public void setInvoiceDate(LocalDateTime invoiceDate) {
this.invoiceDate = invoiceDate;
}
public String getPurchaserRegisterNum() {
return purchaserRegisterNum;
}
public void setPurchaserRegisterNum(String purchaserRegisterNum) {
this.purchaserRegisterNum = purchaserRegisterNum;
}
public String getInvoiceTypeOrg() {
return invoiceTypeOrg;
}
public void setInvoiceTypeOrg(String invoiceTypeOrg) {
this.invoiceTypeOrg = invoiceTypeOrg;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAgent() {
return agent;
}
public void setAgent(String agent) {
this.agent = agent;
}
public String getPurchaserBank() {
return purchaserBank;
}
public void setPurchaserBank(String purchaserBank) {
this.purchaserBank = purchaserBank;
}
public String getChecker() {
return checker;
}
public void setChecker(String checker) {
this.checker = checker;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPurchaserName() {
return purchaserName;
}
public void setPurchaserName(String purchaserName) {
this.purchaserName = purchaserName;
}
public String getCommodityType() {
return commodityType;
}
public void setCommodityType(String commodityType) {
this.commodityType = commodityType;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getInvoiceType() {
return invoiceType;
}
public void setInvoiceType(String invoiceType) {
this.invoiceType = invoiceType;
}
public String getSheetNum() {
return sheetNum;
}
public void setSheetNum(String sheetNum) {
this.sheetNum = sheetNum;
}
public String getPurchaserAddress() {
return purchaserAddress;
}
public void setPurchaserAddress(String purchaserAddress) {
this.purchaserAddress = purchaserAddress;
}
public String getCommodityUnit() {
return commodityUnit;
}
public void setCommodityUnit(String commodityUnit) {
this.commodityUnit = commodityUnit;
}
public String getPayee() {
return payee;
}
public void setPayee(String payee) {
this.payee = payee;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public Byte getInvoiceCheck() {
return invoiceCheck;
}
public void setInvoiceCheck(Byte invoiceCheck) {
this.invoiceCheck = invoiceCheck;
}
public Byte getInvoiceSeal() {
return invoiceSeal;
}
public void setInvoiceSeal(Byte invoiceSeal) {
this.invoiceSeal = invoiceSeal;
}
public String getInvoiceQrcode() {
return invoiceQrcode;
}
public void setInvoiceQrcode(String invoiceQrcode) {
this.invoiceQrcode = invoiceQrcode;
}
public String getInvoiceQrnum() {
return invoiceQrnum;
}
public void setInvoiceQrnum(String invoiceQrnum) {
this.invoiceQrnum = invoiceQrnum;
}
public Byte getQrCheckCode() {
return qrCheckCode;
}
public void setQrCheckCode(Byte qrCheckCode) {
this.qrCheckCode = qrCheckCode;
}
public Byte getInvoiceState() {
return invoiceState;
}
public void setInvoiceState(Byte invoiceState) {
this.invoiceState = invoiceState;
}
public LocalDateTime getPrintNum() {
return printNum;
}
public void setPrintNum(LocalDateTime printNum) {
this.printNum = printNum;
}
public Byte getInoutMark() {
return inoutMark;
}
public void setInoutMark(Byte inoutMark) {
this.inoutMark = inoutMark;
}
public String getInvalidMark() {
return invalidMark;
}
public void setInvalidMark(String invalidMark) {
this.invalidMark = invalidMark;
}
public Byte getDuplicateMark() {
return duplicateMark;
}
public void setDuplicateMark(Byte duplicateMark) {
this.duplicateMark = duplicateMark;
}
public Byte getCheckTrue() {
return checkTrue;
}
public void setCheckTrue(Byte checkTrue) {
this.checkTrue = checkTrue;
}
public String getEncrypt() {
return encrypt;
}
public void setEncrypt(String encrypt) {
this.encrypt = encrypt;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public Long getRecordId() {
return recordId;
}
public void setRecordId(Long recordId) {
this.recordId = recordId;
}
public Long getCompanyId() {
return companyId;
}
public void setCompanyId(Long companyId) {
this.companyId = companyId;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public Long getDeptId() {
return deptId;
}
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getAttr1() {
return attr1;
}
public void setAttr1(String attr1) {
this.attr1 = attr1;
}
public String getAttr2() {
return attr2;
}
public void setAttr2(String attr2) {
this.attr2 = attr2;
}
public Integer getAttr3() {
return attr3;
}
public void setAttr3(Integer attr3) {
this.attr3 = attr3;
}
public Integer getAttr4() {
return attr4;
}
public void setAttr4(Integer attr4) {
this.attr4 = attr4;
}
public String getArchiveState() {
return archiveState;
}
public void setArchiveState(String archiveState) {
this.archiveState = archiveState;
}
public InvoicesExcelVO(){};
public InvoicesExcelVO(Long id, Long voucherId, String invoiceCode, String invoiceNum, String amountinWords, String price, String totalTax, String taxRate, String totalAmount, String commodityTax, String commodityAmount, String amountinFiguers, String foteDrawer, String sellerAddress, String commodityNum, String sellerRegisterNum, String machineCode, String remarks, String sellerBank, String checkCode, LocalDateTime invoiceDate, String purchaserRegisterNum, String invoiceTypeOrg, String password, String agent, String purchaserBank, String checker, String city, String purchaserName, String commodityType, String province, String invoiceType, String sheetNum, String purchaserAddress, String commodityUnit, String payee, String commodityName, String sellerName, Byte invoiceCheck, Byte invoiceSeal, String invoiceQrcode, String invoiceQrnum, Byte qrCheckCode, Byte invoiceState, LocalDateTime printNum, Byte inoutMark, String invalidMark, Byte duplicateMark, Byte checkTrue, String encrypt, String createBy, LocalDateTime createTime, Long recordId, Long companyId, String company, Long deptId, String deptName, Long userId, String remark, String attr1, String attr2, Integer attr3, Integer attr4, String archiveState) {
this.id = id;
this.voucherId = voucherId;
this.invoiceCode = invoiceCode;
this.invoiceNum = invoiceNum;
this.amountinWords = amountinWords;
this.price = price;
this.totalTax = totalTax;
this.taxRate = taxRate;
this.totalAmount = totalAmount;
this.commodityTax = commodityTax;
this.commodityAmount = commodityAmount;
this.amountinFiguers = amountinFiguers;
this.foteDrawer = foteDrawer;
this.sellerAddress = sellerAddress;
this.commodityNum = commodityNum;
this.sellerRegisterNum = sellerRegisterNum;
this.machineCode = machineCode;
this.remarks = remarks;
this.sellerBank = sellerBank;
this.checkCode = checkCode;
this.invoiceDate = invoiceDate;
this.purchaserRegisterNum = purchaserRegisterNum;
this.invoiceTypeOrg = invoiceTypeOrg;
this.password = password;
this.agent = agent;
this.purchaserBank = purchaserBank;
this.checker = checker;
this.city = city;
this.purchaserName = purchaserName;
this.commodityType = commodityType;
this.province = province;
this.invoiceType = invoiceType;
this.sheetNum = sheetNum;
this.purchaserAddress = purchaserAddress;
this.commodityUnit = commodityUnit;
this.payee = payee;
this.commodityName = commodityName;
this.sellerName = sellerName;
this.invoiceCheck = invoiceCheck;
this.invoiceSeal = invoiceSeal;
this.invoiceQrcode = invoiceQrcode;
this.invoiceQrnum = invoiceQrnum;
this.qrCheckCode = qrCheckCode;
this.invoiceState = invoiceState;
this.printNum = printNum;
this.inoutMark = inoutMark;
this.invalidMark = invalidMark;
this.duplicateMark = duplicateMark;
this.checkTrue = checkTrue;
this.encrypt = encrypt;
this.createBy = createBy;
this.createTime = createTime;
this.recordId = recordId;
this.companyId = companyId;
this.company = company;
this.deptId = deptId;
this.deptName = deptName;
this.userId = userId;
this.remark = remark;
this.attr1 = attr1;
this.attr2 = attr2;
this.attr3 = attr3;
this.attr4 = attr4;
this.archiveState = archiveState;
}
@Override
public String toString() {
return "InvoicesExcelVO{" +
"id=" + id +
", voucherId=" + voucherId +
", invoiceCode='" + invoiceCode + '\'' +
", invoiceNum='" + invoiceNum + '\'' +
", amountinWords='" + amountinWords + '\'' +
", price='" + price + '\'' +
", totalTax='" + totalTax + '\'' +
", taxRate='" + taxRate + '\'' +
", totalAmount='" + totalAmount + '\'' +
", commodityTax='" + commodityTax + '\'' +
", commodityAmount='" + commodityAmount + '\'' +
", amountinFiguers='" + amountinFiguers + '\'' +
", foteDrawer='" + foteDrawer + '\'' +
", sellerAddress='" + sellerAddress + '\'' +
", commodityNum='" + commodityNum + '\'' +
", sellerRegisterNum='" + sellerRegisterNum + '\'' +
", machineCode='" + machineCode + '\'' +
", remarks='" + remarks + '\'' +
", sellerBank='" + sellerBank + '\'' +
", checkCode='" + checkCode + '\'' +
", invoiceDate=" + invoiceDate +
", purchaserRegisterNum='" + purchaserRegisterNum + '\'' +
", invoiceTypeOrg='" + invoiceTypeOrg + '\'' +
", password='" + password + '\'' +
", agent='" + agent + '\'' +
", purchaserBank='" + purchaserBank + '\'' +
", checker='" + checker + '\'' +
", city='" + city + '\'' +
", purchaserName='" + purchaserName + '\'' +
", commodityType='" + commodityType + '\'' +
", province='" + province + '\'' +
", invoiceType='" + invoiceType + '\'' +
", sheetNum='" + sheetNum + '\'' +
", purchaserAddress='" + purchaserAddress + '\'' +
", commodityUnit='" + commodityUnit + '\'' +
", payee='" + payee + '\'' +
", commodityName='" + commodityName + '\'' +
", sellerName='" + sellerName + '\'' +
", invoiceCheck=" + invoiceCheck +
", invoiceSeal=" + invoiceSeal +
", invoiceQrcode='" + invoiceQrcode + '\'' +
", invoiceQrnum='" + invoiceQrnum + '\'' +
", qrCheckCode=" + qrCheckCode +
", invoiceState=" + invoiceState +
", printNum=" + printNum +
", inoutMark=" + inoutMark +
", invalidMark='" + invalidMark + '\'' +
", duplicateMark=" + duplicateMark +
", checkTrue=" + checkTrue +
", encrypt='" + encrypt + '\'' +
", createBy='" + createBy + '\'' +
", createTime=" + createTime +
", recordId=" + recordId +
", companyId=" + companyId +
", company='" + company + '\'' +
", deptId=" + deptId +
", deptName='" + deptName + '\'' +
", userId=" + userId +
", remark='" + remark + '\'' +
", attr1='" + attr1 + '\'' +
", attr2='" + attr2 + '\'' +
", attr3=" + attr3 +
", attr4=" + attr4 +
", archiveState='" + archiveState + '\'' +
'}';
}
} }

@ -0,0 +1,89 @@
package cn.iocoder.yudao.module.accounting.controller.admin.listener;
import cn.iocoder.yudao.module.accounting.controller.admin.invoices.vo.InvoicesExcelVO;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Map;
// 有个很重要的点 DemoDataListener 不能被spring管理要每次读取excel都要new,然后里面用到spring可以构造方法传进去
@Slf4j
public class DemoDataListener implements ReadListener<InvoicesExcelVO> {
/**
* 5使100list 便
*/
private static final int BATCH_COUNT = 100;
/**
*
*/
private List<InvoicesExcelVO> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
/**
* DAOservice
*/
private InvoicesExcelVO demoDAO;
public DemoDataListener() {
// 这里是demo所以随便new一个。实际使用如果到了spring,请使用下面的有参构造函数
demoDAO = new InvoicesExcelVO();
}
/**
* 使spring,使Listenerspring
*
* @param demoDAO
*/
public DemoDataListener(InvoicesExcelVO demoDAO) {
this.demoDAO = demoDAO;
}
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
ReadListener.super.onException(exception, context);
}
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
ReadListener.super.invokeHead(headMap, context);
}
/**
*
*
* @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
* @param context
*/
@Override
public void invoke(InvoicesExcelVO data, AnalysisContext context) {
log.info("解析到一条数据:{}", JSON.toJSONString(data));
cachedDataList.add(data);
// 达到BATCH_COUNT了需要去存储一次数据库防止数据几万条数据在内存容易OOM
if (cachedDataList.size() >= BATCH_COUNT) {
// 存储完成清理 list
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
@Override
public void extra(CellExtra extra, AnalysisContext context) {
ReadListener.super.extra(extra, context);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
@Override
public boolean hasNext(AnalysisContext context) {
return ReadListener.super.hasNext(context);
}
}

@ -0,0 +1,63 @@
package cn.iocoder.yudao.module.accounting.controller.admin.listener;
import cn.iocoder.yudao.framework.common.util.string.StringUtils;
import cn.iocoder.yudao.module.accounting.controller.admin.invoices.vo.InvoicesExcelVO;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.google.common.collect.Lists;
import java.util.List;
public class InvoicesImportListener extends AnalysisEventListener {
List misCodes = Lists.newArrayList();
/**
*
* @param data
* @param context
*/
@Override
public void invoke(Object data, AnalysisContext context) {
String misCode = ((InvoicesExcelVO) data).getInvoiceCode();
if (StringUtils.isEmpty(misCode)) {
throw new RuntimeException(String.format("第%s行InvoiceCode为空请核实", context.readRowHolder().getRowIndex() + 1));
}
if (misCodes.contains(misCodes)) {
throw new RuntimeException(String.format("第%s行InvoiceCode已重复请核实", context.readRowHolder().getRowIndex() + 1));
} else {
misCodes.add(misCode);
}
}
/**
*
* @param exception
* @param context
* @throws Exception
*/
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
// ExcelDataConvertException:当数据转换异常的时候,会抛出该异常,此处可以得知第几行,第几列的数据
if (exception instanceof ExcelDataConvertException) {
Integer columnIndex = ((ExcelDataConvertException) exception).getColumnIndex() + 1;
Integer rowIndex = ((ExcelDataConvertException) exception).getRowIndex() + 1;
String message = "第" + rowIndex + "行,第" + columnIndex + "列" + "数据格式有误,请核实";
throw new RuntimeException(message);
} else if (exception instanceof RuntimeException) {
throw exception;
} else {
super.onException(exception, context);
}
}
/**
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
misCodes.clear();
}
}

@ -25,6 +25,8 @@ public interface InvoicesConvert {
InvoicesRespVO convert(InvoicesDO bean); InvoicesRespVO convert(InvoicesDO bean);
InvoicesDO convert(InvoicesExcelVO bean);
List<InvoicesRespVO> convertList(List<InvoicesDO> list); List<InvoicesRespVO> convertList(List<InvoicesDO> list);
PageResult<InvoicesRespVO> convertPage(PageResult<InvoicesDO> page); PageResult<InvoicesRespVO> convertPage(PageResult<InvoicesDO> page);

@ -67,4 +67,6 @@ public interface InvoicesService {
*/ */
List<InvoicesDO> getInvoicesList(InvoicesExportReqVO exportReqVO); List<InvoicesDO> getInvoicesList(InvoicesExportReqVO exportReqVO);
int batchInsert(List<InvoicesExcelVO> invoicesList);
} }

@ -79,4 +79,18 @@ public class InvoicesServiceImpl implements InvoicesService {
return invoicesMapper.selectList(exportReqVO); return invoicesMapper.selectList(exportReqVO);
} }
@Override
public int batchInsert(List<InvoicesExcelVO> invoicesList) {
if ( invoicesList == null ) {
return 0;
}
List<InvoicesDO> list1 = new ArrayList<InvoicesDO>( invoicesList.size() );
for ( InvoicesExcelVO invoicesExcelVO : invoicesList ) {
list1.add( InvoicesConvert.INSTANCE.convert(invoicesExcelVO ) );
}
invoicesMapper.insertBatch(list1);
return 1;
}
} }

@ -356,8 +356,8 @@ public class BaiduOcrHandler {
String jsonStr = HttpUtil.post(BaiduOcrConstant.INVOICEURL_OFD, accessToken, param); String jsonStr = HttpUtil.post(BaiduOcrConstant.INVOICEURL_OFD, accessToken, param);
JSONObject jsonObject = JSON.parseObject(jsonStr); JSONObject jsonObject = JSON.parseObject(jsonStr);
if (!jsonObject.containsKey("words_result")) { if (!jsonObject.containsKey("words_result")) {
log.error(baseStr+"ofd发票识别失败"+jsonObject.toString()); log.error("发票识别失败"+jsonObject.toString());
throw exception("ofd发票识别失败"); throw exception("发票识别失败"+jsonObject.toString());
} }
return jsonObject; return jsonObject;

Loading…
Cancel
Save