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

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

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

        public void dosomething()
        {
            while (true)
            {
                string data = RedisDbconn.Instance.RPop<string>("TradeFilterQueue");
                if (!string.IsNullOrEmpty(data))
                {
                    try
                    {
                        Utils.WriteLog(data, "训练营60拦截队列日志");
                        JsonData jsonOj = JsonMapper.ToObject(data);
                        int Kind = int.Parse(function.CheckInt(jsonOj["Kind"].ToString()));
                        int OrderId = int.Parse(function.CheckInt(jsonOj["OrderId"].ToString()));
                        int PrizeUserId = int.Parse(function.CheckInt(jsonOj["PrizeUserId"].ToString()));
                        decimal Amount = decimal.Parse(function.CheckNum(jsonOj["Amount"].ToString()));
                        WebCMSEntities db = new WebCMSEntities();
                        Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId) ?? new Orders();
                        Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
                        Users prizeuser = db.Users.FirstOrDefault(m => m.Id == PrizeUserId) ?? new Users();
                        db.TradeFilterRecord.Add(new TradeFilterRecord()
                        {
                            CreateDate = DateTime.Now,
                            IntercetpAmount = Amount,
                            OrderDate = order.PayDate,
                            OrderNo = order.OrderNo,
                            OrderId = order.Id,
                            PrizeUserId = PrizeUserId,
                            OrderUserId = order.UserId,
                            Kind = Kind,
                        });
                        db.SaveChanges();
                        db.Dispose();
                        Utils.WriteLog("入库成功", "训练营60拦截队列日志");

                        JavaModels.WebCMSEntities javadb = new JavaModels.WebCMSEntities();
                        JavaModels.KxsCampBonusLog log = javadb.KxsCampBonusLog.FirstOrDefault(m => m.Remark.Contains(order.OrderNo));
                        if(log != null)
                        {
                            log.FilterUser = prizeuser.RealName;
                            log.FilterCode = prizeuser.MakerCode;
                            javadb.SaveChanges();
                        }
                        javadb.Dispose();
                    }
                    catch (Exception ex)
                    {                        
                        Utils.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "训练营拦截奖励异常");
                    }
                }
                else
                {
                    Thread.Sleep(500);
                }
            }
        }
    }
}