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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getlist":
                    context.Response.Write(LoadList(context));
                    break;
                case "getGCSCountsList":
                    context.Response.Write(LoadGCSCountsList(context));
                    break;
                case "delete":
                    context.Response.Write(Delete(context));
                    break;
                //case "getstatelist":
                //    context.Response.Write(GetStateList(typeid == "" ? "-1" : typeid));
                //    break;
            }
        }


        #region 删除数据

        private string Delete(HttpContext context)
        {
            bool res = false;
            string arr = CommonRequest.GetQueryString("arrid");
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_Wf_WorkFlow().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_Wf_WorkFlow().DeleteList(arr);
            }
            return res ? "success" : "field";
        }
        #endregion
        #region 获取数据
        private string LoadGCSCountsList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = "SELECT F_UserId, F_UserCode,F_UserName,(select  COUNT(*) from V_WorkOrderBase where   F_INSTANCEID in(select F_INSTANCEID from T_Wo_WorkOrderTask where F_LOSEFLAG=0 and F_OWNERID=T_Sys_UserAccount.F_UserId) and F_WORKORDERSTATEID!=0) as F_GroupId  FROM T_Sys_UserAccount where F_RoleId=31 ";
            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

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

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

                dt = BLL.PagerBLL.GetListPager(
                    "T_Sys_UserAccount",
                    "F_UserId",
                    "F_UserId, F_UserCode,F_UserName,(select  COUNT(*) from V_WorkOrderBase where   F_INSTANCEID in(select F_INSTANCEID from T_Wo_WorkOrderTask where  F_LOSEFLAG=0 and F_OWNERID=T_Sys_UserAccount.F_UserId) and F_WORKORDERSTATEID!=0) as F_GroupId ",
                    " and F_RoleId=31",
                    "ORDER BY F_UserId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Sys_UserAccount> modelList = new BLL.T_Sys_UserAccount().DataTableToList1(dt);
                for (int i = 0; i < modelList.Count; i++)
                {

                }
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.T_Sys_UserAccount>));

                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;
        }

        private string LoadList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";
            try
            {
                string customername = HttpUtility.UrlDecode(CommonRequest.GetQueryString("customername"));
                string time = HttpUtility.UrlDecode(CommonRequest.GetQueryString("createdate"));
                string repairmanname = HttpUtility.UrlDecode(CommonRequest.GetQueryString("repairmanname"));

                string F_Name = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_Name"));
                string F_TypeName = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_TypeName"));
                string F_USERNAME = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_USERNAME"));
                string F_CODE = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_CODE"));
                string F_REPAIRMANPHONE = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_REPAIRMANPHONE"));
                string F_CustomerNature = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_CustomerNature"));
                string F_SystemType = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_SystemType"));
                string F_ServiceDept = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_ServiceDept"));
                string F_QualityGuaranteeEndTime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("F_QualityGuaranteeEndTime"));

                string userid = CommonRequest.GetQueryString("userid");

                sql = " and F_INSTANCEID in(select F_INSTANCEID from T_Wo_WorkOrderTask where  F_LOSEFLAG=0 and F_OWNERID=" + userid + ") and F_WORKORDERSTATEID=9";
                if (customername.Trim() != "")
                {
                    sql += " and F_CUSTOMERNAME like '%" + customername.Trim() + "%' ";
                }
                if (repairmanname.Trim() != "")
                {
                    sql += " and F_REPAIRMANNAME like '%" + repairmanname.Trim() + "%' ";
                }
                if (time.Trim() != "")
                {
                    sql += " and CONVERT(varchar , F_CREATEDATE, 120)>=CONVERT(varchar , '" + time.Trim() + " 00:00:01', 120) and CONVERT(varchar , F_CREATEDATE, 120)<=CONVERT(varchar , '" + time.Trim() + " 23:59:59', 120) ";
                }


                if (F_Name.Trim() != "")
                {
                    sql += " and F_Name='" + F_Name.Trim() + "' ";
                }
                if (F_TypeName.Trim() != "")
                {
                    sql += " and F_TypeName='" + F_TypeName.Trim() + "' ";
                }
                if (F_USERNAME.Trim() != "")
                {
                    sql += " and F_USERNAME like '%" + F_USERNAME.Trim() + "%' ";
                }
                if (F_CODE.Trim() != "")
                {
                    sql += " and F_CODE like '%" + F_CODE.Trim() + "%' ";
                }
                if (F_REPAIRMANPHONE.Trim() != "")
                {
                    sql += " and F_REPAIRMANPHONE like '%" + F_REPAIRMANPHONE.Trim() + "%' ";
                }
                if (F_CustomerNature.Trim() != "")
                {
                    sql += " and F_CustomerNature like '%" + F_CustomerNature.Trim() + "%' ";
                }
                if (F_SystemType.Trim() != "")
                {
                    sql += " and F_SystemType like '%" + F_SystemType.Trim() + "%' ";
                }
                if (F_ServiceDept.Trim() != "")
                {
                    sql += " and F_ServiceDept like '%" + F_ServiceDept.Trim() + "%' ";
                }
                if (F_QualityGuaranteeEndTime.Trim() != "")
                {
                    sql += " and CONVERT(varchar , F_QualityGuaranteeEndTime, 120)>=CONVERT(varchar , '" + F_QualityGuaranteeEndTime.Trim() + " 00:00:01', 120) and CONVERT(varchar , F_QualityGuaranteeEndTime, 120)<=CONVERT(varchar , '" + F_QualityGuaranteeEndTime.Trim() + " 23:59:59', 120) ";
                }

                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }
                sql += CommonRequest.GetQueryString("whereSql");

                int recordCount = 0;

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

                dt = BLL.PagerBLL.GetListPager(
                    "V_WorkOrderBase",
                    "F_WORKORDERID",
                    "*",
                    sql,
                    "ORDER BY F_WORKORDERID desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Wo_WorkOrderBase> modelList = new BLL.T_Wo_WorkOrderBase().DataTableToList(dt);
                for (int i = 0; i < modelList.Count; i++)
                {

                }
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.T_Wo_WorkOrderBase>));

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