using System; using System.Linq; using BLL; using Newtonsoft.Json.Linq; namespace FineUIPro.Web.HSSE.SitePerson { public partial class MonthReportEdit : PageBase { #region 定义变量 /// /// 主键 /// public string MonthReportId { get { return (string)ViewState["MonthReportId"]; } set { ViewState["MonthReportId"] = value; } } /// /// 主键 /// public string ProjectId { get { return (string)ViewState["ProjectId"]; } set { ViewState["ProjectId"] = value; } } #endregion #region 加载页面 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.btnClose.OnClientClick = ActiveWindow.GetHideReference(); this.ProjectId = this.CurrUser.LoginProjectId; this.MonthReportId = Request.Params["MonthReportId"]; var monthReport = BLL.SitePerson_MonthReportService.GetMonthReportByMonthReportId(this.MonthReportId); if (monthReport != null) { this.ProjectId = monthReport.ProjectId; this.txtMonthReportCode.Text = BLL.CodeRecordsService.ReturnCodeByDataId(this.MonthReportId); this.txtCompileMan.Text = BLL.UserService.GetUserNameByUserId(monthReport.CompileMan); this.txtDate.Text = String.Format("{0:yyyy-MM}", monthReport.CompileDate); } BindGrid(); ///初始化审核菜单 //this.ctlAuditFlow.MenuId = BLL.Const.ProjectMonthReportMenuId; //this.ctlAuditFlow.DataId = this.MonthReportId; //this.ctlAuditFlow.ProjectId = this.ProjectId; //this.ctlAuditFlow.UnitId = this.CurrUser.UnitId; } } #endregion #region 绑定明细数据 /// /// 绑定明细数据 /// private void BindGrid() { using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString)) { if (!string.IsNullOrWhiteSpace(this.MonthReportId)) { var q = from x in db.SitePerson_MonthReportDetail join a in db.SitePerson_MonthReport on x.MonthReportId equals a.MonthReportId join b in db.Base_Unit on x.UnitId equals b.UnitId where x.MonthReportId == this.MonthReportId orderby b.UnitCode select new { x.MonthReportDetailId, x.MonthReportId, x.UnitId, x.StaffData, x.DayNum, x.WorkTime, x.CheckPersonNum, x.RealPersonNum, x.PersonWorkTime, YearPersonWorkTime = (from y in db.SitePerson_MonthReportDetail where (from z in db.SitePerson_MonthReport where z.CompileDate <= a.CompileDate && z.CompileDate.Value.Year == a.CompileDate.Value.Year && x.UnitId == y.UnitId && z.ProjectId == this.ProjectId select z.MonthReportId).Contains(y.MonthReportId) select y.PersonWorkTime ?? 0).Sum(), TotalPersonWorkTime = (from y in db.SitePerson_MonthReportDetail where (from z in db.SitePerson_MonthReport where z.CompileDate <= a.CompileDate && x.UnitId == y.UnitId && z.ProjectId == this.ProjectId select z.MonthReportId).Contains(y.MonthReportId) select y.PersonWorkTime ?? 0).Sum(), x.Remark, b.UnitName, }; Grid1.DataSource = q; } else { var units = from x in db.Project_ProjectUnit join u in db.Base_Unit on x.UnitId equals u.UnitId where x.ProjectId == this.CurrUser.LoginProjectId && (x.UnitType == "1" || x.UnitType == "2") select new { DayReportDetailId = "", DayReportId = "", x.UnitId, StaffData = "", DayNum = 0, WorkTime = 8, CheckPersonNum = 0, RealPersonNum = 0, PersonWorkTime = 0, YearPersonWorkTime = 0, TotalPersonWorkTime = 0, Remark = "", u.UnitName, x.UnitType }; Grid1.DataSource = units.ToList(); } Grid1.DataBind(); } } #endregion #region 提交按钮 /// /// 提交按钮 /// /// /// protected void btnSubmit_Click(object sender, EventArgs e) { //if (this.ctlAuditFlow.NextStep == BLL.Const.State_1 && this.ctlAuditFlow.NextPerson == BLL.Const._Null) //{ // ShowNotify("请选择下一步办理人!", MessageBoxIcon.Warning); // return; //} this.SaveData(BLL.Const.BtnSubmit); PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference()); } #endregion #region 保存按钮 /// /// 保存按钮 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { this.SaveData(BLL.Const.BtnSave); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } #endregion /// /// 保存数据 /// /// private void SaveData(string type) { Model.SitePerson_MonthReport monthReport = BLL.SitePerson_MonthReportService.GetMonthReportByMonthReportId(this.MonthReportId); if (monthReport != null) { //if (type == BLL.Const.BtnSubmit) //{ // monthReport.States = this.ctlAuditFlow.NextStep; //} JArray mergedData = Grid1.GetMergedData(); foreach (JObject mergedRow in mergedData) { JObject values = mergedRow.Value("values"); //行数据 var MonthReportDetailId = values.Value("MonthReportDetailId").ToString(); var MonthReportDetail = BLL.SitePerson_MonthReportDetailService.GetMonthReportDetailByMonthReportDetailId(MonthReportDetailId); if (MonthReportDetail != null) { MonthReportDetail.CheckPersonNum = Funs.GetNewIntOrZero(values.Value("CheckPersonNum").ToString()); MonthReportDetail.RealPersonNum = Funs.GetNewIntOrZero(values.Value("RealPersonNum").ToString()); MonthReportDetail.DayNum = Funs.GetNewIntOrZero(values.Value("DayNum").ToString()); MonthReportDetail.PersonWorkTime = Funs.GetNewDecimalOrZero(values.Value("PersonWorkTime").ToString()); BLL.SitePerson_MonthReportDetailService.UpdateReportDetail(MonthReportDetail); } } BLL.LogService.AddSys_Log(this.CurrUser, null, monthReport.MonthReportId, BLL.Const.ProjectMonthReportMenuId, BLL.Const.BtnModify); BLL.SitePerson_MonthReportService.UpdateMonthReport(monthReport); ////保存流程审核数据 // this.ctlAuditFlow.btnSaveData(this.ProjectId, BLL.Const.ProjectMonthReportMenuId, this.MonthReportId, (type == BLL.Const.BtnSubmit ? true : false), this.txtCompileDate.Text.Trim(), "../SitePerson/MonthReportView.aspx?MonthReportId={0}"); } else { DateTime? compileDate = BLL.Funs.GetNewDateTime(this.txtDate.Text); if (compileDate.HasValue && !BLL.SitePerson_MonthReportService.IsExistMonthReport(compileDate.Value, this.CurrUser.LoginProjectId)) { var monthDayReports = from x in Funs.DB.SitePerson_DayReport where x.ProjectId == this.CurrUser.LoginProjectId && x.CompileDate >= compileDate && x.CompileDate < Convert.ToDateTime(compileDate).AddMonths(1) select x; this.MonthReportId = SQLHelper.GetNewID(typeof(Model.SitePerson_MonthReport)); Model.SitePerson_MonthReport newMonthReport = new Model.SitePerson_MonthReport { MonthReportId = this.MonthReportId, ProjectId = this.CurrUser.LoginProjectId, CompileMan = this.CurrUser.UserId, CompileDate = compileDate, States = BLL.Const.State_0 //待提交 }; BLL.SitePerson_MonthReportService.AddMonthReport(newMonthReport); JArray mergedData = Grid1.GetMergedData(); foreach (JObject mergedRow in mergedData) { JObject values = mergedRow.Value("values"); //行数据 decimal personWorkTime = 0; Model.SitePerson_MonthReportDetail newMonthReportDetail = new Model.SitePerson_MonthReportDetail(); string newKeyID = SQLHelper.GetNewID(typeof(Model.SitePerson_MonthReportDetail)); newMonthReportDetail.MonthReportDetailId = newKeyID; newMonthReportDetail.MonthReportId = this.MonthReportId; newMonthReportDetail.UnitId = values.Value("UnitId").ToString(); var UnitType = values.Value("UnitType").ToString(); newMonthReportDetail.StaffData = this.GetStaffData(newMonthReportDetail.UnitId, UnitType, compileDate.Value); newMonthReportDetail.DayNum = 28; newMonthReportDetail.WorkTime = 8; foreach (var dayItem in monthDayReports) { var dayItemDetail = (from x in Funs.DB.SitePerson_DayReportDetail where x.DayReportId == dayItem.DayReportId && x.UnitId == newMonthReportDetail.UnitId select x).FirstOrDefault(); if (dayItemDetail != null) { decimal itemTime = dayItemDetail.PersonWorkTime.HasValue ? dayItemDetail.PersonWorkTime.Value : 0; personWorkTime += itemTime; } } newMonthReportDetail.PersonWorkTime = personWorkTime; BLL.SitePerson_MonthReportDetailService.AddMonthReportDetail(newMonthReportDetail); } BLL.LogService.AddSys_Log(this.CurrUser, null, this.MonthReportId, BLL.Const.DayReportMenuId, BLL.Const.BtnAdd); Alert.ShowInTop("保存成功", MessageBoxIcon.Success); } else { Alert.ShowInTop("当月月报已存在,请到列表点击日报日期查看!", MessageBoxIcon.Warning); return; } } } #region 得到当前单位人员情况 /// /// 得到当前单位人员情况 /// /// /// private string GetStaffData(string unitId, string unitType, DateTime compileDate) { string allStaffData = string.Empty; var allSum = from x in Funs.DB.SitePerson_Person where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitId == unitId && x.IsUsed == true && (x.InTime < compileDate.AddMonths(1) || !x.InTime.HasValue) select x; ///管理人员集合 var glAllPerson = from x in allSum join y in Funs.DB.Base_WorkPost on x.WorkPostId equals y.WorkPostId where (y.PostType == "1" || y.PostType == "4") //一般管理岗位和特种管理人员 select x; ///安全专职人员集合 var hsseAllPerson = from x in allSum join y in Funs.DB.Base_WorkPost on x.WorkPostId equals y.WorkPostId where y.IsHsse == true //HSSE管理人员 select x; ///单位作业人员集合 var zyAllPerson = from x in allSum join y in Funs.DB.Base_WorkPost on x.WorkPostId equals y.WorkPostId where (y.PostType == "2" || y.PostType == "3") //特种作业人员和一般作业岗位 select x; if (unitType == "1") { allStaffData += "总人数:" + allSum.Count().ToString() + ",管理人员总数" + glAllPerson.Count().ToString() + "人,专职安全人员共" + hsseAllPerson.Count().ToString() + " 人。"; } else { allStaffData += "总人数:" + allSum.Count().ToString() + ",管理人员总数" + glAllPerson.Count().ToString() + "人,专职安全人员共" + hsseAllPerson.Count().ToString() + " 人,施工单位作业人员总数" + zyAllPerson.Count().ToString() + "人。"; } return allStaffData; } #endregion #region 关闭弹出窗 /// /// 关闭弹出窗 /// /// /// protected void Window1_Close(object sender, EventArgs e) { BindGrid(); } #endregion #region Grid双击事件 /// /// Grid行双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { btnMenuEdit_Click(null, null); } #endregion #region 编辑 /// /// 编辑按钮 /// /// /// protected void btnMenuEdit_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string monthReportDetailId = Grid1.SelectedRowID; PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("MonthReportDetailEdit.aspx?MonthReportDetailId={0}", monthReportDetailId, "编辑 - "))); } #endregion } }