using DBUtility;
using System;
using System.Data;
using System.Reflection;
using System.Web;
using Tools;
using DBUtility;
using MESClassLibrary.BLL.Log;

namespace PaintingScreen.Handler
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {
        HttpRequest Request = null;
        HttpResponse Response = null;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Request = context.Request;
            Response = context.Response;
            string method = Request.Params["method"];
            switch (method)
            {
                case "GetTableContent":
                    GetTableContent();
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        void GetTableContent()
        {
            string starttime = Request.Params["StartTime"];
            string endtime = Request.Params["EndTime"];
            if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 07:45:00") &&
                GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 19:45:00"))
            {
                starttime = GetDateTime().ToShortDateString() + " 07:45:00";
                endtime = GetDateTime().ToShortDateString() + " 19:45:00";
            }

            if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 19:45:00") &&
                GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 00:00:00"))
            {
                starttime = GetDateTime().ToShortDateString() + " 07:45:00";
                endtime = GetDateTime().ToShortDateString() + " 00:00:00";
            }
            if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 00:00:00") &&
                GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 07:45:00"))
            {
                starttime = GetDateTime().AddDays(-1).ToShortDateString() + " 19:45:00";
                endtime = GetDateTime().ToShortDateString() + " 07:45:00";
            }

            
            Response.Write(JSONTools.DataTableToJson("result", GetTable(starttime, endtime)));
            Response.End();
        }

        public DateTime GetDateTime()
        {
            string sql = "";
            DateTime time;
            DataTable dt;
            try
            {
                sql = @"select convert(char(23),getdate(),121) as time";

                dt = SqlHelper.GetDataDateTable(SqlHelper.SqlConnString, CommandType.Text, sql, null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    //time = Convert.ToDateTime(dt.Rows[0]["time"].ToString());
                    time = DateTime.ParseExact(dt.Rows[0]["time"].ToString(), "yyyy-MM-dd HH:mm:ss.fff",
                        System.Globalization.CultureInfo.CurrentCulture);

                }
                else
                {
                    time = DateTime.Now;
                }
                return time;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return DateTime.Now;
            }
        }

        private static DataTable GetTable(string starttime, string endtime)
        {
            DataTable res = new DataTable();
            try
            {
                
                string sql = @"
                                  declare @starttime datetime;
                                  declare @endtime datetime;
                                  set @starttime = '" + starttime + @"';
                                  set @endtime = '" + endtime + @"';
                                  select substring(barcode,1, 10) stockNo
                                      ,[ColorName]
	                                  into #a
                                  from tb_PaintLoad 
                                  where CreateTime >= @starttime and CreateTime <= @endtime



                                  select b.productName col1, a.ColorName col2, count(0) col3
                                  from #a a
                                  join tb_Product b
                                  on a.stockNo = b.stockNo
                                  group by b.productName, a.ColorName;

                                  drop table #a;
                            ";

                res = SqlHelper.GetDataDateTable(SqlHelper.SqlConnString, CommandType.Text, sql, null);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogManager(ex);
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
            }
            return res;
        }
    }
}