From 672fe0665a7aed2a26a03dc8cfc933c500f76c33 Mon Sep 17 00:00:00 2001 From: wanghaomei Date: Thu, 24 Nov 2022 14:18:50 +0800 Subject: [PATCH] =?UTF-8?q?ocr=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Myshipping.Application.xml | 27 ++++ .../BookingOrder/BookingOrderService.cs | 144 ++++++++++++++++-- .../BookingPrintTemplateService.cs | 14 +- 3 files changed, 165 insertions(+), 20 deletions(-) diff --git a/Myshipping.Application/Myshipping.Application.xml b/Myshipping.Application/Myshipping.Application.xml index 42244474..f98b88b1 100644 --- a/Myshipping.Application/Myshipping.Application.xml +++ b/Myshipping.Application/Myshipping.Application.xml @@ -4905,6 +4905,33 @@ 船公司ID 返回适用的路由枚举 + + + 上传ocr文件 + + + + + + + 获取图片 + + 文件名称 + 缩放比例,默认为1.5 + + + + + 获取文字 + + 文件名称 + 缩放比例 + x坐标 + y坐标 + 宽度 + 高度 + + 获取用户报表的json diff --git a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs index 6e04a87b..d182d39d 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs @@ -51,6 +51,7 @@ using System.Threading; using Furion.JsonSerialization; using System.Xml.Linq; using Myshipping.Application.Helper; +using System.Net; namespace Myshipping.Application { @@ -151,7 +152,7 @@ namespace Myshipping.Application .WhereIF(!string.IsNullOrWhiteSpace(input.VESSELID), u => u.VESSELID.Contains(input.VESSELID)) .WhereIF(!string.IsNullOrWhiteSpace(input.VESSEL), u => u.VESSEL.ToLower().Contains(u.VESSEL.ToLower())) .WhereIF(!string.IsNullOrWhiteSpace(input.VOYNO), u => u.VOYNO.Contains(input.VOYNO)) - .WhereIF(!string.IsNullOrWhiteSpace(input.VOYNOINNER), u => u.VOYNOINNER.Contains(input.VOYNOINNER) ) + .WhereIF(!string.IsNullOrWhiteSpace(input.VOYNOINNER), u => u.VOYNOINNER.Contains(input.VOYNOINNER)) .WhereIF(input.BETD != null, u => u.ETD >= input.BETD) .WhereIF(input.EETD != null, u => u.ETD <= input.EETD) .WhereIF(input.BATD != null, u => u.ATD >= input.BATD) @@ -166,9 +167,9 @@ namespace Myshipping.Application .WhereIF(input.BETA != null, u => u.ETA >= input.BETA) .WhereIF(input.EETA != null, u => u.ETA <= input.EETA) .WhereIF(!string.IsNullOrWhiteSpace(input.PLACERECEIPT), u => u.PLACERECEIPT.Contains(input.PLACERECEIPT)) - .WhereIF(!string.IsNullOrWhiteSpace(input.PORTLOAD), u => u.PORTLOAD.Contains(input.PORTLOAD) ) - .WhereIF(!string.IsNullOrWhiteSpace(input.PORTLOADID), u => u.PORTLOADID== input.PORTLOADID) - .WhereIF(!string.IsNullOrWhiteSpace(input.PORTDISCHARGEID), u => u.PORTDISCHARGEID==input.PORTDISCHARGEID) + .WhereIF(!string.IsNullOrWhiteSpace(input.PORTLOAD), u => u.PORTLOAD.Contains(input.PORTLOAD)) + .WhereIF(!string.IsNullOrWhiteSpace(input.PORTLOADID), u => u.PORTLOADID == input.PORTLOADID) + .WhereIF(!string.IsNullOrWhiteSpace(input.PORTDISCHARGEID), u => u.PORTDISCHARGEID == input.PORTDISCHARGEID) .WhereIF(!string.IsNullOrWhiteSpace(input.PORTDISCHARGE), u => u.PORTDISCHARGE.Contains(input.PORTDISCHARGE)) .WhereIF(!string.IsNullOrWhiteSpace(input.PLACEDELIVERY), u => u.PLACEDELIVERY.Contains(input.PLACEDELIVERY)) @@ -193,7 +194,7 @@ namespace Myshipping.Application .WhereIF(!string.IsNullOrWhiteSpace(input.PAYABLEATID), u => u.PAYABLEATID == input.PAYABLEATID) .WhereIF(!string.IsNullOrWhiteSpace(input.SERVICE), u => u.SERVICE.Contains(input.SERVICE)) .WhereIF(!string.IsNullOrWhiteSpace(input.MARKS), u => u.MARKS.Contains(input.MARKS)) - .WhereIF(!string.IsNullOrWhiteSpace(input.HSCODE), u => u.HSCODE.Contains(input.HSCODE) ) + .WhereIF(!string.IsNullOrWhiteSpace(input.HSCODE), u => u.HSCODE.Contains(input.HSCODE)) .WhereIF(!string.IsNullOrWhiteSpace(input.DESCRIPTION), u => u.DESCRIPTION.Contains(input.DESCRIPTION)) .WhereIF(!string.IsNullOrWhiteSpace(input.KINDPKGS), u => u.KINDPKGS == input.KINDPKGS) .WhereIF(!string.IsNullOrWhiteSpace(input.CNTRTOTAL), u => u.CNTRTOTAL == input.CNTRTOTAL) @@ -216,7 +217,7 @@ namespace Myshipping.Application .WhereIF(!string.IsNullOrWhiteSpace(input.SIREMARK), u => u.SIREMARK.Contains(input.SIREMARK)) .WhereIF(!string.IsNullOrWhiteSpace(input.YARDREMARK), u => u.YARDREMARK.Contains(input.YARDREMARK)) .WhereIF(!string.IsNullOrWhiteSpace(input.COMPID), u => u.COMPID == input.COMPID) - .WhereIF(!string.IsNullOrWhiteSpace(input.COMPNAME), u => u.COMPNAME.Contains(input.COMPNAME) ) + .WhereIF(!string.IsNullOrWhiteSpace(input.COMPNAME), u => u.COMPNAME.Contains(input.COMPNAME)) .WhereIF(!string.IsNullOrWhiteSpace(input.SHIPPERNAME), u => u.SHIPPERNAME.Contains(input.SHIPPERNAME)) .WhereIF(!string.IsNullOrWhiteSpace(input.SHIPPERCITY), u => u.SHIPPERCITY.Contains(input.SHIPPERCITY)) .WhereIF(!string.IsNullOrWhiteSpace(input.SHIPPERPROVINCE), u => u.SHIPPERPROVINCE == input.SHIPPERPROVINCE) @@ -248,7 +249,7 @@ namespace Myshipping.Application .WhereIF(!string.IsNullOrWhiteSpace(input.CUSTSERVICEID), u => u.CUSTSERVICEID == input.CUSTSERVICEID) .WhereIF(!string.IsNullOrWhiteSpace(input.CUSTSERVICE), u => u.CUSTSERVICE.Contains(input.CUSTSERVICE)) .WhereIF(!string.IsNullOrWhiteSpace(input.CUSTOMERNAME), u => u.CUSTOMERNAME.Contains(input.CUSTOMERNAME)) - .WhereIF(input.CUSTOMERID != null&& input.CUSTOMERID!=0, u => u.CUSTOMERID == input.CUSTOMERID) + .WhereIF(input.CUSTOMERID != null && input.CUSTOMERID != 0, u => u.CUSTOMERID == input.CUSTOMERID) .WhereIF(!string.IsNullOrWhiteSpace(input.FORWARDER), u => u.FORWARDER.Contains(input.FORWARDER)) .WhereIF(!string.IsNullOrWhiteSpace(input.CUSTSERVICE), u => u.CUSTSERVICE == input.CUSTSERVICE) .WhereIF(!string.IsNullOrWhiteSpace(input.CUSTOMERNAME), u => u.CUSTOMERNAME == input.CUSTOMERNAME) @@ -326,7 +327,7 @@ namespace Myshipping.Application var entity = input.Adapt(); - await _rep.InsertAsync(entity); + await _rep.InsertAsync(entity); if (input.ctnInputs != null) { foreach (var item in input.ctnInputs) @@ -501,7 +502,7 @@ namespace Myshipping.Application } string _oldvalue = oldvalue != null ? oldvalue.ToString() : ""; string _value = value != null ? value.ToString() : ""; - if (_oldvalue != _value&&!string.IsNullOrWhiteSpace(descriptor.Description)) + if (_oldvalue != _value && !string.IsNullOrWhiteSpace(descriptor.Description)) { if (flag) { @@ -522,7 +523,7 @@ namespace Myshipping.Application PId = bid, Field = descriptor.Description, OldValue = _oldvalue, - NewValue = _value, + NewValue = _value, }); if (descriptor.Name == "YARDID" || descriptor.Name == "YARD" || descriptor.Name == "MBLNO") { @@ -592,7 +593,7 @@ namespace Myshipping.Application //检索EDI扩展 var ediExtEntity = await _bookingEDIExt.FirstOrDefaultAsync(u => u.BookingId == Id); - if(ediExtEntity != null) + if (ediExtEntity != null) { ordOut.BookingEDIExt = ediExtEntity.Adapt(); } @@ -784,7 +785,8 @@ namespace Myshipping.Application { _logger.LogInformation("提单号:" + MBLNO + " 调用运踪接口"); var key = _repWebAcc.FirstOrDefault(x => x.TenantId == Convert.ToInt64(UserManager.TENANT_ID) && x.TypeCode == "seae_billtraceurl"); - if (key==null) { + if (key == null) + { throw Oops.Bah("调用运踪接口相关账号未维护!"); } @@ -1932,7 +1934,7 @@ namespace Myshipping.Application primaryModel.CTNGOODSLIST.Add(cargoModel); } - + ediModel.BSLIST.Add(primaryModel); @@ -2196,6 +2198,122 @@ namespace Myshipping.Application } #endregion + #region ocr + + /// + /// 上传ocr文件 + /// + /// + /// + [HttpPost("/BookingOrder/OcrUpFile")] + public async Task OcrUpFile(IFormFile file) + { + //未上传文件 + if (file == null || file.Length == 0) + { + throw Oops.Bah("未上传文件"); + } + + var originalFilename = file.FileName; // 文件原始名称 + var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀 + + var serverUrl = (await _cache.GetAllDictData()).FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "ocr_api_url").Value; + if (!serverUrl.EndsWith("/")) + { + serverUrl += "/"; + } + + var ms = new MemoryStream(); + await file.CopyToAsync(ms); + _logger.LogInformation($"调用ocr处理文件{originalFilename}"); + + //Furion框架上传文件名称带有双引号,导致上传失败 + //var strRtn = await $"{serverUrl}pdf/upload" + // .SetContentType("multipart/form-data") + // .SetContentEncoding(Encoding.UTF8) + // .SetBodyBytes(("file", ms.GetBuffer(), "test.pdf")) + // .PostAsStringAsync(); + //var jobj = strRtn.ToJObject(); + //if (jobj.GetIntValue("code") == 0) + //{ + // var fn = jobj.GetStringValue("data"); + // return fn; + //} + //else + //{ + // throw Oops.Bah(jobj.GetStringValue("message")); + //} + + //使用HttpClient方式上传文件 + ms.Position = 0; + var formData = new MultipartFormDataContent(); + formData.Add(new StreamContent(ms, (int)ms.Length), "file", originalFilename); + var _httpclient = new HttpClient(); + var response = await _httpclient.PostAsync($"{serverUrl}pdf/upload", formData); + if (response.IsSuccessStatusCode) + { + var strRtn = response.Content.ReadAsStringAsync().Result; + var jobj = strRtn.ToJObject(); + if (jobj.GetIntValue("code") == 0) + { + var fn = jobj.GetStringValue("data"); + return fn; + } + else + { + throw Oops.Bah(jobj.GetStringValue("message")); + } + } + + throw Oops.Bah("文件上传失败"); + } + + /// + /// 获取图片 + /// + /// 文件名称 + /// 缩放比例,默认为1.5 + /// + [HttpPost("/BookingOrder/OcrGetImg")] + public async Task OcrGetImg(string fileName, float scale = 1.5f) + { + var serverUrl = (await _cache.GetAllDictData()).FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "ocr_api_url").Value; + if (!serverUrl.EndsWith("/")) + { + serverUrl += "/"; + } + + var bs = await $"{serverUrl}pdf/getCanvasImage?fileName={fileName}&scale={scale}" + .GetAsByteArrayAsync(); + var result = new FileContentResult(bs, "application/octet-stream") { FileDownloadName = "ocr.jpg" }; + return result; + } + + /// + /// 获取文字 + /// + /// 文件名称 + /// 缩放比例 + /// x坐标 + /// y坐标 + /// 宽度 + /// 高度 + /// + [HttpPost("/BookingOrder/OcrGetText")] + public async Task OcrGetText(string fileName, float scale, int x, int y, int w, int h) + { + var serverUrl = (await _cache.GetAllDictData()).FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "ocr_api_url").Value; + if (!serverUrl.EndsWith("/")) + { + serverUrl += "/"; + } + + var str = await $"{serverUrl}pdf/getRegionText?fileName={fileName}&scale={scale}&x={x}&y={y}&w={w}&h={h}" + .PostAsStringAsync(); + return str; + } + #endregion + #region 其他 /// /// 获取用户报表的json diff --git a/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs b/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs index 4c67d28f..36d8037d 100644 --- a/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs +++ b/Myshipping.Application/Service/BookingPrintTemplate/BookingPrintTemplateService.cs @@ -64,7 +64,7 @@ namespace Myshipping.Application //未上传打印模板文件 if (file == null || file.Length == 0) { - throw Oops.Oh(BookingErrorCode.BOOK113); + throw Oops.Bah(BookingErrorCode.BOOK113); } var opt = App.GetOptions(); @@ -72,7 +72,7 @@ namespace Myshipping.Application var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀 if (!opt.fileType.Contains(fileSuffix)) { - throw Oops.Oh(BookingErrorCode.BOOK114); + throw Oops.Bah(BookingErrorCode.BOOK114); } var dirAbs = string.Empty; @@ -124,7 +124,7 @@ namespace Myshipping.Application //未上传打印模板文件 if (file == null || file.Length == 0) { - throw Oops.Oh(BookingErrorCode.BOOK113); + throw Oops.Bah(BookingErrorCode.BOOK113); } var opt = App.GetOptions(); @@ -132,14 +132,14 @@ namespace Myshipping.Application var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀 if (!opt.fileType.Contains(fileSuffix)) { - throw Oops.Oh(BookingErrorCode.BOOK114); + throw Oops.Bah(BookingErrorCode.BOOK114); } _rep.Context.QueryFilter.Clear(); var entity = _rep.FirstOrDefault(x => x.Id == input.Id); if (entity == null) { - throw Oops.Oh(BookingErrorCode.BOOK112); + throw Oops.Bah(BookingErrorCode.BOOK112); } entity = input.Adapt(entity); @@ -219,7 +219,7 @@ namespace Myshipping.Application var printFile = await _rep.FirstOrDefaultAsync(u => u.Id == id); if (printFile == null) { - throw Oops.Oh(BookingErrorCode.BOOK115); + throw Oops.Bah(BookingErrorCode.BOOK115); } @@ -233,7 +233,7 @@ namespace Myshipping.Application var fileFullPath = Path.Combine(dirAbs, printFile.FilePath); if (!File.Exists(fileFullPath)) { - throw Oops.Oh(BookingErrorCode.BOOK115); + throw Oops.Bah(BookingErrorCode.BOOK115); } var fileName = HttpUtility.UrlEncode(printFile.FileName, Encoding.GetEncoding("UTF-8"));