private void Export() { try { ClearStatusMessage(); var loadTask = GetSectionWithDepartmentData(); var pathTask = GetServerPath(); ProgressDialog.Start(ParentForm, Task.WhenAll(loadTask, pathTask), false, SessionKey); var list = loadTask.Result; list = list.OrderBy(x => x.SectionCode).ThenBy(x => x.DepartmentCode).ToList(); if (!list.Any()) { ShowWarningDialog(MsgWngNoExportData, "エクスポート"); return; } string serverPath = pathTask.Result; if (!Directory.Exists(serverPath)) { serverPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } var filePath = string.Empty; string fileName = $"入金・請求部門対応マスター{DateTime.Today:yyyyMMdd}.csv"; if (!ShowSaveExportFileDialog(serverPath, fileName, out filePath)) { return; } var definition = new SectionWithDepartmentFileDefinition( 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); Settings.SavePath <SectionWithDepartment>(Login, filePath); } 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); var departmentTask = departmentProcessor.GetAsync(new DepartmentSearch { CompanyId = source.CompanyId, }, token); await Task.WhenAll(companyTask, loginUserTask, appConTask, sectionTask, departmentTask); var company = companyTask.Result.First(); var loginUser = loginUserTask.Result.First(); var appCon = appConTask.Result; var sectionDictionary = sectionTask.Result.ToDictionary(x => x.Code); var departmentDictionary = departmentTask.Result.ToDictionary(x => x.Code); var definition = new SectionWithDepartmentFileDefinition(new DataExpression(appCon)); var parser = new CsvParser { Encoding = encoding, StreamCreator = new PlainTextMemoryStreamCreator(), }; definition.SectionCodeField.GetModelsByCode = codes => sectionDictionary; definition.DepartmentCodeField.GetModelsByCode = codes => departmentDictionary; var importer = definition.CreateImporter(x => new { x.SectionId, x.DepartmentId }, parser); importer.UserId = source.LoginUserId; importer.UserCode = loginUser.Code; importer.CompanyId = source.CompanyId; importer.CompanyCode = company.Code; importer.AdditionalWorker = async worker => { var dbItems = (await sectionWithDepartmentProcessor.GetAsync(new SectionWithDepartmentSearch { CompanyId = source.CompanyId, }, token)).ToList(); var dbDepartmentCheckList = new SortedList <int, SectionWithDepartment[]>(dbItems.GroupBy(x => x.DepartmentId) .ToDictionary(x => x.Key, x => x.ToArray())); var csvDepartmentCheckList = new SortedList <int, SectionWithDepartment[]>(worker.Models.Values .GroupBy(x => x.DepartmentId) .ToDictionary(x => x.Key, x => x.ToArray())); var def = worker.RowDef as SectionWithDepartmentFileDefinition; def.DepartmentCodeField.ValidateAdditional = (val, param) => { var reports = new List <WorkingReport>(); foreach (var pair in val) { if (IsDuped(dbDepartmentCheckList, pair.Value.DepartmentId, g => g.Any(x => x.SectionId != pair.Value.SectionId))) { reports.Add(new WorkingReport(pair.Key, def.DepartmentCodeField.FieldIndex, def.DepartmentCodeField.FieldName, "他の入金部門に登録されているため、インポートできません。" )); } if (IsDuped(csvDepartmentCheckList, pair.Value.DepartmentId, g => g.Any(x => x.SectionId != pair.Value.SectionId && pair.Value.DepartmentId != 0))) { reports.Add(new WorkingReport(pair.Key, def.DepartmentCodeField.FieldIndex, def.DepartmentCodeField.FieldName, "他の入金部門に登録されているため、インポートできません。" )); } } return(reports); }; }; importer.LoadAsync = () => sectionWithDepartmentProcessor.GetAsync(new SectionWithDepartmentSearch { CompanyId = source.CompanyId, }, token); importer.RegisterAsync = x => sectionWithDepartmentProcessor.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.SectionWithDepartment); // 取込設定取得 ProgressDialog.Start(ParentForm, task, false, SessionKey); importSetting = task.Result; var definition = new SectionWithDepartmentFileDefinition(new DataExpression(ApplicationControl)); definition.SectionCodeField.GetModelsByCode = val => { Dictionary <string, Section> product = null; ServiceProxyFactory.LifeTime(factory => { var section = factory.Create <SectionMasterClient>(); var result = section.GetByCode(SessionKey, CompanyId, val); if (result.ProcessResult.Result) { product = result.Sections .ToDictionary(c => c.Code); } }); return(product ?? new Dictionary <string, Section>()); }; definition.DepartmentCodeField.GetModelsByCode = val => { Dictionary <string, Department> product = null; ServiceProxyFactory.LifeTime(factory => { var department = factory.Create <DepartmentMasterClient>(); var result = department.GetByCode(SessionKey, CompanyId, val); if (result.ProcessResult.Result) { product = result.Departments.ToDictionary(d => d.Code); } }); return(product ?? new Dictionary <string, Department>()); }; definition.DepartmentCodeField.ValidateAdditional = (val, param) => { var reports = new List <WorkingReport>(); Func <SortedList <int, SectionWithDepartment[]>, int, Func <SectionWithDepartment[], bool>, bool> dupeCheck = (list, key, condition) => { if (!list.ContainsKey(key)) { return(false); } return(condition?.Invoke(list[key]) ?? false); }; List <SectionWithDepartment> dbItems = null; ServiceProxyFactory.LifeTime(factory => { var sectionWithDepartment = factory.Create <SectionWithDepartmentMasterClient>(); var sectionWithDepartmentSearch = new SectionWithDepartmentSearch(); dbItems = sectionWithDepartment .GetItems(SessionKey, CompanyId, sectionWithDepartmentSearch) .SectionDepartments; }); var dbDepartmentCheckList = new SortedList <int, SectionWithDepartment[]>(dbItems.GroupBy(x => x.DepartmentId) .ToDictionary(x => x.Key, x => x.ToArray())); var csvDepartmentCheckList = new SortedList <int, SectionWithDepartment[]>(val.Select(x => x.Value) .GroupBy(x => x.DepartmentId) .ToDictionary(x => x.Key, x => x.ToArray())); foreach (var pair in val) { if (dupeCheck(dbDepartmentCheckList, pair.Value.DepartmentId, g => g.Any(x => x.SectionId != pair.Value.SectionId))) { reports.Add(new WorkingReport(pair.Key, definition.DepartmentCodeField.FieldIndex, definition.DepartmentCodeField.FieldName, "他の入金部門に登録されているため、インポートできません。" )); } if (dupeCheck(csvDepartmentCheckList, pair.Value.DepartmentId, g => g.Any(x => x.SectionId != pair.Value.SectionId && pair.Value.DepartmentId != 0))) { reports.Add(new WorkingReport(pair.Key, definition.DepartmentCodeField.FieldIndex, definition.DepartmentCodeField.FieldName, "他の入金部門に登録されているため、インポートできません。" )); } } return(reports); }; var importer = definition.CreateImporter(m => new { m.SectionId, m.DepartmentId }); importer.UserId = Login.UserId; importer.UserCode = Login.UserCode; importer.CompanyId = CompanyId; importer.CompanyCode = Login.CompanyCode; importer.LoadAsync = async() => await GetSectionWithDepartmentData(); importer.RegisterAsync = async unitOfWork => await RegisterForImportAsync(unitOfWork); var importResult = DoImport(importer, importSetting, Clear); } catch (Exception ex) { Debug.Fail(ex.ToString()); NLogHandler.WriteErrorLog(this, ex, SessionKey); ShowWarningDialog(MsgErrImportErrorWithoutLog); } }