using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; using Lottomat.Application.Busines.CommonManage; using Lottomat.Application.Code; using Lottomat.Application.Entity.CommonEntity; using Lottomat.Application.Entity.LotteryNumberManage.Parameter; using Lottomat.Application.Entity.LotteryNumberManage.ViewModel; using Lottomat.SOA.API.Controllers.Base; using Lottomat.Util.Extension; using Lottomat.Utils; using Lottomat.Utils.Date; namespace Lottomat.SOA.API.Controllers.V1 { /// /// 地方彩 /// public class DFCController : BaseApiController { /// /// 公共BLL /// private static readonly CommonBLL commonBll = new CommonBLL(); #region 获取地方彩彩种历史记录 /// /// 获取地方彩彩种历史记录 /// /// [HttpPost] public HttpResponseMessage GetDFCHistoryLotteryList(HistoryLotteryArgEnyity arg) { BaseJson resultMsg = new BaseJson { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null }; Logger(typeof(DFCController), arg.TryToJson(), "获取地方彩彩种历史记录-GetDFCHistoryLotteryList", () => { if (!string.IsNullOrEmpty(arg.t)) { if (arg.t.CheckTimeStamp()) { if (!string.IsNullOrEmpty(arg.EnumCode)) { bool isSucc = Enum.TryParse(arg.EnumCode, true, out SCCLottery type); //SCCLottery type = (SCCLottery)Enum.Parse(typeof(SCCLottery), arg.EnumCode, true); if (!isSucc) { resultMsg = new BaseJson { Status = (int)JsonObjectStatus.Fail, Data = null, Message = $"参数值{arg.EnumCode}无效。", BackUrl = null }; } else { //获取组装完成后的Json字符串 string res = GetResultByEnumCode(type, arg); resultMsg = new BaseJson { Status = (int)JsonObjectStatus.Success, Data = res.ToString(), Message = JsonObjectStatus.Success.GetEnumText(), BackUrl = null }; } } else { resultMsg = new BaseJson { Status = (int)JsonObjectStatus.Fail, Data = null, Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数EnumCode为空。", BackUrl = null }; } } else { resultMsg = new BaseJson { Status = (int)JsonObjectStatus.Fail, Data = null, Message = JsonObjectStatus.Fail.GetEnumText() + ",无效参数。", BackUrl = null }; } } else { resultMsg = new BaseJson { Status = (int)JsonObjectStatus.Fail, Data = null, Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数为空。", BackUrl = null }; } }, e => { resultMsg = new BaseJson { Status = (int)JsonObjectStatus.Exception, Data = null, Message = JsonObjectStatus.Exception.GetEnumText() + ",异常信息:" + e.Message, BackUrl = null }; }); return resultMsg.TryToJson().ToHttpResponseMessage(); } /// /// 获取开奖历史 /// /// /// /// private string GetResultByEnumCode(SCCLottery type, HistoryLotteryArgEnyity arg) { string res = String.Empty; DataTable data = GetData(type, arg); switch (type) { case SCCLottery.DF6J1: res = AppendCommonResult(data, SCCLottery.DF6J1); break; case SCCLottery.HD15X5: res = AppendHD15X5Result(data, SCCLottery.HD15X5); break; default: res = AppendCommonResult(data, type); break; } return res; } /// /// 组装公共记录 /// /// /// /// private string AppendCommonResult(DataTable data, SCCLottery type) { List res = new List(); if (data.Rows.Count > 0) { //总共球个数 int total = type.GetEnumText().TryToInt32(); for (int j = 0; j < data.Rows.Count; j++) { //开奖号集合 List openCodeList = new List(); StringBuilder builder = new StringBuilder(); for (int i = 1; i <= total; i++) { int openCode = data.Rows[j]["OpenCode" + i].TryToInt32(); openCodeList.Add(openCode); } builder.Append(GetOpenCodeTemplate(type, openCodeList)); res.Add(new DFCCommonHistoryLotteryViewEntity { Term = data.Rows[j]["Term"].ToStringEx(), OpenTime = data.Rows[j]["OpenTime"].TryToDateTimeToString("yyyy-MM-dd"), NormalOpenCode = builder.ToString(), ParityRatio = LotteryUtils.GetProportionOfJO(openCodeList), Parity = LotteryUtils.GetJOString(openCodeList), TheSum = LotteryUtils.GetTheSum(openCodeList, 0, GetSumNumberCount(type), false) }); } } return res.ToJson(); } /// /// 组装华东15选记录 /// /// /// /// private string AppendHD15X5Result(DataTable data, SCCLottery type) { List res = new List(); if (data.Rows.Count > 0) { //总共球个数 int total = type.GetEnumText().TryToInt32(); for (int j = 0; j < data.Rows.Count; j++) { //开奖号集合 List openCodeList = new List(); List openCodeListStr = new List(); StringBuilder builder = new StringBuilder(); for (int i = 1; i <= total; i++) { int openCode = data.Rows[j]["OpenCode" + i].TryToInt32(); openCodeList.Add(openCode); openCodeListStr.Add(openCode.ToString()); } builder.Append(GetOpenCodeTemplate(type, openCodeList)); string ac; try { ac = LotteryUtils.GetAC(openCodeListStr.ToArray()).ToString(); } catch (Exception) { ac = ""; } res.Add(new DFCHD15X5HistoryLotteryViewEntity { Term = data.Rows[j]["Term"].ToStringEx(), OpenTime = data.Rows[j]["OpenTime"].TryToDateTimeToString("yyyy-MM-dd"), NormalOpenCode = builder.ToString(), Parity = LotteryUtils.GetJOString(openCodeList, "双", "单"), TheSum = LotteryUtils.GetTheSum(openCodeList, GetSizeRatioSplitNumber(type), GetSumNumberCount(type)), Size = LotteryUtils.GetDXString(openCodeList, 5), ThreeZoneRatio = LotteryUtils.Hd15x5SanQu(openCodeList), SizeRatio = LotteryUtils.GetProportionOfDX(openCodeList, 6), ParityRatio = LotteryUtils.GetProportionOfJO(openCodeList), RatioOf012 = LotteryUtils.GetProportionOf012(openCodeList), Span = LotteryUtils.GetSpan(openCodeList).ToString(), AC = ac }); } } return res.ToJson(); } /// /// 查询数据集 /// /// /// /// private DataTable GetData(SCCLottery type, HistoryLotteryArgEnyity arg) { string key = $"GetDFCHistoryLotteryList/{type.ToString()}/{arg.EnumCode}/{arg.TotalRecord}/{arg.StartTime}/{arg.Year}"; DataTable o = null; if (webCache.IsExist(key)) { o = webCache.GetObject(key) as DataTable; } else { //组装查询语句 string sql = GetSeleteSQL(type, arg); //查询结果 o = commonBll.ExcuteSqlDataTable(sql, DatabaseLinksEnum.LotteryNumber, null); webCache.AddObject(key, o, (int)CacheTime.Data); } return o; } #endregion #region 公共私有方法 /// /// 组装查询语句 /// /// 枚举码 /// /// private string GetSeleteSQL(SCCLottery type, HistoryLotteryArgEnyity arg) { StringBuilder builder = new StringBuilder(); string res = String.Empty; int total = type.GetEnumText().TryToInt32(); string tableName = type.GetSCCLotteryTableName(); for (int i = 1; i <= total; i++) { builder.Append("[OpenCode" + i + "],"); } if (!arg.Year.HasValue) { arg.Year = 0; } if (arg.TotalRecord > 0) { res = string.Format(GetLotterySqlByTableNameWithTop, arg.TotalRecord, StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year); } else if (!string.IsNullOrEmpty(arg.StartTime)) { string time = arg.StartTime.CheckDateTime() ? arg.StartTime : DateTimeHelper.Now.AddDays(-7).ToString("yyyy-MM-dd"); res = string.Format(GetLotterySqlByTableNameWithStartTime, StringHelper.DelLastChar(builder.ToString(), ","), tableName, time, arg.Year); } else if (arg.Year.HasValue) { res = string.Format(GetLotterySqlByTableName, StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year); } else { res = string.Format(GetLotterySqlByTableNameWithTop, "20", StringHelper.DelLastChar(builder.ToString(), ","), tableName, arg.Year); } return res; } #endregion #region SQL语句 /// /// 通过年份表名查询数据为校验后的所有数据 /// 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 /// /// 通过表名查询数据为校验后的前n行数据 /// 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 /// /// 通过开奖时间查询数据为校验后的所有数据 /// 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 #endregion } }