using System.IO; using System.Linq; using System.Net; using System.Text; using Muchinfo.MTPClient.Adapter.Quote; using Muchinfo.MTPClient.Data; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.Analysis; using Muchinfo.MTPClient.Data.Quote; using Muchinfo.MTPClient.Infrastructure.Cache; using Muchinfo.MTPClient.Infrastructure.Helpers; using Muchinfo.MTPClient.Infrastructure.LinkProxy; using Muchinfo.MTPClient.IService; using System; using System.Collections.Generic; using Muchinfo.MTPClient.NetworkCore; using Muchinfo.MTPClient.Service.Utilities; using Muchinfo.MTPClient.Resources; namespace Muchinfo.MTPClient.Service { public class QuoteDataService : IQuoteDataService { private QuoteDateAdapter _quoteDateAdapter = new QuoteDateAdapter(); public void GetHistoryCycleData(QuoteGoods goods, CycleType Type, DateTime startTime, DateTime endTime, short count, Action successAction, Action errorAction) { var buffers = _quoteDateAdapter.HistoryCycleBytesRequst(goods, startTime, endTime, Type, count); var gram = new TCPPackage40 { Tag = (byte)Datagram40TagPrimaryFunction.QuoteHistory, //历史K线 Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值 }; gram.SetData(buffers); var proxy = LinkManager.Instance.SelectQuoteProxy(false, goods.ContainsGoodsSrc); if (proxy == null) { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = -6000, ReturnDesc = Resources.Client_Resource.Func_Desc_ConnectError, RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData }); } return; } proxy.SendPackage(gram, (package) => { byte[] contents = package.GetData(); var returnCode = GetReturnCode(contents); //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法) if (returnCode >= 0) { var goodsHistory = _quoteDateAdapter.HistoryCycleBytesRespone(contents); if (successAction != null) { successAction(goodsHistory); } } else { if (errorAction != null) { //RequestFunc = 历史行情请求 errorAction(new ErrorEntity() { ReturnCode = returnCode, ReturnDesc = string.Empty, RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData }); } } }, (code, message) => { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = code, ReturnDesc = message, RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData }); } }); } public void GetHistoryCycleData(QuoteGoods goods, CycleType Type, DateTime startTime, DateTime endTime, short count, Action successAction, Action errorAction) { var buffers = _quoteDateAdapter.HistoryCycleBytesRequst(goods, startTime, endTime, Type, count); var gram = new TCPPackage40 { Tag = (Type == CycleType.Day || Type == CycleType.Week ||Type == CycleType.Month ||Type == CycleType.Quarter ||Type == CycleType.Year) ?(byte)Datagram40TagPrimaryFunction.QueryDayHis : (byte)Datagram40TagPrimaryFunction.QuoteHistory, //历史K线 Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值 }; gram.SetData(buffers); var proxy = LinkManager.Instance.SelectQuoteProxy(false, goods.ContainsGoodsSrc); if (proxy == null) { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = -6000, ReturnDesc = Resources.Client_Resource.Func_Desc_ConnectError, RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData }); } return; } proxy.SendPackage(gram, (package) => { byte[] contents = package.GetData(); var returnCode = GetReturnCode(contents); //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法) if (returnCode >= 0) { // var goodsHistory = _quoteDateAdapter.HistoryCycleBytesRespone(contents); if (successAction != null) { successAction(contents); } } else { if (errorAction != null) { //RequestFunc = 历史行情请求 errorAction(new ErrorEntity() { ReturnCode = returnCode, ReturnDesc = string.Empty, RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData }); } } }, (code, message) => { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = code, ReturnDesc = message, RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData }); } }); } //todo:在转换实现 /// /// Gets the return code. /// /// The bytes. /// System.Int32. private int GetReturnCode(byte[] bytes) { /* 查询返回结构体 * 查询通用回应(包括订阅、盘面查询等) typedef struct tagQUERY_COMMON_REQ { int32_t iCount; // >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法) void* pRecords; // 根据请求类别分别指向不同的结果集(GOODS/QUERY_HISTROY_RSP/...) } QUERY_COMMON_REQ; */ //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法) if (bytes == null || bytes.Length < 4) return -1; var returnCodeBytes = bytes.Take(4).ToArray(); return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(returnCodeBytes, 0)); } public void QuerySettlementPlanDetail(string marketTypeId, Action> successAction, Action errorAction) { var queryCommonParams = new List(); queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId }); QueryCommonHelper.QueryCommon(QueryStatement.QueryReckonPlanDetail, queryCommonParams, new Action>((orders) => { if (successAction != null) { successAction(orders); } }), (error) => { if (errorAction != null) { //RequestFunc = 结算计划详细 error.RequestFunc = Client_Resource.Resources_Service_QuerySettlementPlanDetail; errorAction(error); } }); } /// /// 查询交易日计划 /// /// /// /// public void QueryTradeDayPlan(string marketTypeId, Action> successAction, Action errorAction) { var queryCommonParams = new List(); queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId }); QueryCommonHelper.QueryCommon(QueryStatement.QueryMarketReckon, queryCommonParams, new Action>((orders) => { if (successAction != null) { successAction(orders); } }), (error) => { if (errorAction != null) { //RequestFunc = 查询交易日计划 error.RequestFunc = Client_Resource.Resources_Service_QueryTradeDayPlan; errorAction(error); } }); } public void QueryReckonTimeWithTradeDetail(string marketTypeId, Action> successAction, Action errorAction) { var queryCommonParams = new List(); queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId }); QueryCommonHelper.QueryCommon(QueryStatement.QueryReckonWithTradeDetail, queryCommonParams, new Action>((orders) => { if (successAction != null) { successAction(orders); } }), (error) => { if (errorAction != null) { error.RequestFunc = "结算计划"; errorAction(error); } }); } public void QueryQuoteGoodsInfo(Action> successAction, Action errorAction) { var gram = new TCPPackage40 { Tag = (byte)Datagram40TagPrimaryFunction.QueryGoods, //行情商品 Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值 }; var proxy = LinkManager.Instance.SelectQuoteProxy(false, (int)GoodsFromScr.Brown); if (proxy == null) { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = -6000, ReturnDesc = Resources.Client_Resource.Func_Desc_ConnectError, RequestFunc = Resources.Client_Resource.Function_QuoteGoods }); } return; } proxy.SendPackage(gram, (package) => { byte[] contents = package.GetData(); var returnCode = GetReturnCode(contents); //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法) if (returnCode >= 0) { var length = contents.Length - 4; var goodsData = contents.Skip(4).Take(length).ToArray(); var goodsGroups = new List(); var goodses = _quoteDateAdapter.ConvertGoodsWithsGoodsGroup(goodsData, returnCode, ref goodsGroups); if (successAction != null) { CacheManager.LoadGoodsBaseInfo(goodses, goodsGroups, GoodsFromScr.Brown); successAction(goodses); } } else { if (errorAction != null) { //RequestFunc = 历史行情请求 errorAction(new ErrorEntity() { ReturnCode = returnCode, ReturnDesc = string.Empty, RequestFunc = Muchinfo.MTPClient.Resources.Client_Resource.Function_QuoteGoods }); } } }, (code, message) => { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = code, ReturnDesc = message, RequestFunc = Muchinfo.MTPClient.Resources.Client_Resource.Function_QuoteGoods }); } }); } public void QueryQuoteSettlementPlan(int marketTypeId, int goodsSrc, Action> successAction, Action errorAction) { var gram = new TCPPackage40 { Tag = (byte)Datagram40TagPrimaryFunction.QuerySettlePlan, //行情商品 Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值 }; using (MemoryStream ms = new MemoryStream()) { byte[] block = new byte[] { }; block = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(marketTypeId)); ms.Write(block, 0, block.Length); gram.SetData(ms.ToArray()); } var proxy = LinkManager.Instance.SelectQuoteProxy(false, goodsSrc); if (proxy == null) { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = -6000, ReturnDesc = Client_Resource.Func_Desc_ConnectError, RequestFunc = Client_Resource.Function_SettlePlans }); } return; } proxy.SendPackage(gram, (package) => { byte[] contents = package.GetData(); var returnCode = GetReturnCode(contents); //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法) if (returnCode >= 0) { var length = contents.Length - 4; var goodsData = contents.Skip(4).Take(length).ToArray(); var errorResult = new ErrorEntity() { ReturnCode = 0, RequestFunc = Client_Resource.Function_SettlePlans }; var quotePlan = _quoteDateAdapter.ConvertFromJson(goodsData, ref errorResult); if (errorResult.ReturnCode == 0) ////解析是不正确 { // quotePlan.MarketTypeID = marketTypeId; if (successAction != null) { successAction(quotePlan); } } else { if (errorAction != null) { errorAction(errorResult); } } } else { if (errorAction != null) { //RequestFunc = 历史行情请求 errorAction(new ErrorEntity() { ReturnCode = returnCode, ReturnDesc = string.Empty, RequestFunc = Client_Resource.Function_SettlePlans }); } } }, (code, message) => { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = code, ReturnDesc = message, RequestFunc = Client_Resource.Function_SettlePlans }); } }); } //public void QuoteCheckToken() //{ // var gram = new TCPPackage40 // { // Tag = (byte)Datagram40TagPrimaryFunction.QuerySettlePlan, //行情商品 // Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值 // }; // var data= _quoteDateAdapter.QuoteCheckToken(c_AccountId, c_checkToken); // gram.SetData(data); // var proxy = LinkManager.Instance.SelectQuoteProxy(false, (int)GoodsFromScr.Brown); // if (proxy == null) // { // return; // } // proxy.SendPackage(gram, (tcpPackage) => // { // }, (code, message) => // { // //todo:写日志 // }); //} public void QuerySaleGoodsParam(Action> successAction, Action errorAction) { var gram = new TCPPackage40 { Tag = (byte)Datagram40TagPrimaryFunction.QuerySaleParam, //行情商品 Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值 }; var proxy = LinkManager.Instance.SelectQuoteProxy(false, (int)GoodsFromScr.Brown); if (proxy == null) { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = -6000, ReturnDesc = Resources.Client_Resource.Func_Desc_ConnectError, RequestFunc = Resources.Client_Resource.Function_QuoteGoods }); } return; } proxy.SendPackage(gram, (package) => { byte[] contents = package.GetData(); var returnCode = GetReturnCode(contents); //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法) if (returnCode >= 0) { var length = contents.Length - 4; var goodsData = contents.Skip(4).Take(length).ToArray(); var goodses = _quoteDateAdapter.ConvertSaleGoodsParam(goodsData, returnCode); if (successAction != null) { successAction(goodses); } } else { if (errorAction != null) { //RequestFunc = 历史行情请求 errorAction(new ErrorEntity() { ReturnCode = returnCode, ReturnDesc = string.Empty, RequestFunc = Muchinfo.MTPClient.Resources.Client_Resource.Function_QuoteGoods }); } } }, (code, message) => { if (errorAction != null) { errorAction(new ErrorEntity() { ReturnCode = code, ReturnDesc = message, RequestFunc = Muchinfo.MTPClient.Resources.Client_Resource.Function_QuoteGoods }); } }); } /// /// 查询交易时间 /// /// /// /// public void QueryQuoteTradeTime(int marketTypeId, Action> successAction, Action errorAction) { var queryCommonParams = new List(); queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId + string.Empty }); QueryCommonHelper.QueryCommon(QueryStatement.QueryGoodsTradeTimeDetail, queryCommonParams, new Action>((tradeTimes) => { if (successAction != null) { successAction(tradeTimes); } }), (error) => { if (errorAction != null) { error.RequestFunc = Client_Resource.Func_OpenCloseTimeQuery; errorAction(error); } }); } } }