using BLL;
using BLL.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace FineUIPro.Web.ManHours
{
    /// <summary>
    /// ManHoursStatisticsHandler 的摘要说明
    /// </summary>
    public class ManHoursStatisticsHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string Start = DateTime.Now.AddYears(-2).ToString("yyyyMM");
            string End = DateTime.Now.ToString("yyyyMM");

            string sId = context.Request["sId"].ToString();
            try
            {
                string[] sIdArr = sId.Split('@');
                string id = sIdArr[1];
                var depart = BLL.DepartService.GetDepartById(id);
                List<SqlParameter> listStr = new List<SqlParameter>();
                if (depart != null && depart.DepartName != "CTE")
                {
                    listStr.Add(new SqlParameter("@departId", depart.DepartId));
                }
                var user = BLL.Sys_UserService.GetCTEUserByUserId(id);
                if (user != null)
                {
                    listStr.Add(new SqlParameter("@userId", user.UserId));
                }
                var eproject = BLL.EProjectService.GeteProjectById(id);
                if (eproject != null)
                {
                    listStr.Add(new SqlParameter("@eprojectId", eproject.EProjectId));
                }
                listStr.Add(new SqlParameter("@startDate", DateTime.Now.AddYears(-2)));

                SqlParameter[] parameter = listStr.ToArray();
               
                string SetJson = "";
                if (sIdArr[0].ToString() == "D2")
                {
                    string strSql = @" SELECT * FROM Sys_ActualManHourMonthSet 
                                       where  Years in ('" + (DateTime.Now.Year - 1) + "','" + (DateTime.Now.Year - 2) + "','" + DateTime.Now.Year + "','" + (DateTime.Now.Year + 1) + "')  and DepartId='" + sIdArr[1].ToString() + "'";
                    DataTable dt = SQLHelper.GetDataTableRunText(strSql, null);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        SetJson = JsonHelper.DataTableToJSON(dt);
                    }
                    
                    DataTable tb = SQLHelper.GetDataTableRunProc("Proc_ManHoursStatisticsGrid2", parameter);
                    string json = JsonHelper.DataTableToJSON(tb);
                    string js = "{\"json\":" + json + ",\"SetJson\":" + SetJson + "}";
                    HttpContext.Current.Response.Write(js);
                }
                else if (sIdArr[0].ToString() == "UD")
                {
                    string strSql = @" SELECT * FROM Sys_ActualManHourMonthSet 
                                       where  Years in ('" + (DateTime.Now.Year - 1) + "','" + (DateTime.Now.Year - 2) + "','" + DateTime.Now.Year + "','" + (DateTime.Now.Year + 1) + "')  and DepartId='" + sIdArr[2] + "'";
                    DataTable dt = SQLHelper.GetDataTableRunText(strSql, null);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        SetJson = JsonHelper.DataTableToJSON(dt);
                    }
                     DataTable table2 =  SQLHelper.GetDataTableRunProc("Proc_ManHoursStatisticsGrid2", parameter);
                    string json = JsonHelper.DataTableToJSON(table2);
                    string js = "{\"json\":" + json + ",\"SetJson\":" + SetJson + "}";
                    HttpContext.Current.Response.Write(js);
                }
                else
                { 
                    DataTable table2 = SQLHelper.GetDataTableRunProc("Proc_ManHoursStatisticsGrid2", parameter) ;
                    string json = JsonHelper.DataTableToJSON(table2);
                    HttpContext.Current.Response.Write(json);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}