using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Library;
using MySystem.JavaProductModels;

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

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

        private void DoWorks()
        {
            // while (true)
            // {
            //     try
            //     {
            //         InitPosList();
            //         int timespan = 1000 * 60 * 60 * 2;
            //         Thread.Sleep(timespan);
            //     }
            //     catch (Exception ex)
            //     {
            //         Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "初始化机具名单异常");
            //     }
            // }
            try
            {
                InitPosList();
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "初始化机具名单异常");
            }
        }

        #region 初始化机具名单

        public void InitPosList()
        {
            List<string> jkPos = function.ReadInstance("tmpdata/jk.txt").Split('\n').ToList();
            List<string> Pos = new List<string>();
            string posString = function.ReadInstance("tmpdata/1.txt");
            Pos.AddRange(posString.Split('\n'));
            posString = function.ReadInstance("tmpdata/2.txt");
            Pos.AddRange(posString.Split('\n'));
            posString = function.ReadInstance("tmpdata/3.txt");
            Pos.AddRange(posString.Split('\n'));
            posString = function.ReadInstance("tmpdata/4.txt");
            Pos.AddRange(posString.Split('\n'));
            WebCMSEntities db = new WebCMSEntities();
            
            //金控
            var list1 = db.KxsMachine.Select(m => new { m.PosSn, m.BrandId, m.MachineRatio, m.SecondFee }).Where(m => (m.BrandId == 1 || m.BrandId == 3) && !jkPos.Contains(m.PosSn)).ToList().Select(m => new PosFeeItem(){ PosSn = m.PosSn, FeeRate = m.MachineRatio, FeeAmt = m.SecondFee }).ToArray();

            //盛付通、来客吧、联动
            var list2 = db.KxsMachine.Select(m => new { m.PosSn, m.BrandId, m.MachineRatio, m.SecondFee }).Where(m => Pos.Contains(m.PosSn)).ToList().Select(m => new PosFeeItem(){ PosSn = m.PosSn, FeeRate = m.MachineRatio, FeeAmt = m.SecondFee }).ToArray();

            RedisDbconn.Instance.Del("PosFeeList");
            RedisDbconn.Instance.AddList("PosFeeList", list1);
            RedisDbconn.Instance.AddList("PosFeeList", list2);

            db.Dispose();
        }

        #endregion 
    
    }
}