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

677 lines
28 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 Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.HJGL.WeldingManage
{
public partial class PipelineMatIn : PageBase
{
#region
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;//"File\\Excel\\DataIn\\";
/// <summary>
/// 错误集合
/// </summary>
public static string errorInfos = string.Empty;
/// <summary>
/// 导入数据分类(材料)
/// </summary>
public static string DataClassification = "Material";
public static List<HJGL_PipeLineMat> matList = new List<HJGL_PipeLineMat>();
public enum ButtonState { Check = 0, Import = 1, Save = 2 }
public static int State;
#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;
errorInfos = string.Empty;
State = (int)ButtonState.Check;
if (matList != null)
{
matList.Clear();
}
lbVersion.Text = BLL.HJGL_DesignBasisDataImportService.GetNowVersionByUnitWorkId(Request.Params["UnitWorkId"], DataClassification).ToString();
BindGrid2();
}
}
#endregion
void BindGrid2()
{
string strSql = @" select Import.DesignBasisDataImportId
,Import.ProjectId
,Import.UnitWorkId
,(Case Import.ImportType when '0' then '补充导入'
when '1' then '更新导入' end) as ImportType
,Import.FileName
,Import.FilePath
,Import.DataClassification
,Import.FileType
,Import.FileSize
,Import.FileId
,Import.Version
,Import.Remark
,Import.CreateMan
,Import.CreateDate
, Users.PersonName
from HJGL_DesignBasisDataImport as Import
left join Person_Persons as Users on Users.PersonId=Import.CreateMan
where Import.UnitWorkId=@UnitWorkId and Import.ProjectId=@ProjectId and Import.DataClassification=@DataClassification
order by Import.CreateDate";
List<SqlParameter> listStr = new List<SqlParameter>();
listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
listStr.Add(new SqlParameter("@UnitWorkId", Request.Params["UnitWorkId"]));
listStr.Add(new SqlParameter("@DataClassification", DataClassification));
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
// 2.获取当前分页数据
//var table = this.GetPagedDataTable(Grid1, tb1);
Grid2.RecordCount = tb.Rows.Count;
tb = GetFilteredTable(Grid2.FilteredData, tb);
var table = this.GetPagedDataTable(Grid2, tb);
Grid2.DataSource = table;
Grid2.DataBind();
}
#region
/// <summary>
/// 模板下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDownLoad_Click(object sender, EventArgs e)
{
PageContext.RegisterStartupScript(Confirm.GetShowReference("确定要下载模板吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel")));
}
/// <summary>
/// 下载导入模板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PageManager1_CustomEvent(object sender, CustomEventArgs e)
{
if (e.EventArgument == "Confirm_OK")
{
string rootPath = Server.MapPath("~/");
string uploadfilepath = rootPath + Const.PipelineMatTemplateUrl;
string filePath = Const.PipelineMatTemplateUrl;
string fileName = Path.GetFileName(filePath);
FileInfo info = new FileInfo(uploadfilepath);
long fileSize = info.Length;
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.ContentType = "excel/plain";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Length", fileSize.ToString().Trim());
Response.TransmitFile(uploadfilepath, 0, fileSize);
Response.End();
}
}
#endregion
#region Dataset的数据导入数据库
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集行数</param>
/// <returns></returns>
private Model.ResponeData AddDatasetToSQL(DataTable pds, int Cols)
{
Model.ResponeData responeData = new Model.ResponeData();
List<string> result = new List<string>();
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;
}
// DataRow[] dv = pds.Select("主项编号 <>'' and 管线号 <>'' and 材料编码 <>'' and 数量 <>''and 管线划分<>'' ");
DataRow[] dv = pds.Select(" 管线号 <>'' and 材料编码 <>'' and 数量 <>'' ");
//导入数据库
if (dv.Length > 0)
{
for (int i = 0; i < dv.Length; i++)
{
HJGL_PipeLineMat item = new HJGL_PipeLineMat();
#region
string unitworkId = string.Empty;
unitworkId = Request.Params["UnitWorkId"];
string PipeArea = string.Empty;//管线划分 1工厂预制 2现场施工
if (dv[i]["管线号"] != null && !string.IsNullOrEmpty(dv[i]["管线号"].ToString()))
{
var pipeline = from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitWorkId == unitworkId && x.PipelineCode == dv[i]["管线号"].ToString().Trim() select x;
if (pipeline.Count() > 0)
{
item.PipelineId = pipeline.First().PipelineId;
PipeArea = pipeline.First().PipeArea;
}
else
{
// errorInfos += (i + 2) + "Line,"+ dv[i]["管线号"].ToString() + " [管线号] 不存在</br>";
result.Add("不存在此管线号-" + dv[i]["管线号"].ToString());
}
}
else
{
//errorInfos += (i + 2) + "Line, [管线号] 不能为空</br>";
result.Add((i + 2) + "Line, [管线号] 不能为空</br>");
}
//if (dv[i]["管线划分"] != null && !string.IsNullOrEmpty(dv[i]["管线划分"].ToString()))
//{
// string txtPipeArea = "";
// switch (PipeArea)
// {
// case "1":
// txtPipeArea = "工厂预制";
// break;
// case "2":
// txtPipeArea = "现场施工";
// break;
// default:
// txtPipeArea = "无";
// break;
// }
// if (!Equals(txtPipeArea, dv[i]["管线划分"].ToString().Trim()))
// {
// errorInfos += (i + 2) + "Line, [管线划分] 不匹配</br>";
// }
//}
//else
//{
// errorInfos += (i + 2) + "Line, [管线划分] 不能为空</br>";
//}
if (dv[i]["材料编码"] != null && !string.IsNullOrEmpty(dv[i]["材料编码"].ToString()))
{
var lib = from x in Funs.DB.HJGL_MaterialCodeLib where x.MaterialCode == dv[i]["材料编码"].ToString() select x;
if (lib.Count() > 0)
{
item.MaterialCode = dv[i]["材料编码"].ToString();
}
else
{
//errorInfos += (i + 2) + "Line," + dv[i]["材料编码"].ToString() + " [材料编码] 不存在</br>";
result.Add("不存在此材料编码-" + dv[i]["材料编码"].ToString());
}
}
else
{
//errorInfos += (i + 2) + "Line, [材料编码] 不能为空</br>";
result.Add((i + 2) + "Line, [材料编码] 不能为空</br>");
}
if (dv[i]["数量"] != null && !string.IsNullOrEmpty(dv[i]["数量"].ToString()))
{
try
{
var number = Funs.GetNewDecimal(dv[i]["数量"].ToString());
item.Number = number;
}
catch (Exception)
{
//errorInfos += "第" + (i + 2).ToString() + "行," + "数量格式输入有误</br>";
result.Add("第" + (i + 2).ToString() + "行," + "数量格式输入有误</br>");
}
}
else
{
//errorInfos += (i + 2) + "Line, [数量] 不能为空</br>";
result.Add((i + 2) + "Line, [数量] 不能为空</br>");
}
if (PipeArea == "1")
{
if (dv[i]["预制组件"] != null && !string.IsNullOrEmpty(dv[i]["预制组件"].ToString()))
{
item.PrefabricatedComponents = dv[i]["预制组件"].ToString();
}
else
{
result.Add((i + 2) + "Line, [预制组件] 不能为空</br>");
}
}
var model = matList.Where(x => x.PipelineId == item.PipelineId && x.MaterialCode == item.MaterialCode && x.PrefabricatedComponents == item.PrefabricatedComponents);
if (model.Count() == 0)
{
matList.Add(item);
}
//if (PipeArea == "1")//工厂预制
//{
// var model= matList.Where(x => x.PrefabricatedComponents == item.PrefabricatedComponents && x.MaterialCode == item.MaterialCode);
// if (model.Count()==0)
// {
// matList.Add(item);
// }
//} else if (PipeArea == "2")//现场施工
//{
// var model = matList.Where(x => x.PipelineId == item.PipelineId && x.MaterialCode == item.MaterialCode);
// if (model.Count() == 0)
// {
// matList.Add(item);
// }
//}
//item.PipeLineMatId= SQLHelper.GetNewID(typeof(Model.HJGL_PipeLineMat));
//if (!matList.Select(x => x.MaterialCode).Contains(item.MaterialCode))
//{
// matList.Add(item);
//}
#endregion
}
}
else
{
responeData.code = 0;
responeData.message = "没有数据";
}
if (result.Count > 0)
{
// result = result.Substring(0, result.LastIndexOf("|"));
errorInfos = string.Join("|", result.Distinct());
//Alert alert = new Alert();
//alert.Message = result;
//alert.Target = Target.Self;
//alert.Show();
responeData.code = 0;
responeData.message = errorInfos;
}
return responeData;
}
private Model.ResponeData AddDatasetToSQL(List<dynamic> pds, int count)
{
Model.ResponeData responeData = new Model.ResponeData();
List<string> result = new List<string>();
if (count < 4)
{
responeData.code = 0;
responeData.message = "导入Excel格式错误Excel只有" + count.ToString().Trim() + "列";
return responeData;
}
if (pds.Count > 0 && pds != null)
{
for (int i = 1; i < pds.Count; i++)
{
HJGL_PipeLineMat item = new HJGL_PipeLineMat();
#region
string unitworkId = string.Empty;
unitworkId = Request.Params["UnitWorkId"];
string PipeArea = string.Empty;//管线划分 1工厂预制 2现场施工
if (pds[i].A != null && !string.IsNullOrEmpty(pds[i].A.ToString()))
{
string pipelineCode = pds[i].A.ToString();
var pipeline = from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitWorkId == unitworkId && x.PipelineCode == pipelineCode select x;
if (pipeline.Count() > 0)
{
item.PipelineId = pipeline.First().PipelineId;
PipeArea = pipeline.First().PipeArea;
}
else
{
result.Add("不存在此管线号-" + pds[i].A.ToString());
}
}
else
{
result.Add((i + 2) + "Line, [管线号] 不能为空</br>");
}
if (pds[i].C != null && !string.IsNullOrEmpty(pds[i].C.ToString()))
{
string materialCode = pds[i].C.ToString();
var lib = from x in Funs.DB.HJGL_MaterialCodeLib where x.MaterialCode == materialCode select x;
if (lib.Count() > 0)
{
item.MaterialCode = pds[i].C.ToString();
}
else
{
result.Add("不存在此材料编码-" + pds[i].C.ToString());
}
}
else
{
result.Add((i + 2) + "Line, [材料编码] 不能为空</br>");
}
if (pds[i].D != null && !string.IsNullOrEmpty(pds[i].D.ToString()))
{
try
{
var number = Funs.GetNewDecimal(pds[i].D.ToString());
item.Number = number;
}
catch (Exception)
{
result.Add("第" + (i + 2).ToString() + "行," + "数量格式输入有误</br>");
}
}
else
{
result.Add((i + 2) + "Line, [数量] 不能为空</br>");
}
if (PipeArea == "1")
{
if (pds[i].B != null && !string.IsNullOrEmpty(pds[i].B.ToString()))
{
item.PrefabricatedComponents = pds[i].B.ToString();
if (!item.PrefabricatedComponents.Contains("-")) //预制组件关联的有图纸,图纸名称又是根据组件名称截取的
{
result.Add((i + 2) + "Line, [预制组件] 不合规</br>");
}
}
else
{
// result.Add((i + 2) + "Line, [预制组件] 不能为空</br>");
}
}
var model = matList.Where(x => x.PipelineId == item.PipelineId && x.MaterialCode == item.MaterialCode && x.PrefabricatedComponents == item.PrefabricatedComponents);
if (model.Count() == 0)
{
matList.Add(item);
}
#endregion
}
if (result.Count > 0)
{
matList.Clear();
// result = result.Substring(0, result.LastIndexOf("|"));
errorInfos = string.Join("|", result.Distinct());
//Alert alert = new Alert();
//alert.Message = result;
//alert.Target = Target.Self;
//alert.Show();
responeData.code = 0;
responeData.message = errorInfos;
}
else
{
errorInfos = string.Empty;
}
}
else
{
responeData.code = 0;
responeData.message = "导入数据为空!";
}
return responeData;
}
#endregion
/// <summary>
/// 审核
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAudit_Click(object sender, EventArgs e)
{
string message = string.Empty;
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;
}
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);
//文件上传服务器后的名称
string fileName = rootPath + initPath + this.hdFileName.Text;
//读取Excel
var ds = MiniExcel.Query(fileName).ToList();
var columns = MiniExcel.GetColumns(fileName);
var cnt = columns.Count;
var reposedata = AddDatasetToSQL(ds, cnt);
if (reposedata.code == 1)
{
State = (int)ButtonState.Import;
ShowNotify("审核完成请点击导入");
}
else
{
Alert alert = new Alert
{
Message = reposedata.message,
Target = Target.Self
};
alert.Show();
//ShowNotify(reposedata.message);
}
}
#region
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnImport_Click(object sender, EventArgs e)
{
if (State == (int)ButtonState.Check)
{
ShowNotify("请先审核");
return;
}
if (string.IsNullOrEmpty(errorInfos))
{
if (!string.IsNullOrEmpty(this.hdFileName.Text))
{
//if (PipelineList.Count > 0)
//{
// this.Grid1.Hidden = false;
// this.Grid1.DataSource = PipelineList;
// this.Grid1.DataBind();
// Grid1.RecordCount = PipelineList.Count;
//}
}
else
{
ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
State = (int)ButtonState.Save;
ShowNotify("数据导入成功!", MessageBoxIcon.Success);
}
#endregion
/// <summary>
/// 提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
if (State != (int)ButtonState.Save)
{
ShowNotify("请先审核/导入");
return;
}
if (string.IsNullOrEmpty(errorInfos))
{
string rootPath = Server.MapPath("~/");
string oldefilePath = rootPath + initPath + this.hdFileName.Text;
string unitworkId = Request.Params["UnitWorkId"];
string filePath = rootPath + Const.DesignBasisDataImportPath + this.hdFileName.Text;
if (oldefilePath != string.Empty && System.IO.File.Exists(oldefilePath))
{
if (!Directory.Exists(rootPath + Const.DesignBasisDataImportPath))
{
Directory.CreateDirectory(rootPath + Const.DesignBasisDataImportPath);
}
File.Move(oldefilePath, filePath);
//System.IO.File.Delete(oldefilePath);//删除上传的XLS文件
}
string FileName = this.fuAttachUrl.FileName;
if (DrpType.SelectedValue == "1")//更新导入
{
BLL.PipelineMatService.DeletePipeLineMatByUnitWorkId(unitworkId);//删除原有管线对应材料
AddHJGL_PipeLineMat(matList);
Model.HJGL_DesignBasisDataImport hJGL_DesignBasisDataImport = new Model.HJGL_DesignBasisDataImport();
hJGL_DesignBasisDataImport.DesignBasisDataImportId = SQLHelper.GetNewID();
hJGL_DesignBasisDataImport.ProjectId = this.CurrUser.LoginProjectId;
hJGL_DesignBasisDataImport.UnitWorkId = unitworkId;
hJGL_DesignBasisDataImport.ImportType = "1";
hJGL_DesignBasisDataImport.DataClassification = DataClassification;
hJGL_DesignBasisDataImport.FileName = FileName;
hJGL_DesignBasisDataImport.FilePath = filePath.Replace(rootPath, "");
hJGL_DesignBasisDataImport.FileType = BLL.HJGL_DesignBasisDataImportService.GetFileType(FileName);
hJGL_DesignBasisDataImport.Version = BLL.HJGL_DesignBasisDataImportService.GetNewVersionByUnitWorkId(unitworkId, DataClassification);
hJGL_DesignBasisDataImport.Remark = txtRemark.Text;
hJGL_DesignBasisDataImport.CreateMan = this.CurrUser.PersonId;
hJGL_DesignBasisDataImport.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
BLL.HJGL_DesignBasisDataImportService.AddHJGL_DesignBasisDataImport(hJGL_DesignBasisDataImport);
BLL.HJGL_DesignBasisDataImportVerSionLogService.UpdateVersion(this.CurrUser.LoginProjectId, unitworkId, (decimal)hJGL_DesignBasisDataImport.Version, DataClassification);
}
else //补充导入
{
AddHJGL_PipeLineMat(matList);
Model.HJGL_DesignBasisDataImport hJGL_DesignBasisDataImport = new Model.HJGL_DesignBasisDataImport();
hJGL_DesignBasisDataImport.DesignBasisDataImportId = SQLHelper.GetNewID();
hJGL_DesignBasisDataImport.ProjectId = this.CurrUser.LoginProjectId;
hJGL_DesignBasisDataImport.UnitWorkId = unitworkId;
hJGL_DesignBasisDataImport.ImportType = "0";
hJGL_DesignBasisDataImport.DataClassification = DataClassification;
hJGL_DesignBasisDataImport.FileName = FileName;
hJGL_DesignBasisDataImport.FilePath = filePath.Replace(rootPath, ""); ;
hJGL_DesignBasisDataImport.FileType = BLL.HJGL_DesignBasisDataImportService.GetFileType(FileName);
hJGL_DesignBasisDataImport.Version = BLL.HJGL_DesignBasisDataImportService.GetNowVersionByUnitWorkId(unitworkId, DataClassification);
hJGL_DesignBasisDataImport.Remark = txtRemark.Text;
hJGL_DesignBasisDataImport.CreateMan = this.CurrUser.PersonId;
hJGL_DesignBasisDataImport.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
BLL.HJGL_DesignBasisDataImportService.AddHJGL_DesignBasisDataImport(hJGL_DesignBasisDataImport);
BLL.HJGL_DesignBasisDataImportVerSionLogService.UpdateVersion(this.CurrUser.LoginProjectId, unitworkId, (decimal)hJGL_DesignBasisDataImport.Version, DataClassification);
}
ShowNotify("导入成功!", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
public void AddHJGL_PipeLineMat(List<HJGL_PipeLineMat> matList)
{
foreach (var item in matList)
{
var pipeLineMat = from x in Funs.DB.HJGL_PipeLineMat where x.MaterialCode == item.MaterialCode && x.PipelineId == item.PipelineId && x.PrefabricatedComponents == item.PrefabricatedComponents select x;
if (pipeLineMat.Count() == 0 || pipeLineMat == null)
{
item.PipeLineMatId = SQLHelper.GetNewID(typeof(Model.HJGL_PipeLineMat));
BLL.PipelineMatService.AddPipeLineMat(item);
}
else
{
item.PipeLineMatId = pipeLineMat.First().PipeLineMatId;
BLL.PipelineMatService.UpdatePipeLineMat(item);
}
BLL.HJGL_PipelineComponentService.SyncPipelineComponentByMatId(item.PipeLineMatId);
}
}
}
}