using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Library;
using LitJson;
using MySystem;

public class LogHelper
{
    public readonly static LogHelper Instance = new LogHelper();
    private LogHelper()
    { }

    string[] BlackList = { "好哒查询商户审核状态", "好哒查询实名认证状态", "好哒分账交易详情查询", "间连商户开户意愿确认(提交申请单)查询申请单状态-请求参数", "查询支付宝商家认证申请单状态", "查询支付宝商户意愿申请状态", "获取微信商户开户意愿确认状态", "获取支付宝商家认证状态" };

    public void WriteLog(string Content, string FileName, string BrandId = "0")
    {
        if(BlackList.Contains(FileName) && Library.ConfigurationManager.EnvironmentFlag == 2)
        {
            string key = function.MD532(Content + FileName);
            if(!string.IsNullOrEmpty(RedisDbconn.Instance.Get<string>(key)))
            {
                return;
            }
            RedisDbconn.Instance.Set(key, "1");
            RedisDbconn.Instance.SetExpire(key, 600);
        }
        function.WriteLog(Content, FileName);
        // Dictionary<string, string> dic = new Dictionary<string, string>();
        // dic.Add("Topic", FileName);
        // dic.Add("Content", Content);
        // dic.Add("BrandId", BrandId);
        // RedisDbconn.Instance.AddList("LogQueue", Newtonsoft.Json.JsonConvert.SerializeObject(dic));
    }

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

    public void DoWorks()
    {
        while (true)
        {
            string content = RedisDbconn.Instance.RPop<string>("LogQueue");
            if (!string.IsNullOrEmpty(content))
            {
                try
                {
                    DoQueue(content);
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "SLS日志异常");
                }
            }
            else
            {
                Thread.Sleep(5000);
            }
        }
    }

    public void DoQueue(string content)
    { 
        // JsonData JsonObj = JsonMapper.ToObject(content);
        // string Topic = JsonObj["Topic"].ToString();
        // string Cont = JsonObj["Content"].ToString();
        // string BrandId = JsonObj["BrandId"].ToString();
        // SLS.WriteLog(DateTime.Now, Topic, Cont, new Dictionary<string, string>()
        // {
        //     {"BrandId", BrandId}
        // });
    }
}