GPCK3Controller.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Text;
  8. using System.Web.Http;
  9. using Lottomat.Application.Busines.CommonManage;
  10. using Lottomat.Application.Code;
  11. using Lottomat.Application.Entity.CommonEntity;
  12. using Lottomat.Application.Entity.LotteryNumberManage.Parameter;
  13. using Lottomat.Application.Entity.LotteryNumberManage.ViewModel;
  14. using Lottomat.SOA.API.Controllers.Base;
  15. using Lottomat.Util.Extension;
  16. using Lottomat.Utils;
  17. using Lottomat.Utils.Date;
  18. namespace Lottomat.SOA.API.Controllers.V1
  19. {
  20. /// <summary>
  21. /// 高频彩快3
  22. /// </summary>
  23. public class GPCK3Controller : BaseApiController
  24. {
  25. /// <summary>
  26. /// 公共BLL
  27. /// </summary>
  28. private static readonly CommonBLL commonBll = new CommonBLL();
  29. #region 获取高频彩快3彩种历史记录
  30. /// <summary>
  31. /// 获取高频彩快3彩种历史记录
  32. /// </summary>
  33. /// <returns></returns>
  34. [HttpPost]
  35. public HttpResponseMessage GetGPCK3HistoryLotteryList(HistoryLotteryArgEnyity arg)
  36. {
  37. BaseJson<string> resultMsg = new BaseJson<string> { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
  38. Logger(typeof(GPCK3Controller), arg.TryToJson(), "获取高频彩快3彩种历史记录-GetGPCK3HistoryLotteryList", () =>
  39. {
  40. if (!string.IsNullOrEmpty(arg.t))
  41. {
  42. if (arg.t.CheckTimeStamp())
  43. {
  44. if (!string.IsNullOrEmpty(arg.EnumCode))
  45. {
  46. bool isSucc = Enum.TryParse<SCCLottery>(arg.EnumCode, true, out SCCLottery type);
  47. //SCCLottery type = (SCCLottery)Enum.Parse(typeof(SCCLottery), arg.EnumCode, true);
  48. if (!isSucc)
  49. {
  50. resultMsg = new BaseJson<string>
  51. {
  52. Status = (int)JsonObjectStatus.Fail,
  53. Data = null,
  54. Message = $"参数值{arg.EnumCode}无效。",
  55. BackUrl = null
  56. };
  57. }
  58. else
  59. {
  60. //获取组装完成后的Json字符串
  61. string res = GetResultByEnumCode(type, arg);
  62. resultMsg = new BaseJson<string>
  63. {
  64. Status = (int)JsonObjectStatus.Success,
  65. Data = res.ToString(),
  66. Message = JsonObjectStatus.Success.GetEnumText(),
  67. BackUrl = null
  68. };
  69. }
  70. }
  71. else
  72. {
  73. resultMsg = new BaseJson<string>
  74. {
  75. Status = (int)JsonObjectStatus.Fail,
  76. Data = null,
  77. Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数EnumCode为空。",
  78. BackUrl = null
  79. };
  80. }
  81. }
  82. else
  83. {
  84. resultMsg = new BaseJson<string>
  85. {
  86. Status = (int)JsonObjectStatus.Fail,
  87. Data = null,
  88. Message = JsonObjectStatus.Fail.GetEnumText() + ",无效参数。",
  89. BackUrl = null
  90. };
  91. }
  92. }
  93. else
  94. {
  95. resultMsg = new BaseJson<string>
  96. {
  97. Status = (int)JsonObjectStatus.Fail,
  98. Data = null,
  99. Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数为空。",
  100. BackUrl = null
  101. };
  102. }
  103. }, e =>
  104. {
  105. resultMsg = new BaseJson<string>
  106. {
  107. Status = (int)JsonObjectStatus.Exception,
  108. Data = null,
  109. Message = JsonObjectStatus.Exception.GetEnumText() + ",异常信息:" + e.Message,
  110. BackUrl = null
  111. };
  112. });
  113. return resultMsg.TryToJson().ToHttpResponseMessage();
  114. }
  115. /// <summary>
  116. /// 获取开奖历史
  117. /// </summary>
  118. /// <param name="type"></param>
  119. /// <param name="arg"></param>
  120. /// <returns></returns>
  121. private string GetResultByEnumCode(SCCLottery type, HistoryLotteryArgEnyity arg)
  122. {
  123. string res = String.Empty;
  124. DataTable data = GetData(type, arg);
  125. switch (type)
  126. {
  127. case SCCLottery.BeiJingK3:
  128. res = AppendCommonResult(data, SCCLottery.BeiJingK3);
  129. break;
  130. default:
  131. res = AppendCommonResult(data, type);
  132. break;
  133. }
  134. return res;
  135. }
  136. /// <summary>
  137. /// 组装公共记录
  138. /// </summary>
  139. /// <param name="data"></param>
  140. /// <param name="type"></param>
  141. /// <returns></returns>
  142. private string AppendCommonResult(DataTable data, SCCLottery type)
  143. {
  144. List<GPCK3HistoryLotteryViewEntity> res = new List<GPCK3HistoryLotteryViewEntity>();
  145. if (data.Rows.Count > 0)
  146. {
  147. //总共球个数
  148. int total = type.GetEnumText().TryToInt32();
  149. for (int j = 0; j < data.Rows.Count; j++)
  150. {
  151. //开奖号集合
  152. List<int> openCodeList = new List<int>();
  153. StringBuilder builder = new StringBuilder();
  154. for (int i = 1; i <= total; i++)
  155. {
  156. int openCode = data.Rows[j]["OpenCode" + i].TryToInt32();
  157. openCodeList.Add(openCode);
  158. }
  159. builder.Append(GetOpenCodeTemplate(type, openCodeList));
  160. res.Add(new GPCK3HistoryLotteryViewEntity
  161. {
  162. Term = data.Rows[j]["Term"].ToStringEx(),
  163. OpenTime = data.Rows[j]["OpenTime"].TryToDateTimeToString("yyyy-MM-dd HH:mm:ss"),
  164. NormalOpenCode = builder.ToString(),
  165. TheSum = LotteryUtils.GetTheSumByK3(openCodeList, 10),
  166. SizeRatio = LotteryUtils.GetProportionOfDX(openCodeList, 11),
  167. ParityRatio = LotteryUtils.GetProportionOfJO(openCodeList),
  168. Span = LotteryUtils.GetSpan(openCodeList).ToString(),
  169. PrimeAndNumberRatio = LotteryUtils.GetProportionOfZh(openCodeList)
  170. });
  171. }
  172. }
  173. return res.ToJson();
  174. }
  175. /// <summary>
  176. /// 查询数据集
  177. /// </summary>
  178. /// <param name="type"></param>
  179. /// <param name="arg"></param>
  180. /// <returns></returns>
  181. private DataTable GetData(SCCLottery type, HistoryLotteryArgEnyity arg)
  182. {
  183. string key = $"GetGPCK3HistoryLotteryList/{type.ToString()}/{arg.EnumCode}/{arg.TotalRecord}/{arg.StartTime}/{arg.Year}";
  184. DataTable o = null;
  185. if (webCache.IsExist(key))
  186. {
  187. o = webCache.GetObject<DataTable>(key) as DataTable;
  188. }
  189. else
  190. {
  191. //组装查询语句
  192. string sql = GetSeleteSQL(type, arg);
  193. //查询结果
  194. o = commonBll.ExcuteSqlDataTable(sql, DatabaseLinksEnum.LotteryNumber, null);
  195. webCache.AddObject(key, o, (int)CacheTime.Data);
  196. }
  197. return o;
  198. }
  199. #endregion
  200. #region 公共私有方法
  201. /// <summary>
  202. /// 组装查询语句
  203. /// </summary>
  204. /// <param name="type">枚举码</param>
  205. /// <param name="arg"></param>
  206. /// <returns></returns>
  207. private string GetSeleteSQL(SCCLottery type, HistoryLotteryArgEnyity arg)
  208. {
  209. StringBuilder builder = new StringBuilder();
  210. string res = String.Empty;
  211. int total = type.GetEnumText().TryToInt32();
  212. string tableName = type.GetSCCLotteryTableName();
  213. for (int i = 1; i <= total; i++)
  214. {
  215. builder.Append("[OpenCode" + i + "],");
  216. }
  217. if (!arg.Year.HasValue)
  218. {
  219. arg.Year = 0;
  220. }
  221. if (arg.TotalRecord > 0)
  222. {
  223. res = string.Format(GetLotterySqlByTableNameWithTop, arg.TotalRecord, StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year);
  224. }
  225. else if (!string.IsNullOrEmpty(arg.StartTime))
  226. {
  227. string time = arg.StartTime.CheckDateTime()
  228. ? arg.StartTime
  229. : DateTimeHelper.Now.AddDays(-7).ToString("yyyy-MM-dd");
  230. res = string.Format(GetLotterySqlByTableNameWithStartTime, StringHelper.DelLastChar(builder.ToString(), ","), tableName, time, arg.Year);
  231. }
  232. else if (arg.Year.HasValue)
  233. {
  234. res = string.Format(GetLotterySqlByTableName, StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year);
  235. }
  236. else
  237. {
  238. res = string.Format(GetLotterySqlByTableNameWithTop, "20", StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year);
  239. }
  240. return res;
  241. }
  242. #endregion
  243. #region SQL语句
  244. /// <summary>
  245. /// 通过年份表名查询数据为校验后的所有数据
  246. /// </summary>
  247. private static string GetLotterySqlByTableName = @"SELECT [ID],[Term],[OpenTime],[Spare],{0} FROM [dbo].[{1}] where year([OpenTime])={2} ORDER BY Term DESC ";//WHERE [IsChecked] = 1 AND [IsPassed] = 1
  248. /// <summary>
  249. /// 通过表名查询数据为校验后的前n行数据
  250. /// </summary>
  251. private static string GetLotterySqlByTableNameWithTop = @"SELECT TOP {0} [ID],[Term],[OpenTime],[Spare],{1} FROM [dbo].[{2}] where {3} =0 or year([OpenTime])={3} ORDER BY Term DESC ";//WHERE [IsChecked] = 1 AND [IsPassed] = 1
  252. /// <summary>
  253. /// 通过开奖时间查询数据为校验后的所有数据
  254. /// </summary>
  255. private static string GetLotterySqlByTableNameWithStartTime = @"SELECT [ID],[Term],[OpenTime],[Spare],{0} FROM [dbo].[{1}] WHERE DATEDIFF(DAY,'{2}',OpenTime) = 0 and ({3} =0 or year([OpenTime])={3}) ORDER BY Term DESC";//AND [IsChecked] = 1 AND [IsPassed] = 1
  256. #endregion
  257. }
  258. }