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

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

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

        public void dosomething()
        {
            while (true)
            {
                string data = RedisDbconn.Instance.RPop<string>("OperateAmountQueue");
                if (!string.IsNullOrEmpty(data))
                {
                    try
                    {
                        WebCMSEntities db = new WebCMSEntities();
                        function.WriteLog(DateTime.Now.ToString() + "\r\n" + data + "\r\n\r\n", "运营中心返额度日志");
                        JsonData jsonObj = JsonMapper.ToObject(data);
                        int UserId = int.Parse(function.CheckInt(jsonObj["UserId"].ToString())); //运营中心所属人创客Id
                        int DataId = int.Parse(function.CheckInt(jsonObj["DataId"].ToString())); //机具Id或机具券Id
                        int Kind = int.Parse(function.CheckInt(jsonObj["Kind"].ToString())); //1-机具，2-机具券
                        decimal Amount = decimal.Parse(function.CheckNum(jsonObj["Amount"].ToString()));
                        
                        UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
                        if (account == null)
                        {
                            account = db.UserAccount.Add(new UserAccount()
                            {
                                Id = UserId,
                                UserId = UserId,
                            }).Entity;
                            db.SaveChanges();
                        }
                        decimal BeforeAmount = account.ValidAmount; //变更前总金额
                        account.ValidAmount += Amount;
                        decimal AfterAmount = account.ValidAmount; //变更后总金额
                        AmountRecord add = db.AmountRecord.Add(new AmountRecord()
                        {
                            CreateDate = DateTime.Now,
                            UpdateDate = DateTime.Now,
                            OperateType = 1,
                            AfterAmount = AfterAmount,
                            BeforeAmount = BeforeAmount,
                            UseAmount = Amount,
                            UserId = UserId,
                            ApplyId = DataId,
                            SeoDescription = Kind == 1 ? "机具激活返还" : "机具券兑换返还",
                        }).Entity;
                        db.SaveChanges();

                        db.Dispose();
                    }
                    catch (Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心返额度异常");
                    }
                }
                else
                {
                    Thread.Sleep(5000);
                }
            }
        }
    }
}