using BLL; using Model.Enums; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; namespace FineUIPro.Web.Customization.CNCEC4.HSSE.Report { public partial class RiskIdentificationEdit : PageBase { public string Id { get { return (string)ViewState["Id"]; } set { ViewState["Id"] = value; } } public string RiskId { get { return (string)ViewState["RiskId"]; } set { ViewState["RiskId"] = value; } } public string action { get { return (string)ViewState["action"]; } set { ViewState["action"] = value; } } /// /// 定义集合 /// private static List details = new List(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.Id = Request.Params[nameof(Id)]; this.action = Request.Params[nameof(action)]; showInfo(); if (this.action == ActionEnums.View.ToString()) { btnSave.Hidden = true; btnSubmit.Hidden = true; btnNew.Hidden = true; } } } /// /// 加载数据 /// void showInfo() { // 删除选中单元格的客户端脚本 string deleteScript = GetDeleteScript(); var project = BLL.ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId); if (project != null) { this.txtProjectName.Text = project.ProjectName; this.txtUnitName.Text = BLL.UnitService.GetUnitNameByUnitId(project.UnitId); } var resultDetail = Funs.DB.Report_RiskIdentification_c.FirstOrDefault(a => a.Id == this.Id); if (resultDetail != null) { this.RiskId = resultDetail.RiskId; var result = Funs.DB.Report_RiskIdentification.FirstOrDefault(a => a.Id == resultDetail.RiskId); project = BLL.ProjectService.GetProjectByProjectId(result.ProjectId); if (project != null) { this.txtProjectName.Text = project.ProjectName; this.txtUnitName.Text = BLL.UnitService.GetUnitNameByUnitId(project.UnitId); } this.txtMonths.Text = string.Format("{0:yyyy-MM}", result.Months); this.txtSignPerson.Text = result.SignPerson; txtWorkModuleA.Text = result.WorkModuleA; txtWorkModuleB.Text = result.WorkModuleB; txtCreateDate.Text = result.CreateDate.ToString(); //加载列表 details.Clear(); var RiskIdentification_cList = Funs.DB.Report_RiskIdentification_c.Where(x => x.RiskId == RiskId).ToList(); details = RiskIdentification_cList; Grid1.DataSource = details; Grid1.DataBind(); } else { this.txtMonths.Text = string.Format("{0:yyyy-MM}", Request.Params["monts"].ToString()); this.txtCreateDate.Text = DateTime.Now.ToString(); } } protected void btnSave_Click(object sender, EventArgs e) { SaveData(BLL.Const.BtnSave); } /// /// 提交 /// /// /// protected void btnSubmit_Click(object sender, EventArgs e) { SaveData(BLL.Const.BtnSubmit); } void SaveData(string type) { Model.Report_RiskIdentification model = new Model.Report_RiskIdentification() { ProjectId = this.CurrUser.LoginProjectId, Unitid = BLL.ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId).UnitId, Months = Convert.ToDateTime(this.txtMonths.Text), WorkModuleA = txtWorkModuleA.Text, WorkModuleB = txtWorkModuleB.Text, SignPerson = txtSignPerson.Text, CreateDate = Convert.ToDateTime(txtCreateDate.Text), }; if (type == BLL.Const.BtnSave) { model.States = "0"; } else { model.States = "1"; } if (this.action == ActionEnums.Edit.ToString() || !string.IsNullOrEmpty(this.Id)) { model.Id = this.RiskId; RiskIdentificationService.Update(model); //删除子表重新添加 RiskIdentificationService.DeleteCbyRiskId(model.Id); } else { model.Id = SQLHelper.GetNewID(typeof(Model.Report_HazardAdminister)); this.RiskId = model.Id; RiskIdentificationService.Insert(model); } var str = this.SaveDetail(type, model); if (str == "保存成功") { ShowNotify(this.SaveDetail(type, model), MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { ShowNotify(this.SaveDetail(type, model), MessageBoxIcon.Warning); } } /// /// 保存明细项 /// private string SaveDetail(string type, Model.Report_RiskIdentification model) { var errMsg = ""; try { var olddetailLists = Funs.DB.Report_RiskIdentification_c.Where(x => x.RiskId == this.RiskId).ToList(); List needRemove = new List(); foreach (var oldDetail in olddetailLists) { bool hasFind = false; for (int i = 0; i < Grid1.Rows.Count; i++) { if (oldDetail.Id == Grid1.Rows[i].DataKeys[0].ToString()) { hasFind = true; break; } } if (!hasFind) { RiskIdentificationService.DeleteC(oldDetail.Id); } } List detailLists = new List(); JArray teamGroupData = Grid1.GetMergedData(); var fIndex = 1; foreach (JObject teamGroupRow in teamGroupData) { JObject values = teamGroupRow.Value("values"); int rowIndex = teamGroupRow.Value("index"); Model.Report_RiskIdentification_c newDetail = new Model.Report_RiskIdentification_c() { Id = values.Value("Id"), RiskId = this.RiskId, HazardText = values.Value("HazardText"), Accident = values.Value("Accident"), NodelL = values.Value("NodelL"), NodelE = values.Value("NodelE"), NodelC = values.Value("NodelC"), Measures = values.Value("Measures"), ControlLevel = values.Value("ControlLevel"), ResPerson = values.Value("ResPerson"), }; if (string.IsNullOrEmpty(newDetail.NodelL)) { errMsg = "第" + fIndex.ToString() + "L(可能性)(0.1-10)不能为空"; return "请选择L(可能性)(0.1-10)"; } if (string.IsNullOrEmpty(newDetail.NodelE)) { errMsg = "第" + fIndex.ToString() + "E(频繁程度)(0.5-10)不能为空"; return "请选择E(频繁程度)(0.5-10)"; } if (string.IsNullOrEmpty(newDetail.NodelC)) { errMsg= "第" + fIndex.ToString() + "C(伤害程度)(1-100)不能为空"; return "请选择C(伤害程度)(1-100)"; } //D(风险值)=L*E*C newDetail.NodelD = (Convert.ToDouble(newDetail.NodelL) * Convert.ToDouble(newDetail.NodelE) * Convert.ToDouble(newDetail.NodelC)).ToString(); var LevelNum = Convert.ToDouble(newDetail.NodelD); if (LevelNum < 70) { newDetail.RiskLevel = "低风险"; } else if (LevelNum >= 70 && LevelNum < 160) { newDetail.RiskLevel = "一般风险"; } else if (LevelNum >= 160 && LevelNum < 320) { newDetail.RiskLevel = "较大风险"; } else if (LevelNum >= 320) { newDetail.RiskLevel = "重大风险"; } if (Grid1.Rows[rowIndex].DataKeys.Length > 0) { newDetail.Id = Grid1.Rows[rowIndex].DataKeys[0].ToString(); } var newDetailModel = Funs.DB.Report_RiskIdentification_c.FirstOrDefault(x => x.Id == newDetail.Id); if (newDetailModel==null) { RiskIdentificationService.InsertC(newDetail); } else { RiskIdentificationService.UpdateC(newDetail); } } } catch (Exception ex) { throw ex; } if (!string.IsNullOrEmpty(errMsg)) { return errMsg; } return "保存成功"; } protected void btnAdd_Click(object sender, EventArgs e) { JArray teamGroupData = Grid1.GetMergedData(); List list = new List(); foreach (JObject teamGroupRow in teamGroupData) { JObject values = teamGroupRow.Value("values"); values.Add("Id", teamGroupRow.Value("id")); list.Add(values); } JObject defaultObj = new JObject { { "Id",Guid.NewGuid() }, { "HazardText", "" }, { "Accident", "" }, { "NodelL",""}, { "NodelE", "" }, { "NodelC","" }, { "Measures", "" }, { "ControlLevel","" }, { "Suggestions",""}, { "ResPerson", "" }, { "Delete", String.Format("", GetDeleteScript(), IconHelper.GetResolvedIconUrl(Icon.Delete)) } }; list.Add(defaultObj); Grid1.DataSource = list; Grid1.DataBind(); } /// /// /// /// /// protected void Grid1_PreDataBound(object sender, EventArgs e) { // 设置LinkButtonField的点击客户端事件 LinkButtonField deleteField = Grid1.FindColumn("Delete") as LinkButtonField; deleteField.OnClientClick = GetDeleteScript(); } /// /// 删除提示 /// /// private string GetDeleteScript() { return Confirm.GetShowReference("删除选中行?", String.Empty, MessageBoxIcon.Question, Grid1.GetDeleteSelectedRowsReference(), String.Empty); } } }