using BLL; using MiniExcelLibs; using Model; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; namespace FineUIPro.Web.CLGL { public partial class AntiCorrosionTrust : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower(); InitTreeMenu(); BindGrid(); } } private void BindGrid() { var filter = new AntiCorrosionTrustOutput { ProjectId = this.CurrUser.LoginProjectId, UnitWorkId = tvControlItem.SelectedNodeID, TrustCode = txtTrustCode.Text.Trim(), OutputMasterCode = txtOutputMasterCode.Text.Trim() }; Grid1.DataSource = TwAntiCorrosionTrustService.GetListData(filter, Grid1); Grid1.RecordCount = TwAntiCorrosionTrustService.Count; Grid1.DataBind(); } private void InitTreeMenu() { this.tvControlItem.Nodes.Clear(); TreeNode rootNode1 = new TreeNode(); rootNode1.NodeID = "1"; rootNode1.Text = "建筑工程"; rootNode1.CommandName = "建筑工程"; rootNode1.Selectable = false; this.tvControlItem.Nodes.Add(rootNode1); TreeNode rootNode2 = new TreeNode(); rootNode2.NodeID = "2"; rootNode2.Text = "安装工程"; rootNode2.CommandName = "安装工程"; rootNode2.Expanded = true; rootNode2.Selectable = false; this.tvControlItem.Nodes.Add(rootNode2); var keyword = txtTreeSearch.Text.Trim(); var unitWorkList = (from x in Funs.DB.WBS_UnitWork where x.ProjectId == this.CurrUser.LoginProjectId && x.SuperUnitWork == null && x.UnitId != null && x.ProjectType != null && (string.IsNullOrEmpty(keyword) || x.UnitWorkName.Contains(keyword) || x.UnitWorkCode.Contains(keyword)) select x).ToList(); foreach (var q in unitWorkList.Where(x => x.ProjectType == "1")) { TreeNode tn = BuildUnitWorkNode(q); rootNode1.Nodes.Add(tn); } foreach (var q in unitWorkList.Where(x => x.ProjectType == "2")) { TreeNode tn = BuildUnitWorkNode(q); rootNode2.Nodes.Add(tn); } } private TreeNode BuildUnitWorkNode(Model.WBS_UnitWork unitWork) { var unitNamesUnitIds = BLL.UnitService.getUnitNamesUnitIds(unitWork.UnitId); TreeNode node = new TreeNode(); node.NodeID = unitWork.UnitWorkId; node.Text = unitWork.UnitWorkName; node.ToolTip = "施工单位:" + unitNamesUnitIds; node.EnableClickEvent = true; return node; } protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e) { BindGrid(); } protected void btnTreeFind_Click(object sender, EventArgs e) { InitTreeMenu(); } protected void btnSearch_Click(object sender, EventArgs e) { BindGrid(); } protected void btnNew_Click(object sender, EventArgs e) { if (!CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, Const.Tw_AntiCorrosionTrustMenuId, Const.BtnAdd)) { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); return; } if (string.IsNullOrEmpty(tvControlItem.SelectedNodeID) || tvControlItem.SelectedNodeID == "1" || tvControlItem.SelectedNodeID == "2") { Alert.ShowInTop("请选择单位工程!", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window1.GetShowReference(string.Format("AntiCorrosionTrustEdit.aspx?UnitWorkId={0}", tvControlItem.SelectedNodeID))); } protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { EditSelected(); } protected void btnMenuEdit_Click(object sender, EventArgs e) { EditSelected(); } private void EditSelected() { if (!CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, Const.Tw_AntiCorrosionTrustMenuId, Const.BtnModify)) { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); return; } if (string.IsNullOrEmpty(Grid1.SelectedRowID)) { Alert.ShowInTop("请选择要编辑的项", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window1.GetShowReference(string.Format("AntiCorrosionTrustEdit.aspx?Id={0}", Grid1.SelectedRowID))); } protected void btnMenuDelete_Click(object sender, EventArgs e) { if (!CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, Const.Tw_AntiCorrosionTrustMenuId, Const.BtnDelete)) { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); return; } if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录", MessageBoxIcon.Warning); return; } foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); TwAntiCorrosionTrustService.DeleteById(rowID); } BindGrid(); ShowNotify("删除成功!", MessageBoxIcon.Success); } protected void btnPrint_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(Grid1.SelectedRowID)) { ShowNotify("请选择要打印的项", MessageBoxIcon.Warning); return; } BLL.FastReportService.ResetData(); DataTable master = TwAntiCorrosionTrustService.GetPrintMasterTable(Grid1.SelectedRowID); DataTable detail = TwAntiCorrosionTrustService.GetPrintDetailTable(Grid1.SelectedRowID); BLL.FastReportService.AddFastreportTable(master); BLL.FastReportService.AddFastreportTable(detail); string rootPath = Server.MapPath("~/"); string initTemplatePath = "File\\Fastreport\\防腐委托单.frx"; if (File.Exists(rootPath + initTemplatePath)) { PageContext.RegisterStartupScript(Window3.GetShowReference(string.Format("~/Controls/Fastreport.aspx?ReportPath={0}", rootPath + initTemplatePath))); } else { ShowNotify("打印模板不存在!", MessageBoxIcon.Warning); } } protected void btnExportTrust_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(Grid1.SelectedRowID)) { ShowNotify("请选择要导出的项", MessageBoxIcon.Warning); return; } string templatePath = Funs.RootPath + @"File\Excel\DataOut\防腐委托单.xlsx"; if (!File.Exists(templatePath)) { ShowNotify("导出模板不存在!", MessageBoxIcon.Warning); return; } DataTable master = TwAntiCorrosionTrustService.GetPrintMasterTable(Grid1.SelectedRowID); DataTable detail = TwAntiCorrosionTrustService.GetPrintDetailTable(Grid1.SelectedRowID); if (master.Rows.Count == 0) { ShowNotify("未找到防腐委托单数据!", MessageBoxIcon.Warning); return; } var value = GetExportValue(master.Rows[0], detail); string tempPath = Funs.RootPath + @"File\Excel\Temp\防腐委托单.xlsx"; tempPath = tempPath.Replace(".xlsx", string.Format("{0:yyyy-MM-dd-HH-mm-ss}", DateTime.Now) + ".xlsx"); Directory.CreateDirectory(Path.GetDirectoryName(tempPath)); MiniExcel.SaveAsByTemplate(tempPath, templatePath, value); string trustCode = Convert.ToString(master.Rows[0]["TrustCode"]); string fileName = string.IsNullOrEmpty(trustCode) ? "防腐委托单.xlsx" : trustCode + ".xlsx"; FileInfo info = new FileInfo(tempPath); 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(tempPath, 0, fileSize); System.Web.HttpContext.Current.Response.Flush(); File.Delete(tempPath); System.Web.HttpContext.Current.Response.End(); } private Dictionary GetExportValue(DataRow master, DataTable detail) { var value = new Dictionary(); foreach (DataColumn column in master.Table.Columns) { value[column.ColumnName] = master[column] == DBNull.Value ? string.Empty : master[column]; } value["Data"] = detail; return value; } protected void Window1_Close(object sender, WindowCloseEventArgs e) { BindGrid(); } private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, BLL.Const.Tw_AntiCorrosionTrustMenuId); if (buttonList.Count > 0) { btnNew.Hidden = !buttonList.Contains(BLL.Const.BtnAdd); btnMenuEdit.Hidden = !buttonList.Contains(BLL.Const.BtnModify); btnMenuDelete.Hidden = !buttonList.Contains(BLL.Const.BtnDelete); btnExportTrust.Hidden = !buttonList.Contains(BLL.Const.BtnPrint); } } } }