2022-09-05 16:36:31 +08:00
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 定 义 项
/// <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 ( ) ;
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, [主项编号] 不存在</br>";
// }
//}
//else
//{
// errorInfos += (i + 2) + "Line, [主项编号] 不能为空</br>";
//}
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
{
2022-10-11 17:12:13 +08:00
errorInfos + = ( i + 2 ) + "Line," + dv [ i ] [ "管线号" ] . ToString ( ) + " [管线号] 不存在</br>" ;
2022-09-05 16:36:31 +08:00
}
}
else
{
errorInfos + = ( 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
{
2022-10-11 17:12:13 +08:00
errorInfos + = ( i + 2 ) + "Line," + dv [ i ] [ "材料编码" ] . ToString ( ) + " [材料编码] 不存在</br>" ;
2022-09-05 16:36:31 +08:00
}
}
else
{
errorInfos + = ( 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>" ;
}
}
else
{
errorInfos + = ( i + 2 ) + "Line, [数量] 不能为空</br>" ;
}
if ( PipeArea = = "1" )
{
if ( dv [ i ] [ "预制组件" ] ! = null & & ! string . IsNullOrEmpty ( dv [ i ] [ "预制组件" ] . ToString ( ) ) )
{
item . PrefabricatedComponents = dv [ i ] [ "预制组件" ] . ToString ( ) ;
}
else
{
errorInfos + = ( 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 ( ! string . IsNullOrEmpty ( errorInfos ) )
{
responeData . code = 0 ;
responeData . message = errorInfos ;
}
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 ! = ".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 ) ;
2022-10-11 17:12:13 +08:00
2022-09-05 16:36:31 +08:00
//验证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 导 入
/// <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 )
{
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 ) ;
}
}
}
}