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

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

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

    private void DoWorks()
    {
        while (true)
        {
            try
            {
                string content = RedisDbconn.Instance.RPop<string>("PosCouponExchangeQueue");
                if(!string.IsNullOrEmpty(content))
                {
                    WebCMSEntities db = new WebCMSEntities();
                    DoSomething(db, content);
                    db.Dispose();
                }
                else
                {
                    Thread.Sleep(2000);
                }
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "电签券对大机券异常");
            }
        }
    }

    public void DoSomething(WebCMSEntities db, string content)
    {
        JsonData data = JsonMapper.ToObject(content);
        int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
        string CouponCodes = data["CouponCodes"].ToString(); //券列表
        Dictionary<string, object> Obj = new Dictionary<string, object>();
        List<string> Codes = CouponCodes.Split(',').ToList();
        List<int> LeaderUserIds = new List<int>();
        int BeforeUserId = 0;
        int AfterUserId = 0;
        int index = 0;
        IQueryable<PosCoupons> list = db.PosCoupons.Where(m => Codes.Contains(m.ExchangeCode) && m.UserId == UserId && m.IsLock == 0 && m.IsUse == 0).OrderBy(m => m.CreateDate);
        foreach(PosCoupons sub in list)
        {
            index += 1;
            if(index % 2 == 1) BeforeUserId = sub.LeaderUserId;
            if(index % 2 == 0)
            {
                AfterUserId = sub.LeaderUserId;
                if(BeforeUserId > 0 && AfterUserId > 0) LeaderUserIds.Add(AfterUserId);
                if(BeforeUserId == 0 && AfterUserId > 0) LeaderUserIds.Add(AfterUserId);
                if(BeforeUserId > 0 && AfterUserId == 0) LeaderUserIds.Add(BeforeUserId);
                if(BeforeUserId == 0 && AfterUserId == 0) LeaderUserIds.Add(0);
                BeforeUserId = 0;
                AfterUserId = 0;
            }
            sub.UserId = 0;
        }
        db.SaveChanges();
        int Count = Codes.Count / 2;
        index = 0;
        string ToCodes = "";
        IQueryable<PosCoupons> chklist = db.PosCoupons.Where(m => m.QueryCount == 2 && m.UserId == 0 && m.IsLock == 0 && m.IsUse == 0).Take(Count);
        foreach(PosCoupons sub in chklist)
        {
            sub.UserId = UserId;
            sub.LeaderUserId = LeaderUserIds[index];
            ToCodes += sub.ExchangeCode + ",";
            index += 1;
        }
        db.PosCouponExchangeRecord.Add(new PosCouponExchangeRecord()
        {
            CreateDate = DateTime.Now,
            Note = "兑换大机",
            ToCount = Count,
            SourceCount = Codes.Count,
            ToCoupons = ToCodes.TrimEnd(','),
            SourceCoupons = CouponCodes,
            UserId = UserId,
        });
        db.SaveChanges();
    }
}