using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace STMes
{
    public interface ILogService
    {
        void WriteLog(string message);
    }
    public class CoreWriteLogFile : ILogService
    {
        private static object lockObj = new object();



        #region ILogService 成员

        public void WriteLog(string message)
        {
            WriteLog(message, LogInfoLevel.Message, "", "");
        }
        public void WriteLog(string message, LogInfoLevel level)
        {
            WriteLog(message, level, "", "");
        }

        public void WriteLog(string message, string UserID, string codeInfo)
        {
            WriteLog(message, LogInfoLevel.Message, UserID, codeInfo);
        }
        public void WriteLog(string message, LogInfoLevel level, string UserID, string codeInfo)
        {
            lock (lockObj)
            {
                try
                {
                    string logFile = System.Environment.CurrentDirectory + "/log/" + "DBProxy_" + DateTime.Now.ToString("yyyyMMdd") + "_log.log";
                    FileInfo fi = new FileInfo(logFile);
                    StreamWriter sw = null;
                    try
                    {
                        if (fi.Exists)
                            sw = fi.AppendText();
                        else
                            sw = fi.CreateText();

                        string str = "";
                        if (level == LogInfoLevel.Error)
                            str = ">>>  Error  <<<";
                        else
                            str = ">>>  Message  <<<";

                        string title = ">>>  " + DateTime.Now + "  <<<";
                        title += str;

                        if (UserID != "")
                        {
                            title += ">>>  " + UserID + "  <<<";
                        }
                        if (codeInfo != "")
                        {
                            title += ">>>  " + codeInfo + "  <<<";
                        }

                        sw.WriteLine(title);
                        sw.WriteLine(message);
                        sw.WriteLine();
                        sw.Flush();
                    }
                    finally
                    {
                        if (sw != null)
                            sw.Close();
                    }
                }
                catch
                {
                    // do nothing.
                }
            }
        }


            ///////////////add longjing 9-29//////

        
        public void WriteLog(bool flag,Object obj,string sql)
        {
            lock (lockObj)
            {
                try
                {
                    string logpath = System.Environment.CurrentDirectory + "/log/DataOperation/" + DateTime.Now.ToString("yyyyMMdd")+"/" ;
                    if (!Directory.Exists(logpath))
                    {
                        Directory.CreateDirectory(logpath);
                    }
                    string logFile=logpath+"Query_" + DateTime.Now.ToString("HH_mm_ss") + "_log.log";
                    FileInfo fi = new FileInfo(logFile);
                    StreamWriter sw = null;

                    DataSet ds = new DataSet();

                    try
                    {
                        ds = obj as DataSet;
                        DataTable dt = ds.Tables[0];

                        if (fi.Exists)
                            sw = fi.AppendText();
                        else
                            sw = fi.CreateText();

                        //string str = "";
                        //if (level == LogInfoLevel.Error)
                        //    str = ">>>  Error  <<<";
                        //else
                        //    str = ">>>  Message  <<<";

                        string title = ">>>  " + DateTime.Now + "  <<<";
                        //title += str;

                        //if (UserID != "")
                        //{
                        //    title += ">>>  " + UserID + "  <<<";
                        //}
                        //if (CodeID != "")
                        //{
                        //    title += ">>>  " + CodeID + "  <<<";
                        //}


                        sw.WriteLine(title);
                        sw.WriteLine("查询语句："+sql);
                        
                        sw.WriteLine("查询数据集结果：\r\n");

                        /////////////////遍历打印DataSet/////////////
                        if (obj == null)
                            sw.WriteLine("DataSet为空!");

                        else if (dt.Rows.Count > 0)
                        {
                            foreach (DataRow dr in dt.Rows)
                            {
                                for (int i = 0; i < dt.Columns.Count; i++)
                                {
                                    sw.Write(dr[i].ToString() + "   ");
                                }
                                sw.WriteLine("\r\n");
                            }
                        }

                        ////////////////////////////////////////////
                        
                        sw.WriteLine();
                        sw.Flush();
                    }
                    finally
                    {
                        if (sw != null)
                            sw.Close();
                    }
                }
                catch
                {
                    // do nothing.
                }
            
            }
        }

        #endregion
    }

    public enum LogInfoLevel
    {
        Error = 1,
        Message = 2
    }


}
