using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.quality
{
    public class IndexBaseController : BaseController
    {
        //获取指标列表
        public ActionResult GetList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            string sql = "";
            DataTable dt = new DataTable();

            string categoryid = RequestString.GetQueryString("categoryid");
            string key = WebHelper.UrlDecode(RequestString.GetQueryString("key"));

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            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() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "vw_QC_IndexBase",
                "F_IndexId",
                "*",
                " and F_DeleteFlag=0 " + sql,
                "ORDER BY F_Sort",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var obj = new
            {
                rows = dt,
                total = recordCount
            };

            res = Content(obj.ToJson());

            //System.Collections.Generic.List<Model.T_QC_IndexBase> modelList = new BLL.T_QC_IndexBase().DataTableToList(dt);

            //if (modelList.Count > 0)
            //{
            //    res = Success("指标列表加载成功", modelList);
            //}
            //else
            //{
            //    res = Error("指标列表加载失败");
            //}

            return res;
        }

        //获取指标
        public ActionResult GetIndexBase(string id)
        {
            ActionResult res = NoToken("未知错误，请重新登录");

            if (id.Trim() != "")
            {
                BLL.T_QC_IndexBase dBLL = new BLL.T_QC_IndexBase();
                Model.T_QC_IndexBase dModel = dBLL.GetModel(int.Parse(id.Trim()));
                if (dModel != null)
                {
                    res = Success("获取指标成功", dModel);
                }
                else
                {
                    res = Error("获取指标失败");
                }
            }
            else
            {
                res = Error("获取参数失败");
            }

            return res;
        }

        //添加指标
        public ActionResult AddIndexBase(string cid, string score, string sort, string title, string remark)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                Model.T_QC_IndexBase dModel = new Model.T_QC_IndexBase();

                if (!string.IsNullOrWhiteSpace(cid.Trim()))
                {
                    dModel.F_CategoryId = int.Parse(cid.Trim());
                }
                if (!string.IsNullOrWhiteSpace(sort.Trim()))
                {
                    dModel.F_Sort = Convert.ToInt32(sort.Trim());
                }
                if (!string.IsNullOrWhiteSpace(score.Trim()))
                {
                    dModel.F_Score = Convert.ToInt32(score.Trim());
                }
                if (!string.IsNullOrWhiteSpace(title.Trim()))
                {
                    dModel.F_Title = WebHelper.UrlDecode(title.Trim());
                }
                if (!string.IsNullOrWhiteSpace(remark.Trim()))
                {
                    dModel.F_Remark = WebHelper.UrlDecode(remark.Trim());
                }

                dModel.F_CreateOn = DateTime.Now;
                dModel.F_CreateBy = CurrentUser.UserData.F_UserId;
                dModel.F_DeleteFlag = 0;

                bool b = new BLL.T_QC_IndexBase().Add(dModel) > 0;
                if (b)
                {
                    //添加成功后要将对应父级的分值增加上
                    SetScoreByCategory(dModel.F_CategoryId.ToString());
                    res = Success("添加成功");
                }
                else
                {
                    res = Success("添加失败");
                }

            }
            return res;
        }

        //编辑指标
        public ActionResult EditIndexBase(string id, string cid, string score, string sort, string title, string remark)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                if (cid.Trim() != "")
                {
                    BLL.T_QC_IndexBase dBLL = new BLL.T_QC_IndexBase();
                    Model.T_QC_IndexBase dModel = dBLL.GetModel(int.Parse(id.Trim()));
                    if (dModel != null)
                    {
                        if (!string.IsNullOrWhiteSpace(cid.Trim()))
                        {
                            dModel.F_CategoryId = int.Parse(cid.Trim());
                        }
                        if (!string.IsNullOrWhiteSpace(sort.Trim()))
                        {
                            dModel.F_Sort = Convert.ToInt32(sort.Trim());
                        }
                        if (!string.IsNullOrWhiteSpace(score.Trim()))
                        {
                            dModel.F_Score = Convert.ToInt32(score.Trim());
                        }
                        if (!string.IsNullOrWhiteSpace(title.Trim()))
                        {
                            dModel.F_Title = WebHelper.UrlDecode(title.Trim());
                        }
                        if (!string.IsNullOrWhiteSpace(remark.Trim()))
                        {
                            dModel.F_Remark = WebHelper.UrlDecode(remark.Trim());
                        }
                        bool b = dBLL.Update(dModel);
                        if (b)
                        {
                            SetScoreByCategory(dModel.F_CategoryId.ToString());
                            res = Success("编辑成功");
                        }
                        else
                        {
                            res = Success("编辑失败");
                        }
                    }
                    else
                        res = Error("获取信息失败");
                }
                else
                {
                    res = Error("请选择要编辑的指标");
                }

            }
            return res;
        }

        //删除指标记录
        public ActionResult DelIndexBase(string id, string cid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            DataTable dt = new DataTable();

            if (!string.IsNullOrEmpty(id.Trim()))
            {
                if (new BLL.T_QC_IndexBase().Delete(Convert.ToInt32(id)))
                {
                    SetScoreByCategory(cid);
                    res = Success("删除成功");
                }
                else
                {
                    res = Error("删除失败");
                }

            }
            else
            {
                res = Error("请选择要删除的记录");
            }

            return res;
        }

        //设置指标分数
        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();
            }
        }
    }
}