using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Data;
using MySystem;
using MySystem.Models;
using Library;
using LitJson;
using GraphQL;
/// <summary>
/// 盟主过期消息推送
/// </summary>
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 < 9)
            {
                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)
                            {
                                string flag = RedisDbconn.Instance.Get<string>("MsgPersonalFirst" + item.UserId);
                                if(string.IsNullOrEmpty(flag))
                                {
                                    RedisDbconn.Instance.Set("MsgPersonalFirst" + item.UserId, "1");
                                    int timespan = 60 * 60 * 24 * 6;
                                    RedisDbconn.Instance.SetExpire("MsgPersonalFirst" + item.UserId, timespan);
                                    RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
                                    {
                                        UserId = item.UserId, //创客
                                        Title = "盟主权益即将到期提醒", //标题
                                        Content = "<div class='f16'>尊敬的" + user.RealName + "盟主您好:<br/>您的盟主权益将在" + day.Days + "天后到期，为保障您的收益持续到账，请尽快续期！</ div > ",//内容
                                        CreateDate = DateTime.Now,
                                    }));
                                }
                            }
                            if (time > leaderTimeOutDate && time <= leaderTimeOutDate.AddMonths(1))
                            {
                                int number = 1; //提示标记（第几次）
                                int timespan = 60 * 60 * 24 * 8;
                                TimeSpan ts = time - leaderTimeOutDate;
                                if(ts.Days == 0 || ts.Days == 1) 
                                {
                                    number = 1;
                                    timespan = 60 * 60 * 24 * 8;
                                }
                                if(ts.Days == 7 || ts.Days == 8) 
                                {
                                    number = 2;
                                    timespan = 60 * 60 * 24 * 8;
                                }
                                if(ts.Days == 14 || ts.Days == 15) 
                                {
                                    number = 3;
                                    timespan = 60 * 60 * 24 * 15;
                                }
                                if(ts.Days == 28 || ts.Days == 29) 
                                {
                                    number = 4;
                                    timespan = 60 * 60 * 24 * 5;
                                }
                                string flag = RedisDbconn.Instance.Get<string>("MsgPersonalAfter:" + number + ":" + item.UserId);
                                if(string.IsNullOrEmpty(flag))
                                {
                                    RedisDbconn.Instance.Set("MsgPersonalAfter:" + number + ":" + item.UserId, "1");
                                    RedisDbconn.Instance.SetExpire("MsgPersonalAfter:" + number + ":" + item.UserId, timespan);
                                    RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
                                    {
                                        UserId = item.UserId, //创客
                                        Title = "盟主权益到期提醒", //标题
                                        Content = "<div class='f16'>尊敬的" + user.RealName + "盟主您好:<br/>您的盟主已经过期，重新开通后可恢复相关权益！</ div > ",//内容
                                        CreateDate = DateTime.Now,
                                    }));
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "盟主过期消息推送线程异常");
                }
            }
            Thread.Sleep(1000);
        }
    }
}