using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Data;
using MySystem;
using MySystem.Models;
using Library;
using LitJson;
///
/// 盟主过期消息推送
///
public class LeaderTimeoutSendMessageService
{
public readonly static LeaderTimeoutSendMessageService Instance = new LeaderTimeoutSendMessageService();
private LeaderTimeoutSendMessageService()
{
}
public void Start()
{
Thread th = new Thread(doSomething);
th.IsBackground = true;
th.Start();
}
private void doSomething()
{
while (true)
{
if (DateTime.Now.Hour < 8)
{
try
{
string check = function.ReadInstance("/LeaderTimeoutSendMessage/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
if (string.IsNullOrEmpty(check))
{
function.WritePage("/LeaderTimeoutSendMessage/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
WebCMSEntities db = new WebCMSEntities();
var leaders = db.Leaders.Select(m => new { m.UserId, m.ExpiredDate }).ToList();
var time = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
foreach (var item in leaders)
{
var user = db.Users.FirstOrDefault(m => m.Id == item.UserId) ?? new Users();
var leaderTimeOutDate = DateTime.Parse(item.ExpiredDate.Value.ToString("yyyy-MM-dd"));
var day = leaderTimeOutDate - time;
if (time >= leaderTimeOutDate.AddDays(-5) && time <= leaderTimeOutDate)
{
RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
{
UserId = item.UserId, //创客
Title = "盟主权益即将到期提醒", //标题
Content = "
尊敬的" + user.RealName + "盟主您好:
您的盟主权益将在" + day.Days + "天后到期,为保障您的收益持续到账,请尽快续期! div > ",//内容
CreateDate = DateTime.Now,
}));
}
if (time > leaderTimeOutDate && time <= leaderTimeOutDate.AddMonths(1))
{
RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
{
UserId = item.UserId, //创客
Title = "盟主权益到期提醒", //标题
Content = "
尊敬的" + user.RealName + "盟主您好:
您的盟主已经过期,重新开通后可恢复相关权益! div > ",//内容
CreateDate = DateTime.Now,
}));
}
}
}
}
catch (Exception ex)
{
function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "盟主过期消息推送线程异常");
}
}
Thread.Sleep(1000);
}
}
}