QuoteDataService.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using System.IO;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Text;
  5. using Muchinfo.MTPClient.Adapter.Quote;
  6. using Muchinfo.MTPClient.Data;
  7. using Muchinfo.MTPClient.Data.Enums;
  8. using Muchinfo.MTPClient.Data.Model;
  9. using Muchinfo.MTPClient.Data.Model.Analysis;
  10. using Muchinfo.MTPClient.Data.Quote;
  11. using Muchinfo.MTPClient.Infrastructure.Cache;
  12. using Muchinfo.MTPClient.Infrastructure.Helpers;
  13. using Muchinfo.MTPClient.Infrastructure.LinkProxy;
  14. using Muchinfo.MTPClient.IService;
  15. using System;
  16. using System.Collections.Generic;
  17. using Muchinfo.MTPClient.NetworkCore;
  18. using Muchinfo.MTPClient.Service.Utilities;
  19. using Muchinfo.MTPClient.Resources;
  20. namespace Muchinfo.MTPClient.Service
  21. {
  22. public class QuoteDataService : IQuoteDataService
  23. {
  24. private QuoteDateAdapter _quoteDateAdapter = new QuoteDateAdapter();
  25. public void GetHistoryCycleData(QuoteGoods goods, CycleType Type, DateTime startTime, DateTime endTime,
  26. short count,
  27. Action<GoodsHistoryCycle> successAction, Action<ErrorEntity> errorAction)
  28. {
  29. var buffers = _quoteDateAdapter.HistoryCycleBytesRequst(goods, startTime, endTime, Type, count);
  30. var gram = new TCPPackage40
  31. {
  32. Tag = (byte)Datagram40TagPrimaryFunction.QuoteHistory, //历史K线
  33. Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值
  34. };
  35. gram.SetData(buffers);
  36. var proxy = LinkManager.Instance.SelectQuoteProxy(false, goods.ContainsGoodsSrc);
  37. if (proxy == null)
  38. {
  39. if (errorAction != null)
  40. {
  41. errorAction(new ErrorEntity()
  42. {
  43. ReturnCode = -6000,
  44. ReturnDesc = Resources.Client_Resource.Func_Desc_ConnectError,
  45. RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData
  46. });
  47. }
  48. return;
  49. }
  50. proxy.SendPackage(gram,
  51. (package) =>
  52. {
  53. byte[] contents = package.GetData();
  54. var returnCode = GetReturnCode(contents);
  55. //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法)
  56. if (returnCode >= 0)
  57. {
  58. var goodsHistory = _quoteDateAdapter.HistoryCycleBytesRespone(contents);
  59. if (successAction != null)
  60. {
  61. successAction(goodsHistory);
  62. }
  63. }
  64. else
  65. {
  66. if (errorAction != null)
  67. {
  68. //RequestFunc = 历史行情请求
  69. errorAction(new ErrorEntity()
  70. {
  71. ReturnCode = returnCode,
  72. ReturnDesc = string.Empty,
  73. RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData
  74. });
  75. }
  76. }
  77. }, (code, message) =>
  78. {
  79. if (errorAction != null)
  80. {
  81. errorAction(new ErrorEntity()
  82. {
  83. ReturnCode = code,
  84. ReturnDesc = message,
  85. RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData
  86. });
  87. }
  88. });
  89. }
  90. public void GetHistoryCycleData(QuoteGoods goods, CycleType Type, DateTime startTime,
  91. DateTime endTime,
  92. short count,
  93. Action<byte[]> successAction, Action<ErrorEntity> errorAction)
  94. {
  95. var buffers = _quoteDateAdapter.HistoryCycleBytesRequst(goods, startTime, endTime, Type, count);
  96. var gram = new TCPPackage40
  97. {
  98. Tag = (Type == CycleType.Day || Type == CycleType.Week ||Type == CycleType.Month ||Type == CycleType.Quarter ||Type == CycleType.Year) ?(byte)Datagram40TagPrimaryFunction.QueryDayHis : (byte)Datagram40TagPrimaryFunction.QuoteHistory, //历史K线
  99. Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值
  100. };
  101. gram.SetData(buffers);
  102. var proxy = LinkManager.Instance.SelectQuoteProxy(false, goods.ContainsGoodsSrc);
  103. if (proxy == null)
  104. {
  105. if (errorAction != null)
  106. {
  107. errorAction(new ErrorEntity()
  108. {
  109. ReturnCode = -6000,
  110. ReturnDesc = Resources.Client_Resource.Func_Desc_ConnectError,
  111. RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData
  112. });
  113. }
  114. return;
  115. }
  116. proxy.SendPackage(gram,
  117. (package) =>
  118. {
  119. byte[] contents = package.GetData();
  120. var returnCode = GetReturnCode(contents);
  121. //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法)
  122. if (returnCode >= 0)
  123. {
  124. // var goodsHistory = _quoteDateAdapter.HistoryCycleBytesRespone(contents);
  125. if (successAction != null)
  126. {
  127. successAction(contents);
  128. }
  129. }
  130. else
  131. {
  132. if (errorAction != null)
  133. {
  134. //RequestFunc = 历史行情请求
  135. errorAction(new ErrorEntity()
  136. {
  137. ReturnCode = returnCode,
  138. ReturnDesc = string.Empty,
  139. RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData
  140. });
  141. }
  142. }
  143. }, (code, message) =>
  144. {
  145. if (errorAction != null)
  146. {
  147. errorAction(new ErrorEntity()
  148. {
  149. ReturnCode = code,
  150. ReturnDesc = message,
  151. RequestFunc = Client_Resource.Resources_Service_GetHistoryCycleData
  152. });
  153. }
  154. });
  155. }
  156. //todo:在转换实现
  157. /// <summary>
  158. /// Gets the return code.
  159. /// </summary>
  160. /// <param name="bytes">The bytes.</param>
  161. /// <returns>System.Int32.</returns>
  162. private int GetReturnCode(byte[] bytes)
  163. {
  164. /* 查询返回结构体
  165. * 查询通用回应(包括订阅、盘面查询等)
  166. typedef struct tagQUERY_COMMON_REQ {
  167. int32_t iCount; // >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法)
  168. void* pRecords; // 根据请求类别分别指向不同的结果集(GOODS/QUERY_HISTROY_RSP/...)
  169. } QUERY_COMMON_REQ;
  170. */
  171. //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法)
  172. if (bytes == null || bytes.Length < 4) return -1;
  173. var returnCodeBytes = bytes.Take(4).ToArray();
  174. return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(returnCodeBytes, 0));
  175. }
  176. public void QuerySettlementPlanDetail(string marketTypeId, Action<List<WeekPlanDetail>> successAction,
  177. Action<ErrorEntity> errorAction)
  178. {
  179. var queryCommonParams = new List<QueryCommonParam>();
  180. queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId });
  181. QueryCommonHelper.QueryCommon(QueryStatement.QueryReckonPlanDetail, queryCommonParams,
  182. new Action<List<WeekPlanDetail>>((orders) =>
  183. {
  184. if (successAction != null)
  185. {
  186. successAction(orders);
  187. }
  188. }), (error) =>
  189. {
  190. if (errorAction != null)
  191. {
  192. //RequestFunc = 结算计划详细
  193. error.RequestFunc = Client_Resource.Resources_Service_QuerySettlementPlanDetail;
  194. errorAction(error);
  195. }
  196. });
  197. }
  198. /// <summary>
  199. /// 查询交易日计划
  200. /// </summary>
  201. /// <param name="marketTypeId"></param>
  202. /// <param name="successAction"></param>
  203. /// <param name="errorAction"></param>
  204. public void QueryTradeDayPlan(string marketTypeId, Action<List<TradeDayPlan>> successAction,
  205. Action<ErrorEntity> errorAction)
  206. {
  207. var queryCommonParams = new List<QueryCommonParam>();
  208. queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId });
  209. QueryCommonHelper.QueryCommon(QueryStatement.QueryMarketReckon, queryCommonParams,
  210. new Action<List<TradeDayPlan>>((orders) =>
  211. {
  212. if (successAction != null)
  213. {
  214. successAction(orders);
  215. }
  216. }), (error) =>
  217. {
  218. if (errorAction != null)
  219. {
  220. //RequestFunc = 查询交易日计划
  221. error.RequestFunc = Client_Resource.Resources_Service_QueryTradeDayPlan;
  222. errorAction(error);
  223. }
  224. });
  225. }
  226. public void QueryReckonTimeWithTradeDetail(string marketTypeId, Action<List<TradeDayPlan>> successAction,
  227. Action<ErrorEntity> errorAction)
  228. {
  229. var queryCommonParams = new List<QueryCommonParam>();
  230. queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId });
  231. QueryCommonHelper.QueryCommon(QueryStatement.QueryReckonWithTradeDetail, queryCommonParams,
  232. new Action<List<TradeDayPlan>>((orders) =>
  233. {
  234. if (successAction != null)
  235. {
  236. successAction(orders);
  237. }
  238. }), (error) =>
  239. {
  240. if (errorAction != null)
  241. {
  242. error.RequestFunc = "结算计划";
  243. errorAction(error);
  244. }
  245. });
  246. }
  247. public void QueryQuoteGoodsInfo(Action<List<QuoteGoods>> successAction, Action<ErrorEntity> errorAction)
  248. {
  249. var gram = new TCPPackage40
  250. {
  251. Tag = (byte)Datagram40TagPrimaryFunction.QueryGoods, //行情商品
  252. Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值
  253. };
  254. var proxy = LinkManager.Instance.SelectQuoteProxy(false, (int)GoodsFromScr.Brown);
  255. if (proxy == null)
  256. {
  257. if (errorAction != null)
  258. {
  259. errorAction(new ErrorEntity()
  260. {
  261. ReturnCode = -6000,
  262. ReturnDesc = Resources.Client_Resource.Func_Desc_ConnectError,
  263. RequestFunc = Resources.Client_Resource.Function_QuoteGoods
  264. });
  265. }
  266. return;
  267. }
  268. proxy.SendPackage(gram,
  269. (package) =>
  270. {
  271. byte[] contents = package.GetData();
  272. var returnCode = GetReturnCode(contents);
  273. //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法)
  274. if (returnCode >= 0)
  275. {
  276. var length = contents.Length - 4;
  277. var goodsData = contents.Skip(4).Take(length).ToArray();
  278. var goodsGroups = new List<GoodsGroup>();
  279. var goodses = _quoteDateAdapter.ConvertGoodsWithsGoodsGroup(goodsData, returnCode, ref goodsGroups);
  280. if (successAction != null)
  281. {
  282. CacheManager.LoadGoodsBaseInfo(goodses, goodsGroups, GoodsFromScr.Brown);
  283. successAction(goodses);
  284. }
  285. }
  286. else
  287. {
  288. if (errorAction != null)
  289. {
  290. //RequestFunc = 历史行情请求
  291. errorAction(new ErrorEntity()
  292. {
  293. ReturnCode = returnCode,
  294. ReturnDesc = string.Empty,
  295. RequestFunc = Muchinfo.MTPClient.Resources.Client_Resource.Function_QuoteGoods
  296. });
  297. }
  298. }
  299. }, (code, message) =>
  300. {
  301. if (errorAction != null)
  302. {
  303. errorAction(new ErrorEntity()
  304. {
  305. ReturnCode = code,
  306. ReturnDesc = message,
  307. RequestFunc = Muchinfo.MTPClient.Resources.Client_Resource.Function_QuoteGoods
  308. });
  309. }
  310. });
  311. }
  312. public void QueryQuoteSettlementPlan(int marketTypeId, int goodsSrc, Action<List<QuoteTradePlan>> successAction,
  313. Action<ErrorEntity> errorAction)
  314. {
  315. var gram = new TCPPackage40
  316. {
  317. Tag = (byte)Datagram40TagPrimaryFunction.QuerySettlePlan, //行情商品
  318. Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值
  319. };
  320. using (MemoryStream ms = new MemoryStream())
  321. {
  322. byte[] block = new byte[] { };
  323. block = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(marketTypeId));
  324. ms.Write(block, 0, block.Length);
  325. gram.SetData(ms.ToArray());
  326. }
  327. var proxy = LinkManager.Instance.SelectQuoteProxy(false, goodsSrc);
  328. if (proxy == null)
  329. {
  330. if (errorAction != null)
  331. {
  332. errorAction(new ErrorEntity()
  333. {
  334. ReturnCode = -6000,
  335. ReturnDesc = Client_Resource.Func_Desc_ConnectError,
  336. RequestFunc = Client_Resource.Function_SettlePlans
  337. });
  338. }
  339. return;
  340. }
  341. proxy.SendPackage(gram,
  342. (package) =>
  343. {
  344. byte[] contents = package.GetData();
  345. var returnCode = GetReturnCode(contents);
  346. //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法)
  347. if (returnCode >= 0)
  348. {
  349. var length = contents.Length - 4;
  350. var goodsData = contents.Skip(4).Take(length).ToArray();
  351. var errorResult = new ErrorEntity() { ReturnCode = 0, RequestFunc = Client_Resource.Function_SettlePlans };
  352. var quotePlan = _quoteDateAdapter.ConvertFromJson(goodsData, ref errorResult);
  353. if (errorResult.ReturnCode == 0) ////解析是不正确
  354. {
  355. // quotePlan.MarketTypeID = marketTypeId;
  356. if (successAction != null)
  357. {
  358. successAction(quotePlan);
  359. }
  360. }
  361. else
  362. {
  363. if (errorAction != null)
  364. {
  365. errorAction(errorResult);
  366. }
  367. }
  368. }
  369. else
  370. {
  371. if (errorAction != null)
  372. {
  373. //RequestFunc = 历史行情请求
  374. errorAction(new ErrorEntity()
  375. {
  376. ReturnCode = returnCode,
  377. ReturnDesc = string.Empty,
  378. RequestFunc = Client_Resource.Function_SettlePlans
  379. });
  380. }
  381. }
  382. }, (code, message) =>
  383. {
  384. if (errorAction != null)
  385. {
  386. errorAction(new ErrorEntity()
  387. {
  388. ReturnCode = code,
  389. ReturnDesc = message,
  390. RequestFunc = Client_Resource.Function_SettlePlans
  391. });
  392. }
  393. });
  394. }
  395. //public void QuoteCheckToken()
  396. //{
  397. // var gram = new TCPPackage40
  398. // {
  399. // Tag = (byte)Datagram40TagPrimaryFunction.QuerySettlePlan, //行情商品
  400. // Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值
  401. // };
  402. // var data= _quoteDateAdapter.QuoteCheckToken(c_AccountId, c_checkToken);
  403. // gram.SetData(data);
  404. // var proxy = LinkManager.Instance.SelectQuoteProxy(false, (int)GoodsFromScr.Brown);
  405. // if (proxy == null)
  406. // {
  407. // return;
  408. // }
  409. // proxy.SendPackage(gram, (tcpPackage) =>
  410. // {
  411. // }, (code, message) =>
  412. // {
  413. // //todo:写日志
  414. // });
  415. //}
  416. public void QuerySaleGoodsParam(Action<List<Data.Model.Sale.SaleGoods>> successAction, Action<ErrorEntity> errorAction)
  417. {
  418. var gram = new TCPPackage40
  419. {
  420. Tag = (byte)Datagram40TagPrimaryFunction.QuerySaleParam, //行情商品
  421. Tag2 = (byte)Datagram40TagPrimaryFunction.Reserve, //随便填值
  422. };
  423. var proxy = LinkManager.Instance.SelectQuoteProxy(false, (int)GoodsFromScr.Brown);
  424. if (proxy == null)
  425. {
  426. if (errorAction != null)
  427. {
  428. errorAction(new ErrorEntity()
  429. {
  430. ReturnCode = -6000,
  431. ReturnDesc = Resources.Client_Resource.Func_Desc_ConnectError,
  432. RequestFunc = Resources.Client_Resource.Function_QuoteGoods
  433. });
  434. }
  435. return;
  436. }
  437. proxy.SendPackage(gram,
  438. (package) =>
  439. {
  440. byte[] contents = package.GetData();
  441. var returnCode = GetReturnCode(contents);
  442. //// >=0: 返回记录数目; <0: 错误码(-1:报文解析失败,-2:Token校验失败,-3:订阅商品不合法)
  443. if (returnCode >= 0)
  444. {
  445. var length = contents.Length - 4;
  446. var goodsData = contents.Skip(4).Take(length).ToArray();
  447. var goodses = _quoteDateAdapter.ConvertSaleGoodsParam(goodsData, returnCode);
  448. if (successAction != null)
  449. {
  450. successAction(goodses);
  451. }
  452. }
  453. else
  454. {
  455. if (errorAction != null)
  456. {
  457. //RequestFunc = 历史行情请求
  458. errorAction(new ErrorEntity()
  459. {
  460. ReturnCode = returnCode,
  461. ReturnDesc = string.Empty,
  462. RequestFunc = Muchinfo.MTPClient.Resources.Client_Resource.Function_QuoteGoods
  463. });
  464. }
  465. }
  466. }, (code, message) =>
  467. {
  468. if (errorAction != null)
  469. {
  470. errorAction(new ErrorEntity()
  471. {
  472. ReturnCode = code,
  473. ReturnDesc = message,
  474. RequestFunc = Muchinfo.MTPClient.Resources.Client_Resource.Function_QuoteGoods
  475. });
  476. }
  477. });
  478. }
  479. /// <summary>
  480. /// 查询交易时间
  481. /// </summary>
  482. /// <param name="marketTypeId"></param>
  483. /// <param name="successAction"></param>
  484. /// <param name="errorAction"></param>
  485. public void QueryQuoteTradeTime(int marketTypeId, Action<List<QuoteTradeTime>> successAction, Action<ErrorEntity> errorAction)
  486. {
  487. var queryCommonParams = new List<QueryCommonParam>();
  488. queryCommonParams.Add(new QueryCommonParam() { ParamKey = "markettypeid", ParamValue = marketTypeId + string.Empty });
  489. QueryCommonHelper.QueryCommon(QueryStatement.QueryGoodsTradeTimeDetail, queryCommonParams,
  490. new Action<List<QuoteTradeTime>>((tradeTimes) =>
  491. {
  492. if (successAction != null)
  493. {
  494. successAction(tradeTimes);
  495. }
  496. }), (error) =>
  497. {
  498. if (errorAction != null)
  499. {
  500. error.RequestFunc = Client_Resource.Func_OpenCloseTimeQuery;
  501. errorAction(error);
  502. }
  503. });
  504. }
  505. }
  506. }