using BLL; using MiniExcelLibs; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace FineUIPro.Web.HJGL.TestPackage { public partial class TestPackageImport : PageBase { #region 定义变量 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 试压包集合 /// public static List PipelineList = new List(); /// /// 错误集合 /// public static string errorInfos = string.Empty; /// /// 导入数据分类(管线) /// //public static string DataClassification = "Pipeline"; public enum ButtonState { Check = 0, Import = 1, Save = 2 } public static int State; #endregion #region 加载 /// /// 加载页面 /// /// /// 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; //lbVersion.Text = BLL.HJGL_DesignBasisDataImportService.GetNowVersionByUnitWorkId(Request.Params["UnitWorkId"], DataClassification).ToString(); //BindGrid2(); } } #endregion //void BindGrid2() //{ // string strSql = @" select Import.DesignBasisDataImportId // ,Import.ProjectId // ,Import.UnitWorkId // ,(Case Import.ImportType when '0' then '补充导入' // when '1' then '更新导入' end) as ImportType // ,Import.FileName // ,Import.FilePath // ,Import.DataClassification // ,Import.FileType // ,Import.FileSize // ,Import.FileId // ,Import.Version // ,Import.Remark // ,Import.CreateMan // ,Import.CreateDate // , Users.PersonName // from HJGL_DesignBasisDataImport as Import // left join Person_Persons as Users on Users.PersonId=Import.CreateMan // where Import.UnitWorkId=@UnitWorkId and Import.ProjectId=@ProjectId and Import.DataClassification=@DataClassification // order by Import.CreateDate"; // List listStr = new List(); // listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId)); // listStr.Add(new SqlParameter("@UnitWorkId", Request.Params["UnitWorkId"])); // //listStr.Add(new SqlParameter("@DataClassification", DataClassification)); // SqlParameter[] parameter = listStr.ToArray(); // DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); // // 2.获取当前分页数据 // //var table = this.GetPagedDataTable(Grid1, tb1); // Grid2.RecordCount = tb.Rows.Count; // tb = GetFilteredTable(Grid2.FilteredData, tb); // var table = this.GetPagedDataTable(Grid2, tb); // Grid2.DataSource = table; // Grid2.DataBind(); // drpVersion.DataSource = BLL.HJGL_DesignBasisDataImportService.GetListVersionByUnitWorkId(Request.Params["UnitWorkId"], DataClassification); // drpVersion.DataBind(); //} #region 审核 /// /// 审核 /// /// /// 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(); } } #region 读Excel提取数据 /// /// 从Excel提取数据--》Dataset /// /// Excel文件路径名 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的数据导入数据库 /// /// 将Dataset的数据导入数据库 /// /// 数据集 /// 数据集行数 /// private Model.ResponeData AddDatasetToSQL(List pds, int count) { Model.ResponeData responeData = new Model.ResponeData(); // string result = string.Empty; List result = new List(); //pds = BLL.Funs.FilterBlankLines(pds); if (count < 5) { responeData.code = 0; responeData.message = "导入Excel格式错误!Excel只有" + count.ToString().Trim() + "列"; return responeData; } if (pds.Count > 0 && pds != null) { Model.SGGLDB db = Funs.DB; //var getPipeline = from x in db.View_HJGL_WeldJoint where x.ProjectId == this.CurrUser.LoginProjectId select x; var getPipeline = from x in db.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitWorkId == Request.Params["UnitWorkId"] select x; 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; } if (pds[i].A != null) { string col0 = pds[i].A.ToString(); if (string.IsNullOrEmpty(col0)) { result.Add("第" + (i + 1).ToString() + "行," + "试压包编号" + "," + "此项为必填项!" + "|"); } else { pipeline.TestPackageNo = col0; } } else { result.Add("第" + (i + 1).ToString() + "行," + "试压包编号" + "," + "此项为必填项!" + "|"); } if (pds[i].B != null) { pipeline.TestPackageName = pds[i].B.ToString();//系统名称 } if (pds[i].C != null) { string col2 = pds[i].C.ToString(); if (string.IsNullOrEmpty(col2)) { result.Add("第" + (i + 1).ToString() + "行," + "管线号" + "," + "此项为必填项!" + "|"); } else { 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; if (ptpLineList.Count() > 0 && DrpType.SelectedValue == "0") { result.Add("第" + (i + 1).ToString() + "行," + "管线号[" + col2 + "]已试压!" + "|"); } else { pipeline.PipelineId = line.PipelineId; pipeline.PipelineCode = col2; } } else { result.Add("第" + (i + 1).ToString() + "行," + "管线号[" + col2 + "]不存在!" + "|"); } } } else { result.Add("第" + (i + 1).ToString() + "行," + "管线号" + "," + "此项为必填项!" + "|"); } if (pds[i].D != null) { pipeline.AdjustTestPressure = pds[i].D.ToString(); } if (pds[i].E != null) { pipeline.Remark = pds[i].E.ToString(); } 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 导入 /// /// 导入 /// /// /// 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 #region 提交 /// /// 提交 /// /// /// 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")//更新导入 { //BLL.PipelineMatService.DeletePipeLineMatByUnitWorkId(unitworkId);//删除原有管线对应材料 //BLL.WeldJointService.DeleteWeldJointByUnitWorkId(unitworkId);////删除原有管线对应焊口 //BLL.PipelineService.DeletePipelineByUnitworkId(unitworkId);//删除原有管线 AddView_TestPackage_PipelineList(PipelineList);//导入数据 //Model.HJGL_DesignBasisDataImport hJGL_DesignBasisDataImport = new Model.HJGL_DesignBasisDataImport(); //hJGL_DesignBasisDataImport.DesignBasisDataImportId = SQLHelper.GetNewID(); //hJGL_DesignBasisDataImport.ProjectId = this.CurrUser.LoginProjectId; //hJGL_DesignBasisDataImport.UnitWorkId = unitworkId; //hJGL_DesignBasisDataImport.ImportType = "1"; //hJGL_DesignBasisDataImport.DataClassification = DataClassification; //hJGL_DesignBasisDataImport.FileName = FileName; //hJGL_DesignBasisDataImport.FilePath = filePath.Replace(rootPath, ""); //hJGL_DesignBasisDataImport.FileType = BLL.HJGL_DesignBasisDataImportService.GetFileType(FileName); //hJGL_DesignBasisDataImport.Version = BLL.HJGL_DesignBasisDataImportService.GetNewVersionByUnitWorkId(unitworkId, DataClassification); //hJGL_DesignBasisDataImport.Remark = txtRemark.Text; //hJGL_DesignBasisDataImport.CreateMan = this.CurrUser.PersonId; //hJGL_DesignBasisDataImport.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"); //BLL.HJGL_DesignBasisDataImportService.AddHJGL_DesignBasisDataImport(hJGL_DesignBasisDataImport); //BLL.HJGL_DesignBasisDataImportVerSionLogService.UpdateVersion(this.CurrUser.LoginProjectId, unitworkId, (decimal)hJGL_DesignBasisDataImport.Version, DataClassification); } else //补充导入 { AddView_TestPackage_PipelineList(PipelineList); //Model.HJGL_DesignBasisDataImport hJGL_DesignBasisDataImport = new Model.HJGL_DesignBasisDataImport(); //hJGL_DesignBasisDataImport.DesignBasisDataImportId = SQLHelper.GetNewID(); //hJGL_DesignBasisDataImport.ProjectId = this.CurrUser.LoginProjectId; //hJGL_DesignBasisDataImport.UnitWorkId = unitworkId; //hJGL_DesignBasisDataImport.ImportType = "0"; //hJGL_DesignBasisDataImport.DataClassification = DataClassification; //hJGL_DesignBasisDataImport.FileName = FileName; //hJGL_DesignBasisDataImport.FilePath = filePath.Replace(rootPath, ""); ; //hJGL_DesignBasisDataImport.FileType = BLL.HJGL_DesignBasisDataImportService.GetFileType(FileName); //hJGL_DesignBasisDataImport.Version = BLL.HJGL_DesignBasisDataImportService.GetNowVersionByUnitWorkId(unitworkId, DataClassification); //hJGL_DesignBasisDataImport.Remark = txtRemark.Text; //hJGL_DesignBasisDataImport.CreateMan = this.CurrUser.PersonId; //hJGL_DesignBasisDataImport.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"); //BLL.HJGL_DesignBasisDataImportService.AddHJGL_DesignBasisDataImport(hJGL_DesignBasisDataImport); //BLL.HJGL_DesignBasisDataImportVerSionLogService.UpdateVersion(this.CurrUser.LoginProjectId, unitworkId, (decimal)hJGL_DesignBasisDataImport.Version, DataClassification); } ShowNotify("导入成功!", MessageBoxIcon.Success); PipelineService.RestPipelineAndJoints(this.CurrUser.LoginProjectId); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning); } } public void AddView_TestPackage_PipelineList(List PipelineList) { string unitworkId = Request.Params["UnitWorkId"]; addTestPackage(PipelineList, unitworkId); addPTP_PipelineList(PipelineList, unitworkId); } void addTestPackage(List 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, Remark = x.Remark, AdjustTestPressure=x.AdjustTestPressure }).DistinctBy(temp => new { temp.PTP_ID, temp.ProjectId, temp.UnitId, temp.UnitWorkId, temp.TestPackageNo, temp.TestPackageName, temp.Remark, temp.AdjustTestPressure }).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; pipeline.AdjustTestPressure = pipelines[i].AdjustTestPressure; 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); } } } void addPTP_PipelineList(List PipelineList, string UnitWorkId) { Model.SGGLDB db = Funs.DB; 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; List pipelineList_add = new List(); var weldJoints = (from x in PipelineList select new Model.PTP_PipelineList { PTP_ID = x.PTP_ID, TestPackageNo=x.TestPackageNo, PipelineId = x.PipelineId, //TestPressure = x.TestPressure, }).DistinctBy(temp => new { temp.PTP_ID, temp.TestPackageNo, temp.PipelineId, //temp.TestPressure }).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(); weldJoint.PTP_ID = getTestPackage.Where(x => x.TestPackageNo == weldJoints[i].TestPackageNo).FirstOrDefault().PTP_ID; weldJoint.PipelineId = weldJoints[i].PipelineId; //weldJoint.TestPressure = weldJoints[i].TestPressure; var isExistJot = allPipelineLists.FirstOrDefault(x => x.PTP_ID == weldJoint.PTP_ID && x.PipelineId == weldJoint.PipelineId); if (isExistJot != null) // 更新试压管线 { weldJoint.PT_PipeId = isExistJot.PT_PipeId; BLL.TestPackageEditService.UpdatePipelineList(weldJoint); } else // 增加试压管线 { weldJoint.PT_PipeId = SQLHelper.GetNewID(); pipelineList_add.Add(weldJoint); } } if (pipelineList_add.Count > 0) { BLL.TestPackageEditService.AddPipelineLists(pipelineList_add); } //Model.SGGLDB db2 = Funs.DB; //List delJoints = new List(); //var allWeldJoints2 = from x in db2.PTP_PipelineList // join y in db2.PTP_TestPackage on x.PTP_ID equals y.PTP_ID // where y.UnitWorkId == Request.Params["UnitWorkId"] // select x; //foreach (var pipelineCode in pipelineCodes) //{ // var pipelineWeldJointCodes = weldJoints.Where(x => x.PTP_ID == pipelineCode).Select(x => x.PipelineId).ToList(); // var q = allWeldJoints2.Where(x => x.PTP_ID == pipelineCode && !pipelineWeldJointCodes.Contains(x.PipelineId)).ToList(); // delJoints.AddRange(q); //} //if (delJoints.Count() > 0) //{ // try // { // db2.PTP_PipelineList.DeleteAllOnSubmit(delJoints); // db2.SubmitChanges(); // } // catch (Exception) // { // string weldJointCodes = string.Empty; // foreach (var item in delJoints) // { // weldJointCodes += item.PipelineId + ","; // } // Alert.ShowInParent(weldJointCodes, MessageBoxIcon.Warning); // } //} } #endregion /// /// 恢复版本 /// /// /// private void RestoreVersion(string unitworkId, decimal version) { //var model = BLL.HJGL_DesignBasisDataImportService.GetDataByUnitWorkIdAndVersion(unitworkId, version, DataClassification); //if (model != null && model.Count > 0) //{ // foreach (var item in model) // { // string rootPath = Server.MapPath("~/"); // // initFullPath = rootPath + initPath; // string filePath = rootPath + item.FilePath; // ImportXlsToData(filePath); // BLL.PipelineMatService.DeletePipeLineMatByUnitWorkId(unitworkId);//删除原有管线对应材料 // BLL.WeldJointService.DeleteWeldJointByUnitWorkId(unitworkId);////删除原有管线对应焊口 // BLL.PipelineService.DeletePipelineByUnitworkId(unitworkId);//删除原有管线 // AddView_HJGL_WeldJoint(PipelineList);//导入数据 // PipelineList.Clear(); // } // BLL.HJGL_DesignBasisDataImportVerSionLogService.UpdateVersion(this.CurrUser.LoginProjectId, unitworkId, version, DataClassification); // PipelineService.RestPipelineAndJoints(this.CurrUser.LoginProjectId); // ShowNotify("恢复成功!"); //} } protected void btnRestore_Click(object sender, EventArgs e) { //if (string.IsNullOrEmpty(drpVersion.SelectedValue)) //{ // ShowNotify("请选择版本"); //} //else //{ // PageContext.RegisterStartupScript(Confirm.GetShowReference("确定恢复至该版本吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_Restore"), PageManager1.GetCustomEventReference("Confirm_Cancel"))); //} } #region 下载模板 /// /// 下载模板按钮 /// /// /// 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"))); } /// /// 下载导入模板 /// /// /// protected void PageManager1_CustomEvent(object sender, CustomEventArgs e) { if (e.EventArgument == "Confirm_OK") { string rootPath = Server.MapPath("~/"); string uploadfilepath = rootPath + Const.TestPackageTemplateUrl; string filePath = Const.TestPackageTemplateUrl; string fileName = Path.GetFileName(filePath); FileInfo info = new FileInfo(uploadfilepath); long fileSize = info.Length; Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.ContentType = "excel/plain"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Length", fileSize.ToString().Trim()); Response.TransmitFile(uploadfilepath, 0, fileSize); Response.End(); } //else if (e.EventArgument == "Confirm_Restore") //{ // var unitworkid = Request.Params["UnitWorkId"]; // decimal version = decimal.Parse(drpVersion.SelectedValue); // RestoreVersion(unitworkid, version); //} } #endregion #region 分页选择下拉改变事件 /// /// 分页选择下拉改变事件 /// /// /// 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 } }