SVN-Revision: r399
This commit is contained in:
wyf
2025-03-26 10:45:36 +00:00
parent 124dae6e45
commit 1cd1ad3c2e
2 changed files with 306 additions and 73 deletions
+44 -16
View File
@@ -101,26 +101,54 @@ namespace Lskj.PubBomImport
{
try
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "选择导入文件夹";
DialogResult dialogResult = dialog.ShowDialog();
if (dialogResult == DialogResult.OK)
if (Model.ShowType.Equals("1"))
{
string dirPath = dialog.SelectedPath;
FrmProgress frmProgress = new FrmProgress();
frmProgress.frmMain = this;
frmProgress.ImportTimer.Tick += (ts, te) =>
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "选择导入文件夹";
DialogResult dialogResult = dialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
frmProgress.ImportTimer.Enabled = false;
frmProgress.StartImport(dirPath);
};
frmProgress.ImportTimer.Enabled = true;
frmProgress.ShowDialog();
frmProgress.Dispose();
string dirPath = dialog.SelectedPath;
FrmProgress frmProgress = new FrmProgress();
frmProgress.frmMain = this;
frmProgress.ImportTimer.Tick += (ts, te) =>
{
frmProgress.ImportTimer.Enabled = false;
frmProgress.StartImportInDir(dirPath);
};
frmProgress.ImportTimer.Enabled = true;
frmProgress.ShowDialog();
frmProgress.Dispose();
}
else
{
this.Close();
}
}
else
else if (Model.ShowType.Equals("2"))
{
this.Close();
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "选择导入文件";
dialog.Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls";
DialogResult dialogResult = dialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string dirPath = dialog.FileName;
FrmProgress frmProgress = new FrmProgress();
frmProgress.frmMain = this;
frmProgress.ImportTimer.Tick += (ts, te) =>
{
frmProgress.ImportTimer.Enabled = false;
frmProgress.StartImportInMxFile(dirPath);
};
frmProgress.ImportTimer.Enabled = true;
frmProgress.ShowDialog();
frmProgress.Dispose();
}
else
{
this.Close();
}
}
}
catch (Exception ex)
+262 -57
View File
@@ -54,7 +54,7 @@ namespace Lskj.PubBomImport
/// 开始导入
/// </summary>
/// <param name="dirPaths"></param>
public void StartImport(string dirPath)
public void StartImportInDir(string dirPath)
{
try
{
@@ -64,13 +64,212 @@ namespace Lskj.PubBomImport
FileInfo parentInfo = fileInfos.Where(n => n.Name.StartsWith("MX") && n.Extension.Equals(".xlsx", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (parentInfo != null)
{
FrmBillInfo frmBillInfo = new FrmBillInfo();
DataTable parentTable = ExcelToDatatable(parentInfo.FullName, Model, "", 5);
parentTable = parentTable.Rows.Cast<DataRow>().Where(n => int.TryParse(n["序号"] + "", out _) && !string.IsNullOrEmpty(n["图号"] + "")).ToList().CopyToDataTable();
DataTable childTable = new DataTable();
foreach (GridColumn gridColumn in frmBillInfo.GcMainView.Columns)
{
if (!childTable.Columns.Contains(gridColumn.FieldName))
{
childTable.Columns.Add(gridColumn.FieldName, gridColumn.ColumnType);
}
}
foreach (DataRow parentRow in parentTable.Rows)
{
string code = parentRow["图号"] + "";
FileInfo childInfo = fileInfos.Where(n => n.Name.StartsWith(code)).FirstOrDefault();
if (childInfo != null && (childInfo.Name.StartsWith("QD") || childInfo.Name.StartsWith("ZZQD")))
{
DataRow childRow = childTable.NewRow();
childRow["ProductId"] = parentRow["图号"] + "";
childRow["appellation"] = parentRow["名称"] + "";
childRow["bak"] = parentRow["备注"] + "";
childRow["tamount"] = parentRow["套数"] + "";
childRow["amount"] = parentRow["套数"] + "";
childTable.Rows.Add(childRow);
}
}
StringBuilder stringBuilder = new StringBuilder();
foreach (DataRow dataRow in parentTable.Rows)
{
stringBuilder.AppendLine($"if not exists (select productid from p_ProductTab where productid = '{dataRow[""] + ""}')");
stringBuilder.AppendLine($"begin insert into p_ProductTab (productid,appellation,bak) values ('{(dataRow[""] + "").Replace("'", "''")}','{(dataRow[""] + "").Replace("'", "''")}','{(dataRow[""] + "").Replace("'", "''")}') end;");
string code = (dataRow["图号"] + "").Trim();
if (code.StartsWith("QD") || code.StartsWith("ZZQD"))
{
stringBuilder.AppendLine($"if not exists (select productid from p_ProductTab where productid = '{dataRow[""] + ""}')");
stringBuilder.AppendLine($"begin insert into p_ProductTab (productid,appellation,bak) values ('{(dataRow[""] + "").Replace("'", "''")}','{(dataRow[""] + "").Replace("'", "''")}','{(dataRow[""] + "").Replace("'", "''")}') end;");
}
}
if (!string.IsNullOrEmpty(stringBuilder.ToString()))
{
SqlHelper.ExecuteNonQuery(stringBuilder.ToString());
}
frmBillInfo.OnControlSourceBind += (s, args) =>
{
Task task = new Task(() =>
{
try
{
SetProcessBar(0, 0);
SetProcessMsg($"正在导入:{Path.GetFileName(parentInfo.FullName)}");
SetProcessBar(20, 0);
frmBillInfo.GcMainView.GridControl.DataSource = childTable;
SetProcessBar(40, 0);
string controlSql = "select '' as billdocument_id,'0' affirmer,'0' rtagid,'{材料代码}' as productid";
frmBillInfo.InitializeHeader(Model.MaintabFocusedRow, controlSql);
SetProcessBar(60, 0);
frmBillInfo.BillSave();
SetProcessBar(80, 0);
frmBillInfo.OnBillAdd();
string productId = Model.MaintabFocusedRow["材料代码"] + "";
string billdocumentId = Model.MaintabFocusedRow["单据编号"] + "";
string idValue = BaseImpl.GetResult($"select top 1 billdocument_id from bom_billListTab where ParentProduct = '{productId}'") + "";
string updateParentVerSql = @"update bom_BillListTab set Ver = '{0}' where Billdocument_Id='{1}' and ProductId='{0}'";
SqlHelper.ExecuteNonQuery(string.Format(updateParentVerSql, idValue, billdocumentId));
//上传文件
bool isUpload = UploadFile(parentInfo.FullName, idValue, out string msg);
SetProcessBar(100, (1 / (parentTable.Rows.Count + 1)) * 100);
SetProcessLabelAllMsg($"总进度:{1}/{parentTable.Rows.Count + 1}");
int.TryParse(Model.FirstRowIndex, out int index);
frmMain.FirstRowIndex = index;
foreach (DataRow dataRow in parentTable.Rows)
{
int childIndex = parentTable.Rows.IndexOf(dataRow);
int nowCount = 1 + childIndex;
int allCount = (int)((decimal)(1 + childIndex) / (parentTable.Rows.Count + 1) * 100);
int finishCount = (int)((decimal)(1 + nowCount) / (parentTable.Rows.Count + 1) * 100);
int.TryParse(dataRow["套数"] + "", out int tamount);
string code = (dataRow["图号"] + "").Trim();
FileInfo childInfo = fileInfos.Where(n => n.Name.StartsWith(code)).FirstOrDefault();
if (childInfo != null && (code.StartsWith("QD") || code.StartsWith("ZZQD")))
{
SetProcessBar(0, allCount);
SetProcessMsg($"正在导入:{Path.GetFileName(childInfo.FullName)}");
DataTable detailTable = new DataTable();
frmMain.Invoke(new Action(() =>
{
detailTable = frmMain.ToExcelDataTable(childInfo.FullName, out int count, out int errorCount, true);
}));
foreach (DataRow detailRow in detailTable.Rows)
{
int.TryParse(detailRow["amount"] + "", out int lastAmount);
detailRow["tamount"] = tamount;
detailRow["amount"] = tamount > 0 ? (lastAmount * tamount) + "" : lastAmount + "";
}
frmBillInfo.GcMainView.GridControl.DataSource = detailTable;
SetProcessBar(20, allCount);
frmBillInfo.InitializeHeader(dataRow, Model.BillMasterSql);
SetProcessBar(40, allCount);
frmBillInfo.BillSave();
SetProcessBar(60, allCount);
frmBillInfo.OnBillAdd();
string updateChildVerSql = @"update bom_BillListTab set Ver = (select top 1 billdocument_id from bom_billListTab where ParentProduct = '{0}') where Billdocument_Id = (select top 1 billdocument_id from bom_billListTab where ParentProduct = '{1}') and ProductId='{0}'";
SqlHelper.ExecuteNonQuery(string.Format(updateChildVerSql, code, productId));
SetProcessBar(80, allCount);
isUpload = UploadFile(childInfo.FullName, idValue, out msg);
SetProcessBar(100, finishCount);
SetProcessLabelAllMsg($"总进度:{nowCount + 1}/{parentTable.Rows.Count + 1}");
}
else
{
isUpload = UploadFile(childInfo.FullName, idValue, out msg);
SetProcessBar(100, finishCount);
SetProcessLabelAllMsg($"总进度:{nowCount + 1}/{parentTable.Rows.Count + 1}");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.DialogResult = DialogResult.OK;
this.Close();
ImportTimer?.Dispose();
}
});
task.Start();
};
frmBillInfo.OnLoad(Model);
frmMain.ParentView = frmBillInfo.GcMainView;
}
else
{
throw new Exception("未包含结构文件");
}
}
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
this.DialogResult = DialogResult.OK;
Close();
ImportTimer?.Dispose();
}
}
/// <summary>
/// 开始导入
/// </summary>
/// <param name="dirPaths"></param>
public void StartImportInMxFile(string dirPath)
{
try
{
Model = frmMain.Model;
FileInfo parentInfo = new FileInfo(dirPath);
if (parentInfo != null && parentInfo.Name.Trim().StartsWith("MX"))
{
if (Directory.Exists("BomImportTemp"))
{
Directory.Delete("BomImportTemp", true);
}
Directory.CreateDirectory("BomImportTemp");
DataTable parentTable = ExcelToDatatable(parentInfo.FullName, Model, "", 5);
parentTable = parentTable.Rows.Cast<DataRow>().Where(n => int.TryParse(n["序号"] + "", out _) && !string.IsNullOrEmpty(n["图号"] + "")).ToList().CopyToDataTable();
Dictionary<string, string> childFileDic = new Dictionary<string, string>();
foreach (DataRow parentRow in parentTable.Rows)
{
string fileName = (parentRow["图号"] + "").Trim();
if (fileName.StartsWith("QD") || fileName.StartsWith("ZZQD"))
{
string fileSql = $"select webpath,sName from P_fm_FileTab where sName like '{fileName}%'";
DataRow fileRow = BaseImpl.GetDataRowResult(fileSql);
string webpath = fileRow != null ? fileRow["webpath"] + "" : "";
string sName = fileRow != null ? fileRow["sName"] + "" : "";
if (string.IsNullOrEmpty(webpath))
{
throw new Exception($"Bom明细附件{fileName}不存在\r\nBom导入失败");
}
else
{
using (WebClient webClient = new WebClient())
{
try
{
webpath = webpath.StartsWith("/") ? webpath : $"/{webpath}";
string url = $"{SystemInfo.Instance.OAUrl}/{webpath}";
string tempFileName = $"BomImportTemp/{sName}";
webClient.DownloadFile(url, tempFileName);
childFileDic.Add(fileName, tempFileName);
}
catch (Exception)
{
throw new Exception($"Bom明细附件{fileName}获取失败\r\nBom导入失败");
}
}
}
}
}
StringBuilder stringBuilder = new StringBuilder();
foreach (DataRow dataRow in parentTable.Rows)
{
string code = (dataRow["图号"] + "").Trim();
if (code.StartsWith("QD") || code.StartsWith("ZZQD"))
{
stringBuilder.AppendLine($"if not exists (select productid from p_ProductTab where productid = '{dataRow[""] + ""}')");
stringBuilder.AppendLine($"begin insert into p_ProductTab (productid,appellation,bak) values ('{(dataRow[""] + "").Replace("'", "''")}','{(dataRow[""] + "").Replace("'", "''")}','{(dataRow[""] + "").Replace("'", "''")}') end;");
}
}
if (!string.IsNullOrEmpty(stringBuilder.ToString()))
{
@@ -93,29 +292,28 @@ namespace Lskj.PubBomImport
childTable.Columns.Add(gridColumn.FieldName, gridColumn.ColumnType);
}
}
List<DataRow> deleteRows = new List<DataRow>();
foreach (DataRow parentRow in parentTable.Rows)
{
string code = parentRow["图号"] + "";
FileInfo childInfo = fileInfos.Where(n => n.Name.Contains(code) && n.Extension.Equals(".xlsx", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (childInfo != null)
string code = (parentRow["图号"] + "").Trim();
if (code.StartsWith("QD") || code.StartsWith("ZZQD"))
{
DataRow childRow = childTable.NewRow();
childRow["ProductId"] = parentRow["图号"] + "";
childRow["appellation"] = parentRow["名称"] + "";
childRow["bak"] = parentRow["备注"] + "";
childRow["tamount"] = parentRow["套数"] + "";
childRow["amount"] = parentRow["套数"] + "";
childTable.Rows.Add(childRow);
string tempFileName = childFileDic[code];
FileInfo childInfo = new FileInfo(tempFileName);
if (childInfo != null)
{
DataRow childRow = childTable.NewRow();
childRow["ProductId"] = parentRow["图号"] + "";
childRow["appellation"] = parentRow["名称"] + "";
childRow["bak"] = parentRow["备注"] + "";
childRow["tamount"] = parentRow["套数"] + "";
childRow["amount"] = parentRow["套数"] + "";
childTable.Rows.Add(childRow);
}
else
{
throw new Exception($"Bom明细附件{code}获取失败\r\nBom导入失败");
}
}
else
{
deleteRows.Add(parentRow);
}
}
foreach (DataRow item in deleteRows)
{
parentTable.Rows.Remove(item);
}
SetProcessBar(20, 0);
frmBillInfo.GcMainView.GridControl.DataSource = childTable;
@@ -144,45 +342,50 @@ namespace Lskj.PubBomImport
int allCount = (int)((decimal)(1 + childIndex) / (parentTable.Rows.Count + 1) * 100);
int finishCount = (int)((decimal)(1 + nowCount) / (parentTable.Rows.Count + 1) * 100);
int.TryParse(dataRow["套数"] + "", out int tamount);
string code = dataRow["图号"] + "";
FileInfo childInfo = fileInfos.Where(n => n.Name.Contains(code) && n.Extension.Equals(".xlsx", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (childInfo != null)
string code = (dataRow["图号"] + "").Trim();
if (code.StartsWith("QD") || code.StartsWith("ZZQD"))
{
SetProcessBar(0, allCount);
SetProcessMsg($"正在导入:{Path.GetFileName(childInfo.FullName)}");
DataTable detailTable = new DataTable();
frmMain.Invoke(new Action(() =>
string tempFileName = childFileDic[code];
FileInfo childInfo = new FileInfo(tempFileName);
if (childInfo != null)
{
detailTable = frmMain.ToExcelDataTable(childInfo.FullName, out int count, out int errorCount, true);
}));
foreach (DataRow detailRow in detailTable.Rows)
{
int.TryParse(detailRow["amount"] + "", out int lastAmount);
detailRow["tamount"] = tamount;
detailRow["amount"] = tamount > 0 ? (lastAmount * tamount) + "" : lastAmount + "";
SetProcessBar(0, allCount);
SetProcessMsg($"正在导入:{Path.GetFileName(childInfo.FullName)}");
DataTable detailTable = new DataTable();
frmMain.Invoke(new Action(() =>
{
detailTable = frmMain.ToExcelDataTable(childInfo.FullName, out int count, out int errorCount, true);
}));
foreach (DataRow detailRow in detailTable.Rows)
{
int.TryParse(detailRow["amount"] + "", out int lastAmount);
detailRow["tamount"] = tamount;
detailRow["amount"] = tamount > 0 ? (lastAmount * tamount) + "" : lastAmount + "";
}
frmBillInfo.GcMainView.GridControl.DataSource = detailTable;
SetProcessBar(20, allCount);
frmBillInfo.InitializeHeader(dataRow, Model.BillMasterSql);
SetProcessBar(40, allCount);
frmBillInfo.BillSave();
SetProcessBar(60, allCount);
frmBillInfo.OnBillAdd();
string updateChildVerSql = @"update bom_BillListTab set Ver = (select top 1 billdocument_id from bom_billListTab where ParentProduct = '{0}') where Billdocument_Id = (select top 1 billdocument_id from bom_billListTab where ParentProduct = '{1}') and ProductId='{0}'";
SqlHelper.ExecuteNonQuery(string.Format(updateChildVerSql, code, productId));
SetProcessBar(80, allCount);
//上传文件
//if (Model.ShowType.Equals("1"))
//{
// isUpload = UploadFile(childInfo.FullName, idValue, out msg);
//}
SetProcessBar(100, finishCount);
SetProcessLabelAllMsg($"总进度:{nowCount + 1}/{parentTable.Rows.Count + 1}");
}
frmBillInfo.GcMainView.GridControl.DataSource = detailTable;
SetProcessBar(20, allCount);
frmBillInfo.InitializeHeader(dataRow, Model.BillMasterSql);
SetProcessBar(40, allCount);
frmBillInfo.BillSave();
SetProcessBar(60, allCount);
frmBillInfo.OnBillAdd();
string updateChildVerSql = @"update bom_BillListTab set Ver = (select top 1 billdocument_id from bom_billListTab where ParentProduct = '{0}') where Billdocument_Id = (select top 1 billdocument_id from bom_billListTab where ParentProduct = '{1}') and ProductId='{0}'";
SqlHelper.ExecuteNonQuery(string.Format(updateChildVerSql, code, productId));
SetProcessBar(80, allCount);
//上传文件
if (Model.ShowType.Equals("1"))
else
{
isUpload = UploadFile(childInfo.FullName, idValue, out msg);
throw new Exception($"Bom明细附件{code}获取失败\r\nBom导入失败");
SetProcessBar(100, finishCount);
SetProcessLabelAllMsg($"总进度:{nowCount + 1}/{parentTable.Rows.Count + 1}");
}
SetProcessBar(100, finishCount);
SetProcessLabelAllMsg($"总进度:{nowCount + 1}/{parentTable.Rows.Count + 1}");
}
else
{
SetProcessBar(100, finishCount);
SetProcessLabelAllMsg($"总进度:{nowCount + 1}/{parentTable.Rows.Count + 1}");
}
}
}
@@ -204,7 +407,8 @@ namespace Lskj.PubBomImport
}
else
{
MessageUtil.Show("未包含结构文件");
MessageUtil.Show("主文件不符合规则");
this.DialogResult = DialogResult.Cancel;
Close();
ImportTimer?.Dispose();
}
@@ -212,6 +416,7 @@ namespace Lskj.PubBomImport
catch (Exception ex)
{
MessageUtil.Show(ex.Message);
this.DialogResult = DialogResult.Cancel;
Close();
ImportTimer?.Dispose();
}