public static string GetBlackListById(int Id)
        {
            string str = string.Empty;

            try
            {
                IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                tbBlackList        temp        = new tbBlackList();
                AchieveDBEntities  myDbContext = new AchieveDBEntities();
                List <tbBlackList> templist    = myDbContext.tbBlackList.Where(p => p.Id == Id).ToList();
                if (templist != null && templist.Count > 0)
                {
                    temp = templist[0];
                    str  = JsonConvert.SerializeObject(temp, Formatting.Indented, timeFormat);
                    str  = ResponseHelper.ResponseMsg("1", "取数成功", str);
                }
                else
                {
                    str = JsonConvert.SerializeObject(temp, Formatting.Indented, timeFormat);
                    str = ResponseHelper.ResponseMsg("-1", "部门不存在", str);
                }
            }
            catch (Exception ex)
            {
                str = ResponseHelper.ResponseMsg("-1", ex.Message, "");
            }

            return(str);
        }
        public static string UpdateBlackList(string BlackListStr)
        {
            string str = string.Empty;

            try
            {
                tbBlackList       tb          = JsonConvert.DeserializeObject <tbBlackList>(BlackListStr);
                AchieveDBEntities myDbContext = new AchieveDBEntities();
                tbBlackList       data        = myDbContext.tbBlackList.Where(p => p.Id == tb.Id).FirstOrDefault();
                //data.Address = tb.Address;
                //data.CreateTime = tb.CreateTime;
                //data.IsAble = tb.IsAble;
                //data.Notes = tb.Notes;
                //data.Port = tb.Port;
                string [] keys = { "Id" };
                ObjectHelper.CopyValueNotKey(tb, data, keys);

                int DataCount = myDbContext.tbBlackList.Where(p => p.Address == data.Address && p.Id != data.Id).Count <tbBlackList>();
                if (DataCount > 0)
                {
                    throw new Exception(string.Format("黑名单IP:{0}重复,请重新输入", data.Address));
                }


                myDbContext.SaveChanges();
                str = ResponseHelper.ResponseMsg("1", "更新成功", "");
            }
            catch (Exception ex)
            {
                str = ResponseHelper.ResponseMsg("-1", ex.Message, "");
            }

            return(str);
        }
        public static string AddBlackList(string BlackListStr)
        {
            string str = string.Empty;

            try
            {
                tbBlackList tb = JsonConvert.DeserializeObject <tbBlackList>(BlackListStr);

                //tbBlackList newtb = new tbBlackList()
                //{
                //    Address = tb.Address,
                //    IsAble = tb.IsAble,
                //    Notes = tb.Notes,
                //    Port = tb.Port,
                //    CreateTime = DateTime.Now
                //};
                tbBlackList newtb = new tbBlackList();
                string[]    keys  = { "Id" };
                ObjectHelper.CopyValueNotKey(tb, newtb, keys);
                newtb.CreateTime = DateTime.Now;
                AchieveDBEntities myDbContext = new AchieveDBEntities();
                int DataCount = myDbContext.tbBlackList.Where(p => p.Address == tb.Address).Count <tbBlackList>();
                if (DataCount > 0)
                {
                    throw new Exception(string.Format("黑名单IP:{0}重复,请重新输入", tb.Address));
                }
                myDbContext.tbBlackList.Add(newtb);
                myDbContext.SaveChanges();
                str = ResponseHelper.ResponseMsg("1", "保存成功", "");
            }
            catch (Exception ex)
            {
                str = ResponseHelper.ResponseMsg("-1", ex.Message, "");
            }

            return(str);
        }
        public static void AnalysisBlackList()
        {
            try
            {
                Logger             logger      = LogManager.GetCurrentClassLogger();
                AchieveDBEntities  myDbContext = new AchieveDBEntities();
                List <tbBlackList> templist    = new List <tbBlackList>();
                logger.Log(LogLevel.Info, string.Format("启动分析黑名单进程!"));
                string BlackCount = System.Configuration.ConfigurationManager.AppSettings["BlackCount"];
                int    blackCount = string.IsNullOrEmpty(BlackCount) ? 0 : Convert.ToInt32(BlackCount);
                List <IPCountInfoModel> ipList = DataCacheHelper.GetData <IPCountInfoModel>();
                if (ipList != null && ipList.Count > 0)
                {
                    ipList = ipList.Where(p => p.Count > blackCount).ToList();
                    if (ipList != null && ipList.Count > 0)
                    {
                        foreach (var temp in ipList)
                        {
                            int DataCount = myDbContext.tbBlackList.Where(p => p.Address == temp.Address && p.IsAble == 1).Count <tbBlackList>();
                            if (DataCount > 0)
                            {
                            }
                            else
                            {
                                DataCount = myDbContext.tbBlackList.Where(p => p.Address == temp.Address).Count <tbBlackList>();
                                if (DataCount > 0)
                                {
                                    tbBlackList tp1 = new tbBlackList()
                                    {
                                        Address = temp.Address,
                                    };

                                    myDbContext.tbBlackList.Remove(tp1);
                                }

                                tbBlackList tp = new tbBlackList()
                                {
                                    Address    = temp.Address,
                                    IsAble     = 1,
                                    CreateTime = DateTime.Now,
                                    Notes      = string.Format("心跳周期内访问{0}次,系统自动屏蔽!", temp.Count)
                                };
                                myDbContext.tbBlackList.Add(tp);
                            }
                        }
                        myDbContext.SaveChanges();

                        logger.Log(LogLevel.Info, string.Format("通过本次分析,黑名单共新增{0}条记录!", ipList.Count));
                    }
                }
                ipList = new List <IPCountInfoModel>();
                DataCacheHelper.Updata <IPCountInfoModel>(ipList);
                logger.Log(LogLevel.Info, string.Format("结束分析黑名单进程!"));
                //缓存重新加载黑名单信息
                LoadBlackList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }