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

namespace MySystem
{
    public class AddSourcePosSnHelper
    {
        public readonly static AddSourcePosSnHelper Instance = new AddSourcePosSnHelper();
        private AddSourcePosSnHelper()
        { }
    
    
        public void Start()
        {
            Thread th = new Thread(StartFor);
            th.IsBackground = true;
            th.Start();
        }
        public void StartFor()
        {
            while (true)
            {
                if(DateTime.Now.Hour >= 0 && DateTime.Now.Hour < 9)
                {
                    try
                    {
                        StartDo();
                    }
                    catch(Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "补来源机具异常");
                    }
                }
                Thread.Sleep(120000);
            }
        }
        public void StartDo()
        {
            WebCMSEntities db = new WebCMSEntities();
            int StartId = int.Parse(function.CheckInt(function.ReadInstance("/PosId.txt")));
            List<PosMachinesTwo> PosList = db.PosMachinesTwo.Where(m => m.Id > StartId && m.BuyUserId > 0 && string.IsNullOrEmpty(m.SourcePosSn)).OrderBy(m => m.Id).Take(20).ToList();
            foreach(PosMachinesTwo Pos in PosList)
            {
                function.WriteLog(Pos.PosSn + ":" + Pos.Id, "补来源机具日志");
                string CouposCode = "";
                string PosSn = Pos.PosSn;
                bool op = true;
                while(op)
                {
                    int index = 0;
                    Orders order = db.Orders.FirstOrDefault(m => m.SnNos.Contains(PosSn) && m.Sort > 0);
                    if(order != null)
                    {
                        string[] SnNoList = order.SnNos.Split(',');
                        for (int i = 0; i < SnNoList.Length; i++)
                        {
                            if(SnNoList[i] == PosSn)
                            {
                                index = i;
                            }
                        }
                        MachineApply apply = db.MachineApply.FirstOrDefault(m => m.QueryCount == order.Id);
                        if(apply != null)
                        {
                            string[] ApplySnNoList = apply.SwapSnExpand.Split('\n');
                            if(apply.SwapSnExpand.Contains(":"))
                            {
                                PosSn = ApplySnNoList[index].Split(':')[0];
                            }
                            else
                            {
                                CouposCode = ApplySnNoList[index];
                                op = false;
                            }
                        }
                    }
                    else
                    {
                        op = false;
                    }
                }
                PosMachinesTwo edit = db.PosMachinesTwo.FirstOrDefault(m => m.Id == Pos.Id);
                if(edit != null)
                {
                    edit.SourcePosSn = CouposCode + "," + PosSn;
                }
                StartId = Pos.Id;
            }
            db.SaveChanges();
            db.Dispose();
            function.WritePage("/", "PosId.txt", StartId.ToString());
        }
    }
}