sinake
3 天以前 eec6adb6ea0f1c3ea222edfed296cd8b22da96c8
ruoyi-admin/src/main/java/com/ruoyi/web/controller/project/ServiceInvoiceController.java
@@ -136,7 +136,7 @@
     * 通用上传请求
     */
    @PostMapping("/uploadOCR")
    public AjaxResult uploadOCR(@RequestParam("file") MultipartFile file, @RequestParam("userId") String userId, @RequestParam("caseId") String caseId) throws Exception {
    public AjaxResult uploadOCR(@RequestParam("file") MultipartFile file, @RequestParam("userId") String userId, @RequestParam("caseId") String caseId, @RequestParam(value = "voucher", required = false) String voucher) throws Exception {
        try {
            // 上传文件路径
@@ -166,41 +166,72 @@
            String fileName = FileUploadUtils.upload(filePath, file);
            String url1 = serverConfig.getUrl();
            String url = url1 + fileName;
            String base64= Base64.getEncoder().encodeToString(file.getBytes());
            List<Map<String,Object>> lists=aliOCR(base64,userId,caseId);
            logger.info(JSON.toJSONString(lists));
            List<ServiceInvoice> invoiceList=new ArrayList<>();
            for(Map<String,Object> row : lists){
                ServiceInvoice invoice=new ServiceInvoice();
                if(ObjectUtils.isNotEmpty(userId)) {
                    invoice.setUserId(Long.parseLong(userId));
            // 勾选“凭证”:不调用 OCR,直接生成一条类型为“凭证”的记录,金额/日期等到第二步手工补录
            if ("1".equals(voucher) || "true".equalsIgnoreCase(voucher)) {
                ServiceInvoice voucherInvoice = new ServiceInvoice();
                if (ObjectUtils.isNotEmpty(userId)) {
                    voucherInvoice.setUserId(Long.parseLong(userId));
                }
                if(ObjectUtils.isNotEmpty(caseId)) {
                    invoice.setCaseId(Long.parseLong(caseId));
                if (ObjectUtils.isNotEmpty(caseId)) {
                    voucherInvoice.setCaseId(Long.parseLong(caseId));
                }
                invoice.setInvoiceType(row.get("type")+"");
                invoice.setInvoiceNo(row.get("发票号码")+"");
                invoice.setOccurDate(row.get("开票日期")+"");
                invoice.setInvoiceAmount(row.get("发票金额")+"");
                invoice.setSellerName(row.get("受票方名称")+"");
                invoice.setInvoiceUrl(url);
                String invoiceName=newFilename.isEmpty() ? filename : newFilename;
                invoice.setInvoiceName(invoiceName.substring(0, invoiceName.lastIndexOf(".")));
                invoice.setExtContent(JSON.toJSONString(row));
                invoice.setCreateBy(userId);
                invoice.setCreateTime(DateUtils.getNowDate());
                invoice.setUpdateBy(userId);
                invoice.setUpdateTime(DateUtils.getNowDate());
                serviceInvoiceService.save(invoice);
                invoiceList.add(invoice);
            }
                String voucherFileName = newFilename.isEmpty() ? filename : newFilename;
                int dotIndex = voucherFileName.lastIndexOf(".");
                voucherInvoice.setInvoiceName(dotIndex > 0 ? voucherFileName.substring(0, dotIndex) : voucherFileName);
                voucherInvoice.setInvoiceType("凭证");
                voucherInvoice.setInvoiceAmount("0");
                voucherInvoice.setInvoiceUrl(url);
                voucherInvoice.setCreateBy(userId);
                voucherInvoice.setCreateTime(DateUtils.getNowDate());
                voucherInvoice.setUpdateBy(userId);
                voucherInvoice.setUpdateTime(DateUtils.getNowDate());
                serviceInvoiceService.save(voucherInvoice);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            ajax.put("OCRList", lists);
            ajax.put("invoiceList", invoiceList);
            return ajax;
                List<ServiceInvoice> voucherList = new ArrayList<>();
                voucherList.add(voucherInvoice);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                ajax.put("url", url);
                ajax.put("OCRList", new ArrayList<>());
                ajax.put("invoiceList", voucherList);
                return ajax;
            }else {
                String base64 = Base64.getEncoder().encodeToString(file.getBytes());
                List<Map<String, Object>> lists = aliOCR(base64, userId, caseId);
                logger.info(JSON.toJSONString(lists));
                List<ServiceInvoice> invoiceList = new ArrayList<>();
                for (Map<String, Object> row : lists) {
                    ServiceInvoice invoice = new ServiceInvoice();
                    if (ObjectUtils.isNotEmpty(userId)) {
                        invoice.setUserId(Long.parseLong(userId));
                    }
                    if (ObjectUtils.isNotEmpty(caseId)) {
                        invoice.setCaseId(Long.parseLong(caseId));
                    }
                    //invoice.setInvoiceType(row.get("type")+"");
                    invoice.setInvoiceNo(row.get("发票号码") + "");
                    invoice.setOccurDate(row.get("开票日期") + "");
                    invoice.setInvoiceAmount(row.get("发票金额") + "");
                    invoice.setSellerName(row.get("受票方名称") + "");
                    invoice.setInvoiceUrl(url);
                    String invoiceName = newFilename.isEmpty() ? filename : newFilename;
                    invoice.setInvoiceName(invoiceName.substring(0, invoiceName.lastIndexOf(".")));
                    invoice.setExtContent(JSON.toJSONString(row));
                    invoice.setCreateBy(userId);
                    invoice.setCreateTime(DateUtils.getNowDate());
                    invoice.setUpdateBy(userId);
                    invoice.setUpdateTime(DateUtils.getNowDate());
                    serviceInvoiceService.save(invoice);
                    invoiceList.add(invoice);
                }
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                ajax.put("url", url);
                ajax.put("OCRList", lists);
                ajax.put("invoiceList", invoiceList);
                return ajax;
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }