public static TradingHistoryModel ToTradingHistoryModel(this TradingHistory tradingHistory) { return(new TradingHistoryModel { Id = tradingHistory.Id, BidTime = tradingHistory.BidTime, BidOrder = tradingHistory.BidOrder, RecordedPrice = tradingHistory.RecordedPrice, Auctioneer = tradingHistory.Auctioneer.ToAuctioneerModel(), Lot = tradingHistory.Lot.ToLotModel(), Trader = tradingHistory.Trader.ToTraderModel() }); }
public async Task <TradingHistoryModel> AddTradingHistory(TradingHistoryModel history) { if (history == null) { throw new ArgumentNullException(nameof(history)); } using (var db = new AuctionContext()) { //var trader = db.Traders.FirstOrDefault(t => t.Id == (history.Trader == null ? 0 : history.Trader.Id)); //var lot = db.Lots.FirstOrDefault(L => L.Id == (history.Lot == null ? 0 : history.Lot.Id)); //var Aunctioneer = db.Lots.FirstOrDefault(a => a.Id == (history.Auctioneer == null ? 0 : history.Auctioneer.Id)); var trader = db.Traders.FirstOrDefault(t => t.Id == history.Trader.Id); var lot = db.Lots.FirstOrDefault(L => L.Id == history.Lot.Id); var auctioneer = db.Auctioneers.FirstOrDefault(a => a.Id == history.Auctioneer.Id); var newRecord = new TradingHistory { Lot = lot, Trader = trader, Auctioneer = auctioneer, LotId = lot.Id, TraderId = trader.Id, AuctioneerId = auctioneer.Id, BidTime = history.BidTime, BidOrder = history.BidOrder, RecordedPrice = history.RecordedPrice, }; db.TradingHistories.Add(newRecord); await db.SaveChangesAsync(); history.Id = newRecord.Id; return(history); } }
/// <summary> /// 除权 /// </summary> /// <param name="StockCode"></param> /// <param name="Market"></param> /// <param name="Quotiety"></param> /// <returns></returns> public RemotingInterface.RI_Result SetExRights(string StockCode, TradingSystem.StockMarket Market, double Quotiety) { bool bConn = false; try { if (StockCode == null || Market == TradingSystem.StockMarket.Unknown) return RemotingInterface.RI_Result.Banned_Stock; else if (Quotiety <= 1) return RemotingInterface.RI_Result.Illegal_Quotiety; else if (!Common.IsWeekend && DateTime.Now.TimeOfDay >= Common.BeginAMTS && DateTime.Now.TimeOfDay <= Common.EndPMTS.Add(new TimeSpan(0, 15, 0))) return RemotingInterface.RI_Result.Out_Of_Maintain_Time; bConn = true; // 盘后方可除权 if (sqlConn_Adm.State == ConnectionState.Closed) sqlConn_Adm.Open(); sqlTrans_Adm = sqlConn_Adm.BeginTransaction(); List<TradingSystem.UserStocks> listStocks = new List<TradingSystem.UserStocks>(); List<TradingHistory> listTrades = new List<TradingHistory>(); sqlCmd_Adm = new SqlCommand("SELECT * FROM [UserStocks] WHERE (StockCode = @StockCode) AND (Market = @Market", sqlConn_Adm, sqlTrans_Adm); sqlCmd_Adm.Parameters.Add("@StockCode", SqlDbType.NVarChar, 6); sqlCmd_Adm.Parameters["@StockCode"].Value = StockCode.Trim(); sqlCmd_Adm.Parameters.Add("@Market", SqlDbType.TinyInt); sqlCmd_Adm.Parameters["@Market"].Value = (byte)Market; sqlReader_Adm = sqlCmd_Adm.ExecuteReader(); while (sqlReader_Adm.Read()) { TradingSystem.UserStocks data_stock = new TradingSystem.UserStocks(); TradingHistory data_trade = new TradingHistory(); data_stock.Initialize(); data_trade.Initialize(); data_stock.UserID = (int)sqlReader_Adm["UserID"]; data_trade.UserID = data_stock.UserID; data_stock.StockCode = sqlReader_Adm["StockCode"].ToString().Trim(); data_trade.StockCode = data_stock.StockCode; data_stock.Market = (TradingSystem.StockMarket)Convert.ToByte(sqlReader_Adm["Market"].ToString().Trim()); data_trade.Market = data_stock.Market; data_stock.Volume = (int)sqlReader_Adm["Volume"]; data_trade.TradeVolume = (int)(((double)data_stock.Volume * (Quotiety - 1) + 50) / 100) * 100; if (data_trade.TradeVolume < 100) continue; data_stock.AveragePrice = Common.ConvertPrice( (double.Parse(sqlReader_Adm["AveragePrice"].ToString().Trim()) * data_stock.Volume) / (data_stock.Volume + data_trade.TradeVolume)); data_stock.Volume += data_trade.TradeVolume; data_stock.Curr = (TradingSystem.Currency)Convert.ToByte(sqlReader_Adm["Currency"].ToString().Trim()); data_trade.Curr = data_stock.Curr; if (sqlReader_Adm["Sellable"].ToString().Trim() == "1") data_stock.Sellable = true; else data_stock.Sellable = false; data_trade.OrderID = (int)Special_OrderID.ExRights; data_trade.Side = true; data_trade.TradeDate = DateTime.Now; data_trade.TradePrice = 0; listStocks.Add(data_stock); listTrades.Add(data_trade); } sqlReader_Adm.Close(); foreach (TradingSystem.UserStocks data in listStocks) { sqlCmd_Adm = new SqlCommand("UPDATE [UserStocks] SET Volume = @Volume, AveragePrice = @AveragePrice " + "WHERE (UserID = @UserID) AND (StockCode = @StockCode) AND (Market = @Market) AND (Sellable = @Sellable)", sqlConn_Adm, sqlTrans_Adm); sqlCmd_Adm.Parameters.Add("@Volume", SqlDbType.Int); sqlCmd_Adm.Parameters["@Volume"].Value = data.Volume; sqlCmd_Adm.Parameters.Add("@AveragePrice", SqlDbType.Money); sqlCmd_Adm.Parameters["@AveragePrice"].Value = data.AveragePrice; sqlCmd_Adm.Parameters.Add("@UserID", SqlDbType.Int); sqlCmd_Adm.Parameters["@UserID"].Value = data.UserID; sqlCmd_Adm.Parameters.Add("@StockCode", SqlDbType.NVarChar, 6); sqlCmd_Adm.Parameters["@StockCode"].Value = data.StockCode.Trim(); sqlCmd_Adm.Parameters.Add("@Market", SqlDbType.TinyInt); sqlCmd_Adm.Parameters["@Market"].Value = (byte)data.Market; sqlCmd_Adm.Parameters.Add("@Sellable", SqlDbType.TinyInt); if (data.Sellable) sqlCmd_Adm.Parameters["@Sellable"].Value = (byte)1; else sqlCmd_Adm.Parameters["@Sellable"].Value = (byte)0; sqlCmd_Adm.ExecuteNonQuery(); } foreach (TradingHistory data in listTrades) { sqlCmd_Adm = new SqlCommand("INSERT INTO [TradingHistory] (OrderID,UserID," + "OrderSide,StockCode,Market,TradeVolume,TradePrice,Currency,TradeDate) " + "VALUES (@OrderID,@UserID,@OrderSide,@StockCode,@Market," + "@TradeVolume,@TradePrice,@Currency,@TradeDate)", sqlConn_Adm, sqlTrans_Adm); sqlCmd_Adm.Parameters.Add("@OrderID", SqlDbType.Int); sqlCmd_Adm.Parameters["@OrderID"].Value = data.OrderID; sqlCmd_Adm.Parameters.Add("@UserID", SqlDbType.Int); sqlCmd_Adm.Parameters["@UserID"].Value = data.UserID; if (data.Side) { sqlCmd_Adm.Parameters.Add("@OrderSide", SqlDbType.Bit); sqlCmd_Adm.Parameters["@OrderSide"].Value = (byte)1; } else { sqlCmd_Adm.Parameters.Add("@OrderSide", SqlDbType.Bit); sqlCmd_Adm.Parameters["@OrderSide"].Value = (byte)0; } sqlCmd_Adm.Parameters.Add("@StockCode", SqlDbType.NVarChar, 6); sqlCmd_Adm.Parameters["@StockCode"].Value = data.StockCode.Trim(); sqlCmd_Adm.Parameters.Add("@Market", SqlDbType.TinyInt); sqlCmd_Adm.Parameters["@Market"].Value = (byte)data.Market; sqlCmd_Adm.Parameters.Add("@TradeVolume", SqlDbType.Int); sqlCmd_Adm.Parameters["@TradeVolume"].Value = data.TradeVolume; sqlCmd_Adm.Parameters.Add("@TradePrice", SqlDbType.Money); sqlCmd_Adm.Parameters["@TradePrice"].Value = data.TradePrice; sqlCmd_Adm.Parameters.Add("@Currency", SqlDbType.TinyInt); sqlCmd_Adm.Parameters["@Currency"].Value = (byte)data.Curr; sqlCmd_Adm.Parameters.Add("@TradeDate", SqlDbType.DateTime); sqlCmd_Adm.Parameters["@TradeDate"].Value = data.TradeDate; sqlCmd_Adm.ExecuteNonQuery(); } sqlTrans_Adm.Commit(); return RemotingInterface.RI_Result.Success; } catch (Exception err) { Common.DBLog(err, ReplaceSqlPara(sqlCmd_Adm), false); Common.Log(err); if (sqlReader_Adm != null && !sqlReader_Adm.IsClosed) sqlReader_Adm.Close(); if (sqlTrans_Adm != null && sqlTrans_Adm.Connection != null && sqlTrans_Adm.Connection.State == ConnectionState.Open) sqlTrans_Adm.Rollback(); return RemotingInterface.RI_Result.Internal_Error; } finally { if (bConn && sqlConn_Adm.State != ConnectionState.Closed) sqlConn_Adm.Close(); } }
/// <summary> /// ����״̬�����ı� /// </summary> /// <param name="userFund"></param> /// <param name="userOrder"></param> /// <param name="userStock"></param> /// <returns></returns> public bool OrderChanged(TradingSystem.UserFund userFund, TradingSystem.UserOrders userOrder, TradingSystem.UserStocks userStock) { try { if (userFund.UserID != userOrder.UserID) { Common.Log("Illegal Trading [UserFund.UserID=" + userFund.UserID + ";UserOrder.UserID=" + userOrder.UserID + "] @ OrderChanged."); return false; } if (userOrder.OrdStatus == TradingSystem.OrderStatus.Finished && userStock.UserID != userOrder.UserID) { Common.Log("Illegal Trading [UserStock.UserID=" + userStock.UserID + ";UserOrder.UserID=" + userOrder.UserID + "] @ OrderChanged."); return false; } bool bExist = false; switch (userOrder.OrdStatus) { case TradingSystem.OrderStatus.Finished: { lock (listUserOrders) { for (int i = 0; i < listUserOrders.Count; i++) { if (listUserOrders[i].OrderID == userOrder.OrderID) { listUserOrders.RemoveAt(i); break; } } } bExist = false; lock (listOrdersHistory) { for (int i = 0; i < listOrdersHistory.Count; i++) { if (listOrdersHistory[i].OrderID == userOrder.OrderID) { bExist = true; listOrdersHistory[i] = userOrder; break; } } if (!bExist) listOrdersHistory.Add(userOrder); } lock (listRemoveOrders) { listRemoveOrders.Add(userOrder.OrderID); } if (userOrder.OrdType == TradingSystem.OrderType.ImmediateOrder || userOrder.OrdType == TradingSystem.OrderType.LimitedOrder) { bExist = false; lock (listUserStocks) { for (int i = 0; i < listUserStocks.Count; i++) { if (listUserStocks[i].UserID == userStock.UserID && listUserStocks[i].StockCode != null && string.Compare(userStock.StockCode.Trim(), listUserStocks[i].StockCode.Trim()) == 0 && userStock.Market == listUserStocks[i].Market) { bExist = true; listUserStocks[i] = userStock; break; } } if (!bExist) listUserStocks.Add(userStock); } TradingHistory TradeItem = new TradingHistory(); TradeItem.Initialize(); TradeItem.OrderID = userOrder.OrderID; TradeItem.Side = userOrder.Side; TradeItem.StockCode = userOrder.StockCode; TradeItem.Market = userOrder.Market; TradeItem.TradeVolume = userOrder.OrderVolume; TradeItem.TradeDate = userOrder.UpdatedDate; if (userOrder.TradePrice > 0) TradeItem.TradePrice = userOrder.TradePrice; else { Common.Log("Illegal Trading Price [TradePrice=" + userOrder.TradePrice.ToString("f4") + "]."); TradeItem.TradePrice = userOrder.OrderPrice; } TradeItem.Curr = userOrder.Curr; TradeItem.UserID = userOrder.UserID; lock (listTradingHistory) { listTradingHistory.Add(TradeItem); } } if (Common.stkBuffer != null) { RemotingInterface.RI_Order stiOrder = new RemotingInterface.RI_Order(); stiOrder.Clear(); stiOrder.ExpiredDate = userOrder.ExpiredDate; stiOrder.OrderDate = userOrder.OrderDate; stiOrder.OrderID = userOrder.OrderID; stiOrder.OrderPrice = Common.ConvertPrice(userOrder.OrderPrice); stiOrder.TradePrice = Common.ConvertPrice(userOrder.TradePrice); stiOrder.OrderStatus = (RemotingInterface.RI_Status)userOrder.OrdStatus; stiOrder.OrderType = (RemotingInterface.RI_Type)userOrder.OrdType; stiOrder.OrderVolume = userOrder.OrderVolume; stiOrder.Side = userOrder.Side; stiOrder.StockCode = userOrder.StockCode.Trim(); stiOrder.StockMarket = (RemotingInterface.RI_Market)userOrder.Market; stiOrder.UpdatedDate = userOrder.UpdatedDate; stiOrder.Curr = (RemotingInterface.RI_Currency)userOrder.Curr; Common.stkBuffer.SetUserOrders(userOrder.UserID, stiOrder); if (userOrder.OrdType == TradingSystem.OrderType.ImmediateOrder || userOrder.OrdType == TradingSystem.OrderType.LimitedOrder) { RemotingInterface.RI_Stock stiStock = new RemotingInterface.RI_Stock(); stiStock.Clear(); stiStock.AveragePrice = Common.ConvertPrice(userStock.AveragePrice); stiStock.Curr = (RemotingInterface.RI_Currency)userStock.Curr; stiStock.StockCode = userStock.StockCode.Trim(); stiStock.StockMarket = (RemotingInterface.RI_Market)userStock.Market; stiStock.Volume = userOrder.OrderVolume; if (userStock.Sellable) stiStock.SellableVolume = userOrder.OrderVolume; else stiStock.SellableVolume = 0; if (userOrder.Side) Common.stkBuffer.AddUserStocks(userStock.UserID, stiStock); else Common.stkBuffer.SubUserStocks(userStock.UserID, stiStock); RemotingInterface.RI_Trading stiTrading = new RemotingInterface.RI_Trading(); stiTrading.Clear(); stiTrading.Side = userOrder.Side; stiTrading.StockCode = userOrder.StockCode.Trim(); stiTrading.StockMarket = (RemotingInterface.RI_Market)userOrder.Market; stiTrading.TradeVolume = userOrder.OrderVolume; stiTrading.TradeDate = userOrder.UpdatedDate; stiTrading.Curr = (RemotingInterface.RI_Currency)userOrder.Curr; if (userOrder.TradePrice > 0) stiTrading.TradePrice = Common.ConvertPrice(userOrder.TradePrice); else stiTrading.TradePrice = Common.ConvertPrice(userOrder.OrderPrice); Common.stkBuffer.SetUserTradings(userOrder.UserID, stiTrading); } } } break; case TradingSystem.OrderStatus.Cancelled: case TradingSystem.OrderStatus.Failure: case TradingSystem.OrderStatus.Unknown: { bExist = false; lock (listUserOrders) { for (int i = 0; i < listUserOrders.Count; i++) { if (listUserOrders[i].OrderID == userOrder.OrderID) { listUserOrders.RemoveAt(i); break; } } } lock (listOrdersHistory) { for (int i = 0; i < listOrdersHistory.Count; i++) { if (listOrdersHistory[i].OrderID == userOrder.OrderID) { bExist = true; listOrdersHistory[i] = userOrder; break; } } if (!bExist) listOrdersHistory.Add(userOrder); } lock (listRemoveOrders) { listRemoveOrders.Add(userOrder.OrderID); } if (Common.stkBuffer != null) { RemotingInterface.RI_Order stiOrder = new RemotingInterface.RI_Order(); stiOrder.Clear(); stiOrder.ExpiredDate = userOrder.ExpiredDate; stiOrder.OrderDate = userOrder.OrderDate; stiOrder.OrderID = userOrder.OrderID; stiOrder.OrderPrice = Common.ConvertPrice(userOrder.OrderPrice); stiOrder.OrderStatus = (RemotingInterface.RI_Status)userOrder.OrdStatus; stiOrder.OrderType = (RemotingInterface.RI_Type)userOrder.OrdType; stiOrder.OrderVolume = userOrder.OrderVolume; stiOrder.Side = userOrder.Side; stiOrder.StockCode = userOrder.StockCode.Trim(); stiOrder.StockMarket = (RemotingInterface.RI_Market)userOrder.Market; stiOrder.UpdatedDate = userOrder.UpdatedDate; stiOrder.Curr = (RemotingInterface.RI_Currency)userOrder.Curr; Common.stkBuffer.SetUserOrders(userOrder.UserID, stiOrder); } } break; case TradingSystem.OrderStatus.Cancelling: case TradingSystem.OrderStatus.Waiting: { bExist = false; lock (listUserOrders) { for (int i = 0; i < listUserOrders.Count; i++) { if (listUserOrders[i].OrderID == userOrder.OrderID) { bExist = true; listUserOrders[i] = userOrder; break; } } if (!bExist) { listUserOrders.Add(userOrder); } } if (Common.stkBuffer != null) { RemotingInterface.RI_Order stiOrder = new RemotingInterface.RI_Order(); stiOrder.Clear(); stiOrder.ExpiredDate = userOrder.ExpiredDate; stiOrder.OrderDate = userOrder.OrderDate; stiOrder.OrderID = userOrder.OrderID; stiOrder.OrderPrice = Common.ConvertPrice(userOrder.OrderPrice); stiOrder.OrderStatus = (RemotingInterface.RI_Status)userOrder.OrdStatus; stiOrder.OrderType = (RemotingInterface.RI_Type)userOrder.OrdType; stiOrder.OrderVolume = userOrder.OrderVolume; stiOrder.Side = userOrder.Side; stiOrder.StockCode = userOrder.StockCode.Trim(); stiOrder.StockMarket = (RemotingInterface.RI_Market)userOrder.Market; stiOrder.UpdatedDate = userOrder.UpdatedDate; stiOrder.Curr = (RemotingInterface.RI_Currency)userOrder.Curr; Common.stkBuffer.SetUserOrders(userOrder.UserID, stiOrder); } } break; default: return false; } bExist = false; lock (listUserCash) { for (int i = 0; i < listUserCash.Count; i++) { if (listUserCash[i].UserID == userFund.UserID && listUserCash[i].Curr == userFund.Curr) { bExist = true; listUserCash[i] = userFund; break; } } if (!bExist) { listUserCash.Add(userFund); } } if (Common.stkBuffer != null) { RemotingInterface.RI_Fund stiFund = new RemotingInterface.RI_Fund(); stiFund.Clear(); stiFund.Cash = userFund.Cash; stiFund.UsableCash = userFund.UsableCash; stiFund.Wealth = Common.ConvertPrice(userFund.Wealth); stiFund.Curr = (RemotingInterface.RI_Currency)userFund.Curr; Common.stkBuffer.SetUserFund(userFund.UserID, stiFund); } try { Common.OrderNotifier.UserOrders_Handled(Common.WebService_PlayID, userOrder.UserID, (byte)userOrder.OrdStatus); } catch { Common.Log("OrderNotifier.RI_UserOrders_Handled() Failed. [" + userOrder.UserID + "/" + userOrder.OrderID + "]"); } return true; } catch (Exception err) { Common.Log(err); return false; } }