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

namespace MySystem
{
    /// <summary>
    /// 设置机具费率标记并推送消息
    /// </summary>
    public class SetFeeService
    {
        public readonly static SetFeeService Instance = new SetFeeService();
        private SetFeeService()
        { }

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

        public void doSomething()
        {
            DataTable dt = CustomerSqlConn.dtable("select Id from PosMachinesTwo where BrandId in (1,3) and BindingState=1 and BindingTime<'" + DateTime.Now.AddDays(-90).ToString("yyyy-MM-dd") + " 00:00:00' and UpFeeFlag=0", Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString());
            foreach(DataRow dr in dt.Rows)
            {
                RedisDbconn.Instance.AddList("JkFeeQueue", dr["Id"].ToString());
            }
            dt = CustomerSqlConn.dtable("select Id from PosMachinesTwo where BrandId in (1,3) and BindingState=1 and BindingTime<'" + DateTime.Now.AddDays(-90).ToString("yyyy-MM-dd") + " 00:00:00' and DownFeeFlag=1", Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString());
            foreach(DataRow dr in dt.Rows)
            {
                RedisDbconn.Instance.AddList("JkFeeQueue", dr["Id"].ToString());
            }
            dt.Dispose();
            dt.Clear();
            while (true)
            {
                string content = RedisDbconn.Instance.RPop<string>("JkFeeQueue");
                if (!string.IsNullOrEmpty(content))
                {
                    try
                    {
                        int PosId = int.Parse(content);
                        WebCMSEntities db = new WebCMSEntities();
                        PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
                        if(pos != null)
                        {
                            PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
                            if(mer != null)
                            {
                                function.WriteLog("KqMerNo:" + mer.KqMerNo + ";PosSn:" + pos.PosSn + ";", "金控机具费率设置日志");
                                string result = PublicImportDataService.Instance.SetFee(mer.KqMerNo, pos.PosSn, 0.63M);
                                function.WriteLog(result, "金控机具费率设置日志");
                                JsonData obj = JsonMapper.ToObject(result);
                                if (obj["code"].ToString() == "000000")
                                {
                                    function.WriteLog(PublicImportDataService.Instance.Decrypt(obj["data"].ToString()), "金控机具费率设置日志");
                                }
                                function.WriteLog("\n\n", "金控机具费率设置日志");
                            }
                        }
                        db.Dispose();
                        Thread.Sleep(100);
                    }
                    catch (Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "金控机具费率设置异常");
                    }
                }
                else
                {
                    Thread.Sleep(60000);
                }
            }
        }
    }
}