using BLL; using MiniExcelLibs; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; namespace FineUIPro.Web.HJGL.PreDesign { public partial class TrainNumberManager : PageBase { public static List hJGL_TrainNumberManages = new List(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.GetButtonPower(); this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString(); // 绑定表格 this.BindGrid(); } } #region 绑定数据 /// /// 绑定数据 /// private void BindGrid() { BindGrid(false); } /// /// 绑定数据(带筛选参数) /// /// 是否应用筛选条件 private void BindGrid(bool isFilter) { Model.HJGL_TrainNumberManage table = new Model.HJGL_TrainNumberManage(); table.ProjectId = this.CurrUser.LoginProjectId; // 应用筛选条件 if (isFilter) { if (!string.IsNullOrEmpty(txtTrainNumber.Text.Trim())) { table.TrainNumber = txtTrainNumber.Text.Trim(); } if (!string.IsNullOrEmpty(ddlState.SelectedValue)) { table.State = Convert.ToInt32(ddlState.SelectedValue); } } var tb = BLL.TrainNumberManageService.GetListByQueryModle(table, Grid1.PageIndex+1, Grid1.PageSize); Grid1.RecordCount = tb.Total; Grid1.DataSource = tb.Data; Grid1.DataBind(); hJGL_TrainNumberManages = tb.Data; } #endregion #region 辅助方法 /// /// 转换状态值 /// /// 状态值 /// 状态文本 protected string ConvertState(object state) { if (state != null) { int stateValue = Convert.ToInt32(state); switch (stateValue) { case 0: return "未发货"; case 1: return "已发货"; case 2: return "已验收"; default: return ""; } } return ""; } #endregion #region GV 数据操作 /// /// 筛选按钮点击事件 /// /// /// protected void btnFilter_Click(object sender, EventArgs e) { this.Grid1.PageIndex = 0; // 重置到第一页 this.BindGrid(true); } /// /// 清除筛选按钮点击事件 /// /// /// protected void btnClearFilter_Click(object sender, EventArgs e) { txtTrainNumber.Text = ""; ddlState.SelectedValue = ""; this.Grid1.PageIndex = 0; // 重置到第一页 this.BindGrid(false); } /// /// 过滤表头 /// /// /// //protected void Grid1_FilterChange(object sender, EventArgs e) //{ // this.BindGrid(); //} /// /// 分页 /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { this.Grid1.PageIndex = e.NewPageIndex; this.BindGrid(); } /// /// 排序 /// /// /// protected void Grid1_Sort(object sender, GridSortEventArgs e) { this.Grid1.SortDirection = e.SortDirection; this.Grid1.SortField = e.SortField; this.BindGrid(); } /// /// 分页显示条数下拉框 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue); this.BindGrid(); } #endregion #region 数据编辑事件 /// /// 新增 /// /// /// protected void btnNew_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TrainNumberManageEdit.aspx?Id={0}", string.Empty, "增加 - "))); } /// /// 编辑按钮 /// /// /// protected void btnEdit_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string ID = Grid1.SelectedRowID; var model = BLL.TrainNumberManageService.GetModelById(ID); if (model != null) ///已上报时不能删除 { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TrainNumberManageEdit.aspx?Id={0}", ID, "编辑 - "))); } } /// /// Grid行双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { this.btnEdit_Click(null, null); } /// /// 批量删除 /// /// /// protected void btnDelete_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); var model = BLL.TrainNumberManageService.GetModelById(rowID); if (model != null) { BLL.TrainNumberManageService.DeleteById(rowID); } } BindGrid(); ShowNotify("删除数据成功!", MessageBoxIcon.Success); } } #endregion #region 关闭弹出窗 /// /// 关闭弹出窗 /// /// /// protected void Window1_Close(object sender, WindowCloseEventArgs e) { BindGrid(); } #endregion #region 获取权限按钮 /// /// 获取按钮权限 /// /// /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, BLL.Const.HJGL_TrainNumberManageMenuId); if (buttonList.Count > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnNew.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnMenuEdit.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnDelete)) { this.btnMenuDelete.Hidden = false; } } } #endregion #region 导出按钮 /// 导出按钮 /// /// /// protected void btnOut_Click(object sender, EventArgs e) { if (hJGL_TrainNumberManages != null) { var q = hJGL_TrainNumberManages .Select((x, index) => new { 序号 = index + 1, // 自增序号,从 1 开始 车次 = x.TrainNumber, 驾驶员姓名 = x.DriverName, 驾驶员电话 = x.DriverPhone, 车牌号 = x.LicensePlateNumber, 联系人姓名 = x.ContactName, 联系人电话 = x.ContactPhone, 状态 = ConvertState(x.State), 签收时间 = x.ReceiveDate?.ToString("g") ?? "", 备注 = x.Remark }); // 根据筛选条件生成文件名 string fileName = "车次管理"; if (!string.IsNullOrEmpty(txtTrainNumber.Text.Trim())) { fileName += "-发货编号_" + txtTrainNumber.Text.Trim(); } if (!string.IsNullOrEmpty(ddlState.SelectedValue)) { fileName += "-状态_" + ddlState.SelectedItem.Text; } fileName += "-" + string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ".xlsx"; string path = Funs.RootPath + @"File\Excel\Temp\TrainNumberManager.xlsx"; path = path.Replace(".xlsx", string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ".xlsx"); MiniExcel.SaveAs(path, q); FileInfo info = new FileInfo(path); long fileSize = info.Length; System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.ContentType = "application/x-zip-compressed"; System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString()); System.Web.HttpContext.Current.Response.TransmitFile(path, 0, fileSize); System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.Close(); File.Delete(path); } } /// /// 导出方法 /// /// /// private string GetGridTableHtml(Grid grid) { StringBuilder sb = new StringBuilder(); sb.Append(""); sb.Append(""); sb.Append(""); foreach (GridColumn column in grid.Columns) { sb.AppendFormat("", column.HeaderText); } sb.Append(""); foreach (GridRow row in grid.Rows) { sb.Append(""); foreach (GridColumn column in grid.Columns) { string html = row.Values[column.ColumnIndex].ToString(); if (column.ColumnID == "tfNumber") { html = (row.FindControl("lblNumber") as System.Web.UI.WebControls.Label).Text; } sb.AppendFormat("", html); } sb.Append(""); } sb.Append("
{0}
{0}
"); return sb.ToString(); } #endregion protected void Grid1_RowCommand(object sender, GridCommandEventArgs e) { if (e.CommandName == "TrainDetail") { string id = Grid1.SelectedRowID; string url = "TrainNumberManagerView.aspx?TrainNumberId={0}"; PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format(url, id, "操作 - "))); } } protected void btnPrint_Click(object sender, EventArgs e) { string Id = this.Grid1.SelectedRowID; Pring(Id); } private void Pring(string Id) { BLL.FastReportService.ResetData(); if (string.IsNullOrEmpty(Id)) { ShowNotify("请选择要打印的项", MessageBoxIcon.Warning); return; } DataTable tb = LINQToDataTable(HJGLPackagingmanageService.GetPackagingManage(Id)); if (tb.Rows.Count > 0) { var model = TrainNumberManageService.GetModelById(Id); Dictionary keyValuePairs = new Dictionary(); keyValuePairs.Add("ProjectName", ProjectService.GetProjectNameByProjectId(this.CurrUser.LoginProjectId)); keyValuePairs.Add("TrainNumber", model.TrainNumber); keyValuePairs.Add("Remark", model.Remark); keyValuePairs.Add("ContactName", model.ContactName); keyValuePairs.Add("ContactPhone", model.ContactPhone); keyValuePairs.Add("ID", "TrainNumberManager$" + model.Id); DataRow dataRow = tb.NewRow(); dataRow["PackagingCode"] = "合计:" + tb.Rows.Count; tb.Rows.Add(dataRow); if (tb != null) { tb.TableName = "Data"; } BLL.FastReportService.AddFastreportTable(tb); BLL.FastReportService.AddFastreportParameter(keyValuePairs); string initTemplatePath = ""; string rootPath = Server.MapPath("~/"); initTemplatePath = "File\\Fastreport\\发货单.frx"; if (File.Exists(rootPath + initTemplatePath)) { PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("~/Controls/Fastreport.aspx?ReportPath={0}", rootPath + initTemplatePath))); } } } } }