protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await Task.Delay(5000, stoppingToken); while (!stoppingToken.IsCancellationRequested) { if (ScannerServiceState.IsScanned) { var limitOptionRequests = (await limitOptionRequestGenericRepository.GetAllAsync()).Where(x => x.InProgress && !x.IsDeleted).ToList(); var marketItems = (await genericMarketItemRepository.GetAllAsync()).Where(x => x.InProgress && !x.IsDeleted).ToList(); foreach (var marketItem in marketItems) { var requests = limitOptionRequests.Where(x => x.ProductId == marketItem.ProductId && x.UnitPrice == marketItem.UnitPrice).OrderBy(x => x.CreatedTime).ToList(); foreach (var request in requests) { if (!marketItem.InProgress) { break; } request.Wallet = await walletGenericRepository.GetByIdAsync(request.WalletId); double satinalinabilecekitemsayisi = request.Wallet.Money / (marketItem.UnitPrice * (1 + ComissionRate)); double satinalinacaksayisi = 0; if (request.Amount <= satinalinabilecekitemsayisi) { if (request.Amount >= marketItem.Amount) { satinalinacaksayisi = marketItem.Amount; } else { satinalinacaksayisi = request.Amount; } } else { satinalinacaksayisi = satinalinabilecekitemsayisi; } //Limit islemina aktarilan para ve adet sistemi guncellendi request.Money -= satinalinacaksayisi * (marketItem.UnitPrice * (1 + ComissionRate)); request.Amount -= satinalinacaksayisi; request.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId); if (request.Amount == 0) { request.InProgress = false; } await limitOptionRequestGenericRepository.UpdateAsync(request); await limitOptionRequestGenericRepository.Commit(); //satan kullaniciya parayi ver WalletUpdateDto sellerWallet = await genericWalletService.GetByIdAsync <WalletUpdateDto>(marketItem.WalletId); sellerWallet.Money += satinalinacaksayisi * marketItem.UnitPrice; sellerWallet.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId); await genericWalletService.UpdateAsync(sellerWallet); //Accounting UPDATE WalletUpdateDto accountingWallet = await genericWalletService.GetByIdAsync <WalletUpdateDto>(AccountingState.AccountingWalletId); accountingWallet.Money += satinalinacaksayisi * marketItem.UnitPrice * ComissionRate; accountingWallet.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId); await genericWalletService.UpdateAsync(accountingWallet); await genericWalletService.Commit(); //kullanicinin hesabindaki urun adedini guncelle var productItem = await productItemService.GetAsync(request.WalletId, request.ProductId); productItem.Amount += satinalinacaksayisi; productItem.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId); await genericProductItemRepository.UpdateAsync(productItem); await genericProductItemRepository.Commit(); //marketItem urun adedini guncelle marketItem.Amount -= satinalinacaksayisi; if (marketItem.Amount == 0) { marketItem.InProgress = false; } MarketItemUpdateDto marketItemUpdateDto = mapper.Map <MarketItemUpdateDto>(marketItem); marketItemUpdateDto.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId); await genericMarketItemService.UpdateAsync(marketItemUpdateDto); await genericMarketItemService.Commit(); //islem gecmisi olustur TransactionCreateDto transaction = new() { Amount = satinalinacaksayisi, CreatedUserId = Guid.Parse(UserStringInfo.SystemUserId), ProductId = request.ProductId, UnitPrice = marketItem.UnitPrice, WalletIdBuyer = request.WalletId, WalletIdSeller = sellerWallet.Id }; await genericTransactionService.AddAsync(transaction); await genericTransactionService.Commit(); genericTransactionService.BeginTransaction(); genericMarketItemService.BeginTransaction(); genericProductItemRepository.BeginTransaction(); genericWalletService.BeginTransaction(); limitOptionRequestGenericRepository.BeginTransaction(); } } ScannerServiceState.IsScanned = false; } } } }