| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- 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<GoodsHistoryCycle> successAction, Action<ErrorEntity> 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<byte[]> successAction, Action<ErrorEntity> 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:在转换实现
- /// <summary>
- /// Gets the return code.
- /// </summary>
- /// <param name="bytes">The bytes.</param>
- /// <returns>System.Int32.</returns>
- 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<List<WeekPlanDetail>> successAction,
- Action<ErrorEntity> errorAction)
- {
- var queryCommonParams = new List<QueryCommonParam>();
- queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId });
- QueryCommonHelper.QueryCommon(QueryStatement.QueryReckonPlanDetail, queryCommonParams,
- new Action<List<WeekPlanDetail>>((orders) =>
- {
- if (successAction != null)
- {
- successAction(orders);
- }
- }), (error) =>
- {
- if (errorAction != null)
- {
- //RequestFunc = 结算计划详细
- error.RequestFunc = Client_Resource.Resources_Service_QuerySettlementPlanDetail;
- errorAction(error);
- }
- });
- }
- /// <summary>
- /// 查询交易日计划
- /// </summary>
- /// <param name="marketTypeId"></param>
- /// <param name="successAction"></param>
- /// <param name="errorAction"></param>
- public void QueryTradeDayPlan(string marketTypeId, Action<List<TradeDayPlan>> successAction,
- Action<ErrorEntity> errorAction)
- {
- var queryCommonParams = new List<QueryCommonParam>();
- queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId });
- QueryCommonHelper.QueryCommon(QueryStatement.QueryMarketReckon, queryCommonParams,
- new Action<List<TradeDayPlan>>((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<List<TradeDayPlan>> successAction,
- Action<ErrorEntity> errorAction)
- {
- var queryCommonParams = new List<QueryCommonParam>();
- queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId });
- QueryCommonHelper.QueryCommon(QueryStatement.QueryReckonWithTradeDetail, queryCommonParams,
- new Action<List<TradeDayPlan>>((orders) =>
- {
- if (successAction != null)
- {
- successAction(orders);
- }
- }), (error) =>
- {
- if (errorAction != null)
- {
- error.RequestFunc = "结算计划";
- errorAction(error);
- }
- });
- }
- public void QueryQuoteGoodsInfo(Action<List<QuoteGoods>> successAction, Action<ErrorEntity> 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<GoodsGroup>();
- 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<List<QuoteTradePlan>> successAction,
- Action<ErrorEntity> 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<List<Data.Model.Sale.SaleGoods>> successAction, Action<ErrorEntity> 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
- });
- }
- });
- }
- /// <summary>
- /// 查询交易时间
- /// </summary>
- /// <param name="marketTypeId"></param>
- /// <param name="successAction"></param>
- /// <param name="errorAction"></param>
- public void QueryQuoteTradeTime(int marketTypeId, Action<List<QuoteTradeTime>> successAction, Action<ErrorEntity> errorAction)
- {
- var queryCommonParams = new List<QueryCommonParam>();
- queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId + string.Empty });
- QueryCommonHelper.QueryCommon(QueryStatement.QueryGoodsTradeTimeDetail, queryCommonParams,
- new Action<List<QuoteTradeTime>>((tradeTimes) =>
- {
- if (successAction != null)
- {
- successAction(tradeTimes);
- }
- }), (error) =>
- {
- if (errorAction != null)
- {
- error.RequestFunc = Client_Resource.Func_OpenCloseTimeQuery;
- errorAction(error);
- }
- });
- }
- }
- }
|