using BLL;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.HJGL.PreDesign
{
public partial class InstallList : PageBase
{
public int pageSize = 20;
public static DataTable GridDataTable = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
this.InitTreeMenu();//加载树
}
}
#region 加载树装置-单位-工作区
///
/// 加载树
///
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;
this.tvControlItem.Nodes.Add(rootNode2);
var pUnits = (from x in Funs.DB.Project_ProjectUnit where x.ProjectId == this.CurrUser.LoginProjectId select x).ToList();
// 获取当前用户所在单位
var currUnit = pUnits.FirstOrDefault(x => x.UnitId == this.CurrUser.UnitId);
var unitWorkList = (from x in Funs.DB.WBS_UnitWork
where x.ProjectId == this.CurrUser.LoginProjectId
&& x.SuperUnitWork == null && x.UnitId != null && x.ProjectType != null
select x).ToList();
List unitWork1 = null;
List unitWork2 = null;
//// 当前为施工单位,只能操作本单位的数据
//if (currUnit != null && currUnit.UnitType == Const.ProjectUnitType_2)
//{
// unitWork1 = (from x in unitWorkList
// where x.UnitId == this.CurrUser.UnitId && x.ProjectType == "1"
// select x).ToList();
// unitWork2 = (from x in unitWorkList
// where x.UnitId == this.CurrUser.UnitId && x.ProjectType == "2"
// select x).ToList();
//}
//else
//{
unitWork1 = (from x in unitWorkList where x.ProjectType == "1" select x).ToList();
unitWork2 = (from x in unitWorkList where x.ProjectType == "2" select x).ToList();
//}
if (unitWork1.Count() > 0)
{
foreach (var q in unitWork1)
{
int a = (from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitWorkId == q.UnitWorkId && x.PipeArea == "1" && x.PipelineCode.Contains(txtPipelineCode.Text.Trim()) select x.FlowingSection).Distinct().Count();
var unitNamesUnitIds = BLL.UnitService.getUnitNamesUnitIds(q.UnitId);
TreeNode tn1 = new TreeNode();
tn1.NodeID = q.UnitWorkId;
//tn1.Text = q.UnitWorkName + "【" + a.ToString() + "】" + "管线";
tn1.Text = q.UnitWorkName;
tn1.ToolTip = "施工单位:" + unitNamesUnitIds;
tn1.CommandName = 1 + "|" + Funs.GetEndPageNumber(a, pageSize);
tn1.EnableClickEvent = true;
tn1.EnableExpandEvent = true;
rootNode1.Nodes.Add(tn1);
if (a > 0)
{
// BindNodes(tn1);
TreeNode newNode = new TreeNode();
newNode.Text = "加载流水段...";
newNode.NodeID = "加载流水段...";
tn1.Nodes.Add(newNode);
}
//if (a > 0)
//{
// BindNodes(tn1);
//}
}
}
if (unitWork2.Count() > 0)
{
foreach (var q in unitWork2)
{
int a = (from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitWorkId == q.UnitWorkId && x.PipeArea == "1" && x.PipelineCode.Contains(txtPipelineCode.Text.Trim()) select x.FlowingSection).Distinct().Count();
var unitNamesUnitIds = BLL.UnitService.getUnitNamesUnitIds(q.UnitId);
TreeNode tn2 = new TreeNode();
tn2.NodeID = q.UnitWorkId;
//tn2.Text = q.UnitWorkName + "【" + a.ToString() + "】" + "管线";
tn2.Text = q.UnitWorkName;
if (q.UnitWorkId == this.hdUnitWorkId.Text)
{
tn2.Expanded = true;
}
tn2.ToolTip = "施工单位:" + unitNamesUnitIds;
tn2.CommandName = 1 + "|" + Funs.GetEndPageNumber(a, pageSize);
tn2.EnableClickEvent = true;
tn2.EnableExpandEvent = true;
rootNode2.Nodes.Add(tn2);
if (a > 0)
{
// BindNodes(tn1);
TreeNode newNode = new TreeNode();
newNode.Text = "加载流水段...";
newNode.NodeID = "加载流水段...";
tn2.Nodes.Add(newNode);
}
//if (a > 0)
//{
// BindNodes(tn2);
//}
}
}
}
private void BindNodes(TreeNode node)
{
List flowingSection = new List();
//List pipeline = new List();
var list = (from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId && x.PipeArea == "1" && x.UnitWorkId == node.NodeID && x.PipelineCode.Contains(this.txtPipelineCode.Text.Trim()) select x.FlowingSection).ToList();
flowingSection = list.Where(x => !string.IsNullOrWhiteSpace(x) == true).Distinct().OrderBy(x => x).ToList();
//int pageindex = int.Parse(node.CommandName.Split('|')[0]);
//int pageCount = int.Parse(node.CommandName.Split('|')[1]);
//if (pageindex <= pageCount)
//{
// flowingSection = flowingSection.Skip(pageSize * (pageindex - 1)).Take(pageSize).ToList();
foreach (var item in flowingSection)
{
//var comCount = (from x in Funs.DB.HJGL_Pipeline_Component where x.PipelineId == item.PipelineId select x).Count();
TreeNode newNode = new TreeNode();
//newNode.Text = item.PipelineCode + "【" + comCount.ToString() + " " + "组件" + "】";
newNode.Text = item;
newNode.ToolTip = "流水段";
newNode.CommandName = "流水段";
newNode.NodeID = SQLHelper.GetNewID();
newNode.EnableClickEvent = true;
node.Nodes.Add(newNode);
}
// if (pageindex < pageCount)
// {
// TreeNode newNode = new TreeNode();
// newNode.Text = "加载";
// newNode.NodeID = "加载";
// //newNode.NodeID = SQLHelper.GetNewID();
// newNode.CommandName = "加载";
// newNode.Icon = Icon.ArrowDown;
// newNode.EnableClickEvent = true;
// node.Nodes.Add(newNode);
// }
//}
}
#endregion
protected void tvControlItem_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
if (e.Node.Nodes[0].NodeID == "加载流水段...")
{
e.Node.Nodes.Clear();
BindNodes(e.Node);
}
}
#region 点击TreeView
///
/// 点击TreeView
///
///
///
protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
{
if (e.CommandName == "流水段")
{
Model.HJGL_Pipeline pipeline = BLL.PipelineService.GetPipelineByFlowingSection(this.tvControlItem.SelectedNode.Text);
this.hdUnitWorkId.Text = string.Empty;
if (pipeline != null)
{
this.hdUnitWorkId.Text = this.tvControlItem.SelectedNode.ParentNode.NodeID;
}
}
this.BindGrid();
}
#endregion
#region 数据绑定
///
/// 数据绑定
///
private void BindGrid()
{
if (tvControlItem.SelectedNode == null) return;
DataTable tb = BindData();
GridDataTable = tb;
// 2.获取当前分页数据
Grid1.RecordCount = tb.Rows.Count;
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
}
///
/// 查询数据
///
///
private DataTable BindData()
{
string strSql = @"WITH cte as (select pipeline.PipelineCode as PipelineCode,
comonent.PipelineComponentCode as PipelineComponentCode,
'' as matdef,
packdetail.Number as Number,
pack.PackagingCode as PackagingCode,
trainnumber.TrainNumber as TrainNumber,
pipeline.FlowingSection as FlowingSection,
pipeline.UnitWorkId as UnitWorkId
from HJGL_Pipeline pipeline
left join HJGL_PackagingManageDetail packdetail on pipeline.PipelineId = packdetail.PipelineId
left join HJGL_Pipeline_Component comonent
on packdetail.PipelineComponentId = comonent.PipelineComponentId
left join HJGL_PackagingManage pack on packdetail.PackagingManageId = pack.PackagingManageId
left join HJGL_TrainNumberManage trainnumber on pack.TrainNumberId = TrainNumber.Id
where (packdetail.PipelineComponentId is not null or packdetail.PipelineComponentId != '')
union all
select pipeline.PipelineCode as PipelineCode,
packdetail.MaterialCode as PipelineComponentCode,
matlib.MaterialDef as matdef,
packdetail.Number as Number,
pack.PackagingCode as PackagingCode,
trainnumber.TrainNumber as TrainNumber,
pipeline.FlowingSection as FlowingSection,
pipeline.UnitWorkId as UnitWorkId
from HJGL_Pipeline pipeline
left join HJGL_PackagingManageDetail packdetail on pipeline.PipelineId = packdetail.PipelineId
left join HJGL_MaterialCodeLib matlib on packdetail.MaterialCode = matlib.MaterialCode
left join HJGL_PackagingManage pack on packdetail.PackagingManageId = pack.PackagingManageId
left join HJGL_TrainNumberManage trainnumber on pack.TrainNumberId = TrainNumber.Id
where (packdetail.PipelineComponentId is null or packdetail.PipelineComponentId = ''))
SELECT * FROM cte WHERE 1=1 ";
List listStr = new List();
if (tvControlItem.SelectedNode.CommandName.Split('|').Length == 2)
{
strSql += " and cte.UnitWorkId =@UnitWorkId";
listStr.Add(new SqlParameter("@UnitWorkId", this.tvControlItem.SelectedNodeID));
}
else if (tvControlItem.SelectedNode.CommandName == "流水段")
{
strSql += " and cte.FlowingSection = @FlowingSection ";
listStr.Add(new SqlParameter("@FlowingSection", this.tvControlItem.SelectedNode.Text));
}
if (!string.IsNullOrEmpty(txtPipelineCode2.Text))
{
strSql += " AND cte.PipelineCode like @PipelineCode";
listStr.Add(new SqlParameter("@PipelineCode", "%" + this.txtPipelineCode2.Text.Trim() + "%"));
}
if (!string.IsNullOrEmpty(txtPipelineComponentCode.Text))
{
strSql += " AND cte.PipelineComponentCode like @PipelineComponentCode";
listStr.Add(new SqlParameter("@PipelineComponentCode", "%" + this.txtPipelineComponentCode.Text.Trim() + "%"));
}
//if (!string.IsNullOrEmpty(drpFlowingSection.SelectedValue) && drpFlowingSection.SelectedValue != Const._Null)
//{
// strSql += " AND cte.FlowingSection =@FlowingSection";
// listStr.Add(new SqlParameter("@FlowingSection", drpFlowingSection.SelectedValue.ToString()));
//}
if (!string.IsNullOrEmpty(txtPipelineCode.Text))
{
strSql += " AND cte.PipelineCode like @pipeline";
listStr.Add(new SqlParameter("@pipeline", "%" + this.txtPipelineCode.Text.Trim() + "%"));
}
strSql += " ORDER BY PipelineCode,PipelineComponentCode ";
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
return tb;
}
#endregion
#region 分页排序
#region 页索引改变事件
///
/// 页索引改变事件
///
///
///
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
BindGrid();
}
#endregion
#region 排序
///
/// 排序
///
///
///
protected void Grid1_Sort(object sender, GridSortEventArgs e)
{
BindGrid();
}
#endregion
#region 分页选择下拉改变事件
///
/// 分页选择下拉改变事件
///
///
///
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
BindGrid();
}
#endregion
#endregion
#region 查询事件
protected void btnSearch_Click(object sender, EventArgs e)
{
BindGrid();
}
#endregion
#region 刷新页面
///
/// 查询
///
///
///
protected void TextBox_TextChanged(object sender, EventArgs e)
{
this.BindGrid();
}
protected void btnTreeFind_Click1(object sender, EventArgs e)
{
this.InitTreeMenu();
this.BindGrid();
}
#endregion
public string ConvertProductionState(object state)
{
string StateName = string.Empty;
if (state != null)
{
if (!string.IsNullOrEmpty(state.ToString()))
{
string txt = HJGL_PipelineComponentService.GetProductionState().FirstOrDefault(x => x.Value == state.ToString()).Text;
return txt;
}
}
return StateName;
}
///
/// 导出
///
///
///
protected void btnOut_Click(object sender, EventArgs e)
{
if (GridDataTable != null)
{
var q = (from x in GridDataTable.AsEnumerable()
select new
{
管线号 = x.Field("PipelineCode"),
组件编号 = x.Field("PipelineComponentCode"),
预制散件材料描述 = !string.IsNullOrWhiteSpace(x.Field("matdef")) ? x.Field("matdef") : "-",
数量 = x.Field("Number"),
所在包装编号 = x.Field("PackagingCode"),
车次 = x.Field("TrainNumber"),
流水段 = x.Field("FlowingSection")
});
string path = Funs.RootPath + @"File\Excel\Temp\PrePipelineInstallList.xlsx";
path = path.Replace(".xlsx", string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ".xlsx");
MiniExcel.SaveAs(path, q);
string fileName = $"安装清单-" + this.tvControlItem.SelectedNode.Text + "-" + string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ".xlsx";
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);
}
}
}
}