using BLL;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.HJGL.WeldingManage
{
public partial class WeldingPlanIn : PageBase
{
#region 定义变量
///
/// 上传预设的虚拟路径
///
private string initPath = Const.ExcelUrl;
///
/// 安装组件集合
///
public static List PipelineList = new List();
public static List Pipeline_ComponentList = new List();
///
/// 错误集合
///
public static string errorInfos = string.Empty;
///
/// 1 预制口,2安装口
///
public string Type
{
get
{
return (string)ViewState["Type"];
}
set
{
ViewState["Type"] = value;
}
}
public string UnitWorkId
{
get
{
return (string)ViewState["UnitWorkId"];
}
set
{
ViewState["UnitWorkId"] = value;
}
}
#endregion
#region 加载
///
/// 加载页面
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.hdFileName.Text = string.Empty;
Type = Request.Params["Type"];
UnitWorkId = Request.Params["UnitWorkId"];
if (PipelineList != null)
{
PipelineList.Clear();
}
if (Pipeline_ComponentList != null)
{
Pipeline_ComponentList.Clear();
}
errorInfos = string.Empty;
}
}
#endregion
#region 审核
///
/// 审核
///
///
///
protected void btnAudit_Click(object sender, EventArgs e)
{
try
{
if (this.fuAttachUrl.HasFile == false)
{
ShowNotify("请您选择Excel文件!", MessageBoxIcon.Warning);
return;
}
string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower();
if (IsXls != ".xlsx")
{
ShowNotify("只可以选择Excel文件!", MessageBoxIcon.Warning);
return;
}
if (PipelineList != null)
{
PipelineList.Clear();
}
if (!string.IsNullOrEmpty(errorInfos))
{
errorInfos = string.Empty;
}
string rootPath = Server.MapPath("~/");
string initFullPath = rootPath + initPath;
if (!Directory.Exists(initFullPath))
{
Directory.CreateDirectory(initFullPath);
}
this.hdFileName.Text = BLL.Funs.GetNewFileName() + IsXls;
string filePath = initFullPath + this.hdFileName.Text;
this.fuAttachUrl.PostedFile.SaveAs(filePath);
ImportXlsToData(rootPath + initPath + this.hdFileName.Text);
}
catch (Exception ex)
{
Alert alert = new Alert
{
Message = "'" + ex.Message + "'",
Target = Target.Self
};
alert.Show();
//ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning);
}
}
#region 读Excel提取数据
///
/// 从Excel提取数据--》Dataset
///
/// Excel文件路径名
private void ImportXlsToData(string fileName)
{
// var rows =Funs.LINQToDataTable(MiniExcel.Query(fileName).ToList()) ;
var rows = MiniExcel.QueryAsDataTable(fileName, useHeaderRow: true);
Model.ResponeData responeData = new Model.ResponeData();
switch (Type)
{
case BLL.PipelineService.PipeArea_SHOP:
responeData = AddDatasetToSQL_SHOP(rows, 14);
break;
case BLL.PipelineService.PipeArea_FIELD:
responeData = AddDatasetToSQL_FIELD(rows, 12);
break;
}
if (responeData.code == 1)
{
ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
}
else
{
Alert alert = new Alert
{
Message = responeData.message,
Target = Target.Self
};
alert.Show();
// ShowNotify(responeData.message, MessageBoxIcon.Success);
}
}
#endregion
#region 将Dataset的数据导入数据库
///
/// 将Dataset的数据导入数据库
///
/// 数据集
/// 数据集行数
///
private Model.ResponeData AddDatasetToSQL_FIELD(DataTable pds, int Cols)
{
Model.ResponeData responeData = new Model.ResponeData();
string result = string.Empty;
int ic, ir;
ic = pds.Columns.Count;
ir = pds.Rows.Count;
if (ic < Cols)
{
responeData.code = 0;
responeData.message = "导入Excel格式错误!Excel只有" + ic.ToString().Trim() + "列";
return responeData;
}
if (pds != null && ir > 0)
{
var getPipeline = from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId select x;
for (int i = 0; i < ir; i++)
{
Model.HJGL_Pipeline pipeline = new Model.HJGL_Pipeline();
string pioelinecode = pds.Rows[i]["管线号"].ToString();
string flowingSection = pds.Rows[i]["流水段"].ToString();
string PlanStartDate = pds.Rows[i]["计划开始时间"].ToString();
string PlanEndDate = pds.Rows[i]["计划结束时间"].ToString();
if (string.IsNullOrEmpty(pioelinecode))
{
result += "第" + (i + 2).ToString() + "行," + "管线号" + "," + "此项为必填项!" + "|";
}
else
{
pipeline = getPipeline.FirstOrDefault(x => x.PipelineCode == pioelinecode);
if (pipeline == null)
{
result += "第" + (i + 2).ToString() + "行," + "不存在此管线号" + "|";
continue;
}
}
try
{
pipeline.PlanStartDate = Convert.ToDateTime(PlanStartDate);
pipeline.PlanEndDate = Convert.ToDateTime(PlanEndDate);
pipeline.FlowingSection = flowingSection;
}
catch (Exception)
{
result += "第" + (i + 2).ToString() + "行," + "无效的时间格式" + "|";
}
PipelineList.Add(pipeline);
}
if (!string.IsNullOrEmpty(result))
{
PipelineList.Clear();
result = result.Substring(0, result.LastIndexOf("|"));
errorInfos = result;
//Alert alert = new Alert();
//alert.Message = result;
//alert.Target = Target.Self;
//alert.Show();
responeData.code = 0;
responeData.message = errorInfos;
}
else
{
errorInfos = string.Empty;
// ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
}
}
else
{
responeData.code = 0;
responeData.message = "导入数据为空!";
}
return responeData;
}
private Model.ResponeData AddDatasetToSQL_SHOP(DataTable pds, int Cols)
{
Model.ResponeData responeData = new Model.ResponeData();
string result = string.Empty;
int ic, ir;
ic = pds.Columns.Count;
ir = pds.Rows.Count;
if (ic < Cols)
{
responeData.code = 0;
responeData.message = "导入Excel格式错误!Excel只有" + ic.ToString().Trim() + "列";
return responeData;
}
if (pds != null && ir > 0)
{
var getPipeline = from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId select x;
for (int i = 0; i < ir; i++)
{
Model.HJGL_Pipeline pipeline = new Model.HJGL_Pipeline();
Model.HJGL_Pipeline_Component pipelinecomponent = new Model.HJGL_Pipeline_Component();
string pioelinecode = pds.Rows[i]["管线号"].ToString();
string PipelineComponentCode = pds.Rows[i]["组件编号"].ToString();
string PlanStartDate_SHOP = pds.Rows[i]["计划开始时间(预制)"].ToString();
string PlanEndDate_SHOP = pds.Rows[i]["计划结束时间(预制)"].ToString();
if (string.IsNullOrEmpty(pioelinecode))
{
result += "第" + (i + 2).ToString() + "行," + "管线号" + "," + "此项为必填项!" + "|";
}
else
{
pipeline = getPipeline.FirstOrDefault(x => x.PipelineCode == pioelinecode);
if (pipeline == null)
{
result += "第" + (i + 2).ToString() + "行," + "不存在此管线号" + "|";
continue;
}
}
if (string.IsNullOrEmpty(PipelineComponentCode))
{
result += "第" + (i + 2).ToString() + "行," + "组件编号" + "," + "此项为必填项!" + "|";
}
else
{
if (!BLL.HJGL_PipelineComponentService.IsExistPipelineComponentCode(PipelineComponentCode, pipeline.PipelineId, ""))
{
result += "第" + (i + 2).ToString() + "行," + "组件与管线不匹配" + "|";
continue;
}
else
{
pipelinecomponent = HJGL_PipelineComponentService.GetPipelineComponentByCodeandpipelineId(PipelineComponentCode, pipeline.PipelineId);
}
}
try
{
pipelinecomponent.PlanStartDate = Convert.ToDateTime(PlanStartDate_SHOP);
pipelinecomponent.PlanEndDate = Convert.ToDateTime(PlanEndDate_SHOP);
}
catch (Exception)
{
result += "第" + (i + 2).ToString() + "行," + "无效的时间格式" + "|";
}
Pipeline_ComponentList.Add(pipelinecomponent);
}
if (!string.IsNullOrEmpty(result))
{
PipelineList.Clear();
result = result.Substring(0, result.LastIndexOf("|"));
errorInfos = result;
//Alert alert = new Alert();
//alert.Message = result;
//alert.Target = Target.Self;
//alert.Show();
responeData.code = 0;
responeData.message = errorInfos;
}
else
{
errorInfos = string.Empty;
// ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
}
}
else
{
responeData.code = 0;
responeData.message = "导入数据为空!";
}
return responeData;
}
#endregion
#endregion
#region 导入
///
/// 导入
///
///
///
protected void btnImport_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(errorInfos))
{
if (!string.IsNullOrEmpty(this.hdFileName.Text))
{
switch (Type)
{
case BLL.PipelineService.PipeArea_SHOP:
if (Pipeline_ComponentList.Count > 0)
{
this.Grid1.Hidden = false;
this.Grid1.DataIDField = "PipelineComponentId";
this.Grid1.DataSource = Pipeline_ComponentList;
this.Grid1.DataBind();
Grid1.RecordCount = Pipeline_ComponentList.Count;
}
break;
case BLL.PipelineService.PipeArea_FIELD:
if (PipelineList.Count > 0)
{
this.Grid1.Hidden = false;
this.Grid1.DataIDField = "PipelineId";
this.Grid1.DataSource = PipelineList;
this.Grid1.DataBind();
Grid1.RecordCount = PipelineList.Count;
}
break;
}
}
else
{
ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
#endregion
#region 提交
///
/// 提交
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(errorInfos))
{
switch (Type)
{
case BLL.PipelineService.PipeArea_SHOP:
if (Pipeline_ComponentList.Count > 0)
{
foreach (var item in Pipeline_ComponentList)
{
BLL.HJGL_PipelineComponentService.UpdatePipelineComponent(item);
}
}
break;
case BLL.PipelineService.PipeArea_FIELD:
if (PipelineList.Count > 0)
{
foreach (var item in PipelineList)
{
BLL.PipelineService.UpdatePipeline(item);
}
}
break;
}
int a = PipelineList.Count();
string rootPath = Server.MapPath("~/");
string initFullPath = rootPath + initPath;
string filePath = initFullPath + this.hdFileName.Text;
if (filePath != string.Empty && System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);//删除上传的XLS文件
}
ShowNotify("导入成功!", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
#endregion
#region 获取名称
public string ConvertPipelineId(object PipelineId)
{
string StateName = string.Empty;
if (!string.IsNullOrEmpty(PipelineId.ToString()))
{
if (PipelineId != null)
{
string txt = PipelineService.GetPipelineByPipelineId(PipelineId.ToString()).PipelineCode;
return txt;
}
return "";
}
return StateName;
}
#endregion
#region 下载模板
///
/// 下载模板按钮
///
///
///
protected void btnDownLoad_Click(object sender, EventArgs e)
{
switch (Type)
{
case BLL.PipelineService.PipeArea_SHOP:
PipelineService.DownPipeArea_SHOPFile(this.CurrUser.LoginProjectId, UnitWorkId);
break;
case BLL.PipelineService.PipeArea_FIELD:
PipelineService.DownPipeArea_FIELDFile(this.CurrUser.LoginProjectId, UnitWorkId);
break;
}
}
#endregion
#region 分页选择下拉改变事件
///
/// 分页选择下拉改变事件
///
///
///
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
this.Grid1.DataSource = PipelineList;
this.Grid1.DataBind();
Grid1.RecordCount = PipelineList.Count;
}
#endregion
}
}