using BLL; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace FineUIPro.Web.EnergyReport { public partial class EnergyStatistics : PageBase { #region 项目id /// /// 项目id /// public string ProjectId { get { return (string)ViewState["ProjectId"]; } set { ViewState["ProjectId"] = value; } } #endregion #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BLL.EnergySettingsService.InitEnergyDropDownList(this.drpEnergyId, true); Funs.DropDownPageSize(this.ddlPageSize); this.ProjectId = this.CurrUser.LoginProjectId; if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId) { this.ProjectId = Request.Params["projectId"]; } ////权限按钮方法 this.ucTree.UnitId = this.CurrUser.UnitId; this.ucTree.ProjectId = this.ProjectId; if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { this.panelLeftRegion.Hidden = true; ////权限按钮方法 this.GetButtonPower(); } else { this.btnNew.Hidden = true; this.btnMenuModify.Hidden = true; this.btnMenuDel.Hidden = true; this.Grid1.EnableRowDoubleClickEvent = false; } if (this.CurrUser != null && this.CurrUser.PageSize.HasValue) { Grid1.PageSize = this.CurrUser.PageSize.Value; } this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 this.BindGrid(); btnNew.OnClientClick = Window1.GetShowReference("EnergyStatisticsEdit.aspx") + "return false;"; } } /// /// 加载树 /// /// /// protected void changeTree(object sender, EventArgs e) { this.ProjectId = this.ucTree.ProjectId; this.BindGrid(); this.GetButtonPower(); } /// /// 绑定数据 /// private void BindGrid() { if (string.IsNullOrEmpty(this.ProjectId)) { return; } string strSql = @"select s.EnergyStatisticsId, s.ProjectId, s.UnitId, s.ReportDate, s.EnergyId, s.Consumption, s.Amount, s.UnitPrice, s.ConversionUnit, s.ConvertedQuantity, s.TCE, s.CompileMan, s.CompileDate, y.EnergyName, y.EnergyUnit, y.ReferenceCoefficient, p.ProjectName, u.UnitName from EnergyReport_EnergyStatistics s left join Base_Energy y on y.EnergyId = s.EnergyId left join Base_Project p on p.ProjectId = s.ProjectId left join Base_Unit u on u.UnitId= s.UnitId WHERE 1=1 "; List listStr = new List(); strSql += " AND s.ProjectId = @ProjectId"; listStr.Add(new SqlParameter("@ProjectId", this.ProjectId)); if (!string.IsNullOrEmpty(this.drpEnergyId.SelectedValue) && this.drpEnergyId.SelectedValue != BLL.Const._Null) { strSql += " AND s.EnergyId = @energyId"; listStr.Add(new SqlParameter("@energyId", this.drpEnergyId.SelectedValue)); } if (BLL.ProjectUnitService.GetProjectUnitTypeByProjectIdUnitId(this.ProjectId, this.CurrUser.UnitId)) { strSql += " AND s.UnitId =@unitId"; listStr.Add(new SqlParameter("@unitId", this.CurrUser.UnitId)); } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.RecordCount = tb.Rows.Count; var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); OutputSummaryData(tb);//求和 } /// /// 计算和 /// /// private void OutputSummaryData(DataTable tb) { //decimal consumption = 0;//用量 decimal amount = 0;//消费金额 //decimal unitPrice = 0;//单价 decimal convertedQuantity = 0; //转换吨标计量 decimal tce = 0; //吨标煤 foreach (DataRow row in tb.Rows) { //consumption += Convert.ToDecimal(row["Consumption"]); amount += Convert.ToDecimal(row["Amount"]); //unitPrice += Convert.ToDecimal(row["UnitPrice"]); convertedQuantity += Convert.ToDecimal(row["ConvertedQuantity"]); tce += Convert.ToDecimal(row["TCE"]); } JObject summary = new JObject(); summary.Add("EnergyName", "能源合计"); //summary.Add("Consumption", consumption.ToString("F2")); summary.Add("Amount", amount.ToString("F2")); //summary.Add("UnitPrice", unitPrice.ToString("F2")); summary.Add("ConvertedQuantity", convertedQuantity.ToString("G7")); summary.Add("TCE", tce.ToString("G5")); Grid1.SummaryData = summary; } #endregion #region 分页、排序 /// /// 改变索引事件 /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { BindGrid(); } /// /// 分页下拉选择事件 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } /// /// 排序 /// /// /// protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e) { BindGrid(); } #endregion #region 查询 /// /// 查询 /// /// /// protected void drpEnergyId_SelectedIndexChanged(object sender, EventArgs e) { this.BindGrid(); } #endregion #region 关闭弹出窗 /// /// 关闭弹出窗 /// /// /// protected void Window1_Close(object sender, WindowCloseEventArgs e) { BindGrid(); } #endregion #region 获取按钮权限 /// /// 获取按钮权限 /// /// /// private void GetButtonPower() { if (string.IsNullOrEmpty(this.ProjectId)) { return; } if (Request.Params["value"] == "0") ///文件柜时 没有操作权限 { return; } var buttonList = BLL.CommonService.GetAllButtonList(this.ProjectId, this.CurrUser.UserId, BLL.Const.EnergyStatisticsMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnNew.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnMenuModify.Hidden = false; this.Grid1.EnableRowDoubleClickEvent = true; } if (buttonList.Contains(BLL.Const.BtnDelete)) { this.btnMenuDel.Hidden = false; } } } #endregion #region 编辑 /// /// 编辑按钮 /// /// /// protected void btnMenuModify_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("EnergyStatisticsEdit.aspx?energyStatisticsId={0}", this.Grid1.SelectedRowID, "编辑 - "))); } #endregion #region Grid双击事件 /// /// Grid行双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { btnMenuModify_Click(null, null); } #endregion #region 删除 /// /// 批量删除 /// /// /// protected void btnMenuDel_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length > 0) { var db = Funs.DB; foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); if (!string.IsNullOrEmpty(rowID)) { BLL.EnergyStatisticsService.DeleteEnergyStatisticsById(rowID); BLL.LogService.AddSys_Log(this.CurrUser, rowID, rowID, BLL.Const.EnergyStatisticsMenuId, BLL.Const.BtnDelete); } } BindGrid(); ShowNotify("删除数据成功!"); } } #endregion } }