using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Xml; using System.Net; using System.Net.Mail; using SCC.Models; using Quartz; namespace SCC.Common { /// /// 公用帮助类 /// public static class CommonHelper { /// /// 本系统当前时间 /// (早上8点之前为昨天,8点之后为今天) /// public static DateTime SCCSysDateTime { get { return DateTime.Now.AddHours(-8); } } /// /// 将XML内容转换成目标对象实体集合 /// /// 目标对象实体 /// 完整文件名(根目录下只需文件名称) /// /// public static List ConvertXMLToObject(string FileName, string WrapperNodeName) { XmlDocument doc = new XmlDocument(); doc.Load(FileName); List result = new List(); var TType = typeof(T); XmlNodeList nodeList = doc.ChildNodes; if (!string.IsNullOrEmpty(WrapperNodeName)) { foreach (XmlNode node in doc.ChildNodes) { if (node.Name == WrapperNodeName) { nodeList = node.ChildNodes; break; } } } object oneT = null; foreach (XmlNode node in nodeList) { if (node.NodeType == XmlNodeType.Comment || node.NodeType == XmlNodeType.XmlDeclaration) continue; oneT = TType.Assembly.CreateInstance(TType.FullName); foreach (XmlNode item in node.ChildNodes) { if (item.NodeType == XmlNodeType.Comment) continue; var property = TType.GetProperty(item.Name); if (property != null) property.SetValue(oneT, Convert.ChangeType(item.InnerText, property.PropertyType), null); } result.Add((T)oneT); } return result; } /// /// 从作业数据地图中获取配置信息 /// /// 作业数据地图 /// public static SCCConfig GetConfigFromDataMap(JobDataMap datamap) { SCCConfig config = new SCCConfig(); var properties = typeof(SCCConfig).GetProperties(); foreach (PropertyInfo info in properties) { if (info.PropertyType == typeof(string)) info.SetValue(config, datamap.GetString(info.Name), null); else if (info.PropertyType == typeof(Int32)) info.SetValue(config, datamap.GetInt(info.Name), null); } return config; } #region 生成期号 /// /// 通过期号编号生成形如YYMMDDQQ的期号 /// /// 期号编号 /// public static string GenerateTodayQiHaoYYMMDDQQ(int QNum) { return GenerateQiHaoYYMMDDQQ(SCCSysDateTime, QNum); } /// /// 通过期号编号生成形如YYMMDDQQQ的期号 /// /// 期号编号 /// public static string GenerateTodayQiHaoYYMMDDQQQ(int QNum) { return GenerateQiHaoYYMMDDQQQ(SCCSysDateTime, QNum); } /// /// 通过期号编号生成昨天形如YYMMDDQQ的期号 /// /// 期号编号 /// public static string GenerateYesterdayQiHaoYYMMDDQQ(int QNum) { return GenerateQiHaoYYMMDDQQ(SCCSysDateTime.AddDays(-1), QNum); } /// /// 通过期号编号生成昨天形如YYMMDDQQ的期号 /// /// 期号编号 /// public static string GenerateYesterdayQiHaoYYMMDDQQQ(int QNum) { return GenerateQiHaoYYMMDDQQQ(SCCSysDateTime.AddDays(-1), QNum); } /// /// 通过时间和编号生成当天形如YYMMDDQQ的期号 /// /// 时间 /// 期号编号 /// private static string GenerateQiHaoYYMMDDQQ(DateTime dt, int QNum) { return dt.ToString("yyMMdd") + (QNum).ToString().PadLeft(2, '0'); } /// /// 通过时间和编号生成当天形如YYMMDDQQQ的期号 /// /// 时间 /// 期号编号 /// private static string GenerateQiHaoYYMMDDQQQ(DateTime dt, int QNum) { return dt.ToString("yyMMdd") + (QNum).ToString().PadLeft(3, '0'); } /// /// 生成广西快乐十分指定日期指定编号的期号 /// /// 时间 /// 配置的跳过时间 /// 期号编号 /// public static string GenerateGXKL10FQiHao(DateTime dt, string SkipDate, int QNum) { TimeSpan datepart = dt - new DateTime(dt.Year, 1, 1); var t = SkipDate.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var beforeDTSkipDates = t.Where(R => Convert.ToInt32(R) < Convert.ToInt32(dt.ToString("yyyyMMdd"))).ToList(); return dt.Year.ToString() + (datepart.Days - beforeDTSkipDates.Count + 1).ToString().PadLeft(3, '0') + QNum.ToString().PadLeft(2, '0'); } #endregion #region 生成开奖时间 /// /// 获取昨天对应期号的开奖时间 /// 期号需形如YYMMDDQQ或YYMMDDQQQ /// /// 配置项 /// 开奖期号 /// public static DateTime GenerateYesterdayOpenTime(SCCConfig config, string QiHao) { var openDay = SCCSysDateTime.AddDays(-1); var StartTime = new DateTime(openDay.Year, openDay.Month, openDay.Day, config.StartHour, config.StartMinute, 0); return StartTime.AddMinutes((Convert.ToInt32(QiHao.Substring(6)) - 1) * config.Interval); } /// /// 获取今日对应期号的开奖时间 /// 期号需形如YYMMDDQQ或YYMMDDQQQ /// /// 配置项 /// 开奖期号 /// public static DateTime GenerateTodayOpenTime(SCCConfig config, string QiHao) { var StartTime = new DateTime(SCCSysDateTime.Year, SCCSysDateTime.Month, SCCSysDateTime.Day, config.StartHour, config.StartMinute, 0); return StartTime.AddMinutes((Convert.ToInt32(QiHao.Substring(6)) - 1) * config.Interval); } public static DateTime GenerateGXKL10FYesterdayOpenTime(SCCConfig config, string QiHao) { var openDay = SCCSysDateTime.AddDays(-1); var StartTime = new DateTime(openDay.Year, openDay.Month, openDay.Day, config.StartHour, config.StartMinute, 0); return StartTime.AddMinutes((Convert.ToInt32(QiHao.Substring(7)) - 1) * config.Interval); } public static DateTime GenerateGXKL10FTodayOpenTime(SCCConfig config, string QiHao) { var StartTime = new DateTime(SCCSysDateTime.Year, SCCSysDateTime.Month, SCCSysDateTime.Day, config.StartHour, config.StartMinute, 0); return StartTime.AddMinutes((Convert.ToInt32(QiHao.Substring(7)) - 1) * config.Interval); } #endregion #region 日志信息 public static string GetJobMainLogInfo(SCCConfig config, string QiHao) { return string.Format("【{0}】通过主站地址抓取{1}期开奖数据成功", config.Area + config.LotteryName, QiHao); } public static string GetJobBackLogInfo(SCCConfig config, string QiHao) { return string.Format("【{0}】通过备用地址抓取{1}期开奖数据成功", config.Area + config.LotteryName, QiHao); } public static string GetJobLogError(SCCConfig config, string QiHao) { return string.Format("【{0}】抓取{1}期开奖数据失败", config.Area + config.LotteryName, QiHao); } #endregion #region 地方彩 /// /// 通过编号生成当天形如YYQQQ的期号 /// (目前只有地方彩使用) /// /// 期号编号 /// public static int GenerateQiHaoYYQQQ(int QNum) { return Convert.ToInt32(SCCSysDateTime.ToString("yy") + (QNum).ToString().PadLeft(3, '0')); } /// /// 通过编号生成当天形如YYYYQQQ的期号 /// (目前只有地方彩使用) /// /// 期号编号 /// public static int GenerateQiHaoYYYYQQQ(int QNum) { return Convert.ToInt32(SCCSysDateTime.ToString("yyyy") + (QNum).ToString().PadLeft(3, '0')); } /// /// 核实该地方彩程序运行时间是否应该获取到数据 /// 开奖第二天检查时应该抓取到开奖数据才正确 /// /// 配置信息 /// public static bool CheckDTIsNeedGetData(SCCConfig config) { var week = SCCSysDateTime.AddDays(-1).DayOfWeek.ToString("d"); if (config.KJTime.Contains(week) && SCCSysDateTime.Hour < 1)//第二天只检查1次 return true; return false; } /// /// 检查今天该地方彩是否应该开奖 /// 是则进行爬取工作 /// /// 配置信息 /// public static bool CheckTodayIsOpenDay(SCCConfig config) { var week = SCCSysDateTime.DayOfWeek.ToString("d"); if (config.KJTime.Contains(week)) return true; return false; } /// /// 生成地方彩昨天的开奖时间 /// /// 配置信息 /// public static DateTime GenerateDTOpenTime(SCCConfig config) { var openday = SCCSysDateTime.AddDays(-1); return new DateTime(openday.Year, openday.Month, openday.Day, config.StartHour, config.StartMinute, 0); } #endregion /// /// 将值转换为T类型数据 /// /// 目标类型 /// 数据值 /// public static T ChangeType(object value) { return ChangeType(value, default(T)); } /// /// 将值转换为T类型数据,失败则返回T类型默认值 /// /// 目标类型 /// 数据值 /// T类型默认值 /// public static T ChangeType(object value, T defaultValue) { if (value != null) { Type nullableType = typeof(T); if (!nullableType.IsInterface && (!nullableType.IsClass || (nullableType == typeof(string)))) { if (nullableType.IsGenericType && (nullableType.GetGenericTypeDefinition() == typeof(Nullable<>))) { return (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(nullableType)); } if (nullableType.IsEnum) { return (T)Enum.Parse(nullableType, value.ToString()); } return (T)Convert.ChangeType(value, nullableType); } if (value is T) { return (T)value; } } return defaultValue; } /// /// 将值转换为type类型的值 /// /// 值 /// 目标类型 /// public static object ChangeType(object value, Type type) { if (value != null) { var nullableType = Nullable.GetUnderlyingType(type); if (nullableType != null)//可空 { return Convert.ChangeType(value, nullableType); } if (Convert.IsDBNull(value))//特殊处理,由于数据库类型与项目中的类型定义不匹配 return type.IsValueType ? Activator.CreateInstance(type) : null; return Convert.ChangeType(value, type); } return null; } #region 邮件功能 /// /// 发送邮件 /// /// 邮件标题 /// 邮件正文 /// public static bool SendEmail(string Subject, string Body) { try { var MailTo = ConfigHelper.GetConfigValue("MailTo"); if (string.IsNullOrEmpty(MailTo)) return false; var MailFrom = ConfigHelper.GetConfigValue("MailFrom"); var MailCC = ConfigHelper.GetConfigValue("MailCC"); var SenderUserName = ConfigHelper.GetConfigValue("SenderUserName"); var SenderPassWord = ConfigHelper.GetConfigValue("SenderPassWord"); var SMTPHost = ConfigHelper.GetConfigValue("SMTPHost"); MailMessage message = new MailMessage(); var toList = MailTo.Trim().Split(new char[] { ';' }); foreach (var to in toList) { if (!string.IsNullOrWhiteSpace(to)) message.To.Add(new MailAddress(to)); } var ccList = MailCC.Trim().Split(new char[] { ';' }); foreach (var cc in ccList) { if (!string.IsNullOrWhiteSpace(cc)) message.CC.Add(new MailAddress(cc)); } message.Subject = Subject; message.SubjectEncoding = Encoding.UTF8; message.Body = Body; message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Priority = MailPriority.High; SmtpClient client = new SmtpClient(); NetworkCredential nc = new NetworkCredential(); if (!string.IsNullOrEmpty(MailFrom) && !string.IsNullOrEmpty(SenderUserName) && !string.IsNullOrEmpty(SenderPassWord) && !string.IsNullOrEmpty(SMTPHost)) { message.From = new MailAddress(MailFrom); nc.UserName = SenderUserName; nc.Password = SenderPassWord; client.Host = SMTPHost; } else { //set default sender #region set default sender message.From = new MailAddress("18111271228@163.com"); nc.UserName = "18111271228@163.com"; //nc.Password = "sccadmin520";//邮箱登陆密码 nc.Password = "sccadmin51520";//客戶端授权密码 client.Host = "smtp.163.com"; #endregion } client.UseDefaultCredentials = false; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = nc; client.EnableSsl = true; client.Send(message); return true; } catch { } return false; } #endregion } }