diff --git a/插件库/Lskj.PubBomImport/FrmMain.cs b/插件库/Lskj.PubBomImport/FrmMain.cs index a4960ee..bec5c74 100644 --- a/插件库/Lskj.PubBomImport/FrmMain.cs +++ b/插件库/Lskj.PubBomImport/FrmMain.cs @@ -184,7 +184,7 @@ namespace Lskj.PubBomImport this.Close(); } } - else if (Model.ShowType.Equals("3")) + else if (Model.ShowType.Equals("3"))//新增多个清单文件 { BomImpRtnModel bomImpRtnModel = new BomImpRtnModel() { @@ -199,9 +199,9 @@ namespace Lskj.PubBomImport ReturnTag = bomImpRtnModel }; this.Tag = dialogModel; - if (StaticControl.RightMenuGridView.Tag is Dictionary filesDic) + DataTable sourceTable = StaticControl.RightMenuGridView.GridControl.DataSourceTable(); + if (sourceTable.Columns.Contains("BomFieldNameDir")) { - DataTable sourceTable = StaticControl.RightMenuGridView.GridControl.DataSourceTable(); List selectRows = new List(); foreach (DataRow sourceRow in sourceTable.Rows) { @@ -210,7 +210,7 @@ namespace Lskj.PubBomImport continue; } string fileName = sourceRow[Model.AfterBomFieldName] + ""; - string dirPath = filesDic.ContainsKey(sourceRow) ? filesDic[sourceRow] : ""; + string dirPath = sourceRow["BomFieldNameDir"] + ""; FileInfo parentInfo = null; if (string.IsNullOrEmpty(dirPath)) { @@ -230,7 +230,7 @@ namespace Lskj.PubBomImport frmProgress.ImportTimer.Tick += (ts, te) => { frmProgress.ImportTimer.Enabled = false; - frmProgress.StartImportInQDFile(selectRows, filesDic); + frmProgress.StartImportInQDFile(selectRows); }; frmProgress.ImportTimer.Enabled = true; DialogResult dialogResult = frmProgress.ShowDialog(); @@ -247,32 +247,32 @@ namespace Lskj.PubBomImport this.Close(); } } + else if (Model.ShowType.Equals("4"))//新增或修改单个清单文件 + { + 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.StartImportInQDFile(dirPath); + }; + frmProgress.ImportTimer.Enabled = true; + frmProgress.ShowDialog(); + frmProgress.Dispose(); + } + else + { + this.Close(); + } + } } - //else if (Model.ShowType.Equals("4"))//禁用 - //{ - // 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.StartImportInMxFileNew(dirPath); - // }; - // frmProgress.ImportTimer.Enabled = true; - // frmProgress.ShowDialog(); - // frmProgress.Dispose(); - // } - // else - // { - // this.Close(); - // } - //} catch (Exception ex) { MessageUtil.Show(ex.Message); diff --git a/插件库/Lskj.PubBomImport/FrmProgress.cs b/插件库/Lskj.PubBomImport/FrmProgress.cs index fc0e432..b347c91 100644 --- a/插件库/Lskj.PubBomImport/FrmProgress.cs +++ b/插件库/Lskj.PubBomImport/FrmProgress.cs @@ -513,7 +513,7 @@ namespace Lskj.PubBomImport /// 开始导入3 /// /// - public void StartImportInQDFile(List selectRows, Dictionary filesDic) + public void StartImportInQDFile(List selectRows) { try { @@ -524,7 +524,7 @@ namespace Lskj.PubBomImport continue; } string fileName = sourceRow[Model.AfterBomFieldName] + ""; - string dirPath = filesDic.ContainsKey(sourceRow) ? filesDic[sourceRow] : ""; + string dirPath = sourceRow["BomFieldNameDir"] + ""; FileInfo parentInfo = null; if (string.IsNullOrEmpty(dirPath)) { @@ -571,7 +571,7 @@ namespace Lskj.PubBomImport int allCount = (int)((decimal)(childIndex) / (selectRows.Count) * 100); int finishCount = (int)((decimal)(1 + childIndex) / (selectRows.Count) * 100); string fileName = sourceRow[Model.AfterBomFieldName] + ""; - string dirPath = filesDic.ContainsKey(sourceRow) ? filesDic[sourceRow] : ""; + string dirPath = sourceRow["BomFieldNameDir"] + ""; FileInfo parentInfo = null; if (string.IsNullOrEmpty(dirPath)) { @@ -716,135 +716,161 @@ namespace Lskj.PubBomImport } } /// - /// 开始导入 + /// 开始导入3 /// /// - public void StartImportInMxFileNew(string dirPath) + public void StartImportInQDFile(string dirPath) { try { Model = frmMain.Model; - FileInfo parentInfo = new FileInfo(dirPath); - if (parentInfo != null && parentInfo.Name.Trim().StartsWith("MX")) + string pattern = @"([A-Za-z]+)(\d*)$"; + //原物料名称 + string productCode = Model.MaintabFocusedRow != null && Model.MaintabFocusedRow.Table.Columns.Contains("qms_laddf_productid") ? Model.MaintabFocusedRow["qms_laddf_productid"] + "" : ""; + Match match = Regex.Match(productCode, pattern); + if (match.Success) { - DataTable parentTable = ExcelToDatatable(parentInfo.FullName, Model, "", 5); - parentTable = parentTable.Rows.Cast().Where(n => int.TryParse(n["序号"] + "", out _) && !string.IsNullOrEmpty(n["图号"] + "")).ToList().CopyToDataTable(); - Dictionary childFileDic = new Dictionary(); - foreach (DataRow parentRow in parentTable.Rows) + string sourceModel = match.Groups[1].Value; + string sourceVersion = match.Groups[2].Value; + sourceVersion = string.IsNullOrEmpty(sourceVersion) ? "0" : sourceVersion; + //修改后的文件名 + string newProductCode = Model.MaintabFocusedRow != null && Model.MaintabFocusedRow.Table.Columns.Contains("qms_laddf_appellationV123") ? Model.MaintabFocusedRow["qms_laddf_appellationV123"] + "" : ""; + FileInfo parentInfo = new FileInfo(dirPath); + if (parentInfo != null && (parentInfo.Name.Trim().StartsWith("QD") || parentInfo.Name.Trim().StartsWith("ZZQD"))) { - string fileName = (parentRow["图号"] + "").Trim(); - if (fileName.StartsWith("QD") || fileName.StartsWith("ZZQD")) + string code = ""; + string name = ""; + string newcode = newProductCode.Trim(); + string newname = ""; + string namepattern = @"^(.+?)\s+(.+)$"; + Match newProductCodematch = Regex.Match(Path.GetFileNameWithoutExtension(newProductCode).Trim(), namepattern); + Match namematch = Regex.Match(Path.GetFileNameWithoutExtension(parentInfo.Name).Trim(), namepattern); + if (namematch.Success) { - 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)) + code = namematch.Groups[1].Value; + name = namematch.Groups[2].Value; + } + if (newProductCodematch.Success) + { + newcode = newProductCodematch.Groups[1].Value; + newname = newProductCodematch.Groups[2].Value; + } + if (!newcode.Equals(code))//选择的文件和版本文件名不匹配 + { + throw new Exception($"选择的文件名{code}和数据文件名{newProductCode}不匹配"); + } + match = Regex.Match(code, pattern); + if (match.Success) + { + DataTable historyTable = BaseImpl.GetDataTableResult($"select * from bom_BillListlogTab where ParentProduct = '{productCode}' and historyVer = '{code}'"); + if (historyTable != null && historyTable.Rows.Count > 0) { - throw new Exception($"Bom明细附件{fileName}不存在\r\nBom导入失败"); + throw new Exception($"版本{code}已存在"); } - else + string newModel = match.Groups[1].Value; + string newVersion = match.Groups[2].Value; + newVersion = string.IsNullOrEmpty(newVersion) ? "0" : newVersion; + //新建或修改清单 + StringBuilder stringBuilder = new StringBuilder(); + if (code.StartsWith("QD") || code.StartsWith("ZZQD")) { - using (WebClient webClient = new WebClient()) + stringBuilder.AppendLine($"if not exists (select productid from p_ProductTab where productid = '{code}')"); + stringBuilder.AppendLine($"begin insert into p_ProductTab (productid,appellation,bak,SpeciesNo) values ('{code.Replace("'", "''")}','{name.Replace("'", "''")}','','0199') end;"); + } + if (!string.IsNullOrEmpty(stringBuilder.ToString())) + { + SqlHelper.ExecuteNonQuery(stringBuilder.ToString()); + } + //更新并备份清单 + FrmBillInfo frmBillInfo = new FrmBillInfo(); + frmBillInfo.OnControlSourceBind += (s, args) => + { + Task task = new Task(() => { try { - webpath = webpath.StartsWith("/") ? webpath : $"/{webpath}"; - string url = $"{SystemInfo.Instance.OAUrl}/{webpath}"; - string tempFileName = $"BomImportTemp/{sName}"; - webClient.DownloadFile(url, tempFileName); - childFileDic.Add(fileName, tempFileName); + int.TryParse(Model.FirstRowIndex, out int index); + frmMain.FirstRowIndex = index; + if (code.StartsWith("QD") || code.StartsWith("ZZQD")) + { + FileInfo childInfo = new FileInfo(dirPath); + if (childInfo != null) + { + frmBillInfo.OnBillAdd(); + SetProcessBar(0, 0); + 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"] = 1; + detailRow["amount"] = lastAmount; + } + SetProcessBar(20, 0); + string billMasterSql = $"select (select top 1 billdocument_id from bom_BillTab where productid='{productCode}') as billdocument_id,'0' affirmer,'0' rtagid,'{productCode}' as productid"; + billMasterSql = ReplaceHelper.ReplaceRowParam(Model.MaintabFocusedRow, billMasterSql); + // 处理单据表头数据 + DataRow masterRow = BillImpl.GetDataRowResult(billMasterSql); + SetProcessBar(40, 0); + frmBillInfo.AddOrUpdateBill(masterRow, detailTable); + SetProcessBar(60, 0); + //备份Bom清单明细 + string bakBomListSql = @"delete bom_BillListlogTab where parentProduct = '{0}' and historyVer = (select top 1 historyVer from bom_BillListTab where parentProduct = '{0}');insert into bom_BillListlogTab select * from bom_BillListTab where parentProduct = '{0}';"; + SqlHelper.ExecuteNonQuery(string.Format(bakBomListSql, productCode)); + string billNo = ""; + frmBillInfo.BillSave(ref billNo); + SetProcessBar(80, 0); + frmBillInfo.OnBillAdd(); + //记录历史版本号 + string updateHistoryVerSql = @"update bom_BillListTab set historyVer = '{0}' where Billdocument_Id='{1}'"; + SqlHelper.ExecuteNonQuery(string.Format(updateHistoryVerSql, code, billNo)); + //上传文件 + bool isUpload = UploadFile(childInfo.FullName, billNo, out string msg); + SetProcessBar(100, 100); + SetProcessLabelAllMsg($"总进度:1/1"); + } + else + { + throw new Exception($"Bom明细获取失败\r\nBom导入失败"); + } + } } - catch (Exception) + catch (Exception ex) { - throw new Exception($"Bom明细附件{fileName}获取失败\r\nBom导入失败"); + MessageUtil.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($"文件名{code}不符合规则"); } } - FrmBillInfo frmBillInfo = new FrmBillInfo(); - frmBillInfo.OnControlSourceBind += (s, args) => + else { - Task task = new Task(() => - { - try - { - SetProcessBar(0, 0); - SetProcessMsg($"正在导入:{Path.GetFileName(parentInfo.FullName)}"); - 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["图号"] + "").Trim(); - if (code.StartsWith("QD") || code.StartsWith("ZZQD")) - { - 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导入失败"); - } - } - } - 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); - string billNo = ""; - frmBillInfo.BillSave(ref billNo); - 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, 100); - SetProcessLabelAllMsg($"总进度:1/1"); - } - catch (Exception ex) - { - MessageUtil.Show(ex.Message); - } - finally - { - this.DialogResult = DialogResult.OK; - this.Close(); - ImportTimer?.Dispose(); - } - }); - task.Start(); - }; - frmBillInfo.OnLoad(Model); - frmMain.ParentView = frmBillInfo.GcMainView; + MessageUtil.Show("不是QD或ZZQD文件"); + this.DialogResult = DialogResult.Cancel; + Close(); + ImportTimer?.Dispose(); + } } else { - MessageUtil.Show("不是MX文件"); - this.DialogResult = DialogResult.Cancel; - Close(); - ImportTimer?.Dispose(); + throw new Exception($"文件名{productCode}不符合规则"); } } catch (Exception ex)