Beispiel #1
0
        private StackTradeResult TradeOneBuyOrder(StoneDelegateBuyOrderInfo buyOrder)
        {
            List <StoneDelegateSellOrderInfo> listTradeSucceedSellOrders = new List <StoneDelegateSellOrderInfo>();
            StackTradeResult buyOrderResult = TradeSell1Orders(buyOrder, listTradeSucceedSellOrders);

            if (buyOrderResult.State == StackTradeState.Failed)
            {
                return(buyOrderResult);
            }

            CustomerMySqlTransaction myTrans = null;

            try
            {
                myTrans = MyDBHelper.Instance.CreateTrans();
                foreach (var item in listTradeSucceedSellOrders)
                {
                    DBProvider.StoneStackDBProvider.SaveFinishedStoneDelegateSellOrderInfo(item);
                    PlayerController.Instance.PayDelegateBuyStonesUpdateSellerInfo(item, myTrans);
                }

                int selfSellHandCount = 0;
                foreach (var item in listTradeSucceedSellOrders)
                {
                    if (item.UserID == buyOrder.UserID)
                    {
                        selfSellHandCount += item.FinishedStoneTradeHandCount;
                    }
                }
                decimal allNeedRMB = (buyOrder.FinishedStoneTradeHandCount - selfSellHandCount) * buyOrder.BuyUnit.Price;
                buyOrder.AwardGoldCoin = (int)((allNeedRMB * GlobalConfig.GameConfig.StoneBuyerAwardGoldCoinMultiple) * GlobalConfig.GameConfig.RMB_GoldCoin);
                DBProvider.StoneStackDBProvider.SaveFinishedStoneDelegateBuyOrderInfo(buyOrder);
                PlayerController.Instance.PayDelegateBuyStonesUpdateBuyerInfo(buyOrder, myTrans);

                myTrans.Commit();

                //计算成交价
                this._todayTradeInfo.ComputeTradePrice(buyOrder.BuyUnit.Price, listTradeSucceedSellOrders[0].SellUnit.Price, buyOrder.FinishedStoneTradeHandCount);
                foreach (var item in listTradeSucceedSellOrders)
                {
                    AddLogNotifySeller(item);
                    LogHelper.Instance.AddInfoLog("委卖矿石交易成功:" + item.ToString());
                }
                AddLogNotifyBuyer(buyOrder);

                LogHelper.Instance.AddInfoLog("委买矿石交易成功:" + buyOrder.ToString());
            }
            catch (Exception exc)
            {
                LogHelper.Instance.AddErrorLog("ThreadStoneStackTrade.SaveStoneSellOrderHandler Exception", exc);
                myTrans.Rollback();
            }
            finally
            {
                myTrans.Dispose();
            }

            return(buyOrderResult);
        }
Beispiel #2
0
        private StackTradeResult TradeSell1Orders(StoneDelegateBuyOrderInfo buyOrder, List <StoneDelegateSellOrderInfo> listTradeSucceedSellOrders)
        {
            StackTradeResult buyOrderResult = new StackTradeResult();

            decimal buyPrice = buyOrder.BuyUnit.Price;

            StackTradeUnit buyUnit = new StackTradeUnit()
            {
                Price = buyOrder.BuyUnit.Price,
                TradeStoneHandCount = buyOrder.BuyUnit.TradeStoneHandCount
            };

            //只处理卖1
            var sellUnit = this._todayTradeInfo.GetSell1Unit();

            if (sellUnit == null)
            {
                return(buyOrderResult);
            }
            if (sellUnit.TradeStoneHandCount == 0 || !this._dicWaitingSellInfos.ContainsKey(sellUnit.Price) || this._dicWaitingSellInfos[sellUnit.Price] == null)
            {
                return(buyOrderResult);
            }

            if (buyPrice >= sellUnit.Price)
            {
                //成交,按量处理
                buyOrderResult = TradeSamePriceSellOrder(sellUnit.Price, this._dicWaitingSellInfos[sellUnit.Price], listTradeSucceedSellOrders, buyUnit, buyOrder.UserName);

                if (buyOrderResult.State == StackTradeState.Succeed)
                {
                    //买单 被全部消化
                    //暂时不清理卖单集合,被拆分的卖单,剩余部分在外部会重新加回卖单集合
                    buyOrder.BuyState = StoneDelegateBuyState.Succeed;
                    buyOrder.FinishedStoneTradeHandCount = buyOrder.BuyUnit.TradeStoneHandCount;
                    buyOrder.FinishedTime = new MyDateTime(DateTime.Now);
                }
                else if (buyOrderResult.State == StackTradeState.Splited)
                {
                    //买单没有被全部消化,需要再对比处理上一级卖价。等所有卖价都对比完时,再返回
                    buyUnit.TradeStoneHandCount         -= buyOrderResult.SucceedStoneHandCount;
                    buyOrder.BuyState                    = StoneDelegateBuyState.Splited;
                    buyOrder.FinishedStoneTradeHandCount = buyOrder.BuyUnit.TradeStoneHandCount - buyUnit.TradeStoneHandCount;
                    buyOrder.FinishedTime                = new MyDateTime(DateTime.Now);
                }
            }

            return(buyOrderResult);
        }
