コード例 #1
0
        public async Task <ImportResult> ImportCommissions(ScopeOptions scope, Guid commissionStatementId, IEnumerable <ImportCommission> importData)
        {
            var importResult = new ImportResult();

            CommissionsToInsert      = new List <CommissionEntity>();
            CommissionErrorsToInsert = new List <CommissionErrorEntity>();

            //Scope check
            var queryOptions = new CommissionStatementQueryOptions(scope, "", "", 0, 0);

            queryOptions.CommissionStatementId = commissionStatementId;
            var statements = await _commissionStatementService.GetCommissionStatements(queryOptions);

            if (!statements.Items.Any())
            {
                return(importResult);
            }

            var statement = statements.Items.Single();

            var commissionTypes = await _commissionLookupService.GetCommissionTypes();

            var commissionTypesDictionary = BuildCommissionTypesDictionary(commissionTypes);
            var company = await _lookupService.GetCompany(statement.CompanyId);

            var policyQueryOptions = new PolicyQueryOptions(scope, "", "", 0, 0);

            policyQueryOptions.CompanyId.Add(statement.CompanyId);
            var policies         = (await _policyService.GetPolicies(policyQueryOptions)).Items.ToList();
            var policyDictionary = BuildPolicyDictionary(policies, company.CommissionPolicyNumberPrefixes.ToList());

            var commissionSplitRulesQueryOptions = new CommissionSplitRuleQueryOptions(scope, "", "", 0, 0);
            var commissionSplitRules             = (await _commissionSplitService.GetCommissionSplitRules(commissionSplitRulesQueryOptions)).Items.ToList();

            var commissionSplitRulePolicyQueryOptions = new CommissionSplitRulePolicyQueryOptions(scope, "", "", 0, 0);
            var commissionSplitRulePolicies           = (await _commissionSplitRulePolicyService.GetCommissionSplitRulePolicies(commissionSplitRulePolicyQueryOptions)).Items.ToList();

            foreach (var data in importData)
            {
                var result = ImportCommission(scope, statement, data, policyDictionary, commissionTypesDictionary, commissionSplitRules, commissionSplitRulePolicies);

                importResult.Results.Add(result);

                if (data.CommissionTypeCode == CommissionType.COMMISSION_TYPE_UNKNOWN_CODE)
                {
                    importResult.AddUnknownCommissionTypeValue(data.CommissionTypeValue);
                }
            }

            if (CommissionsToInsert.Any())
            {
                await _bulkActions.BulkInsertCommissionsAsync(_context, CommissionsToInsert);
            }

            if (CommissionErrorsToInsert.Any())
            {
                await _bulkActions.BulkInsertCommissionErrorsAsync(_context, CommissionErrorsToInsert);
            }

            await _auditService.InsertAuditLog(scope, "Import", "Commission", statement.Id,
                                               new
            {
                commissionStatementId = commissionStatementId,
                importCount           = importResult.ImportCount,
                errorCount            = importResult.ErrorCount,
                errors = importResult.Results.Where(r => !r.Success).ToList()
            }
                                               );

            return(importResult);
        }