示例#1
0
        public async Task <TransactionResponse> Execute(TransactionModel transactionModel)
        {
            SellerData sell = new SellerData(transactionModel.Price, transactionModel.Quantity);

            try
            {
                await _sellerRepository.AddSellerData(sell);

                List <BuyerData> BuyerList = await _buyerRepository.GetGreaterBuyerPriceListFromSellerPrice(sell.SellPrice);

                foreach (var buy in BuyerList)
                {
                    decimal Quantities = 0.0m;
                    if (sell.RemainingQuantity >= buy.RemainingQuantity)
                    {
                        Quantities = buy.RemainingQuantity;
                    }
                    else
                    {
                        Quantities = sell.RemainingQuantity;
                    }
                    sell.RemainingQuantity -= Quantities;
                    sell.SettledQuantity   += Quantities;
                    buy.RemainingQuantity  -= Quantities;
                    buy.SettledQuantity    += Quantities;

                    if (sell.RemainingQuantity == 0)
                    {
                        sell.StatusChangeToSettleStatus();
                    }
                    else
                    {
                        sell.StatusChangeToOnHoldStatus();
                    }
                    if (buy.RemainingQuantity == 0)
                    {
                        buy.StatusChangeToSettleStatus();
                    }
                    else
                    {
                        buy.StatusChangeToOnHoldStatus();
                    }

                    await _sellerRepository.UpdateSellerData(sell);

                    await _buyerRepository.UpdateBuyerData(buy);

                    await _ledgerRepository.AddLedgerData(sell, buy, Quantities);

                    if (sell.RemainingQuantity == 0)
                    {
                        break;
                    }
                }
                TransactionResponse transactionResponse = new TransactionResponse();
                transactionResponse.UniqId        = sell.SellerId.ToString();
                transactionResponse.StatusCode    = (int)sell.TransactionStatus;
                transactionResponse.StatusMessage = sell.TransactionStatus.ToString();
                return(transactionResponse);
            }
            catch (Exception)
            {
                return(new TransactionResponse {
                    ErrorCode = enErrorCode.InternalError, StatusCode = (int)TransactionStatus.Validationfail, StatusMessage = TransactionStatus.Validationfail.ToString()
                });
            }
        }