Beispiel #3
0
        /// <summary>
        /// 返回买单处理结果
        /// </summary>
        /// <param name="sellPrice"></param>
        /// <param name="queueSellOrders"></param>
        /// <param name="listTradeSucceedSellOrders"></param>
        /// <param name="buyOrder"></param>
        /// <returns></returns>
        private StackTradeResult TradeSamePriceSellOrder(decimal sellPrice, ListStoneDelegateSellOrderInfoCollection queueSellOrders,
                                                         List <StoneDelegateSellOrderInfo> listTradeSucceedSellOrders, StackTradeUnit buyUnit, string buyerUserName)
        {
            StackTradeResult buyOrderResult = new StackTradeResult();

            //取出卖价对应的卖单
            StoneDelegateSellOrderInfo sellOrder = queueSellOrders.Dequeue();

            if (sellOrder == null)
            {
                return(buyOrderResult);
            }

            int surplusStoneHandCount = buyUnit.TradeStoneHandCount - sellOrder.SellUnit.TradeStoneHandCount;

            //买单量大于或等于卖单量
            while (surplusStoneHandCount > 0)//继续找其他卖单凑量
            {
                sellOrder.SellState = StoneDelegateSellState.Succeed;
                sellOrder.FinishedStoneTradeHandCount = sellOrder.SellUnit.TradeStoneHandCount;
                sellOrder.FinishedTime = new MyDateTime(DateTime.Now);
                listTradeSucceedSellOrders.Add(sellOrder);

                var operResult = this._todayTradeInfo.DecreaseSellUnit(sellOrder.SellUnit.Price, sellOrder.SellUnit.TradeStoneHandCount);
                if (operResult.OperResultCode != OperResult.RESULTCODE_TRUE)
                {
                    LogHelper.Instance.AddErrorLog("TradeBuy1Orders DecreaseSellUnit Error1. " + operResult.Message, null);
                }

                var nextSellOrder = queueSellOrders.Dequeue();
                if (nextSellOrder == null)
                {
                    //说明已经没有当前价格的卖单了。
                    //买单只消化一部,需要被拆分
                    buyOrderResult.State = StackTradeState.Splited;
                    buyOrderResult.SucceedStoneHandCount = buyUnit.TradeStoneHandCount - surplusStoneHandCount;
                    return(buyOrderResult);
                }
                else
                {
                    //判断下一卖单
                    sellOrder             = nextSellOrder;
                    surplusStoneHandCount = surplusStoneHandCount - sellOrder.SellUnit.TradeStoneHandCount;
                }
            }
            //卖单量等于买单量
            if (surplusStoneHandCount == 0)
            {
                sellOrder.SellState = StoneDelegateSellState.Succeed;
                sellOrder.FinishedStoneTradeHandCount = sellOrder.SellUnit.TradeStoneHandCount;
                sellOrder.FinishedTime = new MyDateTime(DateTime.Now);
                listTradeSucceedSellOrders.Add(sellOrder);

                var operResult = this._todayTradeInfo.DecreaseSellUnit(sellOrder.SellUnit.Price, sellOrder.SellUnit.TradeStoneHandCount);
                if (operResult.OperResultCode != OperResult.RESULTCODE_TRUE)
                {
                    LogHelper.Instance.AddErrorLog("TradeBuy1Orders DecreaseSellUnit Error2. " + operResult.Message, null);
                }
            }
            else
            {
                //卖单量大于买单量,需要将当前卖单拆分。将已成功的部分拆成一新订单添加到数据库中;将老订单缩减到剩余部分,继续留在市场中。
                sellOrder.SellState = StoneDelegateSellState.Splited;
                sellOrder.FinishedStoneTradeHandCount = sellOrder.SellUnit.TradeStoneHandCount + surplusStoneHandCount;
                sellOrder.FinishedTime = new MyDateTime(DateTime.Now);
                listTradeSucceedSellOrders.Add(sellOrder);

                var operResult = this._todayTradeInfo.DecreaseSellUnit(sellOrder.SellUnit.Price, sellOrder.FinishedStoneTradeHandCount);
                if (operResult.OperResultCode != OperResult.RESULTCODE_TRUE)
                {
                    LogHelper.Instance.AddErrorLog("TradeBuy1Orders DecreaseSellUnit Error3. " + operResult.Message, null);
                }


                DateTime nowTime = DateTime.Now;

                //拆分后的订单,再保存到数据库,并添加到集合中。
                StoneDelegateSellOrderInfo newSellOrder = new StoneDelegateSellOrderInfo()
                {
                    OrderNumber  = OrderController.Instance.CreateOrderNumber(sellOrder.UserName, nowTime, AlipayTradeInType.StackStoneSell),
                    UserID       = sellOrder.UserID,
                    UserName     = sellOrder.UserName,
                    SellState    = StoneDelegateSellState.Waiting,
                    DelegateTime = new MyDateTime(nowTime),
                    SellUnit     = new StackTradeUnit()
                    {
                        Price = sellOrder.SellUnit.Price,
                        TradeStoneHandCount = sellOrder.SellUnit.TradeStoneHandCount - sellOrder.FinishedStoneTradeHandCount
                    },
                    IsSubOrder = true
                };

                try
                {
                    DBProvider.StoneStackDBProvider.SaveWaitingStoneDelegateSellOrderInfo(newSellOrder);
                    queueSellOrders.Enqueue(newSellOrder);
                }
                catch (Exception exc)
                {
                    LogHelper.Instance.AddErrorLog("股票操作。TradeSamePriceSellOrder.SaveWaitingStoneDelegateSellOrderInfo 异常。信息为:" + newSellOrder.ToString(), exc);
                }
            }

            //买单已全部被消化
            buyOrderResult.State = StackTradeState.Succeed;
            buyOrderResult.SucceedStoneHandCount = buyUnit.TradeStoneHandCount;
            return(buyOrderResult);
        }