KL12Trend.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CB.Common;
  6. using CB.Data;
  7. using CB.Data.Frequency;
  8. using CB.Data.Lottery.Frequency;
  9. using CB.Entity;
  10. using CB.Entity.Frequency;
  11. using CB.Interface;
  12. using CB.Interface.Infrastructure;
  13. using CB.Interface.Infrastructure.Frequency;
  14. using System.Collections.Concurrent;
  15. namespace CB.TrendChart.FrequencyTrendChart
  16. {
  17. /// <summary>
  18. /// 高频彩-快乐12通用走势图生成工具
  19. /// </summary>
  20. public class KL12Trend<T> where T : LotteryOpenCode, new()
  21. {
  22. //public static int Cid = 59;//Cid用于设置样式值
  23. private static readonly ConcurrentDictionary<string, string[]> cdict = new ConcurrentDictionary<string, string[]>();
  24. #region DJP
  25. public static Tuple<bool, string, List<TrendChartData>> CreateGPTrendChart(int cId, int chartId, TrendChartType chartType, string tableName, GpLotterySearchField fields = null)
  26. {
  27. var cacheKey = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", cId, chartId, (int)chartType, tableName.Substring(3), fields.EndTerm, fields.SearchDate.Value.ToString("yyyyMMdd"), fields.SearchDay ? "1" : "0", fields.StartTerm, fields.TopSize, fields.Year);//缓存Key
  28. var t = System.Threading.Tasks.Task.Factory.StartNew(() =>
  29. {
  30. var cacheData = CB.Cache.CBCache.GetCacheService().GetObject(cacheKey);
  31. if (cacheData != null)
  32. return new Tuple<bool, string, List<TrendChartData>>(true, null, (List<TrendChartData>)cacheData);
  33. do
  34. {
  35. System.Threading.Thread.Sleep(1);
  36. } while (cdict.ContainsKey(cacheKey));
  37. return CreateGpTrendChart(cacheKey, cId, chartId, chartType, tableName, fields);
  38. });
  39. System.Threading.Tasks.Task.WaitAny(new System.Threading.Tasks.Task[] { t }, 1000);
  40. return t.Result;
  41. }
  42. /// <summary>
  43. /// 动态生成高频走势图,生成当前term数据及以后数据
  44. /// midified by djp 2016-06-08 修正传入cid用于设置样式值
  45. /// </summary>
  46. /// <param name="cId">彩种编号</param>
  47. /// <param name="chartId">走势图ID</param>
  48. /// <param name="chartType">走势图类型</param>
  49. /// <param name="fields">彩种开奖数据额外查询条件</param>
  50. /// <returns></returns>
  51. private static Tuple<bool, string, List<TrendChartData>> CreateGpTrendChart(string cacheKey, int cId, int chartId, TrendChartType chartType, string tableName, GpLotterySearchField fields = null)
  52. {
  53. //获取走势图项及配置数据
  54. //var cacheKey = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", cId, chartId, (int)chartType, tableName.Substring(3), fields.EndTerm, fields.SearchDate.Value.ToString("yyyyMMdd"), fields.SearchDay ? "1" : "0", fields.StartTerm, fields.TopSize, fields.Year);//缓存Key
  55. var cacheData = CB.Cache.CBCache.GetCacheService().GetObject(cacheKey);
  56. if (cacheData != null)
  57. {
  58. return new Tuple<bool, string, List<TrendChartData>>(true, null, (List<TrendChartData>)cacheData);
  59. }
  60. var trendChartItem = TrendChartItemService.ToListByChartId(chartId, chartType);
  61. if (null == trendChartItem || 0 >= trendChartItem.Count)
  62. return new Tuple<bool, string, List<TrendChartData>>(false, string.Format("未找到【ChartId={0}】走势图的项配置数据", chartId), null);
  63. int count = trendChartItem.Count;
  64. IList<IChartItem<T>> chartItem = new List<IChartItem<T>>(count);
  65. IList<ChartCssConfigInfo> cssConfig = new List<ChartCssConfigInfo>(count);
  66. foreach (var item in trendChartItem)
  67. {
  68. //实例化项类型
  69. chartItem.Add(TrendChartUtils.GetTrendChartInterface<T>(item.ClassName));
  70. //获取项CSS配置信息
  71. cssConfig.Add(ChartCssConfigService.Get(item.FuntionType, item.ChartCssId));
  72. }
  73. //各项初始化
  74. for (int i = 0; i < count; i++)
  75. {
  76. trendChartItem[i].Cid = cId;
  77. chartItem[i].Init(cssConfig[i], trendChartItem[i]);
  78. }
  79. //大于等于term开奖数据
  80. IList<T> ListToEnd = GPTrendService<T>.GetListToEnd(tableName, fields);
  81. if (null == ListToEnd || 0 >= ListToEnd.Count)
  82. return new Tuple<bool, string, List<TrendChartData>>(false, "未找到有效开奖数据", null);
  83. int j = 0;
  84. TrendChartData entity = null;
  85. var TrendChartDataList = new List<TrendChartData>();
  86. foreach (var currentData in ListToEnd)
  87. {
  88. var ResultEntity = new TrendChartData
  89. {
  90. ChartId = chartId,
  91. ChartType = chartType,
  92. LocalMiss = new string[count],
  93. LastMiss = new string[count],
  94. AllMaxMiss = new string[count],
  95. AllAvgMiss = new string[count],
  96. AllTimes = new string[count]
  97. };
  98. //根据参数term期数获取近两期开奖信息TOP 2 [Term]<=term ORDER BY [Term] DESC
  99. IList<T> list = ListToEnd.Where(p => p.Term <= currentData.Term).OrderByDescending(p => p.Term).Take(2).ToList();
  100. //GP11x5SiChuanService.ToListForTrend(currentData.Term, fields);
  101. var info = list[0]; //ListToEnd[ListToEnd.Count - 1];
  102. // info = list[0];
  103. if (2 == list.Count)
  104. {
  105. //取当前期数的上一期的走势图信息及遗漏数据
  106. if (0 == j)
  107. {
  108. entity = SCKL12TrendChartDataService.GetTrendChartDataByTerm(chartId, chartType, list[1].Term);
  109. }
  110. else if (TrendChartDataList.Count > 0)
  111. {
  112. entity = TrendChartDataList[TrendChartDataList.Count - 1];
  113. }
  114. }
  115. bool yes = true;
  116. var sp = new StringBuilder(20000);
  117. sp.Append("<tr>");
  118. for (int i = 0; i < count; i++)
  119. {
  120. //初始化遗漏数据(根据上期结果集计算_entity)
  121. chartItem[i].MissDataInit(entity, i);
  122. //计算项值及遗漏计算
  123. yes = yes && chartItem[i].SetItemValue(info);
  124. if (yes)
  125. {
  126. //结果集赋值
  127. ResultEntity.LocalMiss[i] = chartItem[i].GetMissData(MissDataType.LocalMiss);
  128. ResultEntity.LastMiss[i] = chartItem[i].GetMissData(MissDataType.LastMiss);
  129. ResultEntity.AllMaxMiss[i] = chartItem[i].GetMissData(MissDataType.AllMaxMiss);
  130. ResultEntity.AllAvgMiss[i] = chartItem[i].GetMissData(MissDataType.AllAvgMiss);
  131. ResultEntity.AllTimes[i] = chartItem[i].GetMissData(MissDataType.AllTimes);
  132. sp.Append(chartItem[i].GetFomartString("<td {0}>{1}</td>"));
  133. }
  134. }
  135. sp.Append("</tr>");
  136. if (null != entity)
  137. ResultEntity.RecordCount = entity.RecordCount + 1;
  138. else
  139. ResultEntity.RecordCount = 1;
  140. ResultEntity.Term = currentData.Term;
  141. ResultEntity.HtmlData = sp.ToString();
  142. TrendChartDataList.Add(ResultEntity);
  143. if (!yes)
  144. {
  145. return new Tuple<bool, string, List<TrendChartData>>(false, string.Format("无效开奖数据:数据生成截止期数为【term={0}】", ResultEntity.Term), TrendChartDataList);
  146. }
  147. j++;
  148. }
  149. TrendChartDataList.Reverse();
  150. //加入缓存int cId, int chartId, TrendChartType chartType, string tableName, GpLotterySearchField fields = null
  151. CB.Cache.CBCache.GetCacheService().AddObject(cacheKey, TrendChartDataList, 240);
  152. return new Tuple<bool, string, List<TrendChartData>>(true, string.Format("正常生成完毕,截止期数为:Term={0}", ListToEnd[ListToEnd.Count - 1].Term), TrendChartDataList);
  153. }
  154. #endregion
  155. }
  156. }