using BLL;
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 定义项
///
/// 上传预设的虚拟路径
///
private string initPath = Const.ExcelUrl;//"File\\Excel\\DataIn\\";
///
/// 错误集合
///
public static string errorInfos = string.Empty;
///
/// 导入数据分类(材料)
///
public static string DataClassification = "Material";
public static List matList = new List();
public enum ButtonState { Check=0,Import=1,Save=2}
public static int State;
#endregion
#region 加载
///
/// 加载页面
///
///
///
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 listStr = new List();
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 模板下载
///
/// 模板下载
///
///
///
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")));
}
///
/// 下载导入模板
///
///
///
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的数据导入数据库
///
/// 将Dataset的数据导入数据库
///
/// 数据集
/// 数据集行数
///
private Model.ResponeData AddDatasetToSQL(DataTable pds, int Cols)
{
Model.ResponeData responeData = new Model.ResponeData();
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 unitWork = from x in Funs.DB.WBS_UnitWork where x.ProjectId == CurrUser.LoginProjectId && x.UnitWorkCode == dv[i]["主项编号"].ToString() && x.ProjectType == "2" select x;
// if (unitWork.Count() > 0)
// {
// unitworkId = unitWork.First().UnitWorkId;
// }
// else
// {
// errorInfos += (i + 2) + "Line, [主项编号] 不存在";
// }
//}
//else
//{
// errorInfos += (i + 2) + "Line, [主项编号] 不能为空";
//}
if (dv[i]["管线号"] != null && !string.IsNullOrEmpty(dv[i]["管线号"].ToString()))
{
var pipeline = from x in Funs.DB.HJGL_Pipeline where x.UnitWorkId == unitworkId && x.PipelineCode == dv[i]["管线号"].ToString() select x;
if (pipeline.Count() > 0)
{
item.PipelineId = pipeline.First().PipelineId;
PipeArea=pipeline.First().PipeArea;
}
else
{
errorInfos += (i + 2) + "Line,"+ dv[i]["管线号"].ToString() + " [管线号] 不存在";
}
}
else
{
errorInfos += (i + 2) + "Line, [管线号] 不能为空";
}
//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, [管线划分] 不匹配";
// }
//}
//else
//{
// errorInfos += (i + 2) + "Line, [管线划分] 不能为空";
//}
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() + " [材料编码] 不存在";
}
}
else
{
errorInfos += (i + 2) + "Line, [材料编码] 不能为空";
}
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() + "行," + "数量格式输入有误";
}
}
else
{
errorInfos += (i + 2) + "Line, [数量] 不能为空";
}
if (PipeArea=="1")
{
if (dv[i]["预制组件"] != null && !string.IsNullOrEmpty(dv[i]["预制组件"].ToString()))
{
item.PrefabricatedComponents = dv[i]["预制组件"].ToString();
}
else
{
errorInfos += (i + 2) + "Line, [预制组件] 不能为空";
}
}
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 (!string.IsNullOrEmpty(errorInfos))
{
responeData.code = 0;
responeData.message = errorInfos;
}
return responeData;
}
#endregion
///
/// 审核
///
///
///
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 != ".xls" && 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
DataSet ds = NPOIHelper.ExcelToDataSet(fileName, out errorInfos, true);
//验证Excel读取是否有误
if (!string.IsNullOrEmpty(errorInfos))
{
ShowNotify(errorInfos, MessageBoxIcon.Warning);
return;
}
var reposedata= AddDatasetToSQL(ds.Tables[0], 4);
if (reposedata.code == 1)
{
State = (int)ButtonState.Import;
ShowNotify("审核完成请点击导入");
}
else
{
ShowNotify(reposedata.message);
}
}
#region 导入
///
/// 导入
///
///
///
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
///
/// 提交
///
///
///
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 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)
{
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);
}
}
}
}