915 lines
42 KiB
C#
915 lines
42 KiB
C#
using BLL;
|
||
using FineUIPro.Web.HJGL.WeldingManage;
|
||
using FineUIPro.Web.HJGLServer.BaseInfo;
|
||
using Model;
|
||
using Newtonsoft.Json.Linq;
|
||
using Org.BouncyCastle.Asn1.Tsp;
|
||
using Spire.Pdf;
|
||
using Spire.Pdf.Utilities;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
|
||
namespace FineUIPro.Web.HJGL.DataIn
|
||
{
|
||
public partial class PipelineGrid : PageBase
|
||
{
|
||
public string fileUrl
|
||
{
|
||
get
|
||
{
|
||
return (string)ViewState["fileUrl"];
|
||
}
|
||
set
|
||
{
|
||
ViewState["fileUrl"] = value;
|
||
}
|
||
}
|
||
public string ckbSkipFirst
|
||
{
|
||
get
|
||
{
|
||
return (string)ViewState["ckbSkipFirst"];
|
||
}
|
||
set
|
||
{
|
||
ViewState["ckbSkipFirst"] = value;
|
||
}
|
||
}
|
||
private string IsPDMS
|
||
{
|
||
get
|
||
{
|
||
return (string)ViewState["IsPDMS"];
|
||
}
|
||
set
|
||
{
|
||
ViewState["IsPDMS"] = value;
|
||
}
|
||
}
|
||
public DataSet set
|
||
{
|
||
get
|
||
{
|
||
return (DataSet)ViewState["set"];
|
||
}
|
||
set
|
||
{
|
||
ViewState["set"] = value;
|
||
}
|
||
}
|
||
|
||
public DataSet setAfterProcess
|
||
{
|
||
get
|
||
{
|
||
return (DataSet)ViewState["setAfterProcess"];
|
||
}
|
||
set
|
||
{
|
||
ViewState["setAfterProcess"] = value;
|
||
}
|
||
}
|
||
public List<Model.HJGL_PW_IsoInfo> isoList
|
||
{
|
||
get
|
||
{
|
||
return (List<Model.HJGL_PW_IsoInfo>)Session["isoList"];
|
||
}
|
||
set
|
||
{
|
||
Session["isoList"] = value;
|
||
}
|
||
}
|
||
|
||
#region 加载
|
||
/// <summary>
|
||
/// 加载页面
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
if (!IsPostBack)
|
||
{
|
||
fileUrl = Request.Params["fileUrl"];
|
||
ckbSkipFirst = Request.Params["ckbSkipFirst"];
|
||
set = new DataSet();
|
||
setAfterProcess = new DataSet();
|
||
isoList = new List<HJGL_PW_IsoInfo>();
|
||
InitGrid();
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 关闭弹出窗
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void InitGrid()
|
||
{
|
||
|
||
// 加载PDF文档
|
||
PdfDocument pdf = new PdfDocument();
|
||
if (!File.Exists(fileUrl))
|
||
{
|
||
return;
|
||
}
|
||
pdf.LoadFromFile(fileUrl);
|
||
PdfTableExtractor extractor = new PdfTableExtractor(pdf);
|
||
// 遍历每一页
|
||
int i = 0;
|
||
if (ckbSkipFirst == "1")
|
||
{
|
||
i = 1;
|
||
}
|
||
var units = from x in Funs.DB.Base_Unit
|
||
join y in Funs.DB.Project_Unit on x.UnitId equals y.UnitId
|
||
where y.ProjectId == CurrUser.LoginProjectId
|
||
select x;
|
||
var installs = from x in Funs.DB.Project_Installation where x.ProjectId == this.CurrUser.LoginProjectId select x;
|
||
var steels = from x in Funs.DB.HJGL_BS_Steel select x;
|
||
var materialStandards = from x in Funs.DB.HJGL_BS_MaterialStandard select x;
|
||
|
||
var rates = from x in Funs.DB.HJGL_BS_NDTRate select x;
|
||
var types = from x in Funs.DB.HJGL_BS_JointType select x;
|
||
var methods = from x in Funs.DB.HJGL_BS_WeldMethod select x;
|
||
var materials = from x in Funs.DB.HJGL_BS_WeldMaterial select x;
|
||
var services = from x in Funs.DB.HJGL_BS_Service select x;
|
||
var slopeTypes = from x in Funs.DB.HJGL_BS_SlopeType select x;
|
||
var isoClasss = from x in Funs.DB.HJGL_BS_IsoClass select x;
|
||
var components = from x in Funs.DB.HJGL_BS_Component select x;
|
||
var dns = from x in Funs.DB.HJGL_BS_SchTab select x;
|
||
var execStandards = from x in Funs.DB.HJGL_BS_ExecStandard select x;
|
||
var dataInTemp = from x in Funs.DB.HJGL_Sys_DataInTemp
|
||
where x.ProjectId == this.CurrUser.LoginProjectId && x.UserId == this.CurrUser.UserId && x.Type == this.IsPDMS
|
||
select x;
|
||
var users = from x in Funs.DB.Sys_User select x;
|
||
for (; i < pdf.Pages.Count; i++)
|
||
{
|
||
PdfPageBase page = pdf.Pages[i];
|
||
// 提取页面中的表格
|
||
PdfTable[] tables = extractor.ExtractTable(i);
|
||
// 输出找到的表格数量
|
||
Console.WriteLine($"第 {i + 1} 页找到 {tables.Length} 个表格");
|
||
// 遍历提取到的表格
|
||
for (int j = 0; j < tables.Length; j++)
|
||
{
|
||
PdfTable table = tables[j];
|
||
DataTable dtTemp = new DataTable();
|
||
for (int col = 0; col < table.GetColumnCount(); col++)
|
||
{
|
||
dtTemp.Columns.Add("col" + col);
|
||
}
|
||
// 遍历表格的行列
|
||
for (int row = 0; row < table.GetRowCount(); row++)
|
||
{
|
||
DataRow rowData = dtTemp.NewRow();
|
||
dtTemp.Rows.Add(rowData);
|
||
for (int col = 0; col < table.GetColumnCount(); col++)
|
||
{
|
||
string text = table.GetText(row, col);
|
||
rowData[col] = table.GetText(row, col);
|
||
Console.Write($"{text}\t");
|
||
}
|
||
Console.WriteLine(); // 换行
|
||
}
|
||
|
||
if (dtTemp.Rows.Count > 0 && dtTemp.Columns.Count > 1)
|
||
{// 2. 转换为CSV
|
||
string csvContent = ConvertDataTableToCsv(dtTemp);
|
||
Console.WriteLine("生成的CSV内容:");
|
||
Console.WriteLine(csvContent);
|
||
|
||
// 3. 上传到服务器
|
||
string uploadUrl = "http://localhost:8000/api/table/clean"; // 替换为实际上传地址
|
||
string fileName = $"data_{DateTime.Now:yyyyMMddHHmmss}.csv";
|
||
//"管线号", "公称直径", "管道等级", "管道材料", "主要介质", "起止点", "操作参数温度", "操作参数压力", "设计参数温度", "设计参数压力", "隔热工程代号", "隔热材料", "厚度", "介质", "压力", "比例", "方法", "合格等级", "PID尾号", "压力管道类别", "应力分析题号", "备注"
|
||
|
||
|
||
try
|
||
{
|
||
Grid grid = new Grid();
|
||
grid.ShowGridHeader = true;
|
||
grid.Title = dtTemp.TableName;
|
||
grid.AllowColumnLocking = true;
|
||
grid.DataKeyNames = new string[] { "Id" };
|
||
foreach (DataColumn dc in dtTemp.Columns)
|
||
{
|
||
grid.Columns.Add(new BoundField() { DataField = dc.ColumnName, HeaderText = dc.ColumnName });
|
||
}
|
||
grid.DataSource = dtTemp;
|
||
grid.DataBind();
|
||
Region2.Items.Add(grid);
|
||
set.Tables.Add(dtTemp);
|
||
string response = UploadCsvFile(csvContent, uploadUrl, fileName);
|
||
Grid grid1 = new Grid();
|
||
grid1.ShowGridHeader = true;
|
||
grid1.Title = dtTemp.TableName;
|
||
grid1.AllowColumnLocking = true;
|
||
grid1.DataKeyNames = new string[] { "Id" };
|
||
JObject dynamicObj = JObject.Parse(response);
|
||
|
||
DataTable dtAfterProcess = new DataTable();
|
||
foreach (JToken employeeToken in dynamicObj["headers"])
|
||
{
|
||
dtAfterProcess.Columns.Add(employeeToken.ToString());
|
||
grid1.Columns.Add(new BoundField() { DataField = employeeToken.ToString(), HeaderText = employeeToken.ToString() });
|
||
}
|
||
dtAfterProcess.Columns.Add("Id");
|
||
|
||
foreach (JToken rowData in dynamicObj["rows"])
|
||
{
|
||
// int index = 0;
|
||
var row = dtAfterProcess.NewRow();
|
||
var datas = rowData.ToList();
|
||
row["Id"] = Guid.NewGuid().ToString();
|
||
|
||
foreach(DataRow row1 in dtTemp.Rows)
|
||
{
|
||
if (datas[0].ToString() == row1[0].ToString())
|
||
{
|
||
for(int index = 0; index < dtTemp.Columns.Count; index++)
|
||
{
|
||
row[index] = row1[index];
|
||
}
|
||
}
|
||
}
|
||
//foreach (var d in datas)
|
||
//{
|
||
// row[index] = d.ToString(); index++;
|
||
//}
|
||
dtAfterProcess.Rows.Add(row);
|
||
}
|
||
grid1.DataSource = dtAfterProcess;
|
||
grid1.DataBind();
|
||
Region1.Items.Add(grid1);
|
||
setAfterProcess.Tables.Add(dtAfterProcess);
|
||
|
||
string urlHeaderCorrespondence = "http://localhost:8000/api/table/head";
|
||
string data = @"{
|
||
""source_headers"": " + dynamicObj["headers"].ToString() + @" ,
|
||
""target_fields"": [
|
||
""管线号"",
|
||
""管道等级"",
|
||
""执行标准"",
|
||
""材质"",
|
||
""材质标准"",
|
||
""介质"",
|
||
""管道类别"",
|
||
""涂漆类别"",
|
||
""绝热类别"",
|
||
""操作压力"",
|
||
""操作温度"",
|
||
""泄露性试验"",
|
||
""设计压力"",
|
||
""设计温度"",
|
||
""耐压试验"",
|
||
""外径"",
|
||
""壁厚"",
|
||
""管线长度"",
|
||
""检测类别序号"",
|
||
""热处理类型"",
|
||
""备注""
|
||
],
|
||
""extra_instructions"": ""管线号优先匹配管线代号,优先将含有 Main Fluid 的字段映射到 介质,优先匹配隔热工程代号到绝热类别,压力管道类别映射管道类别,有名称首先匹配名称,没有名称匹配代号,按匹配度从高低输出""
|
||
}
|
||
";
|
||
string resHeaderCorrespondence = HeaderCorrespondence(urlHeaderCorrespondence, data, "POST", "application/json;charset=utf-8");
|
||
|
||
if (!string.IsNullOrEmpty(resHeaderCorrespondence))
|
||
{
|
||
JObject dynamicHeaderCorrespondence = JObject.Parse(resHeaderCorrespondence);
|
||
Dictionary<string, string> headerCorrespondenceDic = new Dictionary<string, string>();
|
||
foreach (JToken employeeToken in dynamicHeaderCorrespondence["mapping"])
|
||
{
|
||
headerCorrespondenceDic.Add(employeeToken["source_header"].ToString(), employeeToken["target_field"].ToString());
|
||
}
|
||
foreach (DataRow row in dtAfterProcess.Rows)
|
||
{
|
||
HJGL_PW_IsoInfo isoInfo = new HJGL_PW_IsoInfo();
|
||
isoInfo.ISO_ID = Guid.NewGuid().ToString();
|
||
isoInfo.ISO_CreateDate = DateTime.Now;
|
||
isoInfo.ISO_Creator = CurrUser.UserCode;
|
||
isoInfo.ProjectId = CurrUser.LoginProjectId;
|
||
isoInfo.BSU_ID = CurrUser.UnitId;
|
||
isoInfo.IsHot = "0";
|
||
foreach (DataColumn dataColumn in dtAfterProcess.Columns)
|
||
{
|
||
|
||
if (headerCorrespondenceDic.ContainsKey(dataColumn.ColumnName))
|
||
switch (headerCorrespondenceDic[dataColumn.ColumnName])
|
||
{
|
||
case "管线号":
|
||
isoInfo.ISO_IsoNo = row[dataColumn.ColumnName].ToString();
|
||
break;
|
||
case "管道等级":
|
||
isoInfo.ISC_ID = isoClasss.Where(x=>x.ISC_IsoName==row[dataColumn.ColumnName].ToString()|| x.ISC_IsoCode == row[dataColumn.ColumnName].ToString()).Select(x=>x.ISC_ID).FirstOrDefault();
|
||
break;
|
||
case "执行标准":
|
||
if (!string.IsNullOrEmpty(row[dataColumn.ColumnName].ToString()))
|
||
{
|
||
isoInfo.ISO_Executive = execStandards.Where(x => x.ExecStandardName == "SH/T3501-2021").Select(x => x.ExecStandardId).FirstOrDefault();
|
||
}
|
||
else
|
||
{
|
||
isoInfo.ISO_Executive = execStandards.Where(x => x.ExecStandardName == "GB 50517-2010").Select(x => x.ExecStandardId).FirstOrDefault();
|
||
}
|
||
break;
|
||
case "材质":
|
||
|
||
var steel = steels.FirstOrDefault(x => x.STE_Code == row[dataColumn.ColumnName].ToString());
|
||
if (steel != null)
|
||
{
|
||
isoInfo.STE_ID = steel.STE_ID;
|
||
if(steel.STE_SteelType == "FeⅣ")
|
||
{
|
||
isoInfo.IsHotType = "1";
|
||
|
||
}
|
||
else if (steel.STE_SteelType == "FeⅠ" || steel.STE_SteelType == "FeⅡ" || steel.STE_SteelType == "FeⅢ" )
|
||
{
|
||
isoInfo.IsHotType = "2";
|
||
|
||
}
|
||
}
|
||
break;
|
||
case "材质标准":
|
||
isoInfo.MaterialStandardId = materialStandards.Where(x => x.MaterialStandardName == row[dataColumn.ColumnName].ToString()||x.MaterialStandardCode == row[dataColumn.ColumnName].ToString()).Select(x => x.MaterialStandardId).FirstOrDefault();
|
||
break;
|
||
case "介质":
|
||
var SER_ID = services.Where(x => x.SER_Name == row[dataColumn.ColumnName].ToString() || x.SER_Code == row[dataColumn.ColumnName].ToString()).Select(x => x.SER_ID).FirstOrDefault();
|
||
if (!string.IsNullOrEmpty(SER_ID))
|
||
{
|
||
isoInfo.SER_ID = SER_ID;
|
||
}
|
||
break;
|
||
case "管道类别":
|
||
isoInfo.PipeLineClass = row[dataColumn.ColumnName].ToString();
|
||
if (!string.IsNullOrEmpty(row[dataColumn.ColumnName].ToString()))
|
||
{
|
||
isoInfo.ISO_Executive = execStandards.Where(x => x.ExecStandardName == "SH/T3501-2021").Select(x => x.ExecStandardId).FirstOrDefault();
|
||
}
|
||
else
|
||
{
|
||
isoInfo.ISO_Executive = execStandards.Where(x => x.ExecStandardName == "GB 50517-2010").Select(x => x.ExecStandardId).FirstOrDefault();
|
||
}
|
||
break;
|
||
case "涂漆类别":
|
||
isoInfo.ISO_Paint = row[dataColumn.ColumnName].ToString();
|
||
break;
|
||
case "绝热类别":
|
||
if (!string.IsNullOrEmpty(row[dataColumn.ColumnName].ToString()))
|
||
{
|
||
isoInfo.ISO_Insulator = row[dataColumn.ColumnName].ToString();
|
||
}
|
||
else
|
||
{
|
||
isoInfo.ISO_Insulator = "/";
|
||
|
||
}
|
||
break;
|
||
case "操作压力":
|
||
isoInfo.OperatingPressure = row[dataColumn.ColumnName].ToString();
|
||
break;
|
||
case "操作温度":
|
||
isoInfo.OperatingTemperature = row[dataColumn.ColumnName].ToString();
|
||
break;
|
||
case "泄露性试验":
|
||
if (isoInfo.ISO_DesignPress.HasValue)
|
||
{
|
||
isoInfo.LeakageTest = isoInfo.ISO_DesignPress.Value.ToString("##.##");// row[dataColumn.ColumnName].ToString();
|
||
}
|
||
break;
|
||
case "设计压力":
|
||
isoInfo.ISO_DesignPress = Funs.GetNewDecimal(row[dataColumn.ColumnName].ToString());
|
||
isoInfo.LeakageTest = row[dataColumn.ColumnName].ToString();
|
||
break;
|
||
case "设计温度":
|
||
isoInfo.ISO_DesignTemperature = Funs.GetNewDecimal(row[dataColumn.ColumnName].ToString());
|
||
break;
|
||
case "耐压试验":
|
||
if (isoInfo.ISO_DesignPress.HasValue)
|
||
{
|
||
isoInfo.ISO_TestPress = isoInfo.ISO_DesignPress.Value * 1.5m;
|
||
}
|
||
break;
|
||
case "外径":
|
||
isoInfo.ISO_Dia = Funs.GetNewDecimal(row[dataColumn.ColumnName].ToString());
|
||
break;
|
||
case "壁厚":
|
||
isoInfo.ISO_Sch = Funs.GetNewDecimal(row[dataColumn.ColumnName].ToString());
|
||
break;
|
||
case "管线长度":
|
||
isoInfo.PipeLineLength = Funs.GetNewDecimal(row[dataColumn.ColumnName].ToString());
|
||
break;
|
||
case "检测类别序号":
|
||
isoInfo.TestCategoryNum = "/";
|
||
break;
|
||
case "热处理类型":
|
||
{
|
||
if (!string.IsNullOrEmpty(row[dataColumn.ColumnName].ToString()))
|
||
{
|
||
//isoInfo.IsHotType = row[dataColumn.ColumnName].ToString();
|
||
isoInfo.IsHot = "1";
|
||
}
|
||
}
|
||
break;
|
||
case "备注":
|
||
isoInfo.ISO_Remark = row[dataColumn.ColumnName].ToString();
|
||
break;
|
||
}
|
||
|
||
if (row[dataColumn.ColumnName].ToString().Contains("漆"))
|
||
{
|
||
isoInfo.ISO_Paint = row[dataColumn.ColumnName].ToString();
|
||
}
|
||
if (row[dataColumn.ColumnName].ToString().Contains("SH"))
|
||
{
|
||
isoInfo.ISC_ID = isoClasss.Where(x => x.ISC_IsoName == row[dataColumn.ColumnName].ToString() || x.ISC_IsoCode == row[dataColumn.ColumnName].ToString()).Select(x => x.ISC_ID).FirstOrDefault();
|
||
}
|
||
if (row[dataColumn.ColumnName].ToString().Contains("焊后热处理"))
|
||
{
|
||
//isoInfo.IsHotType = row[dataColumn.ColumnName].ToString();
|
||
isoInfo.IsHot = "1";
|
||
}
|
||
}
|
||
isoList.Add(isoInfo);
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (Exception e11)
|
||
{
|
||
|
||
}
|
||
|
||
}
|
||
Console.WriteLine($"\n--- 表格 {j + 1} ---");
|
||
}
|
||
}
|
||
|
||
|
||
|
||
this.IsoGrid.DataSource = this.isoList;
|
||
this.IsoGrid.DataBind();
|
||
}
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 将DataTable转换为标准CSV格式字符串
|
||
/// </summary>
|
||
/// <param name="dt">待转换的DataTable</param>
|
||
/// <returns>CSV格式字符串</returns>
|
||
public static string ConvertDataTableToCsv(DataTable dt)
|
||
{
|
||
if (dt == null || dt.Rows.Count == 0)
|
||
throw new ArgumentNullException(nameof(dt), "DataTable不能为空或无数据");
|
||
|
||
StringBuilder csvBuilder = new StringBuilder();
|
||
|
||
// 1. 写入表头(列名)
|
||
for (int i = 0; i < dt.Columns.Count; i++)
|
||
{
|
||
// 处理表头中的特殊字符
|
||
csvBuilder.Append(EscapeCsvValue(dt.Columns[i].ColumnName));
|
||
if (i < dt.Columns.Count - 1)
|
||
csvBuilder.Append(",");
|
||
}
|
||
csvBuilder.AppendLine();
|
||
|
||
// 2. 写入数据行
|
||
foreach (DataRow row in dt.Rows)
|
||
{
|
||
for (int i = 0; i < dt.Columns.Count; i++)
|
||
{
|
||
// 处理单元格中的特殊字符
|
||
string cellValue = row[i] == DBNull.Value ? "" : row[i].ToString();
|
||
csvBuilder.Append(EscapeCsvValue(cellValue));
|
||
if (i < dt.Columns.Count - 1)
|
||
csvBuilder.Append(",");
|
||
}
|
||
csvBuilder.AppendLine();
|
||
}
|
||
|
||
return csvBuilder.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 转义CSV中的特殊字符(处理逗号、换行、引号)
|
||
/// </summary>
|
||
/// <param name="value">原始值</param>
|
||
/// <returns>转义后的值</returns>
|
||
private static string EscapeCsvValue(string value)
|
||
{
|
||
if (string.IsNullOrEmpty(value))
|
||
return "";
|
||
|
||
// 如果包含逗号、换行符、双引号,需要用双引号包裹,并将内部双引号替换为两个双引号
|
||
if (value.Contains(",") || value.Contains("\n") || value.Contains("\r") || value.Contains("\""))
|
||
{
|
||
return "\"" + value.Replace("\"", "\"\"") + "\"";
|
||
}
|
||
return value;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将CSV字符串保存为临时文件并上传到指定服务器
|
||
/// </summary>
|
||
/// <param name="csvContent">CSV内容</param>
|
||
/// <param name="uploadUrl">服务器上传接口地址</param>
|
||
/// <param name="fileName">上传的文件名</param>
|
||
/// <param name="timeout">超时时间(毫秒),默认30秒</param>
|
||
/// <returns>服务器返回的响应内容</returns>
|
||
public static string UploadCsvFile(string csvContent, string uploadUrl, string fileName, int timeout = 300000)
|
||
{
|
||
string boundary = "----WebKitFormBoundary" + Guid.NewGuid().ToString("N");
|
||
|
||
using (MemoryStream ms = new MemoryStream())
|
||
{
|
||
void WriteString(string text)
|
||
{
|
||
byte[] bytes = Encoding.UTF8.GetBytes(text);
|
||
ms.Write(bytes, 0, bytes.Length);
|
||
}
|
||
|
||
// 构建请求体
|
||
WriteString($"--{boundary}\r\n");
|
||
WriteString($"Content-Disposition: form-data; name=\"file\"; filename=\"{fileName}\"\r\n");
|
||
WriteString("Content-Type: text/csv\r\n");
|
||
WriteString("\r\n");
|
||
WriteString(csvContent);
|
||
WriteString("\r\n");
|
||
|
||
WriteString($"--{boundary}\r\n");
|
||
WriteString("Content-Disposition: form-data; name=\"extra_instructions\"\r\n");
|
||
WriteString("\r\n");
|
||
WriteString($"注意数据不要少列\r\n");
|
||
|
||
WriteString($"--{boundary}--\r\n");
|
||
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl);
|
||
request.Method = "POST";
|
||
request.ContentType = $"multipart/form-data; boundary={boundary}";
|
||
request.ContentLength = ms.Length;
|
||
request.Accept = "application/json";
|
||
request.Timeout = timeout;
|
||
|
||
try
|
||
{
|
||
// 发送数据
|
||
using (Stream requestStream = request.GetRequestStream())
|
||
{
|
||
ms.Position = 0;
|
||
ms.CopyTo(requestStream);
|
||
}
|
||
|
||
// 获取响应
|
||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
||
{
|
||
string responseText = reader.ReadToEnd();
|
||
return responseText;
|
||
}
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
// 处理HTTP错误
|
||
if (ex.Response != null)
|
||
{
|
||
using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream(), Encoding.UTF8))
|
||
{
|
||
string errorText = reader.ReadToEnd();
|
||
throw new Exception($"HTTP错误: {errorText}", ex);
|
||
}
|
||
}
|
||
throw new Exception($"网络错误: {ex.Message}", ex);
|
||
}
|
||
}
|
||
|
||
|
||
//// 定义分隔符(随机生成避免冲突)
|
||
//string boundary = "----WebKitFormBoundary" + Guid.NewGuid().ToString("N");
|
||
//byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
|
||
|
||
//// 创建内存流存储请求内容
|
||
//using (MemoryStream ms = new MemoryStream())
|
||
//{
|
||
|
||
|
||
// string header = $"--{boundary}\r\nContent-Disposition: form-data; name=\"extra_instructions\"\r\n\r\n注意数据不要少列,不要串列\r\n";
|
||
// byte[] headerBytes = Encoding.UTF8.GetBytes (header);
|
||
// ms.Write(headerBytes, 0, headerBytes.Length);
|
||
// // 1. 写入表单字段头
|
||
// ms.Write(boundaryBytes, 0, boundaryBytes.Length);
|
||
|
||
// // 2. 写入文件内容头
|
||
// string fileHeader =
|
||
// $"Content-Disposition: form-data; name=\"file\"; filename=\"{fileName}\"\r\n" +
|
||
// "Content-Type: text/csv; charset=utf-8\r\n\r\n";
|
||
// byte[] fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader);
|
||
// ms.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
|
||
|
||
// // 3. 写入CSV内容
|
||
// byte[] csvBytes = Encoding.UTF8.GetBytes(csvContent);
|
||
// ms.Write(csvBytes, 0, csvBytes.Length);
|
||
|
||
// // 4. 写入结束边界
|
||
// byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
|
||
// ms.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
||
|
||
|
||
|
||
// // 5. 创建HTTP请求
|
||
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl);
|
||
// request.Method = "POST";
|
||
// request.ContentType = "multipart/form-data; boundary=" + boundary;
|
||
// request.ContentLength = ms.Length;
|
||
// request.Timeout = timeout;
|
||
// request.KeepAlive = true;
|
||
|
||
// // 6. 写入请求数据
|
||
// using (Stream requestStream = request.GetRequestStream())
|
||
// {
|
||
// ms.Position = 0;
|
||
// ms.CopyTo(requestStream);
|
||
// }
|
||
|
||
// // 7. 获取响应
|
||
// using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||
// using (Stream responseStream = response.GetResponseStream())
|
||
// using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
|
||
// {
|
||
// return reader.ReadToEnd();
|
||
// }
|
||
//}
|
||
}
|
||
|
||
|
||
public static string HeaderCorrespondence(string uploadUrl, string data, string method, string contenttype)
|
||
{
|
||
// 5. 创建HTTP请求
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl);
|
||
request.Method = string.IsNullOrEmpty(method) ? "GET" : method;
|
||
request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype;
|
||
if (uploadUrl.IndexOf("https") >= 0)
|
||
{
|
||
request.ProtocolVersion = HttpVersion.Version10;
|
||
}
|
||
if (!string.IsNullOrEmpty(data))
|
||
{
|
||
Stream RequestStream = request.GetRequestStream();
|
||
byte[] bytes = Encoding.UTF8.GetBytes(data);
|
||
RequestStream.Write(bytes, 0, bytes.Length);
|
||
RequestStream.Close();
|
||
}
|
||
|
||
HttpWebResponse response = null;
|
||
Stream ResponseStream = null;
|
||
StreamReader StreamReader = null;
|
||
try
|
||
{
|
||
response = (HttpWebResponse)request.GetResponse();
|
||
ResponseStream = response.GetResponseStream();
|
||
StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
|
||
|
||
string re = StreamReader.ReadToEnd();
|
||
StreamReader.Close();
|
||
ResponseStream.Close();
|
||
return re;
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
response = (HttpWebResponse)ex.Response;
|
||
ResponseStream = response.GetResponseStream();
|
||
StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8"));
|
||
|
||
string re = StreamReader.ReadToEnd();
|
||
return re;
|
||
}
|
||
finally
|
||
{
|
||
if (StreamReader != null)
|
||
{
|
||
StreamReader.Close();
|
||
}
|
||
if (ResponseStream != null)
|
||
{
|
||
ResponseStream.Close();
|
||
}
|
||
if (response != null)
|
||
{
|
||
response.Close();
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
protected void IsoGrid_RowDoubleClick(object sender, GridRowClickEventArgs e)
|
||
{
|
||
HJGL_PW_IsoInfo iso = null;
|
||
|
||
foreach (var item in isoList)
|
||
{
|
||
if (item.ISO_ID == IsoGrid.SelectedRowID)
|
||
{
|
||
iso = item;
|
||
break;
|
||
}
|
||
}
|
||
if (PipelineInEdit.dic.ContainsKey(IsoGrid.SelectedRowID))
|
||
{
|
||
PipelineInEdit.dic[IsoGrid.SelectedRowID] = iso;
|
||
}
|
||
else
|
||
{
|
||
PipelineInEdit.dic.Add(IsoGrid.SelectedRowID, iso);
|
||
}
|
||
hdIds.Text = "";
|
||
PageContext.RegisterStartupScript(Window1.GetSaveStateReference(hdIds.ClientID) + Window1.GetShowReference(String.Format("PipelineInEdit.aspx?ISO_ID={0}", IsoGrid.SelectedRowID, "编辑 - ")));
|
||
}
|
||
protected void Window1_Close(object sender, WindowCloseEventArgs e)
|
||
{
|
||
if (!string.IsNullOrEmpty(hdIds.Text))
|
||
{
|
||
HJGL_PW_IsoInfo iso = null;
|
||
|
||
foreach (var item in isoList)
|
||
{
|
||
if (item.ISO_ID == hdIds.Text)
|
||
{
|
||
iso = item;
|
||
break;
|
||
}
|
||
}
|
||
for (int i = 0; i < isoList.Count; i++)
|
||
{
|
||
|
||
|
||
isoList[i].ISO_Remark = iso.ISO_Remark;
|
||
|
||
|
||
isoList[i].ISO_ID = iso.ISO_ID;
|
||
isoList[i].ProjectId = iso.ProjectId;
|
||
isoList[i].BSU_ID = iso.BSU_ID;
|
||
isoList[i].SER_ID = iso.SER_ID;
|
||
|
||
isoList[i].BAW_ID = iso.BAW_ID;
|
||
isoList[i].ISO_IsoNo = iso.ISO_IsoNo;
|
||
|
||
isoList[i].ISO_TotalDin = iso.ISO_TotalDin;
|
||
isoList[i].ISO_Paint = iso.ISO_Paint;
|
||
isoList[i].ISO_Insulator = iso.ISO_Insulator;
|
||
isoList[i].STE_ID = iso.STE_ID;
|
||
|
||
isoList[i].ISO_Executive = iso.ISO_Executive;
|
||
isoList[i].ISO_Dia = iso.ISO_Dia;
|
||
isoList[i].ISO_Sch = iso.ISO_Sch;
|
||
isoList[i].ISO_Modifier = iso.ISO_Modifier;
|
||
isoList[i].ISO_ModifyDate = iso.ISO_ModifyDate;
|
||
isoList[i].ISO_Creator = iso.ISO_Creator;
|
||
isoList[i].ISO_CreateDate = iso.ISO_CreateDate;
|
||
|
||
isoList[i].ISO_DesignPress = iso.ISO_DesignPress;
|
||
isoList[i].ISO_DesignTemperature = iso.ISO_DesignTemperature;
|
||
isoList[i].ISO_TestPress = iso.ISO_TestPress;
|
||
isoList[i].ISO_TestTemperature = iso.ISO_TestTemperature;
|
||
isoList[i].ISC_ID = iso.ISC_ID;
|
||
isoList[i].ISO_Remark = iso.ISO_Remark;
|
||
isoList[i].MaterialStandardId = iso.MaterialStandardId;
|
||
isoList[i].PressureTestPackageNo = iso.PressureTestPackageNo;
|
||
isoList[i].OperatingPressure = iso.OperatingPressure;
|
||
isoList[i].OperatingTemperature = iso.OperatingTemperature;
|
||
isoList[i].PipeLineClass = iso.PipeLineClass;
|
||
isoList[i].PipeLineLength = iso.PipeLineLength;
|
||
isoList[i].LeakageTest = iso.LeakageTest;
|
||
isoList[i].TestCategoryNum = iso.TestCategoryNum;
|
||
isoList[i].IsHot = iso.IsHot;
|
||
isoList[i].IsHotType = iso.IsHotType;
|
||
|
||
isoList[i].Joty_B_Rate = iso.Joty_B_Rate;
|
||
isoList[i].Joty_C_Rate = iso.Joty_C_Rate;
|
||
isoList[i].Joty_D_Rate = iso.Joty_D_Rate;
|
||
isoList[i].Joty_B_Level = iso.Joty_B_Level;
|
||
isoList[i].Joty_C_Level = iso.Joty_C_Level;
|
||
isoList[i].Joty_D_Level = iso.Joty_D_Level;
|
||
|
||
isoList[i].Specification = iso.Specification;
|
||
|
||
}
|
||
}
|
||
this.IsoGrid.DataSource = this.isoList;
|
||
this.IsoGrid.DataBind();
|
||
}
|
||
|
||
|
||
|
||
public static string ConvertSteel(object STE_ID)
|
||
{
|
||
if (STE_ID != null)
|
||
{
|
||
var steel= Funs.DB.HJGL_BS_Steel.FirstOrDefault(x => x.STE_ID == STE_ID.ToString());
|
||
if (steel != null)
|
||
{
|
||
return steel.STE_Code;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
public static string ConvertISOExecutive(object ExecStandardId)
|
||
{
|
||
if (ExecStandardId != null)
|
||
{
|
||
var execStandard = Funs.DB.HJGL_BS_ExecStandard.FirstOrDefault(x => x.ExecStandardId == ExecStandardId.ToString());
|
||
if (execStandard != null)
|
||
{
|
||
return execStandard.ExecStandardName;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
public static string ConvertMaterialStandard(object MaterialStandardId)
|
||
{
|
||
if (MaterialStandardId != null)
|
||
{
|
||
var materialStandard = Funs.DB.HJGL_BS_MaterialStandard.FirstOrDefault(x => x.MaterialStandardId == MaterialStandardId.ToString());
|
||
if (materialStandard != null)
|
||
{
|
||
return materialStandard.MaterialStandardName;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
public static string ConvertHJGLBSService(object SER_ID)
|
||
{
|
||
if (SER_ID != null)
|
||
{
|
||
var service = Funs.DB.HJGL_BS_Service.FirstOrDefault(x => x.SER_ID == SER_ID.ToString());
|
||
if (service != null)
|
||
{
|
||
return service.SER_Name;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
public static string ConvertIsoClass(object ISC_ID)
|
||
{
|
||
if (ISC_ID != null)
|
||
{
|
||
var isoClass = Funs.DB.HJGL_BS_IsoClass.FirstOrDefault(x => x.ISC_ID == ISC_ID.ToString());
|
||
if (isoClass != null)
|
||
{
|
||
return isoClass.ISC_IsoName;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
protected void btnImport_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
if (isoList.Count() > 0)
|
||
{
|
||
|
||
|
||
Funs.DB.HJGL_PW_IsoInfo.InsertAllOnSubmit(isoList);
|
||
Funs.DB.SubmitChanges();
|
||
|
||
|
||
}
|
||
|
||
|
||
isoList.Clear();
|
||
this.IsoGrid.DataSource = this.isoList;
|
||
this.IsoGrid.DataBind();
|
||
Alert.ShowInTop("导入成功!", MessageBoxIcon.Success);
|
||
|
||
}
|
||
catch (SqlException ex)
|
||
{
|
||
|
||
throw;
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
}
|
||
} |