using System;
using System.Collections.Generic;
using Library;
using LitJson;
using System.Linq;
using System.Data;
using System.Threading;
using MySystem.Models;

namespace MySystem
{
    public class MessageCenterService
    {
        public readonly static MessageCenterService Instance = new MessageCenterService();
        private MessageCenterService()
        { }

        public void Start()
        {
            Thread th = new Thread(dosomething);
            th.IsBackground = true;
            th.Start();
        }

        public void dosomething()
        {
            while (true)
            {
                string data = RedisDbconn.Instance.RPop<string>("MsgPersonalQueue");
                if (!string.IsNullOrEmpty(data))
                {
                    try
                    {
                        WebCMSEntities db = new WebCMSEntities();
                        MsgPersonal msg = Newtonsoft.Json.JsonConvert.DeserializeObject<MsgPersonal>(data);
                        db.MsgPersonal.Add(msg);
                        db.SaveChanges();
                        db.Dispose();
                    }
                    catch (Exception ex)
                    {
                        
                        function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "消息中心队列异常");
                    }
                }
                else
                {
                    Thread.Sleep(60000);
                }
            }
        }
    }
}