/// <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 loginUsersTask = loginUserProcessor.GetAsync(new LoginUserSearch {
                CompanyId = source.CompanyId,
            }, token);
            var appConTask   = applicationControlProcessor.GetAsync(source.CompanyId, token);
            var customerTask = customerProcessor.GetAsync(new CustomerSearch {
                CompanyId = source.CompanyId
            }, token);
            var departmentTask = departmentProcessor.GetAsync(new DepartmentSearch {
                CompanyId = source.CompanyId,
            }, token);
            var accountTitleTask = accountTitleProcessor.GetAsync(new AccountTitleSearch {
                CompanyId = source.CompanyId,
            }, token);

            await Task.WhenAll(companyTask, loginUsersTask, appConTask, customerTask, departmentTask, accountTitleTask);

            var company             = companyTask.Result.First();
            var loginUserDictionary = loginUsersTask.Result.ToDictionary(x => x.Code);
            var loginUser           = loginUsersTask.Result.First(x => x.Id == source.LoginUserId);
            var appCon                 = appConTask.Result;
            var customerDictionary     = customerTask.Result.ToDictionary(x => x.Code);
            var departmentDictionary   = departmentTask.Result.ToDictionary(x => x.Code);
            var accountTitleDictionary = accountTitleTask.Result.ToDictionary(x => x.Code);

            var definition = new CustomerDiscountFileDefinition(new DataExpression(appCon));
            var parser     = new CsvParser {
                Encoding      = encoding,
                StreamCreator = new PlainTextMemoryStreamCreator(),
            };

            definition.CustomerIdField.GetModelsByCode      = val => customerDictionary;
            definition.DepartmentId1Field.GetModelsByCode   = val => departmentDictionary;
            definition.DepartmentId2Field.GetModelsByCode   = val => departmentDictionary;
            definition.DepartmentId3Field.GetModelsByCode   = val => departmentDictionary;
            definition.DepartmentId4Field.GetModelsByCode   = val => departmentDictionary;
            definition.DepartmentId5Field.GetModelsByCode   = val => departmentDictionary;
            definition.AccountTitleId1Field.GetModelsByCode = val => accountTitleDictionary;
            definition.AccountTitleId2Field.GetModelsByCode = val => accountTitleDictionary;
            definition.AccountTitleId3Field.GetModelsByCode = val => accountTitleDictionary;
            definition.AccountTitleId4Field.GetModelsByCode = val => accountTitleDictionary;
            definition.AccountTitleId5Field.GetModelsByCode = val => accountTitleDictionary;
            definition.Rate1Field.ValidateAdditional        = (val, param) => ValidateRate(val, param, definition.Rate1Field);
            definition.Rate2Field.ValidateAdditional        = (val, param) => ValidateRate(val, param, definition.Rate2Field);
            definition.Rate3Field.ValidateAdditional        = (val, param) => ValidateRate(val, param, definition.Rate3Field);
            definition.Rate4Field.ValidateAdditional        = (val, param) => ValidateRate(val, param, definition.Rate4Field);
            definition.Rate5Field.ValidateAdditional        = (val, param) => ValidateRate(val, param, definition.Rate5Field);

            var importer = definition.CreateImporter(x => x.CustomerCode, parser);

            importer.UserId      = source.LoginUserId;
            importer.UserCode    = loginUser.Code;
            importer.CompanyId   = source.CompanyId;
            importer.CompanyCode = company.Code;

            importer.LoadAsync = () => customerDiscountProcessor.GetItemsAsync(new CustomerSearch {
                CompanyId = source.CompanyId,
            }, token);
            importer.RegisterAsync = x => customerDiscountProcessor.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 ExportDiscount(CustomerSearch customerSearch)
        {
            List <CustomerDiscount> list = null;
            string serverPath            = null;

            var task = ServiceProxyFactory.LifeTime(async factory =>
            {
                serverPath  = await GetServerPath();
                var service = factory.Create <CustomerMasterClient>();
                CustomerDiscountsResult result = await service.GetDiscountItemsAsync(SessionKey, customerSearch);

                if (result.ProcessResult.Result)
                {
                    list = result.CustomerDiscounts;
                }
            });

            ProgressDialog.Start(ParentForm, task, false, SessionKey);

            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 CustomerDiscountFileDefinition(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 <CustomerDiscount>(Login, filePath);
        }