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

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

        public void Start()
        {
            Thread th = new Thread(StartDo);
            th.IsBackground = true;
            th.Start();
        }
        public void StartDo()
        {
            while(true)
            {
                try
                {
                    string content = RedisDbconn.Instance.RPop<string>("PrizeFlowQueue");
                    if (!string.IsNullOrEmpty(content))
                    {
                        DoSomething(content);
                    }
                    else
                    {
                        Thread.Sleep(2000);
                    }
                }
                catch(Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "奖励返现流程入库异常");
                    Thread.Sleep(600000);
                }
            }
        }
        public void DoSomething(string content)
        {
            PrizeFlowRecord item = Newtonsoft.Json.JsonConvert.DeserializeObject<PrizeFlowRecord>(content);
            WebCMSEntities db = new WebCMSEntities();
            db.PrizeFlowRecord.Add(item);
            db.SaveChanges();
            db.Dispose();
        }
    }
}