SGGL_SHJ/SGGL/FineUIPro.Web/HJGL/TestPackage/TestPackageImport.aspx.cs

591 lines
24 KiB
C#
Raw Normal View History

2025-06-13 10:54:16 +08:00
using BLL;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.TestPackage
{
public partial class TestPackageImport : PageBase
{
#region
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 试压包集合
/// </summary>
public static List<Model.View_TestPackage_PipelineList> PipelineList = new List<Model.View_TestPackage_PipelineList>();
/// <summary>
/// 错误集合
/// </summary>
public static string errorInfos = string.Empty;
/// <summary>
/// 导入数据分类(管线)
/// </summary>
//public static string DataClassification = "Pipeline";
public enum ButtonState { Check = 0, Import = 1, Save = 2 }
public static int State;
#endregion
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.hdFileName.Text = string.Empty;
State = (int)ButtonState.Check;
if (PipelineList != null)
{
PipelineList.Clear();
}
errorInfos = string.Empty;
2025-08-18 10:15:28 +08:00
2025-06-13 10:54:16 +08:00
}
}
#endregion
2025-07-04 13:50:37 +08:00
2025-06-13 10:54:16 +08:00
#region
/// <summary>
/// 审核
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAudit_Click(object sender, EventArgs e)
{
try
{
if (this.fuAttachUrl.HasFile == false)
{
ShowNotify("请您选择Excel文件", MessageBoxIcon.Warning);
return;
}
string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower();
if (IsXls != ".xlsx")
{
ShowNotify("只可以选择Excel文件", MessageBoxIcon.Warning);
return;
}
if (PipelineList != null)
{
PipelineList.Clear();
}
if (!string.IsNullOrEmpty(errorInfos))
{
errorInfos = string.Empty;
}
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;
this.fuAttachUrl.PostedFile.SaveAs(filePath);
ImportXlsToData(rootPath + initPath + this.hdFileName.Text);
}
catch (Exception ex)
{
Alert alert = new Alert
{
Message = "'" + ex.Message + "'",
Target = Target.Self
};
alert.Show();
}
}
2025-07-04 13:50:37 +08:00
2025-06-13 10:54:16 +08:00
#region Excel提取数据
/// <summary>
/// 从Excel提取数据--》Dataset
/// </summary>
/// <param name="filename">Excel文件路径名</param>
private void ImportXlsToData(string fileName)
{
try
{
var ds = MiniExcel.Query(fileName).ToList();
var columns = MiniExcel.GetColumns(fileName);
var cnt = columns.Count;
var reposedata = AddDatasetToSQL(ds, cnt);
if (reposedata.code == 1)
{
State = (int)ButtonState.Import;
ShowNotify("审核完成请点击导入");
}
else
{
Alert alert = new Alert
{
Message = reposedata.message,
Target = Target.Self
};
alert.Show();
//ShowNotify(reposedata.message);
}
}
catch (Exception exc)
{
ErrLogInfo.WriteLog("试压包导入数据上传失败!", exc);
}
}
#endregion
#region Dataset的数据导入数据库
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集行数</param>
/// <returns></returns>
private Model.ResponeData AddDatasetToSQL(List<dynamic> pds, int count)
{
Model.ResponeData responeData = new Model.ResponeData();
// string result = string.Empty;
List<string> result = new List<string>();
//pds = BLL.Funs.FilterBlankLines(pds);
2025-08-18 10:15:28 +08:00
if (count < 11)
2025-06-13 10:54:16 +08:00
{
responeData.code = 0;
responeData.message = "导入Excel格式错误Excel只有" + count.ToString().Trim() + "列";
return responeData;
}
if (pds.Count > 0 && pds != null)
{
Model.SGGLDB db = Funs.DB;
2025-07-04 13:50:37 +08:00
//var getPipeline = from x in db.View_HJGL_WeldJoint where x.ProjectId == this.CurrUser.LoginProjectId select x;
2025-07-31 11:59:31 +08:00
var getPipeline = from x in db.HJGL_Pipeline
where x.ProjectId == this.CurrUser.LoginProjectId
&& x.UnitWorkId == Request.Params["UnitWorkId"]
2025-07-04 13:50:37 +08:00
select x;
2025-06-13 10:54:16 +08:00
Model.WBS_UnitWork unitWork = BLL.UnitWorkService.GetUnitWorkByUnitWorkId(Request.Params["UnitWorkId"]);
for (int i = 1; i < pds.Count; i++)
{
Model.View_TestPackage_PipelineList pipeline = new Model.View_TestPackage_PipelineList();
if (unitWork != null)
{
pipeline.UnitWorkId = Request.Params["UnitWorkId"];
pipeline.UnitId = unitWork.UnitId;
2025-06-13 10:54:16 +08:00
}
2025-09-24 17:31:28 +08:00
if (pds[i].B != null)
2025-06-13 10:54:16 +08:00
{
2025-09-24 17:31:28 +08:00
string col0 = pds[i].B.ToString();
2025-07-04 13:50:37 +08:00
if (string.IsNullOrEmpty(col0))
{
result.Add("第" + (i + 1).ToString() + "行," + "试压包编号" + "," + "此项为必填项!" + "|");
}
else
{
pipeline.TestPackageNo = col0;
}
2025-06-13 10:54:16 +08:00
}
else
{
2025-07-04 13:50:37 +08:00
result.Add("第" + (i + 1).ToString() + "行," + "试压包编号" + "," + "此项为必填项!" + "|");
2025-06-13 10:54:16 +08:00
}
2025-07-04 13:50:37 +08:00
if (pds[i].C != null)
2025-06-13 10:54:16 +08:00
{
2025-09-24 17:31:28 +08:00
pipeline.TestPackageName = pds[i].C.ToString();//系统名称
2025-08-18 10:15:28 +08:00
}
if (pds[i].D != null)
{
2025-09-24 17:31:28 +08:00
pipeline.AdjustTestPressure = pds[i].D.ToString();//调整试验压力
2025-08-18 10:15:28 +08:00
}
2025-09-24 17:31:28 +08:00
if (pds[i].E != null)
2025-08-18 10:15:28 +08:00
{
2025-09-24 17:31:28 +08:00
pipeline.Remark = pds[i].E.ToString(); //备注
}
if (pds[i].F != null) //管线号
{
string col2 = pds[i].F.ToString();
2025-07-04 13:50:37 +08:00
if (string.IsNullOrEmpty(col2))
2025-06-13 10:54:16 +08:00
{
2025-07-04 13:50:37 +08:00
result.Add("第" + (i + 1).ToString() + "行," + "管线号" + "," + "此项为必填项!" + "|");
2025-06-13 10:54:16 +08:00
}
2025-07-04 13:50:37 +08:00
else
2025-06-13 10:54:16 +08:00
{
2025-07-04 13:50:37 +08:00
var line = getPipeline.FirstOrDefault(u => u.PipelineCode == col2);
if (line != null)
{
var ptpLineList = from x in db.PTP_PipelineList where x.PipelineId == line.PipelineId select x;
2025-07-31 11:59:31 +08:00
if (ptpLineList.Count() > 0 && DrpType.SelectedValue == "0")
2025-07-04 13:50:37 +08:00
{
result.Add("第" + (i + 1).ToString() + "行," + "管线号[" + col2 + "]已试压!" + "|");
}
else
{
pipeline.PipelineId = line.PipelineId;
pipeline.PipelineCode = col2;
}
}
else
{
result.Add("第" + (i + 1).ToString() + "行," + "管线号[" + col2 + "]不存在!" + "|");
}
2025-06-13 10:54:16 +08:00
}
}
2025-07-04 13:50:37 +08:00
else
{
result.Add("第" + (i + 1).ToString() + "行," + "管线号" + "," + "此项为必填项!" + "|");
}
2025-06-13 10:54:16 +08:00
pipeline.Tabler = this.CurrUser.PersonId;
pipeline.TableDate = DateTime.Now;
if (!string.IsNullOrEmpty(pipeline.PipelineCode) && !string.IsNullOrEmpty(pipeline.TestPackageNo))
{
pipeline.ProjectId = this.CurrUser.LoginProjectId;
PipelineList.Add(pipeline);
}
}
if (result.Count > 0)
{
PipelineList.Clear();
errorInfos = string.Join("|", result.Distinct());
responeData.code = 0;
responeData.message = errorInfos;
}
else
{
errorInfos = string.Empty;
}
}
else
{
responeData.code = 0;
responeData.message = "导入数据为空!";
}
return responeData;
}
#endregion
#endregion
#region
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnImport_Click(object sender, EventArgs e)
{
if (State != (int)ButtonState.Import)
{
ShowNotify("请先审核");
return;
}
if (string.IsNullOrEmpty(errorInfos))
{
if (!string.IsNullOrEmpty(this.hdFileName.Text))
{
if (PipelineList.Count > 0)
{
this.Grid1.Hidden = false;
this.Grid1.DataSource = PipelineList;
this.Grid1.DataBind();
Grid1.RecordCount = PipelineList.Count;
State = (int)ButtonState.Save;
}
}
else
{
ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
#endregion
2025-07-04 13:50:37 +08:00
2025-06-13 10:54:16 +08:00
#region
/// <summary>
/// 提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
if (State != (int)ButtonState.Save)
{
ShowNotify("请先审核/导入");
return;
}
if (string.IsNullOrEmpty(errorInfos))
{
string rootPath = Server.MapPath("~/");
string oldefilePath = rootPath + initPath + this.hdFileName.Text;
string unitworkId = Request.Params["UnitWorkId"];
string filePath = rootPath + Const.TestPackageDataImportPath + this.hdFileName.Text;
if (oldefilePath != string.Empty && System.IO.File.Exists(oldefilePath))
{
if (!Directory.Exists(rootPath + Const.TestPackageDataImportPath))
{
Directory.CreateDirectory(rootPath + Const.TestPackageDataImportPath);
}
File.Move(oldefilePath, filePath);
}
string FileName = this.fuAttachUrl.FileName;
if (DrpType.SelectedValue == "1")//更新导入
{
2025-07-04 13:50:37 +08:00
AddView_TestPackage_PipelineList(PipelineList);//导入数据
2025-06-13 10:54:16 +08:00
}
else //补充导入
{
2025-07-04 13:50:37 +08:00
AddView_TestPackage_PipelineList(PipelineList);
2025-06-13 10:54:16 +08:00
}
ShowNotify("导入成功!", MessageBoxIcon.Success);
PipelineService.RestPipelineAndJoints(this.CurrUser.LoginProjectId);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
2025-07-04 13:50:37 +08:00
public void AddView_TestPackage_PipelineList(List<Model.View_TestPackage_PipelineList> PipelineList)
2025-06-13 10:54:16 +08:00
{
string unitworkId = Request.Params["UnitWorkId"];
addTestPackage(PipelineList, unitworkId);
2025-07-04 13:50:37 +08:00
addPTP_PipelineList(PipelineList, unitworkId);
2025-06-13 10:54:16 +08:00
}
void addTestPackage(List<Model.View_TestPackage_PipelineList> PipelineList, string UnitWorkId)
{
var pipelines = (from x in PipelineList
select new Model.PTP_TestPackage
{
PTP_ID = x.PTP_ID,
ProjectId = this.CurrUser.LoginProjectId,
UnitId = x.UnitId,
UnitWorkId = x.UnitWorkId,
TestPackageNo = x.TestPackageNo,
TestPackageName = x.TestPackageName,
Tabler = x.Tabler,
TableDate = x.TableDate,
2025-07-04 13:50:37 +08:00
Remark = x.Remark,
AdjustTestPressure = x.AdjustTestPressure
2025-06-13 10:54:16 +08:00
}).DistinctBy(temp => new
{
temp.PTP_ID,
temp.ProjectId,
temp.UnitId,
temp.UnitWorkId,
temp.TestPackageNo,
temp.TestPackageName,
2025-07-04 13:50:37 +08:00
temp.Remark,
temp.AdjustTestPressure
2025-06-13 10:54:16 +08:00
}).ToList();
for (int i = 0; i < pipelines.Count(); i++)
{
Model.PTP_TestPackage pipeline = new Model.PTP_TestPackage();
pipeline.PTP_ID = pipelines[i].PTP_ID;
pipeline.ProjectId = this.CurrUser.LoginProjectId;
pipeline.UnitId = pipelines[i].UnitId;
pipeline.UnitWorkId = pipelines[i].UnitWorkId;
pipeline.TestPackageNo = pipelines[i].TestPackageNo;
pipeline.TestPackageName = pipelines[i].TestPackageName;
pipeline.Tabler = pipelines[i].Tabler;
pipeline.TableDate = pipelines[i].TableDate;
pipeline.Remark = pipelines[i].Remark;
2025-07-04 13:50:37 +08:00
pipeline.AdjustTestPressure = pipelines[i].AdjustTestPressure;
2025-06-13 10:54:16 +08:00
var isExistTestPackageNo = TestPackageEditService.GetTestPackageByNo(pipeline.TestPackageNo, pipeline.UnitWorkId);
if (isExistTestPackageNo != null) // 更新试压包
{
pipeline.PTP_ID = isExistTestPackageNo.PTP_ID;
BLL.TestPackageEditService.UpdateTestPackage(pipeline);
}
else // 增加试压包
{
pipeline.PTP_ID = SQLHelper.GetNewID();
BLL.TestPackageEditService.AddTestPackage(pipeline);
}
}
}
2025-07-04 13:50:37 +08:00
void addPTP_PipelineList(List<Model.View_TestPackage_PipelineList> PipelineList, string UnitWorkId)
2025-06-13 10:54:16 +08:00
{
Model.SGGLDB db = Funs.DB;
2025-07-04 13:50:37 +08:00
var getTestPackage = from y in db.PTP_TestPackage where y.UnitWorkId == UnitWorkId select y;
var allPipelineLists = from x in db.PTP_PipelineList
join y in db.PTP_TestPackage on x.PTP_ID equals y.PTP_ID
where y.UnitWorkId == UnitWorkId
select x;
2025-07-04 13:50:37 +08:00
List<Model.PTP_PipelineList> pipelineList_add = new List<Model.PTP_PipelineList>();
2025-06-13 10:54:16 +08:00
var weldJoints = (from x in PipelineList
select new Model.PTP_PipelineList
{
2025-06-13 10:54:16 +08:00
PTP_ID = x.PTP_ID,
TestPackageNo = x.TestPackageNo,
2025-06-13 10:54:16 +08:00
PipelineId = x.PipelineId,
2025-07-04 13:50:37 +08:00
//TestPressure = x.TestPressure,
2025-06-13 10:54:16 +08:00
}).DistinctBy(temp => new
{
temp.PTP_ID,
2025-07-04 13:50:37 +08:00
temp.TestPackageNo,
2025-06-13 10:54:16 +08:00
temp.PipelineId,
2025-07-04 13:50:37 +08:00
//temp.TestPressure
2025-06-13 10:54:16 +08:00
}).ToList();
var pipelineCodes = weldJoints.Select(x => x.PTP_ID).Distinct().ToList();
for (int i = 0; i < weldJoints.Count(); i++)
{
Model.PTP_PipelineList weldJoint = new Model.PTP_PipelineList();
2025-07-04 13:50:37 +08:00
weldJoint.PTP_ID = getTestPackage.Where(x => x.TestPackageNo == weldJoints[i].TestPackageNo).FirstOrDefault().PTP_ID;
weldJoint.PipelineId = weldJoints[i].PipelineId;
//weldJoint.TestPressure = weldJoints[i].TestPressure;
2025-06-13 10:54:16 +08:00
2025-07-04 13:50:37 +08:00
var isExistJot = allPipelineLists.FirstOrDefault(x => x.PTP_ID == weldJoint.PTP_ID && x.PipelineId == weldJoint.PipelineId);
if (isExistJot != null) // 更新试压管线
2025-06-13 10:54:16 +08:00
{
weldJoint.PT_PipeId = isExistJot.PT_PipeId;
BLL.TestPackageEditService.UpdatePipelineList(weldJoint);
}
else // 增加试压管线
{
weldJoint.PT_PipeId = SQLHelper.GetNewID();
2025-07-04 13:50:37 +08:00
pipelineList_add.Add(weldJoint);
2025-06-13 10:54:16 +08:00
}
}
2025-07-04 13:50:37 +08:00
if (pipelineList_add.Count > 0)
2025-06-13 10:54:16 +08:00
{
2025-07-04 13:50:37 +08:00
BLL.TestPackageEditService.AddPipelineLists(pipelineList_add);
2025-06-13 10:54:16 +08:00
}
}
#endregion
#region
/// <summary>
/// 下载模板按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDownLoad_Click(object sender, EventArgs e)
{
PageContext.RegisterStartupScript(Confirm.GetShowReference("确定下载导入模板吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel")));
}
2025-08-18 10:15:28 +08:00
protected void btnDownLoadLine_Click(object sender, EventArgs e)
{
PageContext.RegisterStartupScript(Confirm.GetShowReference("确定下载导入模板吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "DownLine_Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel")));
}
2025-06-13 10:54:16 +08:00
/// <summary>
/// 下载导入模板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PageManager1_CustomEvent(object sender, CustomEventArgs e)
{
2025-08-18 10:15:28 +08:00
string rootPath = Server.MapPath("~/");
string uploadfilepath = rootPath + Const.TestPackageTemplateUrl;
string path = uploadfilepath.Replace(".xlsx", string.Format("{0:yyyy-MM-dd-HH-mm}", DateTime.Now) + ".xlsx");
string fileName = Path.GetFileName(uploadfilepath);
if (e.EventArgument == "DownLine_Confirm_OK")
2025-06-13 10:54:16 +08:00
{
2025-08-18 10:15:28 +08:00
string UnitWorkId = Request.Params["UnitWorkId"];
var queryList = from x in Funs.DB.View_HJGL_Pipeline
join y in Funs.DB.PTP_PipelineList on x.PipelineId equals y.PipelineId into yGroup
from y in yGroup.DefaultIfEmpty()
where x.UnitWorkId == UnitWorkId && y == null
select new
{
x.PipelineId,
x.PipelineCode,
x.PipingClassCode,
x.MediumName,
x.DesignPress,
x.DesignTemperature,
x.TestMediumCode,
x.TestPressure
};
2025-08-18 10:15:28 +08:00
2025-09-24 17:31:28 +08:00
// materialize and add sequence number
var list = queryList.ToList();
var indexed = list.Select((x, idx) => new
{
No = idx + 1,
x.PipelineId,
x.PipelineCode,
x.PipingClassCode,
x.MediumName,
x.DesignPress,
x.DesignTemperature,
x.TestMediumCode,
x.TestPressure
}).ToList();
2025-08-18 10:15:28 +08:00
var value = new
{
2025-09-24 17:31:28 +08:00
model = indexed
2025-08-18 10:15:28 +08:00
};
MiniExcel.SaveAsByTemplate(path, uploadfilepath, value);
2025-06-13 10:54:16 +08:00
}
2025-08-18 10:15:28 +08:00
else if (e.EventArgument == "Confirm_OK")
{
var value = new
{
model = new List<object>()
2025-08-18 10:15:28 +08:00
};
MiniExcel.SaveAsByTemplate(path, uploadfilepath, value);
2025-08-18 10:15:28 +08:00
}
FileInfo info = new FileInfo(path);
long fileSize = info.Length;
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.ContentType = "excel/plain";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString().Trim());
System.Web.HttpContext.Current.Response.TransmitFile(path, 0, fileSize);
System.Web.HttpContext.Current.Response.Flush();
2025-08-18 10:15:28 +08:00
File.Delete(path);
System.Web.HttpContext.Current.Response.End();
2025-06-13 10:54:16 +08:00
}
#endregion
2025-08-18 10:15:28 +08:00
2025-06-13 10:54:16 +08:00
#region
/// <summary>
/// 分页选择下拉改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
this.Grid1.DataSource = PipelineList;
this.Grid1.DataBind();
Grid1.RecordCount = PipelineList.Count;
}
#endregion
}
}