SGGL_SHJ/SGGL/FineUIPro.Web/HJGL/WeldingManage/WeldingPlanIn.aspx.cs

505 lines
18 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BLL;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.HJGL.WeldingManage
{
public partial class WeldingPlanIn : PageBase
{
#region
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 安装组件集合
/// </summary>
public static List<Model.HJGL_Pipeline> PipelineList = new List<Model.HJGL_Pipeline>();
public static List<Model.HJGL_Pipeline_Component> Pipeline_ComponentList = new List<Model.HJGL_Pipeline_Component>();
/// <summary>
/// 错误集合
/// </summary>
public static string errorInfos = string.Empty;
/// <summary>
/// 1 预制口2安装口
/// </summary>
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
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
/// <summary>
/// 审核
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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)
{
ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning);
}
}
#region Excel提取数据
/// <summary>
/// 从Excel提取数据--》Dataset
/// </summary>
/// <param name="filename">Excel文件路径名</param>
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, 13);
break;
case BLL.PipelineService.PipeArea_FIELD:
responeData = AddDatasetToSQL_FIELD(rows, 8);
break;
}
if (responeData.code==1)
{
ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
}
else
{
ShowNotify(responeData.message, MessageBoxIcon.Success);
}
}
#endregion
#region Dataset的数据导入数据库
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集行数</param>
/// <returns></returns>
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 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);
}
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
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
/// <summary>
/// 提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
/// <summary>
/// 下载模板按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
/// <summary>
/// 分页选择下拉改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
}
}