using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
namespace Stat.BLL
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class Log
    {
        private static Stat.IDAL.ILog dal = Stat.DALFactory.AbstractFactory.GetInstance().CreateLog();

        /// <summary>
        /// 记录日志在 磁盘(非数据库)
        /// </summary>
        /// <param name="message">要记录的信息</param>
        /// <param name="fullPath">保存物理路径</param>
        public static void SaveMessage(string fullPath, string message)
        {           
            try
            {
                System.IO.File.AppendAllText(fullPath, message);
            }
            catch (Exception) { }
        }
        /// <summary>
        /// 获取日志文件（非数据）
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        private static string GetMessage(string fullPath)
        {
            string msg = "";
            if (File.Exists(fullPath))
            {
                StreamReader sr = System.IO.File.OpenText(fullPath);
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    msg += s;
                }
            }
            return msg;
        }
        /// <summary>
        /// 清除日志文件
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        private static bool ClearLog(string fullPath)
        {
            try
            {
                System.IO.File.Delete(fullPath);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        /*---------【运行错误】-------------------------------------------------------------------*/
        /// <summary>
        /// 获取程序【运行错误】信息 的【保存路径】
        /// </summary>
        /// <returns></returns>
        public static string GetAppErrFullPath()
        {
            return HttpContext.Current.Request.PhysicalApplicationPath + "CacheTxt\\sys_log.txt";
        }
        /// <summary>
        /// Global 中使用（【运行错误 保存路径】）
        /// </summary>
        /// <param name="rq"></param>
        /// <returns></returns>
        public static string GetAppErrFullPath(System.Web.HttpRequest rq)
        {
            return rq.PhysicalApplicationPath + "CacheTxt\\sys_log.txt";
        }
        /// <summary>
        /// 获取程序【运行程序报错】信息
        /// </summary>
        /// <returns></returns>
        public static string GetApplication_Err()
        {

            return GetMessage(GetAppErrFullPath());
        }
        /// <summary>
        /// 记录 【运行错误】
        /// </summary>
        /// <param name="rq"></param>
        /// <param name="message">要记录的信息</param>
        public static void SaveApplication_Err(System.Web.HttpRequest rq, string message)
        {
            SaveMessage(GetAppErrFullPath(rq), message);
        }
        /// <summary>
        /// 清空 Appliction_Err
        /// </summary>
        /// <returns></returns>
        public static bool ClearAppliction_Err()
        {
            string fullPath = GetAppErrFullPath();
            return ClearLog(fullPath);
        }
        /*----------【升级参考】------------------------------------------------------------------*/
        /// <summary>
        /// 获取程序【升级参考】信息
        /// </summary>
        /// <returns></returns>
        public static string GetConsult()
        {
            return GetMessage(GetConsultFullPath());
        }
        /// <summary>
        /// 记录 【升级参考】
        /// </summary>
        /// <param name="message">要记录的信息</param>
        public static void SaveConsult(string message)
        {
            if (message != null && message.Trim().Length > 2)
            {
                SaveMessage(GetConsultFullPath(), message);
            }
        }
        /// <summary>
        /// 获取程序【升级参考】信息 的【保存路径】
        /// </summary>
        /// <returns></returns>
        public static string GetConsultFullPath()
        {
            return HttpContext.Current.Request.PhysicalApplicationPath + "CacheTxt\\stat_log.txt";
        }
        /// <summary>
        /// 清空 Consult
        /// </summary>
        /// <returns></returns>
        public static bool ClearConsult()
        {
            string fullPath = GetConsultFullPath();
            return ClearLog(fullPath);
        }
        /*--------------------------------------------------------------------------------------*/
        /// <summary>
        /// 新增/删除/修改
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>-1:存储过程执行失败,-2:存在相同的主键,Insert:返回插入自动ID,Update:返回更新记录数,Delete:返回删除记录数</returns>
        public static Int32 InsertUpdateDelete(Model.Log model)
        {
            return dal.InsertUpdateDelete(model);
        }
        /// <summary>
        ///  获取 日志 （可根据 ID name, Value ,Type查）
        /// </summary>
        /// <returns></returns>
        public static IList<Model.Log> Get(Model.Log logModel, ref Model.PageInfo pageModel)
        {

            return dal.Get(logModel, ref pageModel);
        }
    }
}
