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

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

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

        private void SetLkbDeposit()
        {
            while (true)
            {
                try
                {
                    string content = RedisDbconn.Instance.RPop<string>("SetLkbDepositQueue");
                    if (!string.IsNullOrEmpty(content))
                    {
                        string[] data = content.Split('|');
                        PublicImportDataService.Instance.LkbSetDeposit(data[0], decimal.Parse(data[1]));
                    }
                    else
                    {
                        Thread.Sleep(2000);
                    }
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "商城订单余额支付异常");
                    Thread.Sleep(2000);
                }
            }
        }







        public void dosomething()
        {
            string content = function.ReadInstance("jk299.txt");
            string[] SnNos = content.Split('\n');
            int count = SnNos.Length;
            int index = 0;
            foreach(string SnNo in SnNos)
            {
                index += 1;
                PublicImportDataService.Instance.LkbSetDeposit(SnNo, 299);
                function.WritePage("/look/", "progress.txt", index + "/" + count);
                function.WritePage("/look/", "sn.txt", SnNo);
                Thread.Sleep(100);
            }

            content = function.ReadInstance("jk0.txt");
            SnNos = content.Split('\n');
            count = SnNos.Length;
            index = 0;
            foreach(string SnNo in SnNos)
            {
                index += 1;
                PublicImportDataService.Instance.LkbSetDeposit(SnNo, 0);
                function.WritePage("/look/", "progress.txt", index + "/" + count);
                function.WritePage("/look/", "sn.txt", SnNo);
                Thread.Sleep(100);
            }
        }

        public void poscheck()
        {
            WebCMSEntities db = new WebCMSEntities();
            try
            {
                List<string> poslist = new List<string>();
                List<int> merids = new List<int>();
                List<string> mernos = new List<string>();

                // //盛付通
                // poslist = function.ReadInstance("tmpdata/1.txt").Replace("\r", "").Split('\n').ToList();
                // List<string> without1 = function.ReadInstance("tmpdata/sft1.txt").Replace("\r", "").Split('\n').ToList();
                // List<string> without2 = function.ReadInstance("tmpdata/sft2.txt").Replace("\r", "").Split('\n').ToList();
                // List<string> without3 = function.ReadInstance("tmpdata/11.txt").Replace("\r", "").Split('\n').ToList();
                // function.WriteLog(poslist.Count.ToString(), "tmp盛付通临时费率");
                // merids = db.KxsMachine.Where(m => poslist.Contains(m.PosSn) && !without1.Contains(m.PosSn) && !without2.Contains(m.PosSn) && !without3.Contains(m.PosSn) && m.BrandId == 7 && m.MerchantId > 0).ToList().Select(m => m.MerchantId).ToList();
                // function.WriteLog(merids.Count.ToString(), "tmp盛付通临时费率");
                // mernos = db.KxsMerchant.Where(m => merids.Contains(m.Id)).ToList().Select(m => m.MerchantNo).ToList();
                // function.WriteLog(mernos.Count.ToString(), "tmp盛付通临时费率");
                // foreach(string merno in mernos)
                // {
                //     if(int.Parse(DateTime.Now.ToString("HHmm")) < 2100)
                //     {
                //         string result = SetDepositPostService.Instance.SetFeeForSft(merno, "0.6500", "3");
                //         function.WriteLog(merno + "\n" + DateTime.Now.ToString() + "\n" + result + "\n\n", "tmp盛付通临时费率");
                //         Thread.Sleep(500);
                //     }
                //     else
                //     {
                //         function.WriteLog(merno, "tmp盛付通临时费率POSN");
                //     }
                // }

                // List<string> list = function.ReadInstance("tmpdata/sft3.txt").Replace("\r", "").Split('\n').ToList();
                // foreach(string merno in list)
                // {
                //     string result = SetDepositPostService.Instance.SetFeeForSft(merno, "0.6500", "3");
                //     function.WriteLog(merno + "\n" + DateTime.Now.ToString() + "\n" + result + "\n\n", "tmp盛付通临时费率");
                //     Thread.Sleep(500);
                // }


                // poslist = function.ReadInstance("tmpdata/sft1.txt").Replace("\r", "").Split('\n').ToList();
                // function.WriteLog(poslist.Count.ToString(), "tmp盛付通临时费率");
                // merids = db.KxsMachine.Where(m => poslist.Contains(m.PosSn) && m.BrandId == 7 && m.MerchantId > 0).ToList().Select(m => m.MerchantId).ToList();
                // function.WriteLog(merids.Count.ToString(), "tmp盛付通临时费率");
                // mernos = db.KxsMerchant.Where(m => merids.Contains(m.Id)).ToList().Select(m => m.MerchantNo).ToList();
                // function.WriteLog(mernos.Count.ToString(), "tmp盛付通临时费率");
                // foreach(string merno in mernos)
                // {
                //     string result = SetDepositPostService.Instance.SetFeeForSft(merno, "0.6300", "0");
                //     function.WriteLog(merno + "\n" + result + "\n\n", "tmp盛付通临时费率");
                //     Thread.Sleep(500);
                // }

                // poslist = function.ReadInstance("tmpdata/sft2.txt").Replace("\r", "").Split('\n').ToList();
                // function.WriteLog(poslist.Count.ToString(), "tmp盛付通临时费率");
                // merids = db.KxsMachine.Where(m => poslist.Contains(m.PosSn) && m.BrandId == 7 && m.MerchantId > 0).ToList().Select(m => m.MerchantId).ToList();
                // function.WriteLog(merids.Count.ToString(), "tmp盛付通临时费率");
                // mernos = db.KxsMerchant.Where(m => merids.Contains(m.Id)).ToList().Select(m => m.MerchantNo).ToList();
                // function.WriteLog(mernos.Count.ToString(), "tmp盛付通临时费率");
                // foreach(string merno in mernos)
                // {
                //     string result = SetDepositPostService.Instance.SetFeeForSft(merno, "0.6000", "0");
                //     function.WriteLog(merno + "\n" + result + "\n\n", "tmp盛付通临时费率");
                //     Thread.Sleep(500);
                // }

                //来客吧电签
                poslist = function.ReadInstance("tmpdata/2.txt").Split('\n').ToList();
                merids = db.KxsMachine.Where(m => poslist.Contains(m.PosSn) && m.BrandId == 15 && m.MerchantId > 0).ToList().Select(m => m.MerchantId).ToList();
                mernos = db.KxsMerchant.Where(m => merids.Contains(m.Id)).ToList().Select(m => m.MerchantNo).ToList();
                foreach(string merno in mernos)
                {
                    List<string> mernoList = new List<string>();
                    mernoList.Add(merno);
                    string result = PublicImportDataService.Instance.PospFee(mernoList, "LKB_0.65+3", "联客宝0.65+3");
                    function.WriteLog(merno + "\n" + result + "\n\n", "tmp来客吧电签临时费率");
                    Thread.Sleep(500);
                }

                // //联动电签
                // poslist = function.ReadInstance("tmpdata/3.txt").Split('\n').ToList();
                // merids = db.KxsMachine.Where(m => poslist.Contains(m.PosSn) && m.BrandId == 10 && m.MerchantId > 0).ToList().Select(m => m.MerchantId).ToList();
                // mernos = db.KxsMerchant.Where(m => merids.Contains(m.Id)).ToList().Select(m => m.MerchantNo).ToList();
                // foreach(string merno in mernos)
                // {
                //     string result = SetDepositPostService.Instance.LDFeeRate(merno, "0.0065", 10, 3);
                //     function.WriteLog(merno + "\n" + result + "\n\n", "tmp联动电签临时费率");
                //     Thread.Sleep(500);
                // }

                // //联动大POS
                // poslist = function.ReadInstance("tmpdata/4.txt").Split('\n').ToList();
                // merids = db.KxsMachine.Where(m => poslist.Contains(m.PosSn) && m.BrandId == 11 && m.MerchantId > 0).ToList().Select(m => m.MerchantId).ToList();
                // mernos = db.KxsMerchant.Where(m => merids.Contains(m.Id)).ToList().Select(m => m.MerchantNo).ToList();
                // foreach(string merno in mernos)
                // {
                //     string result = SetDepositPostService.Instance.LDFeeRate(merno, "0.0065", 11, 3);
                //     function.WriteLog(merno + "\n" + result + "\n\n", "tmp联动大POS临时费率");
                //     Thread.Sleep(500);
                // }
            }
            catch(Exception ex)
            {
                function.WriteLog(ex.ToString() + "\n\n", "tmp临时费率异常");
            }

            db.Dispose();
        }
    }
}