using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace FineUIPro.Web.HSSE.HazardousMG { /// /// 编辑安全巡检记录页面 /// public partial class SafetyPatrolEdit : PageBase { #region 定义项 /// /// 主键 /// private string PatrolId { get { return (string)ViewState["PatrolId"]; } set { ViewState["PatrolId"] = value; } } /// /// 巡检状态(编辑页已不提供选择,加载时保留原值,新增默认正常) /// private string PatrolStatus { get { return (string)ViewState["PatrolStatus"]; } set { ViewState["PatrolStatus"] = value; } } #endregion #region 加载 /// /// 加载页面 /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.btnClose.OnClientClick = ActiveWindow.GetHideReference(); this.InitDropDownList(); this.PatrolId = Request.Params["PatrolId"]; if (!string.IsNullOrEmpty(this.PatrolId)) { ////编辑:回填数据 Model.HSSE_SafetyPatrol info = BLL.SafetyPatrolService.GetById(this.PatrolId); if (info != null) { if (!string.IsNullOrEmpty(info.HazardProjectId)) { this.ddlHazardProject.SelectedValue = info.HazardProjectId; } if (info.PatrolDate.HasValue) { this.dpPatrolDate.SelectedDate = info.PatrolDate.Value; } this.txtCode.Text = info.Code; this.PatrolStatus = string.IsNullOrEmpty(info.PatrolStatus) ? "正常" : info.PatrolStatus; this.InitUserDropDownList(info.PatrolMan, info.PatrolManId, info.Receiver, info.ReceiverId); this.RenderContent(info.Content); this.txtProblems.Text = info.Problems; this.txtHandleRequire.Text = info.HandleRequire; this.txtRectifyReview.Text = info.RectifyReview; this.txtRemarks.Text = info.Remarks; } } else { ////新增(扫码巡检):默认巡检日期为当天、巡检状态为正常;巡检人默认当前登录账号 this.dpPatrolDate.SelectedDate = DateTime.Now; this.PatrolStatus = "正常"; this.InitUserDropDownList(null, null, null, null); } ////让父级编辑窗口高度自适应内容,避免固定高度留下大空白;超过 720px 时内部滚动 string fitJs = "setTimeout(function(){try{var w=F.activeWindow;if(w){var el=document.getElementById('patrolContent');var h=el?el.offsetHeight:0;h+=40;if(h>720){h=720;}w.setSize(780,h);}}catch(e){}},200);"; PageContext.RegisterStartupScript(fitJs); } } /// /// 初始化危大、超危清单下拉 /// private void InitDropDownList() { if (this.CurrUser == null || string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { return; } List projects = BLL.HazardProjectService.GetList(this.CurrUser.LoginProjectId); foreach (Model.HSSE_HazardProject p in projects) { this.ddlHazardProject.Items.Add(new FineUIPro.ListItem(p.ProjectName + "(" + p.ProjectCode + ")", p.HazardProjectId)); } } /// /// 绑定巡检人、签收人下拉(来自项目用户) /// /// 巡检人名称(回显用) /// 巡检人ID(回显用) /// 签收人名称(回显用) /// 签收人ID(回显用) private void InitUserDropDownList(string patrolMan, string patrolManId, string receiver, string receiverId) { if (this.CurrUser == null || string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { return; } List users = BLL.UserService.GetProjectUserListByProjectId(this.CurrUser.LoginProjectId); ////巡检人(默认当前登录账号);签收人允许留空 this.BindPatrolUser(this.ddlPatrolMan, users, patrolMan, patrolManId, this.CurrUser.UserId); this.BindPatrolUser(this.ddlReceiver, users, receiver, receiverId, null); } /// /// 绑定单个用户下拉:优先按ID回显,其次按名称回显;均不匹配且名称有值时保留原名称 /// /// 目标下拉 /// 项目用户列表 /// 回显名称 /// 回显ID /// 默认选中用户ID(可空) private void BindPatrolUser(FineUIPro.DropDownList ddl, List users, string selectName, string selectId, string defaultUserId) { ddl.Items.Clear(); ////签收人允许留空(首项为占位) if (ddl.ID == "ddlReceiver") { ddl.Items.Add(new FineUIPro.ListItem("请选择", "")); } string selectValue = string.IsNullOrEmpty(selectId) ? string.Empty : selectId; foreach (Model.Sys_User u in users) { ////默认选中项(巡检人默认当前登录账号) if (string.IsNullOrEmpty(selectValue) && !string.IsNullOrEmpty(defaultUserId) && u.UserId == defaultUserId) { selectValue = u.UserId; } ////名称回显(老数据无ID) if (string.IsNullOrEmpty(selectValue) && !string.IsNullOrEmpty(selectName) && u.UserName == selectName) { selectValue = u.UserId; } ddl.Items.Add(new FineUIPro.ListItem(u.UserName, u.UserId)); } ////名称在项目用户中找不到(如外部人员):追加原名称选项,保留旧数据 if (string.IsNullOrEmpty(selectValue) && !string.IsNullOrEmpty(selectName)) { ddl.Items.Insert(0, new FineUIPro.ListItem(selectName, "")); } if (!string.IsNullOrEmpty(selectValue)) { ddl.SelectedValue = selectValue; } else if (ddl.Items.Count > 0) { ddl.SelectedIndex = 0; } } /// /// 根据巡视内容渲染"其它"输入框。1-4 项为固定默认值(写死、始终显示),不再从 Content 解析勾选。 /// Content 以后只存「其它」内容:兼容旧格式「…;其它:xxx」,也兼容直接为其它文本的新格式。 /// /// Content 字段(以后仅其它内容;旧数据可能形如:1、…;4、…;其它:xxx) private void RenderContent(string content) { if (string.IsNullOrEmpty(content)) { return; } string other = null; foreach (string seg in content.Split(';')) { string s = seg.Trim(); if (s.Length == 0) { continue; } if (s.StartsWith("其它") || s.StartsWith("其他")) { int idx = s.IndexOf(':'); other = idx >= 0 ? s.Substring(idx + 1).Trim() : string.Empty; break; } } // 新格式:Content 无「其它:」前缀、且不以项号前缀(1、…)开头,则整段即为其它内容 if (other == null) { other = Regex.IsMatch(content, @"^\s*\d+、") ? string.Empty : content.Trim(); } if (other.Length > 0) { this.txtContentOther.Text = other; } } /// /// 构造 Content 文本。1-4 项固定写死(始终显示,不参与存储),Content 仅存「其它」内容。 /// 「其它」无多选:输入框有内容即表示选中。 /// /// 如:其它:基坑围挡损坏;无其它内容返回空串 private string BuildContent() { string other = this.txtContentOther.Text.Trim(); if (other.Length > 0) { return "其它:" + other; } return string.Empty; } #endregion #region 保存 /// /// 保存按钮事件 /// protected void btnSave_Click(object sender, EventArgs e) { if (this.CurrUser == null || string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { Alert.ShowInTop("请先选择项目!", MessageBoxIcon.Warning); return; } ////新增时先生成主键 if (string.IsNullOrEmpty(this.PatrolId)) { this.PatrolId = Guid.NewGuid().ToString(); } if (string.IsNullOrEmpty(this.ddlHazardProject.SelectedValue)) { Alert.ShowInTop("请选择危大工程类别!", MessageBoxIcon.Warning); return; } ////根据所选清单回填工程名称 string hazardProjectName = string.Empty; Model.HSSE_HazardProject project = BLL.HazardProjectService.GetById(this.ddlHazardProject.SelectedValue); if (project != null) { hazardProjectName = project.ProjectName; } ////巡检人、签收人(下拉选中项:ID + 名称;外部人员无ID时仅保留名称) string patrolManId = this.ddlPatrolMan.SelectedValue; string patrolManName = this.ddlPatrolMan.SelectedItem == null ? string.Empty : this.ddlPatrolMan.SelectedItem.Text.Trim(); if (patrolManName == "请选择") { patrolManName = string.Empty; } string receiverId = this.ddlReceiver.SelectedValue; string receiverName = this.ddlReceiver.SelectedItem == null ? string.Empty : this.ddlReceiver.SelectedItem.Text.Trim(); if (receiverName == "请选择") { receiverName = string.Empty; } Model.HSSE_SafetyPatrol info = new Model.HSSE_SafetyPatrol { PatrolId = this.PatrolId, ProjectId = this.CurrUser.LoginProjectId, HazardProjectId = this.ddlHazardProject.SelectedValue, HazardProjectName = hazardProjectName, PatrolDate = this.dpPatrolDate.SelectedDate, PatrolMan = patrolManName, PatrolManId = patrolManId, PatrolStatus = this.PatrolStatus, Content = this.BuildContent(), Problems = this.txtProblems.Text.Trim(), Receiver = receiverName, ReceiverId = receiverId, HandleRequire = this.txtHandleRequire.Text.Trim(), RectifyReview = this.txtRectifyReview.Text.Trim(), Remarks = this.txtRemarks.Text.Trim() }; ////存在则更新,不存在则新增(服务层已处理) BLL.SafetyPatrolService.Update(info); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } /// /// 上传巡检图片 /// protected void btnAttach_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.PatrolId)) { this.PatrolId = Guid.NewGuid().ToString(); } PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/SafetyPatrol&menuId={1}", this.PatrolId, BLL.Const.SafetyPatrolMenuId))); } /// /// 上传整改图片 /// protected void btnAttachRectify_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.PatrolId)) { this.PatrolId = Guid.NewGuid().ToString(); } PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/SafetyPatrolCorrect&menuId={1}", this.PatrolId, BLL.Const.SafetyPatrolRectifyMenuId))); } #endregion } }