using System;
using System.Threading;
using System.Linq;
using System.Data;
using Library;
using MySystem.Models;
using System.Collections.Generic;

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

        public void Start()//启动
        {
            Thread thread = new Thread(ImportPostDo);
            thread.IsBackground = true;
            thread.Start();

            
            Thread thread2 = new Thread(ImportPost2Do);
            thread2.IsBackground = true;
            thread2.Start();

            
            Thread thread3 = new Thread(ImportPost3Do);
            thread3.IsBackground = true;
            thread3.Start();
        }

        public void ImportPostDo()
        {
            while (true)
            {
                string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:1");
                if (!string.IsNullOrEmpty(data))
                {
                    int count = int.Parse(function.CheckInt(data));
                    if (count > 0)
                    {
                        MakerCoupons(1, count);
                    }
                }
                else
                {
                    Thread.Sleep(10000);
                }
            }
        }
        public void ImportPost2Do()
        {
            while (true)
            {
                string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:2");
                if (!string.IsNullOrEmpty(data))
                {
                    int count = int.Parse(function.CheckInt(data));
                    if (count > 0)
                    {
                        MakerCoupons(2, count);
                    }
                }
                else
                {
                    Thread.Sleep(10000);
                }
            }
        }
        public void ImportPost3Do()
        {
            while (true)
            {
                string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:3");
                if (!string.IsNullOrEmpty(data))
                {
                    int count = int.Parse(function.CheckInt(data));
                    if (count > 0)
                    {
                        MakerCoupons(3, count);
                    }
                }
                else
                {
                    Thread.Sleep(10000);
                }
            }
        }

        //生成券,Kind:1-电签券，2-大机券
        public void MakerCoupons(int Kind = 1, int count = 0)
        {
            try
            {
                WebCMSEntities db = new WebCMSEntities();
                string StartCode = "0" + Kind + "00000000";
                if(db.PosCoupons.Any(m => m.QueryCount == Kind))
                {
                    StartCode = db.PosCoupons.Where(m => m.QueryCount == Kind).OrderByDescending(m => m.Id).FirstOrDefault().ExchangeCode;
                }
                int QueryCount = Kind;
                Kind += 1;
                string title = "0" + Kind.ToString();
                int start = int.Parse(function.CheckInt(StartCode.Substring(2).TrimStart('0'))) + 1;
                if (start > 0)
                {
                    int end = start + count - 1;
                    for (int i = start; i <= end; i++)
                    {
                        string Code = i.ToString();
                        for (int j = 0; j < 8 - i.ToString().Length; j++)
                        {
                            Code = "0" + Code;
                        }
                        Code = title + Code;
                        bool check = db.PosCoupons.Any(m => m.ExchangeCode == Code);
                        if (!check)
                        {
                            db.PosCoupons.Add(new PosCoupons()
                            {
                                ExchangeCode = Code,
                                QueryCount = QueryCount,
                            });
                            db.SaveChanges();
                        }
                    }
                }
                db.Dispose();
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "生成兑换券异常");
            }
        }
    }
}