464 lines
18 KiB
C#
464 lines
18 KiB
C#
|
using BLL;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Data;
|
|||
|
using System.Data.OleDb;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Web.UI.WebControls;
|
|||
|
|
|||
|
namespace FineUIPro.Web.HJGL.WeldingManage
|
|||
|
{
|
|||
|
public partial class TestPackageDataIn : PageBase
|
|||
|
{
|
|||
|
#region 定义变量
|
|||
|
/// <summary>
|
|||
|
/// 上传预设的虚拟路径
|
|||
|
/// </summary>
|
|||
|
private string initPath = Const.ExcelUrl;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 导入模版文件原始的虚拟路径
|
|||
|
/// </summary>
|
|||
|
private string initTemplatePath = Const.HJGL_TestPackageImportTemplateUrl;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 错误集合
|
|||
|
/// </summary>
|
|||
|
public static List<Model.ErrorInfo> errorInfos = new List<Model.ErrorInfo>();
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 加载
|
|||
|
/// <summary>
|
|||
|
/// 加载页面
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
protected void Page_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (!IsPostBack)
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
if (errorInfos != null)
|
|||
|
{
|
|||
|
errorInfos.Clear();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
|
|||
|
#region 模板下载
|
|||
|
/// <summary>
|
|||
|
/// 模板下载
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
protected void imgbtnUpload_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
string uploadfilepath = Server.MapPath("~/") + initTemplatePath;
|
|||
|
string fileName = Path.GetFileName(initTemplatePath);
|
|||
|
FileInfo info = new FileInfo(uploadfilepath);
|
|||
|
if (info.Exists)
|
|||
|
{
|
|||
|
long fileSize = info.Length;
|
|||
|
Response.Clear();
|
|||
|
Response.ContentType = "application/x-zip-compressed";
|
|||
|
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
|
|||
|
Response.AddHeader("Content-Length", fileSize.ToString());
|
|||
|
Response.TransmitFile(uploadfilepath, 0, fileSize);
|
|||
|
Response.Flush();
|
|||
|
Response.Close();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ShowNotify("文件不存在!", MessageBoxIcon.Warning);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 审核
|
|||
|
/// <summary>
|
|||
|
/// 审核
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
protected void btnAudit_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (this.FileExcel.HasFile == false)
|
|||
|
{
|
|||
|
Alert.ShowInTop("请您选择Excel文件!", MessageBoxIcon.Warning);
|
|||
|
return;
|
|||
|
}
|
|||
|
string IsXls = Path.GetExtension(FileExcel.FileName).ToString().Trim().ToLower();
|
|||
|
if (IsXls != ".xls")
|
|||
|
{
|
|||
|
Alert.ShowInTop("只可以选择Excel文件!", MessageBoxIcon.Warning);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (errorInfos != null)
|
|||
|
{
|
|||
|
errorInfos.Clear();
|
|||
|
}
|
|||
|
string rootPath = Server.MapPath("~/");
|
|||
|
string initFullPath = rootPath + initPath;
|
|||
|
if (!Directory.Exists(initFullPath))
|
|||
|
{
|
|||
|
Directory.CreateDirectory(initFullPath);
|
|||
|
}
|
|||
|
|
|||
|
this.hdfileName.Text = BLL.Funs.GetNewFileName() + IsXls;
|
|||
|
string filePath = initFullPath + this.hdfileName.Text;
|
|||
|
FileExcel.PostedFile.SaveAs(filePath);
|
|||
|
ImportXlsToData(filePath);
|
|||
|
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Alert.ShowInTop(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region 读Excel提取数据
|
|||
|
/// <summary>
|
|||
|
/// 从Excel提取数据--》Dataset
|
|||
|
/// </summary>
|
|||
|
/// <param name="filename">Excel文件路径名</param>
|
|||
|
private void ImportXlsToData(string fileName)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
string oleDBConnString = String.Empty;
|
|||
|
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
|
|||
|
oleDBConnString += "Data Source=";
|
|||
|
oleDBConnString += fileName;
|
|||
|
oleDBConnString += ";Extended Properties=Excel 8.0;";
|
|||
|
OleDbConnection oleDBConn = null;
|
|||
|
OleDbDataAdapter oleAdMaster = null;
|
|||
|
DataTable m_tableName = new DataTable();
|
|||
|
DataSet ds = new DataSet();
|
|||
|
|
|||
|
oleDBConn = new OleDbConnection(oleDBConnString);
|
|||
|
oleDBConn.Open();
|
|||
|
m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
|
|||
|
|
|||
|
if (m_tableName != null && m_tableName.Rows.Count > 0)
|
|||
|
{
|
|||
|
|
|||
|
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
|
|||
|
|
|||
|
}
|
|||
|
string sqlMaster;
|
|||
|
sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
|
|||
|
oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
|
|||
|
oleAdMaster.SelectCommand.CommandTimeout = 1200;
|
|||
|
oleAdMaster.Fill(ds, "m_tableName");
|
|||
|
oleAdMaster.Dispose();
|
|||
|
oleDBConn.Close();
|
|||
|
oleDBConn.Dispose();
|
|||
|
|
|||
|
AddDatasetToSQL(ds.Tables[0]);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#endregion
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 将Dataset的数据导入数据库
|
|||
|
/// </summary>
|
|||
|
/// <param name="pds">数据集</param>
|
|||
|
/// <param name="Cols">数据集列数</param>
|
|||
|
/// <returns></returns>
|
|||
|
private bool AddDatasetToSQL(DataTable pds )
|
|||
|
{
|
|||
|
string result = string.Empty;
|
|||
|
int ic, ir;
|
|||
|
ic = pds.Columns.Count;
|
|||
|
|
|||
|
|
|||
|
ir = pds.Rows.Count;
|
|||
|
if (pds != null && ir > 0)
|
|||
|
{
|
|||
|
|
|||
|
for (int i = 0; i < ir; i++)
|
|||
|
{
|
|||
|
string unitid = "";
|
|||
|
|
|||
|
if (string.IsNullOrEmpty(pds.Rows[i][0].ToString()))
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "单位名称" + "," + "此项为必填项!" + "|";
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var unit = Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitName == pds.Rows[i][0].ToString() || x.UnitCode == pds.Rows[i][0].ToString());
|
|||
|
if (unit == null)
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "单位名称" + "," + "数据库中不存在!" + "|";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
unitid = unit.UnitId;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (string.IsNullOrEmpty(pds.Rows[i][1].ToString()))
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "单元号" + "," + "此项为必填项!" + "|";
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (Funs.DB.ProjectData_WorkArea.FirstOrDefault(x => x.ProjectId == this.CurrUser.LoginProjectId && x.UnitId==unitid && x.WorkAreaCode == pds.Rows[i][1].ToString()) == null)
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "单元号" + "," + "数据库中不存在!" + "|";
|
|||
|
}
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(pds.Rows[i][2].ToString()))
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "试压包" + "," + "此项为必填项!" + "|";
|
|||
|
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(pds.Rows[i][3].ToString()))
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "管线号" + "," + "此项为必填项!" + "|";
|
|||
|
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (Funs.DB.PW_IsoInfo.FirstOrDefault(x => x.UnitId==unitid&& x.ISO_IsoNo == pds.Rows[i][3].ToString()) == null)
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "管线号" + "," + "数据库中不存在!" + "|";
|
|||
|
}
|
|||
|
}
|
|||
|
if (!string.IsNullOrEmpty(pds.Rows[i][6].ToString()))
|
|||
|
{
|
|||
|
if (Funs.DB.Base_PipingClass.FirstOrDefault(x => x.PipingClassCode == pds.Rows[i][6].ToString() || x.PipingClassName == pds.Rows[i][6].ToString()) == null)
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "管道等级" + "," + "数据库中不存在!" + "|";
|
|||
|
}
|
|||
|
}
|
|||
|
if (!string.IsNullOrEmpty(pds.Rows[i][11].ToString()))
|
|||
|
{
|
|||
|
if (Funs.DB.Base_TestMedium.FirstOrDefault(x => x.MediumName == pds.Rows[i][11].ToString()|| x.MediumCode == pds.Rows[i][11].ToString()) == null)
|
|||
|
{
|
|||
|
result += (i + 2).ToString() + "," + "试验介质" + "," + "数据库中不存在!" + "|";
|
|||
|
}
|
|||
|
}
|
|||
|
//a++;
|
|||
|
}
|
|||
|
|
|||
|
if (!string.IsNullOrEmpty(result))
|
|||
|
{
|
|||
|
result = result.Substring(0, result.LastIndexOf("|"));
|
|||
|
}
|
|||
|
errorInfos.Clear();
|
|||
|
if (!string.IsNullOrEmpty(result))
|
|||
|
{
|
|||
|
string results = result;
|
|||
|
List<string> errorInfoList = results.Split('|').ToList();
|
|||
|
foreach (var item in errorInfoList)
|
|||
|
{
|
|||
|
string[] errors = item.Split(',');
|
|||
|
Model.ErrorInfo errorInfo = new Model.ErrorInfo();
|
|||
|
errorInfo.Row = errors[0];
|
|||
|
errorInfo.Column = errors[1];
|
|||
|
errorInfo.Reason = errors[2];
|
|||
|
errorInfos.Add(errorInfo);
|
|||
|
}
|
|||
|
if (errorInfos.Count > 0)
|
|||
|
{
|
|||
|
Grid1.DataSource = errorInfos;
|
|||
|
Grid1.DataBind();
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new Exception("导入数据为空!");
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
#region 导入
|
|||
|
/// <summary>
|
|||
|
/// 导入
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
protected void btnSave_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (errorInfos.Count <= 0)
|
|||
|
{
|
|||
|
string rootPath = Server.MapPath("~/");
|
|||
|
string initFullPath = rootPath + initPath;
|
|||
|
if (!Directory.Exists(initFullPath))
|
|||
|
{
|
|||
|
Directory.CreateDirectory(initFullPath);
|
|||
|
}
|
|||
|
|
|||
|
string filePath = initFullPath + this.hdfileName.Text;
|
|||
|
ImportXlsToData2(filePath);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Alert.ShowInTop("请先将错误数据修正,再重新导入保存!", MessageBoxIcon.Warning);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region 读Excel提取数据
|
|||
|
/// <summary>
|
|||
|
/// 从Excel提取数据--》Dataset
|
|||
|
/// </summary>
|
|||
|
/// <param name="filename">Excel文件路径名</param>
|
|||
|
private void ImportXlsToData2(string fileName)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
string oleDBConnString = String.Empty;
|
|||
|
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
|
|||
|
oleDBConnString += "Data Source=";
|
|||
|
oleDBConnString += fileName;
|
|||
|
oleDBConnString += ";Extended Properties=Excel 8.0;";
|
|||
|
OleDbConnection oleDBConn = null;
|
|||
|
OleDbDataAdapter oleAdMaster = null;
|
|||
|
DataTable m_tableName = new DataTable();
|
|||
|
DataSet ds = new DataSet();
|
|||
|
|
|||
|
oleDBConn = new OleDbConnection(oleDBConnString);
|
|||
|
oleDBConn.Open();
|
|||
|
m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
|
|||
|
|
|||
|
if (m_tableName != null && m_tableName.Rows.Count > 0)
|
|||
|
{
|
|||
|
|
|||
|
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
|
|||
|
|
|||
|
}
|
|||
|
string sqlMaster;
|
|||
|
sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
|
|||
|
oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
|
|||
|
oleAdMaster.SelectCommand.CommandTimeout = 1200;
|
|||
|
oleAdMaster.Fill(ds, "m_tableName");
|
|||
|
oleAdMaster.Dispose();
|
|||
|
oleDBConn.Close();
|
|||
|
oleDBConn.Dispose();
|
|||
|
|
|||
|
AddDatasetToSQL2(ds.Tables[0]);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 将Dataset的数据导入数据库
|
|||
|
/// </summary>
|
|||
|
/// <param name="pds">数据集</param>
|
|||
|
/// <param name="Cols">数据集列数</param>
|
|||
|
/// <returns></returns>
|
|||
|
private bool AddDatasetToSQL2(DataTable pds)
|
|||
|
{
|
|||
|
string result = string.Empty;
|
|||
|
HashSet<string> packageNums = new HashSet<string>();
|
|||
|
foreach (DataRow row in pds.Rows)
|
|||
|
{
|
|||
|
packageNums.Add(row["试压包"].ToString());
|
|||
|
}
|
|||
|
foreach (string packageNum in packageNums)
|
|||
|
{
|
|||
|
Model.TP_TestPackageImport testPackageImport = Funs.DB.TP_TestPackageImport.FirstOrDefault(x => x.PTP_TestPackageNo == packageNum && x.ProjectId == this.CurrUser.LoginProjectId);
|
|||
|
if (testPackageImport != null)
|
|||
|
{
|
|||
|
var oldIsoImportList = Funs.DB.TP_IsoImportList.Where(x => x.PTP_ID == testPackageImport.PTP_ID).ToList();
|
|||
|
Funs.DB.TP_IsoImportList.DeleteAllOnSubmit(oldIsoImportList);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
testPackageImport = new Model.TP_TestPackageImport();
|
|||
|
}
|
|||
|
testPackageImport.ProjectId = this.CurrUser.LoginProjectId;
|
|||
|
testPackageImport.CreateDate = DateTime.Now;
|
|||
|
testPackageImport.CreateManId = this.CurrUser.UserId;
|
|||
|
testPackageImport.PTP_TestPackageNo = packageNum;
|
|||
|
var rows = pds.Select("[试压包]='" + packageNum + "' ");
|
|||
|
if (rows.Length > 0)
|
|||
|
{
|
|||
|
testPackageImport.UnitId = Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitName == rows[0][0].ToString()|| x.UnitCode == rows[0][0].ToString()).UnitId;
|
|||
|
testPackageImport.WorkAreaId = Funs.DB.ProjectData_WorkArea.FirstOrDefault(x => x.ProjectId == this.CurrUser.LoginProjectId && x.WorkAreaCode == rows[0][1].ToString()).WorkAreaId;
|
|||
|
}
|
|||
|
|
|||
|
if (string.IsNullOrEmpty(testPackageImport.PTP_ID))
|
|||
|
{
|
|||
|
testPackageImport.PTP_ID = Guid.NewGuid().ToString();
|
|||
|
Funs.DB.TP_TestPackageImport.InsertOnSubmit(testPackageImport);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
foreach (DataRow row in rows)
|
|||
|
{
|
|||
|
Model.TP_IsoImportList isoImportList = new Model.TP_IsoImportList();
|
|||
|
isoImportList.PTP_ID = testPackageImport.PTP_ID;
|
|||
|
isoImportList.PT_ID = Guid.NewGuid().ToString();
|
|||
|
var iso = Funs.DB.PW_IsoInfo.FirstOrDefault(x => x.ISO_IsoNo == row[3].ToString());
|
|||
|
if (iso != null)
|
|||
|
{
|
|||
|
isoImportList.ISO_ID = iso.ISO_ID;
|
|||
|
iso.ISO_DesignPress = row[7].ToString();
|
|||
|
iso.ISO_DesignTemperature = row[8].ToString();
|
|||
|
iso.ISO_TestPress = row[12].ToString();
|
|||
|
if (!string.IsNullOrEmpty(row[11].ToString()))
|
|||
|
{
|
|||
|
iso.TestMediumId = Funs.DB.Base_TestMedium.FirstOrDefault(x => x.MediumName == row[11].ToString()|| x.MediumCode == row[11].ToString()).TestMediumId;
|
|||
|
}
|
|||
|
if (!string.IsNullOrEmpty(row[6].ToString()))
|
|||
|
{
|
|||
|
iso.PipingClassId = Funs.DB.Base_PipingClass.FirstOrDefault(x => x.PipingClassId == row[6].ToString() || x.PipingClassCode == row[6].ToString()).PipingClassId;
|
|||
|
}
|
|||
|
}
|
|||
|
isoImportList.FluidCode = row[5].ToString();
|
|||
|
isoImportList.OperTemperature = row[10].ToString();
|
|||
|
isoImportList.OperPress = row[9].ToString();
|
|||
|
isoImportList.PipingGrade = row[13].ToString();
|
|||
|
isoImportList.PAndID = row[14].ToString();
|
|||
|
isoImportList.NDECode = row[15].ToString();
|
|||
|
isoImportList.Size = row[16].ToString();
|
|||
|
isoImportList.Caliber = row[17].ToString();
|
|||
|
isoImportList.Start = row[18].ToString();
|
|||
|
isoImportList.End = row[19].ToString();
|
|||
|
isoImportList.LeakPress = row[20].ToString();
|
|||
|
isoImportList.PurgeWay = row[21].ToString();
|
|||
|
isoImportList.Remark = row[22].ToString();
|
|||
|
|
|||
|
Funs.DB.TP_IsoImportList.InsertOnSubmit(isoImportList);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
Funs.DB.SubmitChanges();
|
|||
|
ShowNotify("导入成功!", MessageBoxIcon.Success);
|
|||
|
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
|
|||
|
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|