using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.IO.Compression; using System.Linq; namespace FineUIPro.Web.HSSE.HazardousMG { /// /// 安全巡检页面 /// public partial class SafetyPatrol : PageBase { #region 关联危大工程主键 /// /// 关联的危大、超危工程主键(从危大清单右键进入时按此过滤) /// private string HazardProjectId { get { return (string)ViewState["HazardProjectId"]; } set { ViewState["HazardProjectId"] = value; } } #endregion #region 加载 /// /// 加载页面 /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.HazardProjectId = Request.Params["HazardProjectId"]; this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); this.BindGrid(); } } /// /// 导出Word:每条记录生成独立文档(文档名使用监督巡视部位)。 /// 单条直接下载 .docx;多条打包为 .zip,压缩包内每个 .docx 以监督巡视部位命名。 /// protected void btnOut_Click(object sender, EventArgs e) { string[] ids = Grid1.SelectedRowIDArray; if (ids == null || ids.Length == 0) { Alert.ShowInTop("请先勾选要导出的巡检记录!", MessageBoxIcon.Warning); return; } List> files = new List>(); int dupIndex = 0; foreach (string id in ids) { if (string.IsNullOrEmpty(id)) { continue; } Model.HSSE_SafetyPatrol info = BLL.SafetyPatrolService.GetById(id); if (info == null) { continue; } byte[] bytes = BLL.SafetyPatrolWordService.Generate(id); if (bytes == null) { continue; } ////文件名 = 监督巡视部位 + 编号(编码唯一,规避同名覆盖) string baseName = SanitizeFileName(info.HazardProjectName); string codeSuffix = string.IsNullOrEmpty(info.Code) ? string.Empty : "_" + info.Code; string fileName = baseName + codeSuffix + ".docx"; // 同名记录去重,避免覆盖 while (files.Any(f => f.Key == fileName)) { dupIndex++; fileName = baseName + "(" + dupIndex + ").docx"; } files.Add(new KeyValuePair(fileName, bytes)); } if (files.Count == 0) { Alert.ShowInTop("未找到有效巡检记录!", MessageBoxIcon.Warning); return; } Response.Clear(); if (files.Count == 1) { // 单条:直接下发 .docx Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(files[0].Key, System.Text.Encoding.UTF8)); Response.BinaryWrite(files[0].Value); } else { // 多条:打包为 .zip Response.ContentType = "application/zip"; Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("安全巡检记录_" + BLL.Funs.GetNewFileName() + ".zip", System.Text.Encoding.UTF8)); using (MemoryStream ms = new MemoryStream()) { using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true)) { foreach (KeyValuePair f in files) { ZipArchiveEntry entry = zip.CreateEntry(f.Key, CompressionLevel.Optimal); using (Stream es = entry.Open()) { es.Write(f.Value, 0, f.Value.Length); } } } Response.BinaryWrite(ms.ToArray()); } } Response.Flush(); Response.End(); } /// /// 清洗文件名(去非法字符),保证文件名合法可下载。 /// /// 监督巡视部位名称 /// 合法文件名(不含扩展名) private static string SanitizeFileName(string name) { if (string.IsNullOrEmpty(name)) { return "监督记录表"; } string fileName = name; foreach (char c in Path.GetInvalidFileNameChars()) { fileName = fileName.Replace(c, '_'); } fileName = fileName.Trim(); if (fileName.Length == 0) { return "监督记录表"; } if (fileName.Length > 80) { fileName = fileName.Substring(0, 80); } return fileName; } /// /// 扫码巡检按钮事件 /// protected void btnNew_Click(object sender, EventArgs e) { if (this.CurrUser == null || string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { Alert.ShowInTop("请先选择项目!", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window1.GetShowReference("SafetyPatrolEdit.aspx")); } #endregion #region 模板绑定辅助方法 /// /// 巡检状态颜色(待整改标红、整改待复查标橙、正常标绿) /// public Color GetPatrolStatusColor(object value) { string state = value == null ? "" : value.ToString(); switch (state) { case "待整改": return Color.Red; case "整改待复查": return Color.Orange; default: return Color.Green; } } #endregion #region 数据绑定 /// /// 绑定数据 /// private void BindGrid() { if (this.CurrUser == null || string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { Alert.ShowInTop("请先登录或选择项目!", MessageBoxIcon.Warning); return; } List list = BLL.SafetyPatrolService.GetList(this.CurrUser.LoginProjectId); ////按关联危大工程过滤(从危大清单右键进入) if (!string.IsNullOrEmpty(this.HazardProjectId)) { list = list.Where(x => x.HazardProjectId == this.HazardProjectId).ToList(); } ////查询条件过滤 string projectName = this.txtProjectName.Text.Trim(); if (!string.IsNullOrEmpty(projectName)) { list = list.Where(x => x.HazardProjectName != null && x.HazardProjectName.Contains(projectName)).ToList(); } ////巡检日期时间段过滤 DateTime startDate; if (DateTime.TryParse(this.txtStartTime.Text, out startDate)) { list = list.Where(x => x.PatrolDate.HasValue && x.PatrolDate.Value.Date >= startDate.Date).ToList(); } DateTime endDate; if (DateTime.TryParse(this.txtEndTime.Text, out endDate)) { list = list.Where(x => x.PatrolDate.HasValue && x.PatrolDate.Value.Date <= endDate.Date).ToList(); } Grid1.RecordCount = list.Count; var table = this.GetPagedDataTable(Grid1, list); Grid1.DataSource = table; Grid1.DataBind(); } /// /// 分页事件 /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { this.BindGrid(); } /// /// 每页记录数下拉事件 /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue); this.BindGrid(); } /// /// 排序事件 /// protected void Grid1_Sort(object sender, GridSortEventArgs e) { this.BindGrid(); } #endregion #region 查询 /// /// 查询 /// protected void TextBox_TextChanged(object sender, EventArgs e) { this.BindGrid(); } /// /// 重置查询条件 /// protected void btnRset_Click(object sender, EventArgs e) { this.txtProjectName.Text = ""; this.txtStartTime.Text = ""; this.txtEndTime.Text = ""; this.BindGrid(); } #endregion #region 编辑 /// /// 双击行编辑事件 /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { this.EditData(); } /// /// 右键菜单编辑事件 /// protected void btnMenuEdit_Click(object sender, EventArgs e) { this.EditData(); } /// /// 编辑数据方法 /// private void EditData() { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string id = Grid1.SelectedRowID; PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("SafetyPatrolEdit.aspx?PatrolId={0}", id))); } #endregion #region 删除 /// /// 右键菜单删除事件 /// protected void btnMenuDelete_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); BLL.SafetyPatrolService.Delete(rowID); } this.BindGrid(); ShowNotify("删除数据成功!", MessageBoxIcon.Success); } } /// /// 编辑窗口关闭事件 /// protected void Window1_Close(object sender, WindowCloseEventArgs e) { this.BindGrid(); } #endregion #region 附件查看 /// /// 附件查看(弹出附件列表窗口) /// protected void Grid1_RowCommand(object sender, GridCommandEventArgs e) { if (e.CommandName == "attchUrl") { PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/SafetyPatrol&menuId={2}", -1, e.RowID, BLL.Const.SafetyPatrolMenuId))); } else if (e.CommandName == "attchUrlRectify") { PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type={0}&toKeyId={1}&path=FileUpload/SafetyPatrolCorrect&menuId={2}", -1, e.RowID, BLL.Const.SafetyPatrolRectifyMenuId))); } } #endregion } }