using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HySoft.Common;
using System.Data;

namespace HySoft.BaseCallCenter.Web.workordermanage.ajax
{
    /// <summary>
    /// YXGCSCountsList 的摘要说明
    /// </summary>
    public class YXGCSCountsList : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getGCSCountsList":
                    context.Response.Write(LoadYXGCSCountsList(context));
                    break;
            }
        }
        private string LoadYXGCSCountsList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = "SELECT (SELECT [F_DeptId] FROM [XKP_CallCenter].[dbo].[T_Sys_UserAccount] WHERE [F_UserId]=[F_OWNERID]) as [F_DEPTID],(SELECT [F_UserName] FROM [XKP_CallCenter].[dbo].[T_Sys_UserAccount] WHERE F_UserId=F_OWNERID) as F_USERNAME,F_OWNERID,COUNT(*) as F_DPGCOUNT FROM [XKP_CallCenter].[dbo].[T_Wo_WorkOrderTask] WHERE F_WORKORDERSTATEID=13 and F_LOSEFLAG=0 and F_OWNERID In (SELECT [F_UserId] FROM [XKP_CallCenter].[dbo].[T_Sys_UserAccount] WHERE F_DeleteFlag=0) Group by F_OWNERID ORDER BY F_DPGCOUNT DESC";
            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 1000;
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }
                int recordCount = 0;

                Model.PageData<Model.LDWorkOrderCount> pageModel = new Model.PageData<Model.LDWorkOrderCount>();

                //dt = BLL.PagerBLL.GetListPager(
                //    "T_Wo_WorkOrderTask",
                //    "F_TASKID",
                //    "(SELECT [F_DeptId] FROM [XKP_CallCenter].[dbo].[T_Sys_UserAccount] WHERE [F_UserId]=[F_OWNERID]) as [F_DEPTID],(SELECT [F_UserName] FROM [XKP_CallCenter].[dbo].[T_Sys_UserAccount] WHERE F_UserId=F_OWNERID) as F_USERNAME,F_OWNERID,COUNT(*) as F_DPGCOUNT",
                //    "AND F_WORKORDERSTATEID=13 and F_LOSEFLAG=0 and F_OWNERID In (SELECT [F_UserId] FROM [XKP_CallCenter].[dbo].[T_Sys_UserAccount] WHERE F_DeleteFlag=0) Group by F_OWNERID",
                //    "ORDER BY F_DPGCOUNT DESC",
                //    pagesize,
                //    pageindex,
                //    true,
                //    out recordCount);
                dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
                System.Collections.Generic.List<Model.LDWorkOrderCount> modelList = new BLL.LDWorkOrderCount().DataTableToList(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
                    new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.LDWorkOrderCount>));

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, pageModel);
                    res = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}