Files
GateDge2023_ljy/GZ.LJY000.PiLot/SAL_OUTSTOCK/UploadExtension/BillEventPlugInEx.cs

504 lines
18 KiB
C#
Raw Normal View History

2024-12-05 15:39:19 +08:00
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Kingdee.BOS.WebApi.FormService;
using System.Web;
using Kingdee.BOS.Orm.DataEntity;
using System.IO;
using OpenCvSharp.Extensions;
using OpenCvSharp;
using Spire.Pdf;
using System.Drawing.Imaging;
using System.Drawing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.ServiceHelper;
2025-02-10 09:24:44 +08:00
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
2024-12-05 15:39:19 +08:00
namespace GZ.LJY000.PiLot.SAL_OUTSTOCK.UploadExtension
{
[HotUpdate, Description("上传附件界面")]
public class BillEventPlugInEx : AbstractDynamicFormPlugIn
{
private string FormId = "";
private bool IsDoUploaded = false;
2025-02-10 09:24:44 +08:00
private const string detector_prototxt_path = "detect.prototxt";
private const string detector_caffe_model_path = "detect.caffemodel";
private const string prototxt_path = "sr.prototxt";
private const string caffe_model_path = "sr.caffemodel";
private WeChatQRCode _wechatQrcode;
2024-12-05 15:39:19 +08:00
public override void OnInitialize(InitializeEventArgs e)
{
base.OnInitialize(e);
//FormId = e.Paramter.FormId;
2025-02-10 09:24:44 +08:00
_wechatQrcode = weChatQRCodeInit();
2024-12-05 15:39:19 +08:00
}
public override void ButtonClick(ButtonClickEventArgs e)
{
base.ButtonClick(e);
if (!fileUploaded)
{
this.View.ShowErrMessage("文件未加载完毕!");
return;
}
2025-01-01 08:21:58 +08:00
2024-12-05 15:39:19 +08:00
if (e.Key.EndsWith("FDoUpLoad", StringComparison.OrdinalIgnoreCase))
{
2025-02-10 09:24:44 +08:00
DoUpLoad();
}
}
private WeChatQRCode weChatQRCodeInit()
{
var modelPath = HttpContext.Current.Request.PhysicalApplicationPath + @"/Bin/opencv_3rdparty-wechat_qrcode/";
return WeChatQRCode.Create(modelPath + detector_prototxt_path, modelPath + detector_caffe_model_path, modelPath + prototxt_path, modelPath + caffe_model_path);
}
/// <summary>
/// 预设图片切割区域,提高识别精度
/// </summary>
/// <param name="srcWidth"></param>
/// <param name="srcHeight"></param>
/// <returns></returns>
private List<Rect> GetRects(int srcWidth, int srcHeight)
{
List<Rect> rects = new List<Rect>();
#region 1/4
var width = srcWidth / 2;
var height = srcHeight / 2;
var x = width * 1;
var y = 0;
rects.Add(new Rect(x, y, width, height));
#endregion
#region 1/9
width = srcWidth / 3;
height = srcHeight / 3;
x = width * 2;
y = 0;
rects.Add(new Rect(x, y, width, height));
#endregion
#region 1/60()
width = srcWidth / 10;
height = srcHeight / 6;
x = width * 7;
y = 0;
width = width * 2;
rects.Add(new Rect(x, y, width, height));
#endregion
return rects;
}
/// <summary>
/// 使用微信扫码开源识别
/// </summary>
/// <param name="src">需要扫描的图片</param>
/// <returns></returns>
private AnalyzeResult WeChatQRCodeRectRegion(Mat src)
{
_wechatQrcode.DetectAndDecode(src, out var rects, out var texts);
if (texts != null && texts.Length > 0)
{
return new AnalyzeResult
{
name = texts[0]
};
}
var rescts = GetRects(src.Width, src.Height);
foreach (var rect in rescts)
{
using (Mat roi = new Mat(src, rect))
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
_wechatQrcode.DetectAndDecode(roi, out rects, out texts);
if (texts != null && texts.Length > 0)
{
return new AnalyzeResult
{
name = texts[0]
};
}
}
}
return null;
}
2024-12-05 15:39:19 +08:00
2025-02-10 09:24:44 +08:00
/// <summary>
2025-09-26 10:17:16 +08:00
/// 执行上传
2025-02-10 09:24:44 +08:00
/// </summary>
private void DoUpLoad()
{
var entity = this.View.Model.BillBusinessInfo.GetEntryEntity("FEntity");
var rows = this.View.Model.GetEntityDataObject(entity);
if (rows != null && rows.Any())
{
var path = HttpContext.Current.Request.PhysicalApplicationPath + @"/FileUpLoadServices/UploadFiles/";
var grid = this.View.GetControl<EntryGrid>("FEntity");
2024-12-05 15:39:19 +08:00
2025-02-10 09:24:44 +08:00
var index = 0;
foreach (var row in rows)
{
var isSucceed = bool.Parse(row["FisSucceed"].ToString());
if (isSucceed)
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
index++;
continue;
}
var fileName = row["FFileName"].ToString();
var serverFileName = row["FServerFileName"].ToString();
var fileSuffix = row["FFileSuffix"].ToString();
var fileBytesLength = row["FFileBytesLength"].Long2Int();
AnalyzeResult resData = null;
//文件地址
var fullName = path + serverFileName;
if (fileSuffix.ToUpper().Equals(".PDF"))
{
// 创建 PdfDocument 类的实例
using (PdfDocument document = new PdfDocument())
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
// 加载 PDF 文档
document.LoadFromFile(fullName);
// 将页面转换为图片
using (Image image = document.SaveAsImage(0, 200, 200))
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
// 创建流来保存图片数据
using (MemoryStream stream = new MemoryStream())
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
// 将图片以 PNG 格式保存到流中
image.Save(stream, ImageFormat.Png);
stream.Position = 0;
using (Mat src = BitmapConverter.ToMat(new Bitmap(image)))
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
resData = WeChatQRCodeRectRegion(src);
2024-12-05 15:39:19 +08:00
}
}
}
}
2025-02-10 09:24:44 +08:00
}
else if (fileSuffix.ToUpper().Equals(".JPG"))
{
using (Mat src = new Mat(fullName))
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
resData = WeChatQRCodeRectRegion(src);
2024-12-05 15:39:19 +08:00
}
2025-02-10 09:24:44 +08:00
}
if (resData != null)
{
var isSuccess = false;
var resMsg = string.Empty;
row["FBillNo"] = resData.name;
2024-12-05 15:39:19 +08:00
2025-02-10 09:24:44 +08:00
if (!resData.name.IsNullOrEmptyOrWhiteSpace())
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
var base64 = FileToBase64String(fullName);
var resultJson = DoAttachmentUpload("SAL_OUTSTOCK", fileName, resData.name, fileBytesLength, fileSuffix, base64);
2024-12-05 15:39:19 +08:00
2025-02-10 09:24:44 +08:00
if (resultJson != null)
2024-12-05 15:39:19 +08:00
{
2025-02-10 09:24:44 +08:00
var result = JsonConvert.DeserializeObject<JToken>(resultJson);
//var result = resultJson as JObject;
var responseData = result["Result"]["ResponseStatus"];
isSuccess = responseData["IsSuccess"].Value<bool>();
2024-12-05 15:39:19 +08:00
2025-02-10 09:24:44 +08:00
resMsg = JsonConvert.SerializeObject(responseData);
2024-12-05 15:39:19 +08:00
}
else
{
2025-02-10 09:24:44 +08:00
resMsg = "识别单号找不到对应销售出库单!";
2024-12-05 15:39:19 +08:00
}
}
2025-02-10 09:24:44 +08:00
if (isSuccess)
{
row["FisSucceed"] = true;
row["FResultMsg"] = "上传完毕";
grid.SetForecolor("FFileName", "#00994C", index);
}
2025-01-01 08:21:58 +08:00
else
{
2025-02-10 09:24:44 +08:00
if (resMsg.IsNullOrEmpty())
resMsg = "识别失败!";
2025-01-01 08:21:58 +08:00
row["FisSucceed"] = false;
2025-02-10 09:24:44 +08:00
row["FResultMsg"] = resMsg;
grid.SetForecolor("FFileName", "#FF3300", index);
2025-01-01 08:21:58 +08:00
}
2024-12-05 15:39:19 +08:00
}
2025-02-10 09:24:44 +08:00
else
{
row["FisSucceed"] = false;
row["FResultMsg"] = "未知错误导致识别失败!";
grid.SetForecolor("FFileName", "#FF3300", index);
}
index++;
2024-12-05 15:39:19 +08:00
}
2025-02-10 09:24:44 +08:00
//fileUploaded = false;
this.View.UpdateView("FEntity");
2024-12-05 15:39:19 +08:00
}
}
2025-02-10 09:24:44 +08:00
2024-12-05 15:39:19 +08:00
/// <summary>
/// 文件转base64
/// </summary>
/// <returns>base64字符串</returns>
public string FileToBase64String(string fullName)
{
string base64Str = "";
try
{
var fileBytes = File.ReadAllBytes(fullName);
return Convert.ToBase64String(fileBytes);
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadLine();
return base64Str;
}
finally
{
//fsForRead.Close();
}
}
/// <summary>
/// 识别二维码
/// </summary>
/// <param name="src"></param>
/// <param name="region"></param>
/// <param name="isReturn"></param>
/// <returns></returns>
private static AnalyzeResult DecodeQRCodes(Mat src, Rect region, bool isReturn)
{
AnalyzeResult analyzeResult = null;
// 裁剪指定区域
using (Mat roi = new Mat(src, region))
{
// 转为灰度图像
using (Mat gray = new Mat())
{
Cv2.CvtColor(roi, gray, ColorConversionCodes.BGR2GRAY);
// 二值化处理
using (Mat binary = new Mat())
{
Cv2.Threshold(gray, binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
//二维码识别
using (QRCodeDetector qRCodeDetector = new QRCodeDetector())
{
Point2f[] points;
var hasQRCode = qRCodeDetector.DetectMulti(binary, out points);
if (hasQRCode)
{
qRCodeDetector.DecodeMulti(binary, points, out string[] qrCodeTexts);
analyzeResult = new AnalyzeResult();
analyzeResult.name = qrCodeTexts[0];
analyzeResult.points = points;
}
else
{
if (isReturn)
return null;
var srcWidth = roi.Width;
var srcHeight = roi.Height;
int x = srcWidth / 2;
int y = 0;
int width = srcWidth / 2;
int height = srcHeight / 2;
var newRegion = new Rect(x, y, width, height);
analyzeResult = DecodeQRCodes(roi, newRegion, true);
}
}
}
}
}
return analyzeResult;
}
public class AnalyzeResult
{
public string fileName { get; set; }
public string name { get; set; }
public Point2f[] points { get; set; }
}
/// <summary>
///
/// </summary>
/// <param name="formId"></param>
/// <param name="fileName"></param>
/// <param name="billNo"></param>
/// <param name="fileBytesLength"></param>
/// <param name="fileSuffix"></param>
/// <param name="base64"></param>
/// <returns></returns>
private string DoAttachmentUpload(string formId, string fileName, string billNo, int fileBytesLength, string fileSuffix, string base64)
{
if (this.Context.DBId.Equals("6735f10547df64"))
billNo = "ZHBR-XSCKD240911002";
var InterId = 0L;
2025-01-01 08:21:58 +08:00
var formBillNo = "";
2024-12-05 15:39:19 +08:00
var sqlL = $@"/*dialect*/
2025-01-01 08:21:58 +08:00
SELECT t0.FBILLNO,t0.FID,t0e.FENTRYID,t0e.FSEQ,t0e_lk.FSTABLENAME,t0e_lk.FSBILLID
2024-12-05 15:39:19 +08:00
FROM T_SAL_OUTSTOCK t0
2025-01-01 08:21:58 +08:00
INNER JOIN T_SAL_OUTSTOCKENTRY t0e on t0.FID = t0e.FID
INNER JOIN T_SAL_OUTSTOCKENTRY_LK t0e_lk on t0e_lk.FENTRYID = t0e.FENTRYID
AND t0e_lk.FSTABLENAME = 'T_SAL_DELIVERYNOTICEENTRY'
INNER JOIN T_SAL_DELIVERYNOTICE t1 on t1.FID = t0e_lk.FSBILLID
WHERE t1.FBILLNO = '{billNo}'
2024-12-05 15:39:19 +08:00
";
var data = DBServiceHelper.ExecuteDynamicObject(this.Context, sqlL);
if (data != null && data.Any())
2025-01-01 08:21:58 +08:00
{
2024-12-05 15:39:19 +08:00
InterId = data[0]["FID"].Long2Int();
2025-01-01 08:21:58 +08:00
formBillNo = data[0]["FBILLNO"].ToString();
}
else
{
return null;
}
2024-12-05 15:39:19 +08:00
var fileSql = @"/*dialect*/
SELECT *
FROM T_BAS_FILESERVERINFO
WHERE FUSED = 1
ORDER BY FENABLE DESC";
var fileServerInfo = DBServiceHelper.ExecuteDynamicObject(this.Context, fileSql);
if (fileServerInfo != null && fileServerInfo.Count > 0)
{
string jsonData = $@"
{{
""FileName"": ""{fileName}"",
""FEntryKey"": "" "",
""FormId"": ""{formId}"",
""IsLast"": true,
""InterId"": {InterId},
2025-01-01 08:21:58 +08:00
""BillNO"": ""{formBillNo}"",
2024-12-05 15:39:19 +08:00
""AliasFileName"": """",
""SendByte"": ""{base64}""
}}
";
var resultJson = WebApiServiceCall.AttachmentUpload(this.Context, jsonData);
return JsonConvert.SerializeObject(resultJson);
}
else
{
var jsonData = $@"
{{
""Model"": {{
""FAttachmentName"": ""{fileName}"",
""FBillType"": ""{formId}"",
""FInterID"": ""{InterId}"",
2025-01-01 08:21:58 +08:00
""FBillNo"": ""{formBillNo}"",
2025-02-10 09:24:44 +08:00
""FAttachmentSize"": {(Math.Round((decimal)fileBytesLength / 1024, 2))},
2024-12-05 15:39:19 +08:00
""FAttachment"":""{base64}"",
""FFILESTORAGE"":0,
""FExtName"": ""{fileSuffix}"",
""FEntryinterId"": ""-1"",
""FEntrykey"": "" "",
""FaliasFileName"": ""{fileName}"",
""FCreateMen"": {{
""FUserID"": ""{this.Context.UserId}""
}},
""FCreateTime"":""{DateTime.Now:F}""
}}
}}
";
var resultJson = WebApiServiceCall.Save(this.Context, "BOS_Attachment", jsonData);
return JsonConvert.SerializeObject(resultJson);
}
}
//K3CloudApi clienter = null;
bool fileUploaded = false;
public override void CustomEvents(CustomEventsArgs e)
{
base.CustomEvents(e);
if (e.EventName.ToUpper().Equals("FILECHANGED") && e.Key.ToUpper().Equals("FFILEUPDATECLIENT"))
{
fileUploaded = false;
var postData = KDObjectConverter.DeserializeObject<JSONObject>(e.EventArgs);
if (postData != null)
{
var uploadInfo = new JSONArray(postData["NewValue"].ToString());
var entity = this.View.Model.BillBusinessInfo.GetEntryEntity("FEntity");
var rows = this.View.Model.GetEntityDataObject(entity);
rows.Clear();
if (uploadInfo.Count > 0)
{
foreach (Dictionary<string, object> fileInfo in uploadInfo)
{
var row = new DynamicObject(entity.DynamicObjectType);
var fileName = fileInfo["FileName"].ToString();
var suffix = Path.GetExtension(fileName);
2025-01-01 08:21:58 +08:00
if (suffix.ToUpper().Equals(".PDF") || suffix.ToUpper().Equals(".JPG"))
2024-12-05 15:39:19 +08:00
{
row["FFileName"] = fileName;
row["FServerFileName"] = fileInfo["ServerFileName"];
row["FFileSuffix"] = suffix;
row["FFileBytesLength"] = fileInfo["FileBytesLength"];
rows.Add(row);
}
}
this.View.UpdateView("FEntity");
fileUploaded = true;
}
}
return;
}
}
}
}