using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Library;
using LitJson;
using MySystem;

public class TxjHelper
{
    public readonly static TxjHelper Instance = new TxjHelper();
    private TxjHelper()
    { }

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

    public void DoWorks()
    {
        while (true)
        {
            string month = RedisDbconn.Instance.RPop<string>("TxjQueue");
            if (!string.IsNullOrEmpty(month))
            {
                try
                {
                    DoQueue(month);
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + "\n" + month + "\n" + ex, "同行奖励异常");
                }
            }
            else
            {
                Thread.Sleep(5000);
            }
        }
    }

    public void DoQueue(string month)
    { 
        DateTime start = DateTime.Parse(month + "-01 00:00:00");
        DateTime end = start.AddMonths(1);
        Dictionary<string, string> txjList = TxjList();
        MySystem.JavaProductModels.WebCMSEntities dbpro = new MySystem.JavaProductModels.WebCMSEntities();
        MySystem.JavaUserModels.WebCMSEntities dbuser = new MySystem.JavaUserModels.WebCMSEntities();
        IQueryable<MySystem.JavaProductModels.KxsMachineAward> dellist = dbpro.KxsMachineAward.Where(m => m.VariationType == 30 && m.CreateTime >= start && m.CreateTime < end);
        foreach(MySystem.JavaProductModels.KxsMachineAward sub in dellist)
        {
            dbpro.KxsMachineAward.Remove(sub);
        }
        dbpro.SaveChanges();
        List<MySystem.JavaProductModels.KxsMachineAward> list = dbpro.KxsMachineAward.Where(m => m.VariationType == 118 && m.CreateTime >= start && m.CreateTime < end).ToList();
        foreach(MySystem.JavaProductModels.KxsMachineAward sub in list)
        {
            MySystem.JavaProductModels.KxsShopOrder order = dbpro.KxsShopOrder.FirstOrDefault(m => m.OrderSn == sub.PosSn && m.ReturnStatus == 0);
            if(order != null)
            {
                int UserId = order.UserId;
                MySystem.JavaUserModels.KxsUser user = dbuser.KxsUser.FirstOrDefault(m => m.Id == UserId);
                if(user != null)
                {
                    string ParentNav = user.PidPath;
                    string[] ParentList = ParentNav.TrimEnd(',').Split(',');
                    Array.Reverse(ParentList);
                    foreach(string ParentId in ParentList)
                    {
                        if(txjList.ContainsKey(ParentId))
                        {
                            MySystem.JavaProductModels.KxsShopOrderInfo orderInfo = dbpro.KxsShopOrderInfo.FirstOrDefault(m => m.OrderId == order.Id) ?? new MySystem.JavaProductModels.KxsShopOrderInfo();
                            string[] userData = txjList[ParentId].Split('|');
                            dbpro.KxsMachineAward.Add(new MySystem.JavaProductModels.KxsMachineAward()
                            {
                                Amount = orderInfo.Number * 60,
                                VariationType = 30,
                                Username = userData[1],
                                UserCode = userData[0],
                                UserId = int.Parse(ParentId),
                                MachineId = sub.MachineId,
                                PosSn = sub.PosSn,
                                CreateTime = DateTime.Parse(month + "-01 00:00:00"),
                                AwardType = 1,
                            });
                            dbpro.SaveChanges();
                            break;
                        }
                    }
                }
            }
        }
        dbpro.Dispose();
        dbuser.Dispose();
    }
    
    private Dictionary<string, string> TxjList()
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        string[] list = function.ReadInstance("/TxjList.txt").Split('\n');
        foreach(string sub in list)
        {
            string[] data = sub.Split('|');
            dic.Add(data[0], data[1] + "|" + data[2]);
        }
        return dic;
    }
}