private void Export() { try { ClearStatusMessage(); List <Section> list = null; var serverPath = string.Empty; var task = ServiceProxyFactory.LifeTime(async factory => { serverPath = await GetServerPath(); }); Task <List <Section> > loadTask = LoadGridAsync(); ProgressDialog.Start(ParentForm, Task.WhenAll(task, loadTask), false, SessionKey); list = loadTask.Result; if (!list.Any()) { ShowWarningDialog(MsgWngNoExportData); return; } if (!Directory.Exists(serverPath)) { serverPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } var filePath = string.Empty; var fileName = $"入金部門マスター{DateTime.Today:yyyyMMdd}.csv"; if (!ShowSaveExportFileDialog(serverPath, fileName, out filePath)) { return; } var definition = new SectionFileDefinition(new DataExpression(ApplicationControl)); var exporter = definition.CreateExporter(); exporter.UserId = Login.UserId; exporter.UserCode = Login.UserCode; exporter.CompanyId = CompanyId; exporter.CompanyCode = Login.CompanyCode; ProgressDialog.Start(ParentForm, (cancel, progress) => { return(exporter.ExportAsync(filePath, list, cancel, progress)); }, true, SessionKey); if (exporter.Exception != null) { NLogHandler.WriteErrorLog(this, exporter.Exception, SessionKey); ShowWarningDialog(MsgErrExportError); return; } DispStatusMessage(MsgInfFinishExport); } catch (Exception ex) { Debug.Fail(ex.ToString()); NLogHandler.WriteErrorLog(this, ex, SessionKey); DispStatusMessage(MsgErrExportError); } }
/// <summary>インポート処理</summary> /// <param name="source"></param> /// <param name="token"></param> /// <returns></returns> public async Task <ImportResult> ImportAsync(MasterImportSource source, CancellationToken token = default(CancellationToken)) { var mode = (ImportMethod)source.ImportMethod; var encoding = Encoding.GetEncoding(source.EncodingCodePage); var csv = encoding.GetString(source.Data); var companyTask = companyProcessor.GetAsync(new CompanySearch { Id = source.CompanyId, }, token); var loginUserTask = loginUserProcessor.GetAsync(new LoginUserSearch { Ids = new[] { source.LoginUserId }, }, token); var appConTask = applicationControlProcessor.GetAsync(source.CompanyId, token); var sectionTask = sectionProcessor.GetAsync(new SectionSearch { CompanyId = source.CompanyId, }, token); await Task.WhenAll(companyTask, loginUserTask, appConTask); var company = companyTask.Result.First(); var loginUser = loginUserTask.Result.First(); var appCon = appConTask.Result; var sectionDictionary = sectionTask.Result.ToDictionary(x => x.Code); var definition = new SectionFileDefinition(new DataExpression(appCon)); var parser = new CsvParser { Encoding = encoding, StreamCreator = new PlainTextMemoryStreamCreator(), }; var importer = definition.CreateImporter(x => x.Code, parser); importer.UserId = source.LoginUserId; importer.UserCode = loginUser.Code; importer.CompanyId = source.CompanyId; importer.CompanyCode = company.Code; importer.AdditionalWorker = async worker => { var codes = worker.Models.Values.Select(x => x.Code).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToArray(); if (mode == ImportMethod.Replace) { var bankAccountTask = sectionProcessor.GetImportItemsBankAccountAsync(source.CompanyId, codes, token); var swdTask = sectionProcessor.GetImportItemsSectionWithDepartmentAsync(source.CompanyId, codes, token); var swlTask = sectionProcessor.GetImportItemsSectionWithLoginUserAsync(source.CompanyId, codes, token); var receiptTask = sectionProcessor.GetImportItemsReceiptAsync(source.CompanyId, codes, token); var nettingTask = sectionProcessor.GetImportItemsNettingAsync(source.CompanyId, codes, token); await Task.WhenAll(bankAccountTask, swdTask, swlTask, receiptTask, nettingTask); (worker.RowDef as SectionFileDefinition).SectionCodeField.ValidateAdditional = (val, param) => { var reports = new List <WorkingReport>(); reports.AddRange(bankAccountTask.Result.Select(x => new WorkingReport { FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"銀行口座マスターに存在する{x.Code}:{x.Name}が存在しないため、インポートできません。", }).ToArray()); reports.AddRange(swdTask.Result.Select(x => new WorkingReport { FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"入金・請求部門対応マスターに存在する{x.Code}:{x.Name}が存在しないため、インポートできません。", }).ToArray()); reports.AddRange(swlTask.Result.Select(x => new WorkingReport { FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"入金部門・担当者対応マスターに存在する{x.Code}:{x.Name}が存在しないため、インポートできません。", }).ToArray()); reports.AddRange(receiptTask.Result.Select(x => new WorkingReport { FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"入金データに存在する{x.Code}:{x.Name}が存在しないため、インポートできません。", }).ToArray()); reports.AddRange(nettingTask.Result.Select(x => new WorkingReport { FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"相殺データに存在する{x.Code}:{x.Name}が存在しないため、インポートできません。", }).ToArray()); return(reports); }; } var payerCodes = worker.Models.Values.Where(x => !string.IsNullOrEmpty(x.PayerCodeLeft) && !string.IsNullOrEmpty(x.PayerCodeRight)) .Select(x => x.PayerCodeLeft + x.PayerCodeRight).Distinct().ToArray(); var sections = (await sectionProcessor.GetAsync(new SectionSearch { CompanyId = source.CompanyId, PayerCodes = payerCodes, }, token)).ToList(); (worker.RowDef as SectionFileDefinition).PayerCodeLeftField.ValidateAdditional = (val, param) => { var reports = new List <WorkingReport>(); var def = (worker.RowDef as SectionFileDefinition); var uniqueKeys = new Dictionary <string, int>(); var duplicatedLines = new List <int>(); foreach (var pair in val) { var leftIsEmpty = string.IsNullOrEmpty(pair.Value.PayerCodeLeft); var rightIsEmpty = string.IsNullOrEmpty(pair.Value.PayerCodeRight); if (leftIsEmpty && rightIsEmpty) { continue; } if (leftIsEmpty ^ rightIsEmpty) { var field = leftIsEmpty ? def.PayerCodeLeftField : def.PayerCodeRightField; reports.Add(new WorkingReport { LineNo = pair.Key, FieldNo = field.FieldIndex, FieldName = field.FieldName, Message = "仮想支店コード・仮想口座番号のどちらかが未入力のため、インポートできません。", }); continue; } var payerCode = pair.Value.PayerCodeLeft + pair.Value.PayerCodeRight; if (mode == ImportMethod.InsertOnly) { if (sections.Any(x => x.PayerCode == payerCode)) { reports.Add(new WorkingReport { LineNo = pair.Key, FieldNo = def.PayerCodeLeftField.FieldIndex, FieldName = "仮想支店コード、仮想口座番号", Message = "既に登録されている仮想支店コード、仮想口座番号のため、インポートできません。", Value = payerCode, }); } } else if (mode == ImportMethod.InsertAndUpdate) { // bug : 更新処理で、振込依頼人番号 の シャッフルができない if (sections.Any(x => x.PayerCode == payerCode && x.Code != pair.Value.Code)) { reports.Add(new WorkingReport { LineNo = pair.Key, FieldNo = def.PayerCodeLeftField.FieldIndex, FieldName = "仮想支店コード、仮想口座番号", Message = "既に登録されている仮想支店コード、仮想口座番号のため、インポートできません。", Value = payerCode, }); } } if (uniqueKeys.ContainsKey(payerCode)) { var line = uniqueKeys[payerCode]; if (!duplicatedLines.Contains(line)) { duplicatedLines.Add(line); } duplicatedLines.Add(pair.Key); } else { uniqueKeys.Add(payerCode, pair.Key); } } foreach (var line in duplicatedLines) { reports.Add(new WorkingReport { LineNo = line, FieldNo = def.PayerCodeLeftField.FieldIndex, FieldName = "仮想支店コード、仮想口座番号", Message = "仮想支店コード、仮想口座番号が重複しているため、インポートできません。", Value = val[line].PayerCodeLeft + val[line].PayerCodeRight, }); } return(reports); }; }; importer.LoadAsync = () => sectionProcessor.GetAsync(new SectionSearch { CompanyId = source.CompanyId, }, token); importer.RegisterAsync = x => sectionProcessor.ImportAsync(x.New, x.Dirty, x.Removed, token); var result = await importer.ImportAsync(csv, mode, token, null); result.Logs = importer.GetErrorLogs(); return(result); }
private void Import() { ClearStatusMessage(); try { ImportSetting importSetting = null; var task = Util.GetMasterImportSettingAsync(Login, ImportFileType.Section); ProgressDialog.Start(ParentForm, task, false, SessionKey); importSetting = task.Result; var definition = new SectionFileDefinition(new DataExpression(ApplicationControl)); definition.SectionCodeField.ValidateAdditional = (val, param) => { var reports = new List <WorkingReport>(); if (((ImportMethod)param) != ImportMethod.Replace) { return(reports); } if (val.Any(a => a.Value.Code == null)) { return(reports); } if (val.Select(x => x.Value.Code).Distinct().Count() != val.Values.Count) { return(reports); } ServiceProxyFactory.LifeTime(factory => { var section = factory.Create <SectionMasterClient>(); MasterDatasResult resBankAccount = section.GetImportItemsForBankAccount( SessionKey, CompanyId, val.Values.Select(x => x.Code).ToArray()); foreach (MasterData ca in resBankAccount.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code))) { reports.Add(new WorkingReport { LineNo = null, FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"銀行口座マスターに存在する{ca.Code}:{ca.Name}が存在しないため、インポートできません。", }); } MasterDatasResult resSectionWithDept = section.GetImportItemsForSectionWithDepartment( SessionKey, CompanyId, val.Values.Select(x => x.Code).ToArray()); foreach (MasterData item in resSectionWithDept.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code))) { reports.Add(new WorkingReport { LineNo = null, FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"入金・請求部門対応マスターに存在する{item.Code}:{item.Name}が存在しないため、インポートできません。", }); } MasterDatasResult resSectionWithLoginUser = section.GetImportItemsForSectionWithLoginUser( SessionKey, CompanyId, val.Values.Select(l => l.Code).ToArray()); foreach (MasterData item in resSectionWithLoginUser.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code))) { reports.Add(new WorkingReport { LineNo = null, FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"入金部門・担当者対応マスターに存在する{item.Code}:{item.Name}が存在しないため、インポートできません。", }); } MasterDatasResult resReceipt = section.GetImportItemsForReceipt( SessionKey, CompanyId, val.Values.Select(l => l.Code).ToArray()); foreach (MasterData item in resReceipt.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code))) { reports.Add(new WorkingReport { LineNo = null, FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"入金データに存在する{item.Code}:{item.Name}が存在しないため、インポートできません。", }); } MasterDatasResult resNetting = section.GetImportItemsForNetting( SessionKey, CompanyId, val.Values.Select(l => l.Code).ToArray()); foreach (MasterData item in resNetting.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code))) { reports.Add(new WorkingReport { LineNo = null, FieldNo = definition.SectionCodeField.FieldIndex, FieldName = definition.SectionCodeField.FieldName, Message = $"相殺データに存在する{item.Code}:{item.Name}が存在しないため、インポートできません。", }); } }); return(reports); }; definition.PayerCodeLeftField.ValidateAdditional = (val, param) => { var reports = new List <WorkingReport>(); var uniqueKeys = new Dictionary <string, int>(); var duplicatedLines = new List <int>(); SectionsResult res = null; ServiceProxyFactory.LifeTime(factory => { var section = factory.Create <SectionMasterClient>(); res = section.GetImportItemsForSection( SessionKey, CompanyId, val.Values.Select(l => l.PayerCodeLeft + l.PayerCodeRight).ToArray()); }); foreach (var pair in val) { var branchCodeIsEmpty = false; var accountNumberIsEmpty = false; if (pair.Value.PayerCodeLeft == "") { branchCodeIsEmpty = true; } if (pair.Value.PayerCodeRight == "") { accountNumberIsEmpty = true; } if ((branchCodeIsEmpty && !accountNumberIsEmpty) || (!branchCodeIsEmpty && accountNumberIsEmpty)) { StringFieldDefinition <Section> field = null; var value = string.Empty; if (branchCodeIsEmpty) { field = definition.PayerCodeLeftField; value = pair.Value.PayerCodeLeft; } else { field = definition.PayerCodeRightField; value = pair.Value.PayerCodeRight; } reports.Add(new WorkingReport { LineNo = pair.Key, FieldNo = field.FieldIndex, FieldName = field.FieldName, Message = "仮想支店コード・仮想口座番号のどちらかが未入力のため、インポートできません。", }); } if (res.Sections.Count != 0) { if (((ImportMethod)param) == ImportMethod.InsertOnly) { reports.Add(new WorkingReport { LineNo = pair.Key, FieldNo = definition.PayerCodeLeftField.FieldIndex, FieldName = "仮想支店コード、仮想口座番号", Message = "既に登録されている仮想支店コード、仮想口座番号のため、インポートできません。", }); } else { if (res.Sections.Any(p => p.Code != pair.Value.Code && p.PayerCode == pair.Value.PayerCodeLeft + pair.Value.PayerCodeRight && p.PayerCode != "")) { reports.Add(new WorkingReport { LineNo = pair.Key, FieldNo = definition.PayerCodeLeftField.FieldIndex, FieldName = "仮想支店コード、仮想口座番号", Message = "既に登録されている仮想支店コード、仮想口座番号のため、インポートできません。", }); } } } string key = pair.Value.PayerCodeLeft + pair.Value.PayerCodeRight; if (key.Length == 10) { if (string.IsNullOrEmpty(key)) { continue; } if (uniqueKeys.ContainsKey(key)) { var duplicated = uniqueKeys[key]; if (uniqueKeys.ContainsKey(key)) { duplicatedLines.Add(duplicated); } duplicatedLines.Add(pair.Key); } else { uniqueKeys.Add(key, pair.Key); } } } duplicatedLines.ForEach(lineNo => { reports.Add(new WorkingReport() // キー重複 { LineNo = lineNo, FieldNo = definition.PayerCodeLeftField.FieldIndex, FieldName = "仮想支店コード、仮想口座番号", Message = "仮想支店コード、仮想口座番号が重複しているため、インポートできません。", Value = val[lineNo].PayerCodeLeft + val[lineNo].PayerCodeRight, }); }); return(reports); }; var importer = definition.CreateImporter(m => new { m.Code }); importer.UserId = Login.UserId; importer.UserCode = Login.UserCode; importer.CompanyId = CompanyId; importer.CompanyCode = Login.CompanyCode; importer.LoadAsync = async() => await LoadGridAsync(); importer.RegisterAsync = async unitOfWork => await RegisterForImportAsync(unitOfWork); var importResult = DoImport(importer, importSetting); if (!importResult) { return; } Clears(); SectionList.Clear(); Task <List <Section> > loadTask = LoadGridAsync(); ProgressDialog.Start(ParentForm, loadTask, false, SessionKey); SectionList.AddRange(loadTask.Result); grdSectionMaster.DataSource = new BindingSource(SectionList, null); } catch (Exception ex) { Debug.Fail(ex.ToString()); NLogHandler.WriteErrorLog(this, ex, SessionKey); ShowWarningDialog(MsgErrImportErrorWithoutLog); } }