using BLL; using MiniExcelLibs; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; namespace FineUIPro.Web.HJGL.PreDesign { public partial class PrePipelineQRCodeIn : PageBase { #region 定义变量 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 安装组件集合 /// public static List PipelineList = new List(); public static List Pipeline_ComponentList = new List(); /// /// 错误集合 /// public static string errorInfos = string.Empty; public string UnitWorkId { get { return (string)ViewState["UnitWorkId"]; } set { ViewState["UnitWorkId"] = value; } } #endregion #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.hdFileName.Text = string.Empty; UnitWorkId = Request.Params["UnitWorkId"]; if (PipelineList != null) { PipelineList.Clear(); } if (Pipeline_ComponentList != null) { Pipeline_ComponentList.Clear(); } errorInfos = string.Empty; } } #endregion #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) { ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning); } } #region 读Excel提取数据 /// /// 从Excel提取数据--》Dataset /// /// Excel文件路径名 private void ImportXlsToData(string fileName) { // var rows =Funs.LINQToDataTable(MiniExcel.Query(fileName).ToList()) ; var rows = MiniExcel.QueryAsDataTable(fileName, useHeaderRow: true); Model.ResponeData responeData = new Model.ResponeData(); responeData = AddDatasetToSQL(rows, 3); if (responeData.code==1) { ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success); } else { ShowNotify(responeData.message, MessageBoxIcon.Success); } } #endregion #region 将Dataset的数据导入数据库 /// /// 将Dataset的数据导入数据库 /// /// 数据集 /// 数据集行数 /// private Model.ResponeData AddDatasetToSQL(DataTable pds, int Cols) { Model.ResponeData responeData = new Model.ResponeData(); //string result = string.Empty; List result = new List () ; int ic, ir; ic = pds.Columns.Count; ir = pds.Rows.Count; if (ic < Cols) { responeData.code = 0; responeData.message = "导入Excel格式错误!Excel只有" + ic.ToString().Trim() + "列"; return responeData; } if (pds != null && ir > 0) { var getPipeline = from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId select x; for (int i = 0; i < ir; i++) { Model.HJGL_Pipeline pipeline = new Model.HJGL_Pipeline(); Model.HJGL_Pipeline_Component hJGL_Pipeline_Component = new Model.HJGL_Pipeline_Component(); string pipelinecode = pds.Rows[i]["管线号"].ToString(); string PipelineComponentCode = pds.Rows[i]["预制组件"].ToString(); string QRCode = pds.Rows[i]["二维码信息"].ToString(); if (string.IsNullOrEmpty(pipelinecode)) { //result += "第" + (i + 2).ToString() + "行," + "管线号" + "," + "此项为必填项!" + "|"; result.Add("第" + (i + 2).ToString() + "行," + "管线号" + "," + "此项为必填项!"); } else { pipeline = getPipeline.FirstOrDefault(x => x.PipelineCode == pipelinecode); if (pipeline == null) { //result += "第" + (i + 2).ToString() + "行," + "不存在此管线号" + "|"; result.Add("不存在此管线号-"+ pipelinecode); continue; } } if (string.IsNullOrEmpty(PipelineComponentCode)) { //result += "第" + (i + 2).ToString() + "行," + "预制组件" + "," + "此项为必填项!" + "|"; result.Add("第" + (i + 2).ToString() + "行," + "预制组件" + "," + "此项为必填项!"); } else { hJGL_Pipeline_Component = HJGL_PipelineComponentService.GetPipelineComponentByCodeandpipelineId(PipelineComponentCode, pipeline.PipelineId); if (hJGL_Pipeline_Component == null) { //result += "第" + (i + 2).ToString() + "行," + "不存在此预制组件 " + PipelineComponentCode + " |"; result.Add("不存在此预制组件-" + PipelineComponentCode); continue; } } } if (result.Count>0) { PipelineList.Clear(); // result = result.Substring(0, result.LastIndexOf("|")); errorInfos = string .Join("|", result.Distinct()) ; //Alert alert = new Alert(); //alert.Message = result; //alert.Target = Target.Self; //alert.Show(); responeData.code = 0; responeData.message = errorInfos; } else { var dt = pds.AsEnumerable() .GroupBy(x => new { pipelinecode = x.Field("管线号"), PipelineComponentCode = x.Field("预制组件"), // DrawingName = x.Field("预制图纸名称"), }) .Select(x => new { pipelinecode = x.Key.pipelinecode, PipelineComponentCode = x.Key.PipelineComponentCode, // DrawingName = x.Key.DrawingName, QRCode = String.Join(";", x.Select(z => z.Field("二维码信息"))) }); foreach (var item in dt) { var pipeline = PipelineService.GetPipelineByPipelineCode(item.pipelinecode.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "")); var hJGL_Pipeline_Component = HJGL_PipelineComponentService.GetPipelineComponentByCodeandpipelineId(item.PipelineComponentCode.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""), pipeline.PipelineId); if (hJGL_Pipeline_Component!=null) { //model.Pipeline_ComponentQRCodeRootName rootName = new model.Pipeline_ComponentQRCodeRootName(); //rootName.QRCode = new List(); //var elementlist = item.QRCode.Split(';'); //foreach (var element in elementlist) //{ // model.Pipeline_ComponentQRCodeQRCodeItem qRCodeQRCodeItem = new model.Pipeline_ComponentQRCodeQRCodeItem(); // qRCodeQRCodeItem.element = element; // rootName.QRCode.Add(qRCodeQRCodeItem); //} //hJGL_Pipeline_Component.QRCode = JsonConvert.SerializeObject(rootName); hJGL_Pipeline_Component.QRCode = item.QRCode; Pipeline_ComponentList.Add(hJGL_Pipeline_Component); } } errorInfos = string.Empty; // ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success); } } else { responeData.code = 0; responeData.message = "导入数据为空!"; } return responeData; } #endregion #endregion #region 导入 /// /// 导入 /// /// /// protected void btnImport_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(errorInfos)) { if (!string.IsNullOrEmpty(this.hdFileName.Text)) { if (Pipeline_ComponentList.Count > 0) { this.Grid1.Hidden = false; this.Grid1.DataIDField = "PipelineComponentId"; this.Grid1.DataSource = Pipeline_ComponentList; this.Grid1.DataBind(); Grid1.RecordCount = Pipeline_ComponentList.Count; } } else { ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning); } } else { ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning); } } #endregion #region 提交 /// /// 提交 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(errorInfos)) { if (Pipeline_ComponentList.Count > 0) { foreach (var item in Pipeline_ComponentList) { BLL.HJGL_PipelineComponentService.UpdatePipelineComponent(item); } } int a = PipelineList.Count(); string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; string filePath = initFullPath + this.hdFileName.Text; if (filePath != string.Empty && System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath);//删除上传的XLS文件 } ShowNotify("导入成功!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning); } } #endregion #region 获取名称 public string ConvertPipelineId(object PipelineId) { string StateName = string.Empty; if (!string.IsNullOrEmpty(PipelineId.ToString())) { if (PipelineId != null) { string txt = PipelineService.GetPipelineByPipelineId(PipelineId.ToString()).PipelineCode; return txt; } return ""; } return StateName; } #endregion #region 下载模板 /// /// 下载模板按钮 /// /// /// protected void btnDownLoad_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/"); string uploadfilepath = rootPath + Const.Pipeline_ComponentPath; string filePath = Const.Pipeline_ComponentPath; 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(); } #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 } }