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

namespace HySoft.BaseCallCenter.Web.qualitymanage.ajax
{
    /// <summary>
    /// indexbase 的摘要说明
    /// </summary>
    public class indexbase : 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 "delete":
                    context.Response.Write(DeleteById(context));
                    break;
            }
        }

        #region 删除试题

        private string DeleteById(HttpContext context)
        {
            string str = "error";
            try
            {
                if (new BLL.T_QC_IndexBase().Delete(Convert.ToInt32(context.Request.Params["id"])))
                {
                    try
                    {
                        SetScoreByCategory(context.Request.Params["categoryid"]);
                    }
                    catch
                    { }
                    str = "success";
                }
                else
                {
                    str = "error";
                }
            }
            catch (Exception ex)
            {
            }
            return str;
        }

        private void SetScoreByCategory(string categoryid)
        {
            DataTable dt = new DataTable();
            try
            {
                Model.T_QC_IndexCategory model = new BLL.T_QC_IndexCategory().GetModel(Convert.ToInt32(categoryid));
                if (model != null)
                {
                    string parentid = model.F_ParentId.ToString();
                    dt = new BLL.T_QC_IndexBase().GetList("F_DeleteFlag=0 and (F_CategoryId=" + model.F_CategoryId + " or F_CategoryId in (select F_CategoryId from T_QC_IndexCategory where F_ParentId=" + categoryid + " )) ").Tables[0];
                    int score = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        score += Convert.ToInt32(dt.Rows[i]["F_Score"].ToString().Trim());
                    }
                    model.F_Expand1 = score.ToString();
                    if (new BLL.T_QC_IndexCategory().Update(model))
                    {
                        if (parentid != "0")
                        {
                            SetScoreByCategory(parentid);
                        }
                    }
                }
            }
            catch { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
        }

        #endregion

        #region 获取数据
        private string LoadList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;

                string categoryid = context.Request.Params["categoryid"];
                string key = context.Request.Params["key"];
                if (categoryid.Trim() != "")
                {
                    sql += " and (F_CategoryId=" + categoryid + " or F_CategoryId in (select F_CategoryId from T_QC_IndexCategory where F_ParentId=" + categoryid + " )) ";
                }

                if (key.Trim() != "")
                {
                    sql += " and (F_Title like '%" + key.Trim() + "%' or F_Content like '%" + key.Trim() + "%') ";
                }

                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_QC_IndexBase> pageModel = new Model.PageData<Model.T_QC_IndexBase>();

                dt = BLL.PagerBLL.GetListPager(
                    "vw_QC_IndexBase",
                    "F_IndexId",
                    "*",
                    " and F_DeleteFlag=0 " + sql,
                    "ORDER BY F_Sort",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_QC_IndexBase> modelList = new BLL.T_QC_IndexBase().DataTableToList(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.T_QC_IndexBase>));

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