using ICSharpCode.SharpZipLib.Tar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using wispro.sp.utility; using SharpCompress.Archives; using SharpCompress.Archives.Rar; using SharpCompress.Common; using System.Collections; using wispro.sp.entity.CompareCase; using wispro.sp.share.webViewObject; using System.Net.Http; using System.Net.Http.Json; using wispro.sp.share; using OpenQA.Selenium.Support.UI; using System.Dynamic; namespace wispro.sp.winClient { public partial class frmCaseFileCompare : Form { private CompareDocx draft_fristReturn; private CompareDocx fristReturn_Finally; public frmCaseFileCompare() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private HttpClient CreateHttp() { HttpClientHandler clientHandler = new HttpClientHandler(); clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }; return new HttpClient(clientHandler); } string strAPIBaseUri = "http://localhost:39476"; private async Task SaveCompareResult(CaseInfo caseinfo) { share.webViewObject.loginDto dto = new share.webViewObject.loginDto(); dto.Name = "caiyangl"; dto.Password = "Lqftiu807005"; var response = await CreateHttp().PostAsJsonAsync($"{strAPIBaseUri}/api/account/Login", dto); if (response.IsSuccessStatusCode) { userToken? Token = await response.Content.ReadFromJsonAsync(); HttpClient http = CreateHttp();// http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", Token?.Token); try { var data = await http.PostAsJsonAsync($"{strAPIBaseUri}/api/CaseFileCompare/Save", caseinfo); if (data.IsSuccessStatusCode) { var ret = await data.Content.ReadFromJsonAsync(); if(ret.Success) { return true; } else { throw new Exception(ret.ErrorMessage); } } else { return false; } } catch (Exception ex) { return false; } } return false; } private CaseInfo getCaseInfo_files(string caseNo,out string draftFile,out string returnFile,out string finalFile) { draftFile = null; returnFile = null; finalFile = null; var retFiles = wispro.sp.utility.IPEasyUtility.DownloadCaseFiles(caseNo); if (retFiles != null) { CaseInfo caseInfo = new CaseInfo(); caseInfo.CaseNo = retFiles.CaseNo; IDictionary keyValuePairs = (IDictionary)retFiles; if (keyValuePairs.ContainsKey("CaseName")) { caseInfo.CaseName = retFiles.CaseName; } if (keyValuePairs.ContainsKey("Customer")) { caseInfo.Customer = new entity.Customer() { Name = retFiles.Customer }; } if (keyValuePairs.ContainsKey("FirstDraftDate")) { caseInfo.DraftDate = string.IsNullOrEmpty(retFiles.FirstDraftDate) ? null : DateTime.Parse(retFiles.FirstDraftDate); } if (keyValuePairs.ContainsKey("ReturnDate")) { caseInfo.ReturnDate = string.IsNullOrEmpty(retFiles.ReturnDate) ? null : DateTime.Parse(retFiles.ReturnDate); } if (keyValuePairs.ContainsKey("FinalizationDate")) { caseInfo.FinalVersionDate = string.IsNullOrEmpty(retFiles.FinalizationDate) ? null : DateTime.Parse(retFiles.FinalizationDate); } if (keyValuePairs.ContainsKey("DoPersons")) { caseInfo.Handlers = retFiles.DoPersons; } if (keyValuePairs.ContainsKey("Reviewer")) { if (!string.IsNullOrEmpty(retFiles.Reviewer)) { caseInfo.Reviewer = new entity.Staff() { Name = retFiles.Reviewer }; } } if (keyValuePairs.ContainsKey("draftFile")) { draftFile = retFiles.draftFile; } if (keyValuePairs.ContainsKey("firstReturnFile")) { returnFile = retFiles.firstReturnFile; } if (keyValuePairs.ContainsKey("finallyFile")) { finalFile = retFiles.finallyFile; } return caseInfo; } else { return null; } } private void compareCaseFile() { DataTable table = wispro.sp.utility.IPEasyUtility.GetFinished3FilesCases(1); if (table != null) { foreach (DataRow row in table.Rows) { if (row["申请类型"].ToString().Trim() == "外观设计") { continue; } string draftFile = string.Empty; string returnFile =string.Empty; string finalFile =string.Empty; string CaseNo = row["我方文号"].ToString(); CaseInfo caseInfo = getCaseInfo_files(CaseNo, out draftFile,out returnFile, out finalFile); int i = (string.IsNullOrEmpty(draftFile) ? 1 : 0) + (string.IsNullOrEmpty(returnFile) ? 1 : 0) + (string.IsNullOrEmpty(finalFile) ? 1 : 0); if (i > 1) { CompareFiles(draftFile, returnFile, finalFile, caseInfo); SaveCompareResult(caseInfo); } } } } private void CompareFiles(string draftFile, string returnFile, string finalFile, CaseInfo caseInfo) { string strExtractPath = @"c:\temp"; string tmpdraftFile = ExtractFile(draftFile,0,strExtractPath); string tmpreturnFile = ExtractFile(returnFile, 1, strExtractPath); string tmpfinalFile = ExtractFile(finalFile, 2, strExtractPath); if (!string.IsNullOrEmpty(tmpdraftFile) && !string.IsNullOrEmpty(tmpreturnFile)) { CompareDocx docx = new CompareDocx(); docx.Compare(tmpdraftFile, tmpreturnFile); if(docx.FulltextResult.oldWordCount > 0) { caseInfo.DRRFulltext = docx.FulltextResult; } if (docx.AbstractResult.oldWordCount > 0) { caseInfo.DRRAbstract = docx.AbstractResult; } if (docx.ClaimResult.oldWordCount > 0) { caseInfo.DRRCalim = docx.ClaimResult; } if (docx.AbstractResult.oldWordCount > 0) { caseInfo.DRRAll = docx.AllStringResult; } } if (!string.IsNullOrEmpty(tmpfinalFile) && !string.IsNullOrEmpty(tmpreturnFile)) { CompareDocx docx = new CompareDocx(); docx.Compare(tmpreturnFile, tmpfinalFile); if (docx.FulltextResult.oldWordCount > 0) { caseInfo.RFRFulltext = docx.FulltextResult; } if (docx.AbstractResult.oldWordCount > 0) { caseInfo.RFRAbstract = docx.AbstractResult; } if (docx.ClaimResult.oldWordCount > 0) { caseInfo.RFRCalim = docx.ClaimResult; } if (docx.AllStringResult.oldWordCount > 0) { caseInfo.RFRAll = docx.AllStringResult; } } #region 清除下载和解压缩后的文件 if (System.IO.File.Exists(returnFile)) { System.IO.File.Delete(returnFile); } if (System.IO.File.Exists(draftFile)) { System.IO.File.Delete(draftFile); } if (System.IO.File.Exists(finalFile)) { System.IO.File.Delete(finalFile); } if (System.IO.Directory.Exists(Path.Combine(strExtractPath,returnFile.Substring(0,returnFile.Length -4)))) { System.IO.Directory.Delete(Path.Combine(strExtractPath, returnFile.Substring(0, returnFile.Length - 4)), true); } if (System.IO.Directory.Exists(Path.Combine(strExtractPath, draftFile.Substring(0, draftFile.Length - 4)))) { System.IO.Directory.Delete(Path.Combine(strExtractPath, draftFile.Substring(0, draftFile.Length - 4)),true); } if (System.IO.Directory.Exists(Path.Combine(strExtractPath, finalFile.Substring(0, finalFile.Length - 4)))) { System.IO.Directory.Delete(Path.Combine(strExtractPath, finalFile.Substring(0, finalFile.Length - 4)),true); } #endregion } private string ExtractFile(string file,int type,string strExtractPath) { if (file.ToLower().EndsWith(".zip") || file.ToLower().EndsWith(".rar")) { string extractPath = Path.Combine(strExtractPath, file.Substring(0,file.Length-4)); if (file.ToLower().EndsWith(".zip")) { ExtractZip(file, extractPath); } else { ExtractRarFile(file, extractPath); } foreach (string f in Directory.GetFiles(extractPath, "*.*", SearchOption.AllDirectories)) { switch (type) { case 0: if (f.ToUpper().IndexOf("V1R", StringComparison.OrdinalIgnoreCase) > 0) { return f; } break; case 1: if (f.ToUpper().IndexOf("V1F", StringComparison.OrdinalIgnoreCase) > 0) { return f; } break; case 2: if (f.IndexOf("定稿", StringComparison.OrdinalIgnoreCase) > 0) { return f; } break; } } return null; } else { return file; } } CaseInfo caseInfo; private void button1_Click(object sender, EventArgs e) { //compareCaseFile(); //return; if (string.IsNullOrEmpty(this.txtCaseNo.Text)) { MessageBox.Show("请输入案号!", "提示框", MessageBoxButtons.OK); this.txtCaseNo.Focus(); return; } string draftFile = string.Empty; string finalFile = string.Empty; string returnFile = string.Empty; caseInfo = getCaseInfo_files(txtCaseNo.Text.Trim(), out draftFile, out returnFile, out finalFile); int i = (string.IsNullOrEmpty(draftFile) ? 0 : 1) + (string.IsNullOrEmpty(returnFile) ? 0 : 1) + (string.IsNullOrEmpty(finalFile) ? 0 : 1); if (i > 1) { CompareFiles(draftFile, returnFile, finalFile, caseInfo); SaveCompareResult(caseInfo); } comboBox1.SelectedIndex = 0; } private void ExtractZip(string zipFilePath, string extractPath) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // 确保提取目录存在 Directory.CreateDirectory(extractPath); // 使用 ZipArchive 解压缩 ZIP 文件 using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Open)) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read, true, Encoding.GetEncoding("GB2312"))) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.FullName.EndsWith("/", StringComparison.Ordinal) || entry.FullName.EndsWith("\\", StringComparison.Ordinal)) { Console.WriteLine($"目录: {entry.FullName}"); } else { string entryPath = Path.Combine(extractPath, entry.FullName); Directory.CreateDirectory(new System.IO.FileInfo(entryPath).Directory.FullName); entry.ExtractToFile(entryPath, true); } } } } } public static void ExtractRarFile(string rarPath, string outputPath) { try { // 确保输出目录存在 Directory.CreateDirectory(outputPath); // 打开RAR文件 using (var archive = RarArchive.Open(rarPath)) { // 遍历所有文件 foreach (var entry in archive.Entries) { if (!entry.IsDirectory) { // 构建完整输出路径 string outputFilePath = Path.Combine(outputPath, entry.Key); // 确保输出文件的目录存在 Directory.CreateDirectory(Path.GetDirectoryName(outputFilePath)); // 解压文件 entry.WriteToFile(outputFilePath, new ExtractionOptions { ExtractFullPath = true, Overwrite = true }); } } } } catch (Exception ex) { throw new Exception($"解压RAR文件时出错: {ex.Message}"); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (caseInfo != null ) { richTextBox1.Clear(); string strHtml = string.Empty; switch (comboBox1.SelectedIndex) { case 0: if (caseInfo.DRRAll != null) { strHtml = $"所有文字比较:

初稿与第一次返稿比较结果:

初稿文件字数:{caseInfo.DRRAll.oldWordCount}

删除文字数量:{caseInfo.DRRAll.DeleteCount}

插入文字数量:{caseInfo.DRRAll.InsertCount}

修改比率:{(caseInfo.DRRAll.diffRate * 100).ToString("0.0000")}%

文档语义相似度:{(caseInfo.DRRAll.TextSimilarity*100).ToString("0.0000")}%

"; } if (caseInfo.RFRAll != null) { strHtml = strHtml + $"

第一次返稿与定稿比较结果:

第一次返稿文件字数:{ caseInfo.RFRAll.oldWordCount}

删除文字数量:{caseInfo.RFRAll.DeleteCount}

插入文字数量:{caseInfo.RFRAll.InsertCount}

修改比率:{(caseInfo.RFRAll.diffRate * 100).ToString("0.0000")}%

文档语义相似度:{(caseInfo.RFRAll.TextSimilarity * 100).ToString("0.0000")}%

"; } richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(strHtml); break; case 1: if (caseInfo.DRRAll != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(caseInfo.DRRAll.CompareResultString); } break; case 2: if (caseInfo.RFRAll != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(caseInfo.RFRAll.CompareResultString); } break; case 3: if (caseInfo.DRRAbstract != null) { strHtml = $"摘要比较:

初稿与第一次返稿比较结果:

初稿文件字数:{caseInfo.DRRAbstract.oldWordCount}

删除文字数量:{caseInfo.DRRAbstract.DeleteCount}

插入文字数量:{caseInfo.DRRAbstract.InsertCount}

修改比率:{(caseInfo.DRRAbstract.diffRate * 100).ToString("0.0000")}%

语义相似度:{(caseInfo.DRRAbstract.TextSimilarity * 100).ToString("0.0000")}%

"; } if (caseInfo.RFRAbstract != null) { strHtml = strHtml + $"

第一次返稿与定稿比较结果:

第一次返稿文件字数:{caseInfo.RFRAbstract.oldWordCount}

删除文字数量:{caseInfo.RFRAbstract.DeleteCount}

插入文字数量:{caseInfo.RFRAbstract.InsertCount}

修改比率:{(caseInfo.RFRAbstract.diffRate * 100).ToString("0.0000")}%

语义相似度:{(caseInfo.RFRAbstract.TextSimilarity * 100).ToString("0.0000")}%

"; } richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(strHtml); break; case 4: if (caseInfo.DRRAbstract != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(caseInfo.DRRAbstract.CompareResultString); } break; case 5: if (caseInfo.RFRAbstract != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(caseInfo.RFRAbstract.CompareResultString); } break; case 6: if (caseInfo.DRRCalim != null) { strHtml = $"权力要求比较:

初稿与第一次返稿比较结果:

初稿文件字数:{caseInfo.DRRCalim.oldWordCount}

删除文字数量:{caseInfo.DRRCalim.DeleteCount}

插入文字数量:{caseInfo.DRRCalim.InsertCount}

修改比率:{(caseInfo.DRRCalim.diffRate * 100).ToString("0.0000")}%

语义相似度:{(caseInfo.DRRCalim.TextSimilarity * 100).ToString("0.0000")}%

"; } if (caseInfo.RFRCalim != null) { strHtml = strHtml + $"

第一次返稿与定稿比较结果:

第一次返稿文件字数:{caseInfo.RFRCalim.oldWordCount}

删除文字数量:{caseInfo.RFRCalim.DeleteCount}

插入文字数量:{caseInfo.RFRCalim.InsertCount}

修改比率:{(caseInfo.RFRCalim.diffRate * 100).ToString("0.0000")}%

语义相似度:{(caseInfo.RFRCalim.TextSimilarity * 100).ToString("0.0000")}%

"; } richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(strHtml); break; case 7: if (caseInfo.DRRCalim != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(caseInfo.DRRCalim.CompareResultString); } break; case 8: if (caseInfo.RFRCalim != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(caseInfo.RFRCalim.CompareResultString); } break; case 9: if (caseInfo.DRRFulltext != null) { strHtml = $"说明书比较:

初稿与第一次返稿比较结果:

初稿文件字数:{caseInfo.DRRFulltext.oldWordCount}

删除文字数量:{caseInfo.DRRFulltext.DeleteCount}

插入文字数量:{caseInfo.DRRFulltext.InsertCount}

修改比率:{(caseInfo.DRRFulltext.diffRate * 100).ToString("0.0000")}%

语义相似度:{(caseInfo.DRRFulltext.TextSimilarity * 100).ToString("0.0000")}%

"; } if (caseInfo.RFRFulltext != null) { strHtml = strHtml + $"

第一次返稿与定稿比较结果:

第一次返稿文件字数:{caseInfo.RFRFulltext.oldWordCount}

删除文字数量:{caseInfo.RFRFulltext.DeleteCount}

插入文字数量:{caseInfo.RFRFulltext.InsertCount}

修改比率:{(caseInfo.RFRFulltext.diffRate * 100).ToString("0.0000")}%

语义相似度:{(caseInfo.RFRFulltext.TextSimilarity * 100).ToString("0.0000")}%

"; } richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(strHtml); break; case 10: if (caseInfo.DRRFulltext != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(caseInfo.DRRFulltext.CompareResultString); } break; case 11: if (caseInfo.RFRFulltext != null) { richTextBox1.Rtf = new HtmlToRtfConverter().ConvertHtmlToRtf(caseInfo.RFRFulltext.CompareResultString); } break; } } } private void button2_Click(object sender, EventArgs e) { new Form1().ShowDialog(); return; var caseInfo = IPEasyUtility.DownloadReport("每月绩效统计--上个月递交完成案件",true); return; var retData = IPEasyUtility.GetFinished3FilesCases(1); return; OpenFileDialog openFileDialog = new OpenFileDialog(); if(openFileDialog.ShowDialog() == DialogResult.OK) { string strPath = openFileDialog.FileName.ToLower().Replace(".rar", "").Replace(".zip",""); if(openFileDialog.FileName.Contains(".rar")) { ExtractRarFile(openFileDialog.FileName, strPath); } else { ExtractZip(openFileDialog.FileName,strPath); } string[] files = Directory.EnumerateFiles(strPath, "*.*", SearchOption.AllDirectories).ToArray(); Array.Sort(files); Array.Reverse(files); draft_fristReturn = new CompareDocx(); draft_fristReturn.Compare(files[0], files[1]); if (files.Length > 2) { fristReturn_Finally = new CompareDocx(); fristReturn_Finally.Compare(files[1], files[2]); } SaveResult(strPath,draft_fristReturn); SaveResult(strPath, fristReturn_Finally); } } private void SaveResult(string strPath,CompareDocx draft_fristReturn) { string strHtml = @" 对比结果
{0}
{1}
全部对比 摘要 权利要求 说明书
原文字数 {2} {3} {4} {5}
删除字数 {6} {7} {8} {9}
插入字数 {10} {11} {12} {13}
修改比例 {14} {15} {16} {17}
语义相似度 {18} {19} {20} {21}
{22}
{23}
{24}
{25}
"; if (draft_fristReturn != null) { var temHtml = strHtml .Replace("{0}", new FileInfo(draft_fristReturn.oldDocument.FilePath).Name) .Replace("{1}", new FileInfo(draft_fristReturn.newDocument.FilePath).Name) .Replace("{2}", draft_fristReturn.AllStringResult.oldWordCount.ToString()) .Replace("{3}", draft_fristReturn.AbstractResult.oldWordCount.ToString()) .Replace("{4}", draft_fristReturn.ClaimResult.oldWordCount.ToString()) .Replace("{5}", draft_fristReturn.FulltextResult.oldWordCount.ToString()) .Replace("{6}", draft_fristReturn.AllStringResult.DeleteCount.ToString()) .Replace("{7}", draft_fristReturn.AbstractResult.DeleteCount.ToString()) .Replace("{8}", draft_fristReturn.ClaimResult.DeleteCount.ToString()) .Replace("{9}", draft_fristReturn.FulltextResult.DeleteCount.ToString()) .Replace("{10}", draft_fristReturn.AllStringResult.InsertCount.ToString()) .Replace("{11}", draft_fristReturn.AbstractResult.InsertCount.ToString()) .Replace("{12}", draft_fristReturn.ClaimResult.InsertCount.ToString()) .Replace("{13}", draft_fristReturn.FulltextResult.InsertCount.ToString()) .Replace("{14}", (draft_fristReturn.AllStringResult.diffRate * 100).ToString("0.0000") + "%") .Replace("{15}", (draft_fristReturn.AbstractResult.diffRate * 100).ToString("0.0000") + "%") .Replace("{16}", (draft_fristReturn.ClaimResult.diffRate * 100).ToString("0.0000") + "%") .Replace("{17}", (draft_fristReturn.FulltextResult.diffRate * 100).ToString("0.0000") + "%") .Replace("{18}", (draft_fristReturn.AllStringResult.TextSimilarity * 100).ToString("0.0000") + "%") .Replace("{19}", (draft_fristReturn.AbstractResult.TextSimilarity * 100).ToString("0.0000") + "%") .Replace("{20}", (draft_fristReturn.ClaimResult.TextSimilarity * 100).ToString("0.0000") + "%") .Replace("{21}", (draft_fristReturn.FulltextResult.TextSimilarity * 100).ToString("0.0000") + "%") .Replace("{22}", draft_fristReturn.AllStringResult.CompareResultString) .Replace("{23}", draft_fristReturn.AbstractResult.CompareResultString) .Replace("{24}", draft_fristReturn.ClaimResult.CompareResultString) .Replace("{25}", draft_fristReturn.FulltextResult.CompareResultString); string fileName = $"{new FileInfo(draft_fristReturn.oldDocument.FilePath).Name}_{new FileInfo(draft_fristReturn.newDocument.FilePath).Name}.html"; using (var f = File.CreateText(Path.Combine(strPath, fileName))) { f.Write(temHtml); } } } } }