DFCController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /// 地方彩
  22. /// </summary>
  23. public class DFCController : BaseApiController
  24. {
  25. /// <summary>
  26. /// 公共BLL
  27. /// </summary>
  28. private static readonly CommonBLL commonBll = new CommonBLL();
  29. #region 获取地方彩彩种历史记录
  30. /// <summary>
  31. /// 获取地方彩彩种历史记录
  32. /// </summary>
  33. /// <returns></returns>
  34. [HttpPost]
  35. public HttpResponseMessage GetDFCHistoryLotteryList(HistoryLotteryArgEnyity arg)
  36. {
  37. BaseJson<string> resultMsg = new BaseJson<string> { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
  38. Logger(typeof(DFCController), arg.TryToJson(), "获取地方彩彩种历史记录-GetDFCHistoryLotteryList", () =>
  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.DF6J1:
  128. res = AppendCommonResult(data, SCCLottery.DF6J1);
  129. break;
  130. case SCCLottery.HD15X5:
  131. res = AppendHD15X5Result(data, SCCLottery.HD15X5);
  132. break;
  133. default:
  134. res = AppendCommonResult(data, type);
  135. break;
  136. }
  137. return res;
  138. }
  139. /// <summary>
  140. /// 组装公共记录
  141. /// </summary>
  142. /// <param name="data"></param>
  143. /// <param name="type"></param>
  144. /// <returns></returns>
  145. private string AppendCommonResult(DataTable data, SCCLottery type)
  146. {
  147. List<DFCCommonHistoryLotteryViewEntity> res = new List<DFCCommonHistoryLotteryViewEntity>();
  148. if (data.Rows.Count > 0)
  149. {
  150. //总共球个数
  151. int total = type.GetEnumText().TryToInt32();
  152. for (int j = 0; j < data.Rows.Count; j++)
  153. {
  154. //开奖号集合
  155. List<int> openCodeList = new List<int>();
  156. StringBuilder builder = new StringBuilder();
  157. for (int i = 1; i <= total; i++)
  158. {
  159. int openCode = data.Rows[j]["OpenCode" + i].TryToInt32();
  160. openCodeList.Add(openCode);
  161. }
  162. builder.Append(GetOpenCodeTemplate(type, openCodeList));
  163. res.Add(new DFCCommonHistoryLotteryViewEntity
  164. {
  165. Term = data.Rows[j]["Term"].ToStringEx(),
  166. OpenTime = data.Rows[j]["OpenTime"].TryToDateTimeToString("yyyy-MM-dd"),
  167. NormalOpenCode = builder.ToString(),
  168. ParityRatio = LotteryUtils.GetProportionOfJO(openCodeList),
  169. Parity = LotteryUtils.GetJOString(openCodeList),
  170. TheSum = LotteryUtils.GetTheSum(openCodeList, 0, GetSumNumberCount(type), false)
  171. });
  172. }
  173. }
  174. return res.ToJson();
  175. }
  176. /// <summary>
  177. /// 组装华东15选记录
  178. /// </summary>
  179. /// <param name="data"></param>
  180. /// <param name="type"></param>
  181. /// <returns></returns>
  182. private string AppendHD15X5Result(DataTable data, SCCLottery type)
  183. {
  184. List<DFCHD15X5HistoryLotteryViewEntity> res = new List<DFCHD15X5HistoryLotteryViewEntity>();
  185. if (data.Rows.Count > 0)
  186. {
  187. //总共球个数
  188. int total = type.GetEnumText().TryToInt32();
  189. for (int j = 0; j < data.Rows.Count; j++)
  190. {
  191. //开奖号集合
  192. List<int> openCodeList = new List<int>();
  193. List<string> openCodeListStr = new List<string>();
  194. StringBuilder builder = new StringBuilder();
  195. for (int i = 1; i <= total; i++)
  196. {
  197. int openCode = data.Rows[j]["OpenCode" + i].TryToInt32();
  198. openCodeList.Add(openCode);
  199. openCodeListStr.Add(openCode.ToString());
  200. }
  201. builder.Append(GetOpenCodeTemplate(type, openCodeList));
  202. string ac;
  203. try
  204. {
  205. ac = LotteryUtils.GetAC(openCodeListStr.ToArray()).ToString();
  206. }
  207. catch (Exception)
  208. {
  209. ac = "";
  210. }
  211. res.Add(new DFCHD15X5HistoryLotteryViewEntity
  212. {
  213. Term = data.Rows[j]["Term"].ToStringEx(),
  214. OpenTime = data.Rows[j]["OpenTime"].TryToDateTimeToString("yyyy-MM-dd"),
  215. NormalOpenCode = builder.ToString(),
  216. Parity = LotteryUtils.GetJOString(openCodeList, "双", "单"),
  217. TheSum = LotteryUtils.GetTheSum(openCodeList, GetSizeRatioSplitNumber(type), GetSumNumberCount(type)),
  218. Size = LotteryUtils.GetDXString(openCodeList, 5),
  219. ThreeZoneRatio = LotteryUtils.Hd15x5SanQu(openCodeList),
  220. SizeRatio = LotteryUtils.GetProportionOfDX(openCodeList, 6),
  221. ParityRatio = LotteryUtils.GetProportionOfJO(openCodeList),
  222. RatioOf012 = LotteryUtils.GetProportionOf012(openCodeList),
  223. Span = LotteryUtils.GetSpan(openCodeList).ToString(),
  224. AC = ac
  225. });
  226. }
  227. }
  228. return res.ToJson();
  229. }
  230. /// <summary>
  231. /// 查询数据集
  232. /// </summary>
  233. /// <param name="type"></param>
  234. /// <param name="arg"></param>
  235. /// <returns></returns>
  236. private DataTable GetData(SCCLottery type, HistoryLotteryArgEnyity arg)
  237. {
  238. string key = $"GetDFCHistoryLotteryList/{type.ToString()}/{arg.EnumCode}/{arg.TotalRecord}/{arg.StartTime}/{arg.Year}";
  239. DataTable o = null;
  240. if (webCache.IsExist(key))
  241. {
  242. o = webCache.GetObject<DataTable>(key) as DataTable;
  243. }
  244. else
  245. {
  246. //组装查询语句
  247. string sql = GetSeleteSQL(type, arg);
  248. //查询结果
  249. o = commonBll.ExcuteSqlDataTable(sql, DatabaseLinksEnum.LotteryNumber, null);
  250. webCache.AddObject(key, o, (int)CacheTime.Data);
  251. }
  252. return o;
  253. }
  254. #endregion
  255. #region 公共私有方法
  256. /// <summary>
  257. /// 组装查询语句
  258. /// </summary>
  259. /// <param name="type">枚举码</param>
  260. /// <param name="arg"></param>
  261. /// <returns></returns>
  262. private string GetSeleteSQL(SCCLottery type, HistoryLotteryArgEnyity arg)
  263. {
  264. StringBuilder builder = new StringBuilder();
  265. string res = String.Empty;
  266. int total = type.GetEnumText().TryToInt32();
  267. string tableName = type.GetSCCLotteryTableName();
  268. for (int i = 1; i <= total; i++)
  269. {
  270. builder.Append("[OpenCode" + i + "],");
  271. }
  272. if (!arg.Year.HasValue)
  273. {
  274. arg.Year = 0;
  275. }
  276. if (arg.TotalRecord > 0)
  277. {
  278. res = string.Format(GetLotterySqlByTableNameWithTop, arg.TotalRecord, StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year);
  279. }
  280. else if (!string.IsNullOrEmpty(arg.StartTime))
  281. {
  282. string time = arg.StartTime.CheckDateTime()
  283. ? arg.StartTime
  284. : DateTimeHelper.Now.AddDays(-7).ToString("yyyy-MM-dd");
  285. res = string.Format(GetLotterySqlByTableNameWithStartTime, StringHelper.DelLastChar(builder.ToString(), ","), tableName, time, arg.Year);
  286. }
  287. else if (arg.Year.HasValue)
  288. {
  289. res = string.Format(GetLotterySqlByTableName, StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year);
  290. }
  291. else
  292. {
  293. res = string.Format(GetLotterySqlByTableNameWithTop, "20", StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year);
  294. }
  295. return res;
  296. }
  297. #endregion
  298. #region SQL语句
  299. /// <summary>
  300. /// 通过年份表名查询数据为校验后的所有数据
  301. /// </summary>
  302. 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
  303. /// <summary>
  304. /// 通过表名查询数据为校验后的前n行数据
  305. /// </summary>
  306. 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
  307. /// <summary>
  308. /// 通过开奖时间查询数据为校验后的所有数据
  309. /// </summary>
  310. 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
  311. #endregion
  312. }
  313. }