using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.Extensions.Hosting;
using MySystem;
using MySystem.Models;
using LitJson;
using Library;

public class TradeStatTimer
{
    public readonly static TradeStatTimer Instance = new TradeStatTimer();
    private TradeStatTimer()
    {
    }
    
    public void Start()
    {
        Thread th = new Thread(DoWorks);
        th.IsBackground = true;
        th.Start();
    }

    private void DoWorks()
    {
        while (true)
        {
            string content = RedisDbconn.Instance.RPop<string>("TradeStatQueue");
            if (!string.IsNullOrEmpty(content))
            {
                JsonData jsonObj = JsonMapper.ToObject(content);
                string DateString = jsonObj["DateString"].ToString();
                int UserId = int.Parse(jsonObj["UserId"].ToString()); //创客ID
                int BrandId = int.Parse(jsonObj["BrandId"].ToString()); //品牌
                int BankCardType = int.Parse(jsonObj["BankCardType"].ToString()); //卡类型
                int MerchantId = int.Parse(jsonObj["MerchantId"].ToString()); //商户Id
                int QrPayFlag = int.Parse(jsonObj["QrPayFlag"].ToString()); //云闪付
                decimal TradeAmount = decimal.Parse(jsonObj["TradeAmount"].ToString()); //当日交易额
                string TradeMonth = DateTime.Parse(DateString).ToString("yyyyMM");
                string TradeDate = DateTime.Parse(DateString).ToString("yyyyMMdd");
                WebCMSEntities db = new WebCMSEntities();
                var tran = db.Database.BeginTransaction();
                try
                {
                    PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == MerchantId) ?? new PosMerchantInfo();
                    MachineForMerNo machineFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo) ?? new MachineForMerNo();
                    PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineFor.SnId) ?? new PosMachinesTwo();
                    int MerchantTypeUserId = int.Parse(function.CheckInt(pos.SeoTitle));
                    if (MerchantTypeUserId > 0)
                    {
                        Users merchantUser = db.Users.FirstOrDefault(m => m.Id == MerchantTypeUserId && m.MerchantType == 1);
                        if (merchantUser != null)
                        {
                            UserId = MerchantTypeUserId;
                        }
                    }
                    Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
                    if (user != null)
                    {
                        if (user.AuthFlag == 1)
                        {
                            if (QrPayFlag == 1)
                            {
                            }
                            else
                            {
                            }

                            string ParentNav = user.ParentNav;
                            if (UserId != pos.UserId)
                            {
                                user = db.Users.FirstOrDefault(m => m.Id == pos.UserId);
                                if (user != null)
                                {
                                    ParentNav = user.ParentNav;
                                    UserId = pos.UserId;
                                }
                            }
                            if (!string.IsNullOrEmpty(ParentNav))
                            {
                                ParentNav += "," + UserId + ",";
                                string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
                                foreach (string UserIdString in ParentNavList)
                                {
                                    int ParentUserId = int.Parse(UserIdString);
                                    if (QrPayFlag == 1)
                                    {
                                    }
                                    else
                                    {
                                    }
                                }
                            }
                        }
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex.ToString(), "统计交易额异常");
                    tran.Rollback();
                }
                tran.Dispose();
                db.Dispose();
            }
            else
            {
                Thread.Sleep(900000);
            }
        }
    }
}