Ejemplo n.º 1
0
 /// <summary>
 /// �����û��ֹ�
 /// </summary>
 /// <param name="UserID"></param>
 /// <param name="UserStock"></param>
 /// <returns></returns>
 public bool AddUserStocks(int UserID, RemotingInterface.RI_Stock UserStock)
 {
     try
     {
         if (UserID <= 0 || UserStock.Volume <= 0 || UserStock.SellableVolume < 0)
             return false;
         if (UserStock.StockCode == null || UserStock.StockCode.Trim().Length != 6
             || UserStock.StockMarket == RemotingInterface.RI_Market.Unknown)
             return false;
         lock (mapRIUserStocks)
         {
             if (mapRIUserStocks.ContainsKey(UserID))
             {
                 List<RemotingInterface.RI_Stock> listStocks = mapRIUserStocks[UserID];
                 bool bExist = false;
                 for (int i = 0; i < listStocks.Count; i++)
                 {
                     RemotingInterface.RI_Stock data = listStocks[i];
                     if (string.Compare(data.StockCode.ToUpper().Trim(), UserStock.StockCode.ToUpper().Trim()) == 0
                         && data.StockMarket == UserStock.StockMarket)
                     {
                         data.AveragePrice = Common.ConvertPrice((data.AveragePrice * data.Volume + UserStock.AveragePrice * UserStock.Volume)
                             / (data.Volume + UserStock.Volume));
                         data.Volume += UserStock.Volume;
                         data.SellableVolume += UserStock.SellableVolume;
                         listStocks[i] = data;
                         bExist = true;
                         Common.Debug("SetStocksInfoInBuffer: UserID-" + UserID + "; StockCode-" + data.StockCode + (byte)data.StockMarket +
                             "; Volume-" + data.Volume + "; SellableVolume-" + data.SellableVolume + "; Price-" + data.AveragePrice.ToString("f3"));
                     }
                 }
                 if (!bExist)
                 {
                     listStocks.Add(UserStock);
                     Common.Debug("AddStocksInfoInBuffer: UserID-" + UserID + "; StockCode-" + UserStock.StockCode + (byte)UserStock.StockMarket +
                        "; Volume-" + UserStock.Volume + "; SellableVolume-" + UserStock.SellableVolume + "; Price-" + UserStock.AveragePrice.ToString("f3"));
                 }
                 mapRIUserStocks[UserID] = listStocks;
             }
             else
             {
                 List<RemotingInterface.RI_Stock> listStocks = new List<RemotingInterface.RI_Stock>();
                 listStocks.Add(UserStock);
                 Common.Debug("AddStocksInfoInBuffer: UserID-" + UserID + "; StockCode-" + UserStock.StockCode + (byte)UserStock.StockMarket +
                     "; Volume-" + UserStock.Volume + "; SellableVolume-" + UserStock.SellableVolume + "; Price-" + UserStock.AveragePrice.ToString("f3"));
                 mapRIUserStocks[UserID] = listStocks;
             }
         }
         return true;
     }
     catch (Exception err)
     {
         Common.Log(err);
         return false;
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// �ϲ��û��ֹ�
 /// </summary>
 /// <param name="UserID"></param>
 /// <param name="UserStock"></param>
 /// <returns></returns>
 public bool SubUserStocks(int UserID, RemotingInterface.RI_Stock UserStock)
 {
     try
     {
         if (UserID <= 0 || UserStock.Volume <= 0)
             return false;
         if (UserStock.StockCode == null || UserStock.StockCode.Trim().Length != 6
             || UserStock.StockMarket == RemotingInterface.RI_Market.Unknown)
             return false;
         lock (mapRIUserStocks)
         {
             if (mapRIUserStocks.ContainsKey(UserID))
             {
                 List<RemotingInterface.RI_Stock> listStocks = mapRIUserStocks[UserID];
                 bool bRtn = false;
                 for (int i = 0; i < listStocks.Count; i++)
                 {
                     if (listStocks[i].SellableVolume >= UserStock.SellableVolume && listStocks[i].StockCode != null
                         && string.Compare(listStocks[i].StockCode.ToUpper().Trim(), UserStock.StockCode.ToUpper().Trim()) == 0
                         && listStocks[i].StockMarket == UserStock.StockMarket && listStocks[i].Volume >= UserStock.Volume)
                     {
                         RemotingInterface.RI_Stock data = listStocks[i];
                         if (data.Volume <= UserStock.Volume)
                             data.AveragePrice = 0;
                         else
                             data.AveragePrice = Common.ConvertPrice((data.AveragePrice * data.Volume - UserStock.AveragePrice * UserStock.Volume)
                                 / (data.Volume - UserStock.Volume));
                         data.Volume -= UserStock.Volume;
                         data.SellableVolume -= UserStock.SellableVolume;
                         Common.Debug("SubStocksInfoInBuffer: UserID-" + UserID + "; StockCode-" + data.StockCode + (byte)data.StockMarket +
                            "; Volume-" + data.Volume + "; SellableVolume-" + data.SellableVolume + "; Price-" + data.AveragePrice.ToString("f3"));
                         if (data.Volume <= 0)
                             listStocks.RemoveAt(i--);
                         else
                             listStocks[i] = data;
                         mapRIUserStocks[UserID] = listStocks;
                         bRtn = true;
                         break;
                     }
                 }
                 return bRtn;
             }
             else
             {
                 return false;
             }
         }
     }
     catch (Exception err)
     {
         Common.Log(err);
         return false;
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// �����û�����
 /// </summary>
 /// <param name="UserID"></param>
 /// <param name="UserTrading"></param>
 /// <returns></returns>
 public bool SetUserTradings(int UserID, RemotingInterface.RI_Trading UserTrading)
 {
     try
     {
         if (UserID <= 0)
             return false;
         if (UserTrading.TradeDate.Date != DateTime.Now.Date
             || DateTime.Now.TimeOfDay > Common.EndPMTS)
             return false;
         lock (mapRIUserTrades)
         {
             if (mapRIUserTrades.ContainsKey(UserID))
             {
                 List<RemotingInterface.RI_Trading> listTrades = mapRIUserTrades[UserID];
                 listTrades.Add(UserTrading);
                 mapRIUserTrades[UserID] = listTrades;
                 Common.Debug("SetTradingInfoInBuffer: UserID-" + UserID + "; StockCode-" + UserTrading.StockCode + (byte)UserTrading.StockMarket +
                     "; Volume-" + UserTrading.TradeVolume + "; Price-" + UserTrading.TradePrice.ToString("f3").Trim() + "; Side-" + UserTrading.Side.ToString());
             }
             else
             {
                 List<RemotingInterface.RI_Trading> listTrades = new List<RemotingInterface.RI_Trading>();
                 listTrades.Add(UserTrading);
                 mapRIUserTrades[UserID] = listTrades;
                 Common.Debug("SetTradingInfoInBuffer: UserID-" + UserID + "; StockCode-" + UserTrading.StockCode + (byte)UserTrading.StockMarket +
                   "; Volume-" + UserTrading.TradeVolume + "; Price-" + UserTrading.TradePrice.ToString("f3").Trim() + "; Side-" + UserTrading.Side.ToString());
             }
         }
         return true;
     }
     catch (Exception err)
     {
         Common.Log(err);
         return false;
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// �����û�����
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="UserOrder"></param>
        /// <returns></returns>
        public bool SetUserOrders(int UserID, RemotingInterface.RI_Order UserOrder)
        {
            try
            {
                if (UserID <= 0)
                    return false;
                if (UserOrder.OrderDate.Date != DateTime.Now.Date
                    || DateTime.Now.TimeOfDay > Common.EndPMTS)
                    return false;

                TradingSystem.StockMarket sMarket = TradingSystem.StockMarket.Unknown;
                if (UserOrder.StockMarket == RemotingInterface.RI_Market.Shanghai)
                    sMarket = TradingSystem.StockMarket.Shanghai;
                else if (UserOrder.StockMarket == RemotingInterface.RI_Market.Shenzhen)
                    sMarket = TradingSystem.StockMarket.Shenzhen;
                TradingSystem.StockType sType = Common.stkTrading.GetStockType(UserOrder.StockCode, sMarket);
                switch (sType)
                {
                    case TradingSystem.StockType.SH_A:
                    case TradingSystem.StockType.SZ_A:
                    case TradingSystem.StockType.SH_Bond:
                    case TradingSystem.StockType.SZ_Bond:
                        {
                            UserOrder.OrderPrice = Common.ConvertPrice(UserOrder.OrderPrice, 2);
                            UserOrder.TradePrice = Common.ConvertPrice(UserOrder.TradePrice, 2);
                        }
                        break;
                    default:
                        {
                            UserOrder.OrderPrice = Common.ConvertPrice(UserOrder.OrderPrice);
                            UserOrder.TradePrice = Common.ConvertPrice(UserOrder.TradePrice);
                        }
                        break;
                }

                if (UserOrder.UpdatedDate < UserOrder.OrderDate)
                    UserOrder.UpdatedDate = UserOrder.OrderDate;

                lock (mapRIUserOrders)
                {
                    if (mapRIUserOrders.ContainsKey(UserID))
                    {
                        Dictionary<int, RemotingInterface.RI_Order> mapOrders = mapRIUserOrders[UserID];
                        mapOrders[UserOrder.OrderID] = UserOrder;
                        mapRIUserOrders[UserID] = mapOrders;
                        Common.Debug("SetOrderInfoInBuffer: UserID-" + UserID + "; StockCode-" + UserOrder.StockCode + (byte)UserOrder.StockMarket +
                           "; OrderID-" + UserOrder.OrderID + "; Type-" + UserOrder.OrderType.ToString() + "; Status-" + UserOrder.OrderStatus.ToString());
                    }
                    else
                    {
                        Dictionary<int, RemotingInterface.RI_Order> mapOrders = new Dictionary<int, RemotingInterface.RI_Order>();
                        mapOrders[UserOrder.OrderID] = UserOrder;
                        mapRIUserOrders[UserID] = mapOrders;
                        Common.Debug("SetOrderInfoInBuffer: UserID-" + UserID + "; StockCode-" + UserOrder.StockCode + (byte)UserOrder.StockMarket +
                           "; OrderID-" + UserOrder.OrderID + "; Type-" + UserOrder.OrderType.ToString() + "; Status-" + UserOrder.OrderStatus.ToString());
                    }
                }
                return true;
            }
            catch (Exception err)
            {
                Common.Log(err);
                return false;
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// �����û��ʽ���ˮ
 /// </summary>
 /// <param name="UserID"></param>
 /// <param name="UserFundChanges"></param>
 /// <returns></returns>
 public bool SetUserFundChanges(int UserID, RemotingInterface.RI_FundChanges UserFundChanges)
 {
     try
     {
         if (UserID <= 0)
             return false;
         if (UserFundChanges.ChangedDate.Date != DateTime.Now.Date
             || DateTime.Now.TimeOfDay > Common.EndPMTS)
             return false;
         lock (mapRIUserFundChanges)
         {
             if (mapRIUserFundChanges.ContainsKey(UserID))
             {
                 List<RemotingInterface.RI_FundChanges> listFundChanges = mapRIUserFundChanges[UserID];
                 listFundChanges.Add(UserFundChanges);
                 mapRIUserFundChanges[UserID] = listFundChanges;
                 Common.Debug("SetUserFundChangesInBuffer: UserID-" + UserID + "; OriginalCash-" + UserFundChanges.OriginalCash.ToString("f3").Trim() +
                     "; ChangedCash-" + UserFundChanges.ChangedCash.ToString("f3").Trim() + "; OrderID-" + UserFundChanges.OrderID.ToString().Trim());
             }
             else
             {
                 List<RemotingInterface.RI_FundChanges> listFundChanges = new List<RemotingInterface.RI_FundChanges>();
                 listFundChanges.Add(UserFundChanges);
                 mapRIUserFundChanges[UserID] = listFundChanges;
                 Common.Debug("SetUserFundChangesInBuffer: UserID-" + UserID + "; OriginalCash-" + UserFundChanges.OriginalCash.ToString("f3").Trim() +
                     "; ChangedCash-" + UserFundChanges.ChangedCash.ToString("f3").Trim() + "; OrderID-" + UserFundChanges.OrderID.ToString().Trim());
             }
         }
         return true;
     }
     catch (Exception err)
     {
         Common.Log(err);
         return false;
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// �����û��ʽ�
 /// </summary>
 /// <param name="UserID"></param>
 /// <param name="UserFund"></param>
 /// <returns></returns>
 public bool SetUserFund(int UserID, RemotingInterface.RI_Fund UserFund)
 {
     try
     {
         if (UserID <= 0 || UserFund.Curr == RemotingInterface.RI_Currency.Unknown)
             return false;
         if (Common.ComparePrice(UserFund.Cash + 0.01, UserFund.UsableCash) < 0)
             return false;
         UserFund.Cash = Common.ConvertPrice(UserFund.Cash, 2);
         UserFund.UsableCash = Common.ConvertPrice(UserFund.UsableCash, 2);
         lock (mapRIUserFund)
         {
             if(mapRIUserFund.ContainsKey(UserID))
             {
                 Dictionary<byte, RemotingInterface.RI_Fund> mapCurr = mapRIUserFund[UserID];
                 mapCurr[(byte)UserFund.Curr] = UserFund;
                 mapRIUserFund[UserID] = mapCurr;
             }
             else
             {
                 Dictionary<byte, RemotingInterface.RI_Fund> mapCurr = new Dictionary<byte, RemotingInterface.RI_Fund>();
                 mapCurr[(byte)UserFund.Curr] = UserFund;
                 mapRIUserFund[UserID] = mapCurr;
             }
         }
         return true;
     }
     catch (Exception err)
     {
         Common.Log(err);
         return false;
     }
 }
Ejemplo n.º 7
0
        private static void SaveBuffer()
        {
            strConn = BaseConfig.ConnStr;
            sqlConn = new SqlConnection(BaseConfig.ConnStr);
            try
            {
                ClearData();
                if (sqlConn.State == ConnectionState.Closed)
                {
                    sqlConn.Open();
                }

                //读取指定区域内参与交易的用户列表
                string strSql = @"SELECT UserId,AreaId,UserName,UserDataBase FROM emtradeplay.dbo.UserList " +
                                " WHERE Validity =1 AND TradeFlag = 1 AND AreaId in (" + BaseConfig.AreaIds + ")" +
                                " ORDER BY UserId  DESC ";
                sqlCmd    = new SqlCommand(strSql, sqlConn);
                sqlReader = sqlCmd.ExecuteReader();
                Console.WriteLine(" Read user start ... ");
                while (sqlReader.Read())
                {
                    int    userId       = Convert.ToInt32(sqlReader["UserId"]);
                    string userDataBase = sqlReader["UserDataBase"].ToString();
                    Console.WriteLine("--- current user = "******"--- warning : userId = " + userId + " DataBase = null ---");
                        continue;
                    }
                    zns.RemotingInterface cRmt = null;
                    cRmt = mapRmtOjb[userDataBase];

                    try
                    {
                        //用户订单
                        Dictionary <int, zns.RemotingInterface.RI_Order> mapUserOrder = new Dictionary <int, zns.RemotingInterface.RI_Order>();
                        mapUserOrder = cRmt.RequestUserOrders(BaseConfig.RmtUserKey, userId);
                        if (mapUserOrder != null)
                        {
                            Loger.Serialize(BasePath + "/UserOrder/" + userId + ".dat", mapUserOrder);
                        }
                        else
                        {
                            Loger.Serialize(BasePath + "/UserOrder/" + userId + ".dat", "");
                        }
                    }
                    catch (System.Exception e)
                    {
                        Loger.Debug(BasePath + "/RmtError.log", userId + "\t" + e.ToString());
                        Loger.Debug(BasePath + "LostUser.log", userId + " -- mapUserOrder --");
                    }
                    try
                    {
                        //用户持股
                        List <zns.RemotingInterface.RI_Stock> listUserStock = new List <zns.RemotingInterface.RI_Stock>();
                        listUserStock = cRmt.RequestUserStocks(BaseConfig.RmtUserKey, userId);
                        if (listUserStock != null)
                        {
                            Loger.Serialize(BasePath + "/UserStock/" + userId + ".dat", listUserStock);
                        }
                        else
                        {
                            Loger.Serialize(BasePath + "/UserStock/" + userId + ".dat", "");
                        }
                    }
                    catch (System.Exception e)
                    {
                        Loger.Debug(BasePath + "/RmtError.log", userId + "\t" + e.ToString());
                        Loger.Debug(BasePath + "LostUser.log", userId + " -- listUserStock --");
                    }
                    try
                    {
                        //用户交易
                        List <zns.RemotingInterface.RI_Trading> listUserTrade = new List <zns.RemotingInterface.RI_Trading>();
                        listUserTrade = cRmt.RequestUserTrades(BaseConfig.RmtUserKey, userId);
                        if (listUserTrade != null)
                        {
                            Loger.Serialize(BasePath + "/UserTrade/" + userId + ".dat", listUserTrade);
                        }
                        else
                        {
                            Loger.Serialize(BasePath + "/UserTrade/" + userId + ".dat", "");
                        }
                    }
                    catch (System.Exception e)
                    {
                        Loger.Debug(BasePath + "/RmtError.log", userId + "\t" + e.ToString());
                        Loger.Debug(BasePath + "LostUser.log", userId + " -- listUserTrade --");
                    }
                    try
                    {
                        //用户资金
                        Dictionary <byte, zns.RemotingInterface.RI_Fund> mapUserFund = new Dictionary <byte, zns.RemotingInterface.RI_Fund>();
                        mapUserFund = cRmt.RequestUserFund(BaseConfig.RmtUserKey, userId);
                        if (mapUserFund != null)
                        {
                            Loger.Serialize(BasePath + "/UserFund/" + userId + ".dat", mapUserFund);
                        }
                        else
                        {
                            Loger.Serialize(BasePath + "/UserFund/" + userId + ".dat", "");
                        }
                    }
                    catch (System.Exception e)
                    {
                        Loger.Debug(BasePath + "/RmtError.log", userId + "\t" + e.ToString());
                        Loger.Debug(BasePath + "LostUser.log", userId + " -- mapUserFund --");
                    }
                    try
                    {
                        //用户资金流水
                        List <zns.RemotingInterface.RI_FundChanges> listUserFundChange = new List <zns.RemotingInterface.RI_FundChanges>();
                        listUserFundChange = cRmt.RequestUserFundChanges(BaseConfig.RmtUserKey, userId);
                        if (listUserFundChange != null)
                        {
                            Loger.Serialize(BasePath + "/UserFundChange/" + userId + ".dat", listUserFundChange);
                        }
                        else
                        {
                            Loger.Serialize(BasePath + "/UserFundChange/" + userId + ".dat", "");
                        }
                    }
                    catch (System.Exception e)
                    {
                        Loger.Debug(BasePath + "/RmtError.log", userId + "\t" + e.ToString());
                        Loger.Debug(BasePath + "LostUser.log", userId + " -- listUserFundChange --");
                    }
                }  //end while(sqlReader.Read())
                sqlReader.Close();
                Console.WriteLine("<<<  获取缓存数据完成 >>> ");
            }
            catch (Exception err)
            {
                Loger.Debug("Proc Error:" + err.ToString());
            }
            finally
            {
                if (sqlConn.State != ConnectionState.Closed)
                {
                    sqlConn.Close();
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 计算当日资产
        /// </summary>
        /// <returns></returns>
        public static bool ProcWealth()
        {
            bool bInTransaction = false;

            try
            {
                Loger.Debug(">>> Processing [Wealth] Started ! <<<");

                if (!Common.QuoteSvc.ReloadQuotation())
                {
                    Loger.Debug("--- Reloading Quotation Failed. ---");
                    return(false);
                }
                Show2003DBFRecord          SHRecord      = new Show2003DBFRecord(); SHRecord.Clear();
                SjshqDBFRecord             SZRecord      = new SjshqDBFRecord(); SZRecord.Clear();
                Dictionary <int, UserRank> mapUserWealth = new Dictionary <int, UserRank>();

                if (sqlConn.State == ConnectionState.Closed)
                {
                    sqlConn.Open();
                }
                sqlTrans       = sqlConn.BeginTransaction();
                bInTransaction = true;

                //读取指定活动参与交易的用户列表
                sqlCmd = new SqlCommand(@"emtradeplay.dbo.GetTradeUserListByPlayId", sqlConn, sqlTrans);
                sqlCmd.Parameters.Add("@playId", SqlDbType.Int).Value = BaseConfig.PlayId;
                sqlReader = sqlCmd.ExecuteReader();

                int nUser = 0;
                while (sqlReader.Read())
                {
                    int userId = Convert.ToInt32(sqlReader["UserId"]);

                    string userDataBase = sqlReader["UserDataBase"].ToString();
                    if (userDataBase == null || userDataBase == string.Empty)
                    {
                        Loger.Debug("--- warning : userId = " + userId + " DataBase = null ---");
                        continue;
                    }

                    zns.RemotingInterface cRmt = null;

                    foreach (KeyValuePair <int, NotifySrv> s in BaseConfig.mapNotifySrv)
                    {
                        if (userDataBase == s.Value.DataBaseChar)
                        {
                            cRmt = Common.znRmtIobj[s.Key];
                            break;
                        }
                    }

                    #region 各币种股票总市值
                    double StocksWealthRMB    = 0;
                    double StocksWealthUSD    = 0;
                    double StocksWealthHKD    = 0;
                    double tempOneStockWealth = 0;
                    double StocksWealth       = 0; //所有币种股票总市值

                    List <zns.RemotingInterface.RI_Stock> listStock = new List <Stock_Trading_Simulator_Kernel.RemotingInterface.RI_Stock>();
                    listStock = cRmt.RequestUserStocks(BaseConfig.RmtUserKey, userId);
                    if (listStock != null)
                    {
                        foreach (zns.RemotingInterface.RI_Stock stock in listStock)
                        {
                            tempOneStockWealth = 0;

                            if (stock.StockMarket == zns.RemotingInterface.RI_Market.Shanghai)
                            {
                                if (Common.QuoteSvc.FindQuotation(stock.StockCode, out SHRecord))
                                {
                                    if (SHRecord.LatestPrice < 0.001 || SHRecord.OpenPrice < 0.001)
                                    {
                                        tempOneStockWealth = ConvertPrice(SHRecord.PreClosePrice) * stock.Volume;
                                    }
                                    else
                                    {
                                        tempOneStockWealth = ConvertPrice(SHRecord.LatestPrice) * stock.Volume;
                                    }
                                }
                            }
                            else if (stock.StockMarket == zns.RemotingInterface.RI_Market.Shenzhen)
                            {
                                if (Common.QuoteSvc.FindQuotation(stock.StockCode, out SZRecord))
                                {
                                    if (SZRecord.LatestPrice < 0.001 || SZRecord.OpenPrice < 0.001)
                                    {
                                        tempOneStockWealth = ConvertPrice(SZRecord.PreClosePrice) * stock.Volume;
                                    }
                                    else
                                    {
                                        tempOneStockWealth = ConvertPrice(SZRecord.LatestPrice) * stock.Volume;
                                    }
                                }
                            }

                            switch (stock.Curr)
                            {
                            case zns.RemotingInterface.RI_Currency.RMB:
                                StocksWealthRMB += tempOneStockWealth; break;

                            case zns.RemotingInterface.RI_Currency.USD:
                                StocksWealthUSD += tempOneStockWealth; break;

                            case zns.RemotingInterface.RI_Currency.HKD:
                                StocksWealthHKD += tempOneStockWealth; break;
                            }
                        }
                        StocksWealth = StocksWealthRMB + StocksWealthUSD * BaseConfig.RateUSD + StocksWealthHKD * BaseConfig.RateHKD;
                    }
                    #endregion

                    #region 各币种现金
                    double CashRMB = 0;
                    double CashUSD = 0;
                    double CashHKD = 0;

                    Dictionary <byte, zns.RemotingInterface.RI_Fund> mapUserFund = new Dictionary <byte, Stock_Trading_Simulator_Kernel.RemotingInterface.RI_Fund>();
                    mapUserFund = cRmt.RequestUserFund(BaseConfig.RmtUserKey, userId);
                    if (mapUserFund != null)
                    {
                        foreach (KeyValuePair <byte, zns.RemotingInterface.RI_Fund> fund in mapUserFund)
                        {
                            switch ((zns.RemotingInterface.RI_Currency)fund.Key)
                            {
                            case zns.RemotingInterface.RI_Currency.RMB:
                                CashRMB = fund.Value.Cash; break;

                            case zns.RemotingInterface.RI_Currency.USD:
                                CashUSD = fund.Value.Cash; break;

                            case zns.RemotingInterface.RI_Currency.HKD:
                                CashHKD = fund.Value.Cash; break;
                            }
                        }
                    }
                    #endregion

                    #region 各币种现有总资产
                    double WealthRMB = 0;
                    double WealthUSD = 0;
                    double WealthHKD = 0;
                    double Wealth    = 0;
                    WealthRMB = StocksWealthRMB + CashRMB;
                    WealthUSD = StocksWealthUSD + CashUSD;
                    WealthHKD = StocksWealthHKD + CashHKD;
                    Wealth    = WealthRMB + WealthUSD * BaseConfig.RateUSD + WealthHKD * BaseConfig.RateHKD;
                    #endregion

                    #region 持仓比例
                    double RatioRMB = 0;
                    double RatioUSD = 0;
                    double RatioHKD = 0;
                    if (WealthRMB > 0)
                    {
                        RatioRMB = 1 - (CashRMB / WealthRMB);
                        if (RatioRMB > 1)
                        {
                            RatioRMB = 1;
                        }
                        else if (RatioRMB < 0)
                        {
                            RatioRMB = 0;
                        }
                    }
                    else
                    {
                        RatioRMB = 0;
                    }

                    if (WealthUSD > 0)
                    {
                        RatioUSD = 1 - (CashUSD / WealthUSD);
                        if (RatioUSD > 1)
                        {
                            RatioUSD = 1;
                        }
                        else if (RatioUSD < 0)
                        {
                            RatioUSD = 0;
                        }
                    }
                    else
                    {
                        RatioUSD = 0;
                    }

                    if (WealthHKD > 0)
                    {
                        RatioHKD = 1 - (CashHKD / WealthHKD);
                        if (RatioHKD > 1)
                        {
                            RatioHKD = 1;
                        }
                        else if (RatioHKD < 0)
                        {
                            RatioHKD = 0;
                        }
                    }
                    else
                    {
                        RatioHKD = 0;
                    }
                    #endregion

                    #region 收益率
                    double Profit   = 0;
                    double InitCash = BaseConfig.InitCashRMB + BaseConfig.InitCashUSD * BaseConfig.RateUSD
                                      + BaseConfig.InitCashHKD * BaseConfig.RateHKD;
                    Profit = (Wealth - InitCash) / InitCash * 100;
                    if (Wealth == 0)
                    {
                        Profit = 0;
                    }
                    #endregion

                    UserRank data = new UserRank();
                    data.Initialize();

                    data.UserId       = userId;
                    data.AreaId       = (int)sqlReader["AreaId"];
                    data.UserName     = sqlReader["UserName"].ToString();
                    data.UserDataBase = sqlReader["UserDataBase"].ToString();

                    data.Wealth    = Wealth;
                    data.WealthRMB = WealthRMB;
                    data.WealthUSD = WealthUSD;
                    data.WealthHKD = WealthHKD;

                    data.StockWealth = StocksWealth;

                    data.RatioRMB = RatioRMB;
                    data.RatioUSD = RatioUSD;
                    data.RatioHKD = RatioHKD;

                    data.Profit           = Profit;
                    data.RankDate         = DateTime.Now.Date;
                    mapUserWealth[userId] = data;

                    nUser++;
                }  //while(sqlReader.Read()) end
                sqlReader.Close();
                Loger.Debug("---calculate [StocksWealth] [Cash] [Wealth] [Ratio] [Profit] finished . UserCount = " + nUser + "---");

                #region 根据已更新的资产缓存更新数据库表
                int nUserID = 0;
                sqlCmd = new SqlCommand("DELETE FROM [DailyRank]", sqlConn, sqlTrans);
                sqlCmd.ExecuteNonQuery();
                nUser = 0;
                foreach (var data in mapUserWealth)
                {
                    sqlCmd = new SqlCommand("INSERT INTO [DailyRank] (UserID,AreaId,UserName,UserDataBase,Wealth, WealthRMB, WealthUSD, WealthHKD,StockWealth, " +
                                            "Profit, DailyProfit, WeeklyProfit, MonthlyProfit, RatioRMB, RatioUSD, RatioHKD, RatioUnderDays, RankDate) " +

                                            "VALUES (@UserID,@AreaId,@UserName,@UserDataBase, @Wealth, @WealthRMB, @WealthUSD, @WealthHKD,@StockWealth," +
                                            "@Profit, @DailyProfit, @WeeklyProfit, @MonthlyProfit, @RatioRMB, @RatioUSD, @RatioHKD, @RatioUnderDays, @RankDate)", sqlConn, sqlTrans);
                    //用户信息
                    sqlCmd.Parameters.Add("@UserID", SqlDbType.Int).Value               = data.Value.UserId;
                    sqlCmd.Parameters.Add("@AreaId", SqlDbType.Int).Value               = data.Value.AreaId;
                    sqlCmd.Parameters.Add("@UserName", SqlDbType.VarChar, 32).Value     = data.Value.UserName;
                    sqlCmd.Parameters.Add("@UserDataBase", SqlDbType.VarChar, 16).Value = data.Value.UserDataBase;
                    //资产总值
                    sqlCmd.Parameters.Add("@Wealth", SqlDbType.Money).Value    = data.Value.Wealth;
                    sqlCmd.Parameters.Add("@WealthRMB", SqlDbType.Money).Value = data.Value.WealthRMB;
                    sqlCmd.Parameters.Add("@WealthUSD", SqlDbType.Money).Value = data.Value.WealthUSD;
                    sqlCmd.Parameters.Add("@WealthHKD", SqlDbType.Money).Value = data.Value.WealthHKD;
                    //股票市值
                    sqlCmd.Parameters.Add("@StockWealth", SqlDbType.Money).Value = data.Value.StockWealth;
                    //收益率
                    sqlCmd.Parameters.Add("@Profit", SqlDbType.Money).Value        = data.Value.Profit;
                    sqlCmd.Parameters.Add("@DailyProfit", SqlDbType.Money).Value   = data.Value.DailyProfit;
                    sqlCmd.Parameters.Add("@WeeklyProfit", SqlDbType.Money).Value  = data.Value.WeeklyProfit;
                    sqlCmd.Parameters.Add("@MonthlyProfit", SqlDbType.Money).Value = data.Value.MonthlyProfit;
                    //持仓比例
                    sqlCmd.Parameters.Add("@RatioRMB", SqlDbType.Money).Value = data.Value.RatioRMB;
                    sqlCmd.Parameters.Add("@RatioUSD", SqlDbType.Money).Value = data.Value.RatioUSD;
                    sqlCmd.Parameters.Add("@RatioHKD", SqlDbType.Money).Value = data.Value.RatioHKD;
                    //低于持仓标准的天数
                    if (mapUserWealth[nUserID].RatioRMB < BaseConfig.RatioBaseLine) //当日持仓未达标则记录
                    {
                        sqlCmd.Parameters.Add("@RatioUnderDays", SqlDbType.Int).Value = 1;
                    }
                    else
                    {
                        sqlCmd.Parameters.Add("@RatioUnderDays", SqlDbType.Int).Value = 0;
                    }
                    //排名日期
                    sqlCmd.Parameters.Add("@RankDate", SqlDbType.DateTime).Value = data.Value.RankDate.ToString("yyyy-MM-dd");
                    sqlCmd.ExecuteNonQuery();
                    nUser++;
                }
                Loger.Debug("---Update table dailyrank finished . userCount = " + nUser + "---");
                #endregion

                #region 当天排名数据备份到单独表
                sqlCmd = new SqlCommand("CreateHistoryRankByDate", sqlConn, sqlTrans);
                sqlCmd.ExecuteNonQuery();
                #endregion

                sqlTrans.Commit();
                Loger.Debug("<<< Processing [Wealth] Finished ! >>>");
                bWealthDone = true;
                return(true);
            }
            catch (Exception err)
            {
                Loger.Debug("ProcWealth error :" + err);
                if (bInTransaction && sqlTrans != null && sqlTrans.Connection != null &&
                    sqlTrans.Connection.State == ConnectionState.Open)
                {
                    if (sqlReader != null && !sqlReader.IsClosed)
                    {
                        sqlReader.Close();
                    }
                    sqlTrans.Rollback();
                }
                return(false);
            }
            finally
            {
                if (sqlConn.State != ConnectionState.Closed)
                {
                    sqlConn.Close();
                }
            }
        }