public ApiResult InvokeMethod(MainAccount account, string methodName, object[] arguments)
 {
     var service = GetTradeService();
     var result = InvokeApi(service, account, methodName, arguments);
     //如果出错,调用其它服务
     if (!string.IsNullOrEmpty(result.Error))
     {
         foreach (var kv in _services)
         {
             if (kv.Key.GetType() == service.GetType())
             {
                 continue;
             }
             if (!kv.Value)
             {
                 result = InvokeApi(kv.Key, account, methodName, arguments);
                 if (string.IsNullOrEmpty(result.Error))
                 {
                     SetMainService(kv.Key);
                     break;
                 }
             }
         }
     }
     return result;
 }
        private static ApiResult InvokeApi(ITradeService service, MainAccount account, string methodName, object[] arguments)
        {
            var host = ManagerCore.Instance.ApiHostManager.GetFastHost(service.GetType());
            service.Account = account;
            service.Host = host;
            service.Login();

            var method = service.GetType().GetMethod(methodName);
            if (method == null)
            {
                throw new Exception("没有找到接口" + methodName);
            }
            var result = (ApiResult)method.Invoke(service, arguments);

            if (!string.IsNullOrEmpty(result.Error))
            {
                //尝试一次登录
                if (result.Error.Contains("连接已断开"))
                {
                    service.Logout();
                    service.Login();
                    result = (ApiResult)method.Invoke(service, arguments);
                }
            }
            return result;
        }
 public void UpdateMainAccount(MainAccount account)
 {
     _account = account;
     foreach (var service in _services)
     {
         service.Account = account;
     }
 }
 public ServiceHost(MainAccount account)
 {
     _account = account;
     _services.Add(new AuthorizeService());
     _services.Add(new CloseAccountService());
     _services.Add(new RefreshDataService());
     _services.Add(new StockSyncService());
 }
 /// <summary>
 /// 查询持仓接口
 /// </summary>
 /// <returns></returns>
 public List<ChildStock> QueryStocks(MainAccount account)
 {
     var list = new List<ChildStock>();
     var result = Core.ServiceManager.QueryStocks(account);
     if (result.Result)
     {
         foreach (var line in result.Data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
         {
             list.Add(ChildStock.Parse(line));
         }
     }
     return list;
 }
        /// <summary>
        /// 通过接口获取“当日委托”列表
        /// </summary>
        public List<ChildAuthorize> GetTodayAuthorize(MainAccount account)
        {
            var result = Core.ServiceManager.QueryAuthroizes(account);
            if (!result.Result)
            {
                throw new Exception("当日委托获取失败\n" + result.Error);
            }

            var list = new List<ChildAuthorize>();
            var rows = result.Data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var row in rows)
            {
                list.Add(ChildAuthorize.Parse(row));
            }
            return list;
        }
 /// <summary>
 /// 同步持仓股票市价
 /// </summary>
 public void SyncStocks(MainAccount account)
 {
     using (var db = GetDbContext())
     {
         var childIds = Core.AccountManager.GetChildIds(account.MainID);
         var list = db.ChildStocks.Where(e => childIds.Contains(e.ChildID));
         var stockCodes = list.Select(e => e.StockCode).ToArray();
         var data = QueryMarket(stockCodes);
         var rows = data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
         foreach (var row in rows)
         {
             var market = StockMarket.Parse(row);
             var entity = list.FirstOrDefault(e => e.StockCode == market.StockCode);
             entity.CurrentPrice = market.CurrentPrice;
         }
         db.SaveChanges();
     }
 }
 public ApiResult QueryHistoryMoney(MainAccount account, DateTime startTime, DateTime endTime)
 {
     return TradeServiceInvoker.InvokeMethod(account, "QueryHistoryMoney", new object[] { startTime, endTime });
 }
 public ApiResult QueryMoney(MainAccount account)
 {
     return TradeServiceInvoker.InvokeMethod(account, "QueryMoney", null);
 }
 public ApiResult QueryAuthroizes(MainAccount account)
 {
     return TradeServiceInvoker.InvokeMethod(account, "QueryAuthroizes", null);
 }
 public ApiResult Cancel(MainAccount account, string stockCode, string authorizeIndex)
 {
     return TradeServiceInvoker.InvokeMethod(account, "Cancel", new object[] { stockCode, authorizeIndex });
 }
 public ApiResult Sell(MainAccount account, string stockCode, int number, double price)
 {
     return TradeServiceInvoker.InvokeMethod(account, "Sell", new object[] { stockCode, number, price });
 }
        public void UpdateChildAccountMoneyAndStocks(MainAccount account)
        {
            using (var db = GetDbContext())
            {
                //更新所有子帐户的可取余额
                var childIds = GetChildIds(account.MainID);
                foreach (var childId in childIds)
                {
                    var child = db.ChildAccounts.FirstOrDefault(e => e.ChildID == childId);
                    child.AdvisableMoney = child.UseableMoney;
                }
                //更新所有子帐户的持仓余额
                var stocks = db.ChildStocks.Where(e => childIds.Contains(e.ChildID));
                foreach (var stock in stocks)
                {
                    stock.UseableCount = stock.AllCount;
                    //如果持仓为0,则删除该股票
                    if (stock.UseableCount == 0)
                    {
                        db.ChildStocks.Remove(stock);
                    }
                }

                db.SaveChanges();
            }
        }