feat(hjgl): 完善图纸识别与材料管段信息管理
扩展图纸识别结果模型和保存流程,按管线及页码替换识别明细, 避免重复导入造成数据累积;同时自动补充材料编码库数据, 新增材料总览、管段长度总览及 PDF 焊口标记功能,提升识别数据 的查询、复核和后续使用效率。
This commit is contained in:
@@ -2,16 +2,14 @@
|
||||
using BLL;
|
||||
using BLL.Common;
|
||||
using Model;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Transactions;
|
||||
using UglyToad.PdfPig;
|
||||
using UglyToad.PdfPig.Content;
|
||||
using UglyToad.PdfPig.Writer;
|
||||
@@ -20,6 +18,9 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
{
|
||||
public partial class DrawingRecognitionContent : PageBase
|
||||
{
|
||||
internal const string MaterialSessionSuffix = ":DrawingRecognitionMaterials";
|
||||
internal const string PipeLengthSessionSuffix = ":DrawingRecognitionPipeLengths";
|
||||
|
||||
public string URL
|
||||
{
|
||||
get
|
||||
@@ -42,44 +43,64 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
ViewState["fileUrl"] = value;
|
||||
}
|
||||
}
|
||||
public DataTable dt
|
||||
private string RecognitionDataKey
|
||||
{
|
||||
get
|
||||
{
|
||||
return (DataTable)ViewState["DataTable"];
|
||||
return (string)ViewState["RecognitionDataKey"];
|
||||
}
|
||||
set
|
||||
{
|
||||
ViewState["DataTable"] = value;
|
||||
ViewState["RecognitionDataKey"] = value;
|
||||
}
|
||||
}
|
||||
public DataTable dtOther
|
||||
|
||||
private string MaterialSessionKey
|
||||
{
|
||||
get
|
||||
{
|
||||
return (DataTable)ViewState["dtOther"];
|
||||
}
|
||||
set
|
||||
{
|
||||
ViewState["dtOther"] = value;
|
||||
return RecognitionDataKey + MaterialSessionSuffix;
|
||||
}
|
||||
}
|
||||
public DataTable dtIsoInfo
|
||||
|
||||
private string PipeLengthSessionKey
|
||||
{
|
||||
get
|
||||
{
|
||||
return (DataTable)ViewState["dtIsoInfo"];
|
||||
return RecognitionDataKey + PipeLengthSessionSuffix;
|
||||
}
|
||||
}
|
||||
|
||||
private List<HJGL_DrawingRecognition_Material> MaterialEntities
|
||||
{
|
||||
get
|
||||
{
|
||||
return Session[MaterialSessionKey] as List<HJGL_DrawingRecognition_Material>;
|
||||
}
|
||||
set
|
||||
{
|
||||
ViewState["dtIsoInfo"] = value;
|
||||
Session[MaterialSessionKey] = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private List<HJGL_DrawingRecognition_PipeLengths> PipeLengthEntities
|
||||
{
|
||||
get
|
||||
{
|
||||
return Session[PipeLengthSessionKey] as List<HJGL_DrawingRecognition_PipeLengths>;
|
||||
}
|
||||
set
|
||||
{
|
||||
Session[PipeLengthSessionKey] = value;
|
||||
}
|
||||
}
|
||||
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
{
|
||||
if (!IsPostBack)
|
||||
{
|
||||
UnitWorkService.InitUnitWorkDownList(drpUnitWork, this.CurrUser.LoginProjectId, true);
|
||||
RecognitionDataKey = Guid.NewGuid().ToString("N");
|
||||
if (!string.IsNullOrEmpty(Request.Params["fileUrl"]))
|
||||
{
|
||||
URL = Funs.SGGLUrl + Request.Params["fileUrl"].Replace("\\", "/");
|
||||
@@ -99,18 +120,49 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
|
||||
protected void btnSave_Click(object sender, EventArgs e)
|
||||
{
|
||||
Grid1.Hidden = true;
|
||||
contentPanel1.Hidden =false ;
|
||||
string rootPath = Server.MapPath("~/");
|
||||
Dictionary<string,List<int>> dic= new Dictionary<string, List<int>>();
|
||||
foreach (DataRow row in dt.Rows)
|
||||
DrawingRecognitionResult recognitionResult;
|
||||
List<HJGL_DrawingRecognition_Material> materialEntities;
|
||||
List<HJGL_DrawingRecognition_PipeLengths> pipeLengthEntities;
|
||||
Dictionary<string, HJGL_Pipeline> pipelinesByCode;
|
||||
if(drpUnitWork.SelectedValue == null|| drpUnitWork.SelectedValue==Const._Null)
|
||||
{
|
||||
if (!dic.ContainsKey(row["pipe_no"].ToString()))
|
||||
{
|
||||
dic.Add(row["pipe_no"].ToString().Trim(),new List<int>());
|
||||
}
|
||||
dic[row["pipe_no"].ToString().Trim()].Add(int.Parse(row["page_no"].ToString()));
|
||||
Alert.ShowInTop("请选择单位工程", MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
recognitionResult = DeserializeRecognitionResult();
|
||||
materialEntities = MaterialEntities;
|
||||
pipeLengthEntities = PipeLengthEntities;
|
||||
if (materialEntities == null || pipeLengthEntities == null)
|
||||
{
|
||||
throw new InvalidOperationException("实体数据已失效,请重新审核识别结果");
|
||||
}
|
||||
|
||||
// DrawingInfo 以原始页码关联 C3 管线号,并同步当前项目的管线基础信息。
|
||||
pipelinesByCode = UpsertPipelinesFromDrawingInfo(recognitionResult);
|
||||
Funs.DB.SubmitChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Alert.ShowInTop("识别数据校验或管线保存失败:" + ex.Message, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
Grid1.Hidden = true;
|
||||
Grid2.Hidden = true;
|
||||
contentPanel1.Hidden = false;
|
||||
string rootPath = Server.MapPath("~/");
|
||||
// PDF 原始页与管线号的关系来自 C3,实体中的 pipe_page_no 保存的是 C1,不能替代原始页码。
|
||||
Dictionary<string, List<int>> dic = recognitionResult.OtherRegions
|
||||
.Where(x => x.Page.HasValue && string.Equals(x.RegionCode, "C3", StringComparison.OrdinalIgnoreCase)
|
||||
&& !string.IsNullOrWhiteSpace(x.Text))
|
||||
.GroupBy(x => x.Text.Trim(), StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(
|
||||
group => group.Key,
|
||||
group => group.Select(x => x.Page.Value).Distinct().ToList(),
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
using (var pdf = PdfDocument.Open(fileUrl))
|
||||
{
|
||||
|
||||
@@ -133,17 +185,23 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
}
|
||||
string fileName = key;
|
||||
|
||||
var row = dtOther.Select(" page='" + (i + 1) + "' and region_type='C' ").FirstOrDefault();
|
||||
if (row != null)
|
||||
var pipePageRegion = recognitionResult.OtherRegions.FirstOrDefault(x =>
|
||||
x.Page == i + 1 && string.Equals(x.RegionCode, "C1", StringComparison.OrdinalIgnoreCase));
|
||||
if (pipePageRegion != null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(row["text"].ToString()))
|
||||
if (!string.IsNullOrWhiteSpace(pipePageRegion.Text))
|
||||
{
|
||||
fileName = row["text"].ToString();
|
||||
fileName = pipePageRegion.Text.Trim();
|
||||
}
|
||||
}
|
||||
outputPath = $"{outputPath}/" + fileName + DateTime.Now.ToString("yyyyMMddHHmm")+(i + 1) + ".pdf";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(outputPath))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
using (var writer = new PdfDocumentBuilder())
|
||||
{
|
||||
@@ -164,82 +222,8 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
var newPdf = writer.Build();
|
||||
File.WriteAllBytes(outputPath, newPdf);
|
||||
|
||||
var isoInfo = Funs.DB.HJGL_Pipeline.FirstOrDefault(x => x.PipelineCode == isono && x.ProjectId==CurrUser.LoginProjectId);
|
||||
if (isoInfo != null)
|
||||
if (pipelinesByCode.TryGetValue(isono, out var isoInfo))
|
||||
{
|
||||
Regex NumberRegex = new Regex(@"\d+\.?\d*");
|
||||
DataRow[] rows = dt.Select(" pipe_no='" + isono + "' and category='PIPE'");
|
||||
if (rows != null && rows.Length > 0)
|
||||
{
|
||||
//string description = rows[0]["description"].ToString();
|
||||
|
||||
//if (!isoInfo.ISO_Dia.HasValue || !isoInfo.ISO_Sch.HasValue || string.IsNullOrEmpty(isoInfo.MaterialStandardId))
|
||||
//{
|
||||
// string res = OpenAIhelper.ChatCompletion("请根据 " + description + ",识别外径,壁厚,材质标准(请严格按照国家标准返回),结果仅以JSON格式返回,不要解释,不要多余文字,不要markdown,例如 {\r\n \"外径\": \"33.4 mm\",\r\n \"壁厚\": \"6.35 mm\",\r\n \"材质标准\": \"GB/T 9948\"\r\n}");
|
||||
// ErrLogInfo.WriteLog(res);
|
||||
// JObject jobject = JObject.Parse(res);
|
||||
// string dia = jobject.Value<string>("外径");
|
||||
// string sch = jobject.Value<string>("壁厚");
|
||||
// string materialStandard = jobject.Value<string>("材质标准");
|
||||
// MatchCollection matchCollection1 = NumberRegex.Matches(dia);
|
||||
// foreach (var match in matchCollection1)
|
||||
// {
|
||||
// // 使用 TryParse 确保转换安全,避免异常
|
||||
// if (decimal.TryParse(match.ToString(), out decimal number))
|
||||
// {
|
||||
// isoInfo.ISO_Dia = number;
|
||||
// }
|
||||
// }
|
||||
|
||||
// MatchCollection matchCollection2 = NumberRegex.Matches(sch);
|
||||
// foreach (var match in matchCollection2)
|
||||
// {
|
||||
// // 使用 TryParse 确保转换安全,避免异常
|
||||
// if (decimal.TryParse(match.ToString(), out decimal number))
|
||||
// {
|
||||
// isoInfo.ISO_Sch = number;
|
||||
// }
|
||||
// }
|
||||
|
||||
// isoInfo.MaterialStandardId = Funs.DB.HJGL_BS_MaterialStandard.Where(x => x.MaterialStandardCode == materialStandard).Select(x => x.MaterialStandardId).FirstOrDefault();
|
||||
//}
|
||||
|
||||
|
||||
//decimal length = 0;
|
||||
//foreach (DataRow row in rows)
|
||||
//{
|
||||
|
||||
// MatchCollection matches = NumberRegex.Matches(row["qty"].ToString());
|
||||
// foreach (var match in matches)
|
||||
// {
|
||||
// // 使用 TryParse 确保转换安全,避免异常
|
||||
// if (decimal.TryParse(match.ToString(), out decimal number))
|
||||
// {
|
||||
// length += number;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//isoInfo.PipeLineLength = length;
|
||||
|
||||
}
|
||||
//DataRow[] rowsPipe = dtIsoInfo.Select(" pipeline_id='" + isono + "' ");
|
||||
//if (rowsPipe != null && rowsPipe.Length > 0)
|
||||
//{
|
||||
// string isNeedHead = rowsPipe[0]["post_weld_heat_treatment"].ToString();
|
||||
// if (isNeedHead == "N")
|
||||
// {
|
||||
// isoInfo.IsHot = "0";
|
||||
// isoInfo.IsHotType = null;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// isoInfo.IsHot = "1";
|
||||
// }
|
||||
|
||||
|
||||
//}
|
||||
Funs.DB.SubmitChanges();
|
||||
//保存文件到附件
|
||||
var attatch = AttachFileService.GetAttachFileByToKeyId(isoInfo.PipelineId);
|
||||
if (attatch != null)
|
||||
@@ -257,7 +241,91 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ReplaceRecognitionDetails(materialEntities, pipeLengthEntities);
|
||||
|
||||
List<Tw_InputDataIn> tw_InputDataIns = new List<Tw_InputDataIn>();
|
||||
|
||||
foreach (var item in materialEntities)
|
||||
{
|
||||
var model = new Tw_InputDataIn()
|
||||
{
|
||||
MaterialCode = item.Code,
|
||||
MaterialUnit = item.Unit!=null? item.Unit.Contains("M") ? "米" : "个":null,
|
||||
MaterialDef = item.Description,
|
||||
MaterialSpec = item.Spec,
|
||||
};
|
||||
if(!tw_InputDataIns.Contains(model)) tw_InputDataIns.Add(model);
|
||||
|
||||
}
|
||||
MaterialCodeLibService.AddListByDraw(tw_InputDataIns);
|
||||
Session.Remove(MaterialSessionKey);
|
||||
Session.Remove(PipeLengthSessionKey);
|
||||
btnSave.Hidden = true;
|
||||
ShowNotify("管线、材料表和管段长度表保存成功!", MessageBoxIcon.Success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按管线号和管线页码替换本次识别范围内的材料及管长数据。
|
||||
/// </summary>
|
||||
private static void ReplaceRecognitionDetails(
|
||||
List<HJGL_DrawingRecognition_Material> materialEntities,
|
||||
List<HJGL_DrawingRecognition_PipeLengths> pipeLengthEntities)
|
||||
{
|
||||
var recognitionPages = materialEntities
|
||||
.Select(x => new { PipeNo = x.Pipe_no, PipePageNo = x.Pipe_page_no })
|
||||
.Concat(pipeLengthEntities.Select(x => new { PipeNo = x.Pipe_no, PipePageNo = x.Pipe_page_no }))
|
||||
.ToList();
|
||||
if (recognitionPages.Any(x => string.IsNullOrWhiteSpace(x.PipeNo) || !x.PipePageNo.HasValue))
|
||||
{
|
||||
throw new InvalidOperationException("材料或管长数据存在缺少管线号、管线页码的记录");
|
||||
}
|
||||
if (recognitionPages.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 两类识别结果共用替换范围,确保某页本次未识别到其中一类数据时也能清理旧记录。
|
||||
var pageNumbersByPipeNo = recognitionPages
|
||||
.GroupBy(x => x.PipeNo.Trim(), StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(
|
||||
group => group.Key,
|
||||
group => new HashSet<int>(group.Select(x => x.PipePageNo.Value)),
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
var pipeNumbers = pageNumbersByPipeNo.Keys.ToList();
|
||||
|
||||
var oldMaterials = Funs.DB.HJGL_DrawingRecognition_Material
|
||||
.Where(x => pipeNumbers.Contains(x.Pipe_no))
|
||||
.ToList()
|
||||
.Where(x => IsRecognitionPage(pageNumbersByPipeNo, x.Pipe_no, x.Pipe_page_no))
|
||||
.ToList();
|
||||
var oldPipeLengths = Funs.DB.HJGL_DrawingRecognition_PipeLengths
|
||||
.Where(x => pipeNumbers.Contains(x.Pipe_no))
|
||||
.ToList()
|
||||
.Where(x => IsRecognitionPage(pageNumbersByPipeNo, x.Pipe_no, x.Pipe_page_no))
|
||||
.ToList();
|
||||
|
||||
using (var scope = new TransactionScope())
|
||||
{
|
||||
Funs.DB.HJGL_DrawingRecognition_Material.DeleteAllOnSubmit(oldMaterials);
|
||||
Funs.DB.HJGL_DrawingRecognition_PipeLengths.DeleteAllOnSubmit(oldPipeLengths);
|
||||
Funs.DB.SubmitChanges();
|
||||
|
||||
Funs.DB.HJGL_DrawingRecognition_Material.InsertAllOnSubmit(materialEntities);
|
||||
Funs.DB.HJGL_DrawingRecognition_PipeLengths.InsertAllOnSubmit(pipeLengthEntities);
|
||||
Funs.DB.SubmitChanges();
|
||||
scope.Complete();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsRecognitionPage(
|
||||
Dictionary<string, HashSet<int>> pageNumbersByPipeNo,
|
||||
string pipeNo,
|
||||
int? pipePageNo)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(pipeNo) &&
|
||||
pipePageNo.HasValue &&
|
||||
pageNumbersByPipeNo.TryGetValue(pipeNo.Trim(), out var pageNumbers) &&
|
||||
pageNumbers.Contains(pipePageNo.Value);
|
||||
}
|
||||
public static string HeaderCorrespondence(string uploadUrl, string data, string method, string contenttype)
|
||||
{
|
||||
@@ -320,129 +388,394 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
|
||||
protected void btnAudit_Click(object sender, EventArgs e)
|
||||
{
|
||||
Grid1.Hidden = false;
|
||||
contentPanel1.Hidden = true;
|
||||
btnAudit.Hidden = true;
|
||||
btnSave.Hidden = false;
|
||||
if (!string.IsNullOrEmpty(resultdata.Value))
|
||||
{
|
||||
JObject jobject = JObject.Parse(resultdata.Value);
|
||||
JArray jArray = jobject.Value<JArray>("material_rows");
|
||||
JArray jArrayOther = jobject.Value<JArray>("other_regions");
|
||||
JArray jArrayIsoInfo = jobject.Value<JArray>("drawing_info");
|
||||
dt = new DataTable();
|
||||
dt.Columns.Add("id");
|
||||
dt.Columns.Add("pipe_no");
|
||||
dt.Columns.Add("drawing_number");
|
||||
dt.Columns.Add("seq_no");
|
||||
dt.Columns.Add("category");
|
||||
dt.Columns.Add("description");
|
||||
dt.Columns.Add("spec");
|
||||
dt.Columns.Add("qty");
|
||||
dt.Columns.Add("code");
|
||||
dt.Columns.Add("page_no");
|
||||
dt.Columns.Add("remark");
|
||||
|
||||
dtOther = new DataTable();
|
||||
dtOther.Columns.Add("id");
|
||||
dtOther.Columns.Add("region_type");
|
||||
dtOther.Columns.Add("region_label");
|
||||
dtOther.Columns.Add("page");
|
||||
dtOther.Columns.Add("text");
|
||||
|
||||
|
||||
dtIsoInfo = new DataTable();
|
||||
dtIsoInfo.Columns.Add("id");
|
||||
dtIsoInfo.Columns.Add("page_no");
|
||||
dtIsoInfo.Columns.Add("drawing_number");
|
||||
dtIsoInfo.Columns.Add("pipeline_id");
|
||||
dtIsoInfo.Columns.Add("operation_pressure_mpa");
|
||||
dtIsoInfo.Columns.Add("operation_temp_c");
|
||||
dtIsoInfo.Columns.Add("design_pressure_mpa");
|
||||
dtIsoInfo.Columns.Add("design_temp_c");
|
||||
dtIsoInfo.Columns.Add("test_pressure_mpa");
|
||||
dtIsoInfo.Columns.Add("hydraulic_test_pressure");
|
||||
dtIsoInfo.Columns.Add("pipe_diameter_dn");
|
||||
dtIsoInfo.Columns.Add("pipe_spec_grade");
|
||||
dtIsoInfo.Columns.Add("heat_insulation");
|
||||
dtIsoInfo.Columns.Add("insulation_thickness_mm");
|
||||
dtIsoInfo.Columns.Add("post_weld_heat_treatment");
|
||||
dtIsoInfo.Columns.Add("radiographic_examination");
|
||||
foreach (JObject item in jArrayOther)
|
||||
try
|
||||
{
|
||||
var row = dtOther.NewRow();
|
||||
row["id"] = item.Value<string>("id");
|
||||
row["region_type"] = item.Value<string>("region_type");
|
||||
row["region_label"] = item.Value<string>("region_label");
|
||||
row["page"] = item.Value<string>("page");
|
||||
row["text"] = item.Value<string>("text");
|
||||
dtOther.Rows.Add(row);
|
||||
var recognitionResult = DeserializeRecognitionResult();
|
||||
var pageRegions = BuildPageRegionMap(recognitionResult.OtherRegions);
|
||||
MaterialEntities = MapMaterialEntities(recognitionResult.MaterialRows, pageRegions);
|
||||
PipeLengthEntities = MapPipeLengthEntities(recognitionResult.PipeLengthRows, pageRegions);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Alert.ShowInTop("识别结果解析失败:" + ex.Message, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
foreach (JObject item in jArrayIsoInfo)
|
||||
{
|
||||
|
||||
|
||||
var row = dtIsoInfo.NewRow();
|
||||
row["id"] = item.Value<string>("id");
|
||||
row["page_no"] = item.Value<string>("page_no");
|
||||
row["drawing_number"] = item.Value<string>("drawing_number");
|
||||
row["pipeline_id"] = item.Value<string>("pipeline_id");
|
||||
row["operation_temp_c"] = item.Value<string>("operation_temp_c");
|
||||
row["design_pressure_mpa"] = item.Value<string>("design_pressure_mpa");
|
||||
row["design_temp_c"] = item.Value<string>("design_temp_c");
|
||||
row["test_pressure_mpa"] = item.Value<string>("test_pressure_mpa");
|
||||
row["hydraulic_test_pressure"] = item.Value<string>("hydraulic_test_pressure");
|
||||
row["pipe_diameter_dn"] = item.Value<string>("pipe_diameter_dn");
|
||||
row["pipe_spec_grade"] = item.Value<string>("pipe_spec_grade");
|
||||
row["heat_insulation"] = item.Value<string>("heat_insulation");
|
||||
row["insulation_thickness_mm"] = item.Value<string>("insulation_thickness_mm");
|
||||
row["post_weld_heat_treatment"] = item.Value<string>("post_weld_heat_treatment");
|
||||
row["radiographic_examination"] = item.Value<string>("radiographic_examination");
|
||||
dtIsoInfo.Rows.Add(row);
|
||||
}
|
||||
|
||||
// 遍历并提取数据
|
||||
foreach (JObject item in jArray)
|
||||
{
|
||||
var row = dt.NewRow();
|
||||
row["id"] = item.Value<string>("id");
|
||||
row["pipe_no"] = item.Value<string>("pipe_no");
|
||||
row["drawing_number"] = item.Value<string>("drawing_number");
|
||||
|
||||
row["code"] = item.Value<string>("code");
|
||||
row["seq_no"] = item.Value<string>("seq_no");
|
||||
row["category"] = item.Value<string>("category");
|
||||
row["description"] = item.Value<string>("description");
|
||||
row["spec"] = item.Value<string>("spec");
|
||||
row["qty"] = item.Value<string>("qty");
|
||||
row["page_no"] = item.Value<string>("page_no");
|
||||
row["remark"] = item.Value<string>("remark");
|
||||
if (string.IsNullOrEmpty(row["pipe_no"].ToString()))
|
||||
{
|
||||
var rowOther = dtOther.Select("page= " + row["page_no"].ToString()).FirstOrDefault();
|
||||
row["pipe_no"] = rowOther["text"];
|
||||
}
|
||||
dt.Rows.Add(row);
|
||||
}
|
||||
|
||||
BindGrid();
|
||||
Grid1.Hidden = false;
|
||||
Grid2.Hidden = false;
|
||||
contentPanel1.Hidden = true;
|
||||
btnAudit.Hidden = true;
|
||||
btnSave.Hidden = false;
|
||||
BindGrids();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void BindGrid()
|
||||
private DrawingRecognitionResult DeserializeRecognitionResult()
|
||||
{
|
||||
|
||||
if (string.IsNullOrWhiteSpace(resultdata.Value))
|
||||
{
|
||||
throw new InvalidOperationException("识别结果为空");
|
||||
}
|
||||
|
||||
Grid1.DataSource = dt;
|
||||
var recognitionResult = JsonConvert.DeserializeObject<DrawingRecognitionResult>(resultdata.Value);
|
||||
if (recognitionResult == null)
|
||||
{
|
||||
throw new InvalidOperationException("识别结果格式不正确");
|
||||
}
|
||||
|
||||
recognitionResult.MaterialRows = recognitionResult.MaterialRows ?? new List<DrawingRecognitionMaterialRow>();
|
||||
recognitionResult.PipeLengthRows = recognitionResult.PipeLengthRows ?? new List<DrawingRecognitionPipeLengthRow>();
|
||||
recognitionResult.OtherRegions = recognitionResult.OtherRegions ?? new List<DrawingRecognitionOtherRegion>();
|
||||
recognitionResult.DrawingInfo = recognitionResult.DrawingInfo ?? new List<DrawingRecognitionInfoRow>();
|
||||
return recognitionResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构建原始页码与识别区域的映射,供 C1、C3 等区域统一取值。
|
||||
/// </summary>
|
||||
private static Dictionary<int, Dictionary<string, string>> BuildPageRegionMap(
|
||||
IEnumerable<DrawingRecognitionOtherRegion> otherRegions)
|
||||
{
|
||||
// 同一页以 region_code 区分 C1 管线页码和 C3 管线号,避免依赖区域返回顺序。
|
||||
return otherRegions
|
||||
.Where(x => x != null && x.Page.HasValue && !string.IsNullOrWhiteSpace(x.RegionCode))
|
||||
.GroupBy(x => x.Page.Value)
|
||||
.ToDictionary(
|
||||
pageGroup => pageGroup.Key,
|
||||
pageGroup => pageGroup
|
||||
.GroupBy(x => x.RegionCode.Trim(), StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(
|
||||
regionGroup => regionGroup.Key,
|
||||
regionGroup => regionGroup.Select(x => x.Text).FirstOrDefault(),
|
||||
StringComparer.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private static string GetPageRegionValueOrDefault(
|
||||
Dictionary<int, Dictionary<string, string>> pageRegions,
|
||||
int? pageNo,
|
||||
string regionCode)
|
||||
{
|
||||
if (!pageNo.HasValue || !pageRegions.TryGetValue(pageNo.Value, out var regions) ||
|
||||
!regions.TryGetValue(regionCode, out var value) || string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return value.Trim();
|
||||
}
|
||||
|
||||
private static string GetRequiredPageRegionValue(
|
||||
Dictionary<int, Dictionary<string, string>> pageRegions,
|
||||
int? pageNo,
|
||||
string regionCode)
|
||||
{
|
||||
if (!pageNo.HasValue)
|
||||
{
|
||||
throw new InvalidOperationException("存在缺少 page_no 的识别数据");
|
||||
}
|
||||
|
||||
string value = GetPageRegionValueOrDefault(pageRegions, pageNo, regionCode);
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
throw new InvalidOperationException(string.Format("第 {0} 页未识别到区域 {1}", pageNo.Value, regionCode));
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 C1 中的管线页码,并校验其必须为整数。
|
||||
/// </summary>
|
||||
private static int GetRequiredPipePageNo(
|
||||
Dictionary<int, Dictionary<string, string>> pageRegions,
|
||||
int? sourcePageNo)
|
||||
{
|
||||
string pipePageNoText = GetRequiredPageRegionValue(pageRegions, sourcePageNo, "C1");
|
||||
if (!int.TryParse(pipePageNoText, out var pipePageNo))
|
||||
{
|
||||
throw new InvalidOperationException(string.Format("第 {0} 页区域 C1 的值“{1}”不是有效整数", sourcePageNo, pipePageNoText));
|
||||
}
|
||||
|
||||
return pipePageNo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据识别数据的原始页码获取 C3 管线号。
|
||||
/// </summary>
|
||||
private static string GetPipelineCodeByPageNo(
|
||||
Dictionary<int, Dictionary<string, string>> pageRegions,
|
||||
int? pageNo)
|
||||
{
|
||||
// 管线号统一通过原始页码匹配 C3,避免各类识别数据重复实现映射逻辑。
|
||||
return GetRequiredPageRegionValue(pageRegions, pageNo, "C3");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将材料识别结果映射为待保存的材料实体。
|
||||
/// </summary>
|
||||
private static List<HJGL_DrawingRecognition_Material> MapMaterialEntities(
|
||||
IEnumerable<DrawingRecognitionMaterialRow> materialRows,
|
||||
Dictionary<int, Dictionary<string, string>> pageRegions)
|
||||
{
|
||||
return materialRows.Select(row => new HJGL_DrawingRecognition_Material
|
||||
{
|
||||
Id = SQLHelper.GetNewID(),
|
||||
Pipe_no = GetPipelineCodeByPageNo(pageRegions, row.PageNo),
|
||||
Pipe_page_no = GetRequiredPipePageNo(pageRegions, row.PageNo),
|
||||
Seq_no = GetNullableInt(row.SeqNo),
|
||||
Category = row.Category,
|
||||
Description = row.Description,
|
||||
Spec = row.Spec,
|
||||
Code = row.Code,
|
||||
Qty = row.Qty,
|
||||
Unit = row.Unit,
|
||||
Material = row.Material,
|
||||
Drawing_number = row.DrawingNumber,
|
||||
Remark = row.Remark
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将管段长度识别结果映射为待保存的管段长度实体。
|
||||
/// </summary>
|
||||
private static List<HJGL_DrawingRecognition_PipeLengths> MapPipeLengthEntities(
|
||||
IEnumerable<DrawingRecognitionPipeLengthRow> pipeLengthRows,
|
||||
Dictionary<int, Dictionary<string, string>> pageRegions)
|
||||
{
|
||||
return pipeLengthRows.Select(row => new HJGL_DrawingRecognition_PipeLengths
|
||||
{
|
||||
Id = SQLHelper.GetNewID(),
|
||||
Pipe_no = GetPipelineCodeByPageNo(pageRegions, row.PageNo),
|
||||
Pipe_page_no = GetRequiredPipePageNo(pageRegions, row.PageNo),
|
||||
Group_index = row.GroupIndex,
|
||||
Pos_no = row.PosNo,
|
||||
Length_mm = row.LengthMm,
|
||||
Dn = row.Dn
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
private static int? GetNullableInt(object value)
|
||||
{
|
||||
return int.TryParse(Convert.ToString(value), out var number) ? number : (int?)null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按当前项目和管线号新增或更新管线,仅同步 DrawingInfo 中识别到的字段。
|
||||
/// </summary>
|
||||
private Dictionary<string, HJGL_Pipeline> UpsertPipelinesFromDrawingInfo(
|
||||
DrawingRecognitionResult recognitionResult)
|
||||
{
|
||||
var pageRegions = BuildPageRegionMap(recognitionResult.OtherRegions);
|
||||
var pipelineDrawingData = BuildPipelineDrawingData(recognitionResult.DrawingInfo, pageRegions);
|
||||
if (pipelineDrawingData.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException("未从区域 C3 识别到管线号");
|
||||
}
|
||||
|
||||
var pipelineCodes = pipelineDrawingData.Keys.ToList();
|
||||
var existingPipelines = Funs.DB.HJGL_Pipeline
|
||||
.Where(x => x.ProjectId == CurrUser.LoginProjectId && pipelineCodes.Contains(x.PipelineCode))
|
||||
.ToList();
|
||||
|
||||
var duplicatePipeline = existingPipelines
|
||||
.GroupBy(x => x.PipelineCode, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(group => group.Count() > 1);
|
||||
if (duplicatePipeline != null)
|
||||
{
|
||||
throw new InvalidOperationException("当前项目存在重复管线号:" + duplicatePipeline.Key);
|
||||
}
|
||||
|
||||
var pipelinesByCode = existingPipelines.ToDictionary(
|
||||
x => x.PipelineCode,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// HJGL_Pipeline 保存 PipingClassId,识别到的等级编码必须先按当前项目批量转换。
|
||||
var pipingClassCodes = pipelineDrawingData.Values
|
||||
.Select(x => x.PipingClassCode)
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x))
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
var pipingClasses = Funs.DB.Base_PipingClass
|
||||
.Where(x => x.ProjectId == CurrUser.LoginProjectId && pipingClassCodes.Contains(x.PipingClassCode))
|
||||
.ToList()
|
||||
.GroupBy(x => x.PipingClassCode, StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(group => group.Key, group => group.First(), StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var missingPipingClassCodes = pipingClassCodes
|
||||
.Where(code => !pipingClasses.ContainsKey(code))
|
||||
.ToList();
|
||||
if (missingPipingClassCodes.Count > 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"当前项目不存在以下管道等级:" + string.Join("、", missingPipingClassCodes));
|
||||
}
|
||||
|
||||
foreach (var item in pipelineDrawingData)
|
||||
{
|
||||
if (!pipelinesByCode.TryGetValue(item.Key, out var pipeline))
|
||||
{
|
||||
pipeline = new HJGL_Pipeline
|
||||
{
|
||||
PipelineId = SQLHelper.GetNewID(),
|
||||
ProjectId = CurrUser.LoginProjectId,
|
||||
PipelineCode = item.Key,
|
||||
UnitWorkId= drpUnitWork.SelectedValue
|
||||
};
|
||||
Funs.DB.HJGL_Pipeline.InsertOnSubmit(pipeline);
|
||||
pipelinesByCode.Add(item.Key, pipeline);
|
||||
}
|
||||
|
||||
// 识别结果为空时保留已有值,避免不完整识别覆盖管线原数据。
|
||||
if (!string.IsNullOrWhiteSpace(item.Value.PipingClassCode))
|
||||
{
|
||||
pipeline.PipingClassId = pipingClasses[item.Value.PipingClassCode].PipingClassId;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(item.Value.DesignPress))
|
||||
{
|
||||
pipeline.DesignPress = item.Value.DesignPress;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(item.Value.DesignTemperature))
|
||||
{
|
||||
pipeline.DesignTemperature = item.Value.DesignTemperature;
|
||||
}
|
||||
if (item.Value.DesignIsHotProess.HasValue)
|
||||
{
|
||||
pipeline.DesignIsHotProess = item.Value.DesignIsHotProess;
|
||||
}
|
||||
}
|
||||
|
||||
return pipelinesByCode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按页码取得管线号后聚合 DrawingInfo,同一管线跨页数据合并处理。
|
||||
/// </summary>
|
||||
private static Dictionary<string, PipelineDrawingData> BuildPipelineDrawingData(
|
||||
IEnumerable<DrawingRecognitionInfoRow> drawingInfo,
|
||||
Dictionary<int, Dictionary<string, string>> pageRegions)
|
||||
{
|
||||
// 同一管线可能对应多个 PDF 页,按 C3 管线号分组后统一校验和取值。
|
||||
var pipelineGroups = drawingInfo
|
||||
.Where(x => x != null)
|
||||
.GroupBy(
|
||||
x => GetPipelineCodeByPageNo(pageRegions, x.PageNo),
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var result = new Dictionary<string, PipelineDrawingData>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var pipelineGroup in pipelineGroups)
|
||||
{
|
||||
string pwht = GetUniqueDrawingFieldValue(
|
||||
pipelineGroup,
|
||||
pipelineGroup.Key,
|
||||
"PWHT");
|
||||
result.Add(pipelineGroup.Key, new PipelineDrawingData
|
||||
{
|
||||
PipingClassCode = GetUniqueDrawingFieldValue(
|
||||
pipelineGroup,
|
||||
pipelineGroup.Key,
|
||||
"Piping class"),
|
||||
DesignPress = GetUniqueDrawingFieldValue(
|
||||
pipelineGroup,
|
||||
pipelineGroup.Key,
|
||||
"Design pressure, MPag"),
|
||||
DesignTemperature = GetUniqueDrawingFieldValue(
|
||||
pipelineGroup,
|
||||
pipelineGroup.Key,
|
||||
"Design temperature",
|
||||
true),
|
||||
DesignIsHotProess = string.IsNullOrWhiteSpace(pwht)
|
||||
? (bool?)null
|
||||
: ParsePwht(pwht, pipelineGroup.Key)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取管线字段的唯一非空值;跨页识别值不一致时阻止保存。
|
||||
/// </summary>
|
||||
private static string GetUniqueDrawingFieldValue(
|
||||
IEnumerable<DrawingRecognitionInfoRow> rows,
|
||||
string pipelineCode,
|
||||
string fieldName,
|
||||
bool startsWith = false)
|
||||
{
|
||||
var values = rows
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x.FieldName) &&
|
||||
(startsWith
|
||||
? x.FieldName.Trim().StartsWith(fieldName, StringComparison.OrdinalIgnoreCase)
|
||||
: string.Equals(x.FieldName.Trim(), fieldName, StringComparison.OrdinalIgnoreCase)))
|
||||
.Select(x => x.FieldValue == null ? null : x.FieldValue.Trim())
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x))
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
if (values.Count > 1)
|
||||
{
|
||||
throw new InvalidOperationException(string.Format(
|
||||
"管线“{0}”的字段“{1}”识别值不一致:{2}",
|
||||
pipelineCode,
|
||||
fieldName,
|
||||
string.Join("、", values)));
|
||||
}
|
||||
|
||||
return values.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将识别结果中的 PWHT Yes/No 标记转换为是否需要焊后热处理。
|
||||
/// </summary>
|
||||
private static bool ParsePwht(string value, string pipelineCode)
|
||||
{
|
||||
string normalizedValue = value.Trim();
|
||||
if (normalizedValue.IndexOf("(Yes)", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
string.Equals(normalizedValue, "Yes", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(normalizedValue, "Y", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(normalizedValue, "1", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(normalizedValue, "是", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(normalizedValue, "True", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (normalizedValue.IndexOf("(No)", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
string.Equals(normalizedValue, "No", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(normalizedValue, "N", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(normalizedValue, "0", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(normalizedValue, "否", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(normalizedValue, "False", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException(string.Format(
|
||||
"管线“{0}”的 PWHT 值“{1}”无法识别为是或否",
|
||||
pipelineCode,
|
||||
value));
|
||||
}
|
||||
|
||||
private sealed class PipelineDrawingData
|
||||
{
|
||||
public string PipingClassCode { get; set; }
|
||||
|
||||
public string DesignPress { get; set; }
|
||||
|
||||
public string DesignTemperature { get; set; }
|
||||
|
||||
public bool? DesignIsHotProess { get; set; }
|
||||
}
|
||||
|
||||
|
||||
private void BindGrids()
|
||||
{
|
||||
Grid1.DataSource = MaterialEntities ?? new List<HJGL_DrawingRecognition_Material>();
|
||||
Grid1.DataBind();
|
||||
|
||||
|
||||
}
|
||||
Grid2.DataSource = PipeLengthEntities ?? new List<HJGL_DrawingRecognition_PipeLengths>();
|
||||
Grid2.DataBind();
|
||||
}
|
||||
#region 双击事件
|
||||
/// <summary>
|
||||
/// Grid行双击事件
|
||||
@@ -458,18 +791,18 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
}
|
||||
if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, Const.HJGL_DataInMenuId, Const.BtnAdd))
|
||||
{
|
||||
if (DrawingRecognitionContentEdit.dicDt.ContainsKey(CurrUser.PersonId))
|
||||
if (MaterialEntities == null)
|
||||
{
|
||||
DrawingRecognitionContentEdit.dicDt[CurrUser.PersonId] = dt;
|
||||
Alert.ShowInTop("实体数据已失效,请重新审核识别结果!", MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawingRecognitionContentEdit.dicDt.Add(CurrUser.PersonId, dt);
|
||||
|
||||
}
|
||||
PageContext.RegisterStartupScript(Window1.GetSaveStateReference(hdIds.ClientID) + Window1.GetShowReference(String.Format("DrawingRecognitionContentEdit.aspx?Index={0}", Grid1.SelectedRowIndex, "编辑 - ")));
|
||||
|
||||
//PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("DrawingRecognitionContentEdit.aspx?Index={0}", Grid1.SelectedRowIndex, "维护 - ")));
|
||||
string editUrl = string.Format(
|
||||
"DrawingRecognitionContentEdit.aspx?Index={0}&DataKey={1}",
|
||||
Grid1.SelectedRowIndex,
|
||||
RecognitionDataKey);
|
||||
PageContext.RegisterStartupScript(
|
||||
Window1.GetSaveStateReference(hdIds.ClientID) + Window1.GetShowReference(editUrl));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -480,8 +813,7 @@ namespace FineUIPro.Web.HJGL.DataIn
|
||||
|
||||
protected void Window1_Close(object sender, WindowCloseEventArgs e)
|
||||
{
|
||||
Grid1.DataSource = dt;
|
||||
Grid1.DataBind();
|
||||
BindGrids();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user