Example #1
0
        public async Task <int> SaveAsync(IEnumerable <MfAggrTransaction> transactions, CancellationToken token = default(CancellationToken))
        {
            using (var scope = transactionScopeBuilder.Create())
            {
                var result        = 0;
                var accounts      = (await mfAggrAccountProcessor.GetAsync(token)).ToArray();
                var accountDic    = accounts.ToDictionary(x => x.Id);
                var subAccountDic = accounts.SelectMany(x => x.SubAccounts).ToDictionary(x => x.Id);

                MfAggrAccount getAccount(long id)
                => accountDic.TryGetValue(id, out var account) ? account : null;

                MfAggrSubAccount getSubAccount(long id)
                => subAccountDic.TryGetValue(id, out var subAccount) ? subAccount : null;

                foreach (var transaction in transactions)
                {
                    if (transaction.IsIncome)
                    {
                        var receipt = transaction.ConvertReceipt(getAccount, getSubAccount);
                        var saved   = await addReceiptQueryProcessor.SaveAsync(receipt, token : token);

                        transaction.ReceiptId = saved.Id;
                        if (saved.ExcludeCategoryId.HasValue)
                        {
                            var exclude = new ReceiptExclude {
                                ReceiptId         = saved.Id,
                                ExcludeAmount     = saved.ExcludeAmount,
                                ExcludeCategoryId = saved.ExcludeCategoryId,
                                //RecordedAt          = saved.RecordedAt,
                                CreateBy = saved.CreateBy,
                                UpdateBy = saved.CreateBy,
                            };
                            await addReceiptExcludeQueryProcessor.SaveAsync(exclude, token);
                        }
                    }
                    await addMfAggrTransactionQueryProcessor.AddAsync(transaction, token);

                    result++;
                }
                scope.Complete();
                return(result);
            }
        }
Example #2
0
 public async Task <MfAggrAccount[]> GetAccounts(CancellationToken token)
 => (await mfAggrAccountProcessor.GetAsync(token)).ToArray();