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
{
///
/// 高频彩快3
///
public class GPCK3Controller : BaseApiController
{
///
/// 公共BLL
///
private static readonly CommonBLL commonBll = new CommonBLL();
#region 获取高频彩快3彩种历史记录
///
/// 获取高频彩快3彩种历史记录
///
///
[HttpPost]
public HttpResponseMessage GetGPCK3HistoryLotteryList(HistoryLotteryArgEnyity arg)
{
BaseJson resultMsg = new BaseJson { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
Logger(typeof(GPCK3Controller), arg.TryToJson(), "获取高频彩快3彩种历史记录-GetGPCK3HistoryLotteryList", () =>
{
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.BeiJingK3:
res = AppendCommonResult(data, SCCLottery.BeiJingK3);
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 GPCK3HistoryLotteryViewEntity
{
Term = data.Rows[j]["Term"].ToStringEx(),
OpenTime = data.Rows[j]["OpenTime"].TryToDateTimeToString("yyyy-MM-dd HH:mm:ss"),
NormalOpenCode = builder.ToString(),
TheSum = LotteryUtils.GetTheSumByK3(openCodeList, 10),
SizeRatio = LotteryUtils.GetProportionOfDX(openCodeList, 11),
ParityRatio = LotteryUtils.GetProportionOfJO(openCodeList),
Span = LotteryUtils.GetSpan(openCodeList).ToString(),
PrimeAndNumberRatio = LotteryUtils.GetProportionOfZh(openCodeList)
});
}
}
return res.ToJson();
}
///
/// 查询数据集
///
///
///
///
private DataTable GetData(SCCLottery type, HistoryLotteryArgEnyity arg)
{
string key = $"GetGPCK3HistoryLotteryList/{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
}
}