using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers.Quality
{
    public class IndexBaseController : BaseController
    {

        //获取指标列表
        public ActionResult GetList(int categoryid, string key, int page = 1, int pagesize = 10)
        {
            string sql = "";
            DataTable dt = new DataTable();

            if (categoryid > 0)
            {
                //sql += " and (F_CategoryId=" + categoryid + " or F_CategoryId in (select F_CategoryId from T_QC_IndexCategory where F_ParentId=" + categoryid + " )) ";
                sql += " and b.F_CategoryId=" + categoryid;
            }

            if (!string.IsNullOrEmpty(key))
            {
                sql += " and b.F_Title like '%" + key.Trim() + "%' ";
            }

            int recordCount = 0;
            dt = Bll.PagerBll.GetListPager(
                "T_QC_IndexBase b with(nolock) left join T_QC_IndexCategory c with(nolock) on b.F_CategoryId=c.F_CategoryId",
                "b.F_IndexId",
                "b.*,c.F_CategoryName",
                " and b.F_DeleteFlag=0 and C.F_ParentId !=0" + sql,
                "ORDER BY b.F_Sort",
                pagesize,
                page,
                true,
                out recordCount);

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

            return Content(obj.ToJson());
        }

        //获取指标
        public ActionResult GetIndexBase(int id)
        {
            if (id > 0)
            {
                Bll.T_QC_IndexBase dBLL = new Bll.T_QC_IndexBase();
                Model.T_QC_IndexBase dModel = dBLL.GetModel(id);
                if (dModel != null)
                {
                    return Success("获取指标成功", dModel);
                }
                else
                {
                    return Error("获取指标失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        //添加指标
        [HttpPost]
        public ActionResult AddIndexBase(int cid, string title, string remark, int score = 0, int sort = 0)
        {
            Model.T_QC_IndexBase dModel = new Model.T_QC_IndexBase();
            if (cid > 0)
                dModel.F_CategoryId = cid;
            else
                return Error("请选择指标分类");
            dModel.F_Sort = sort;
            dModel.F_Score = score;

            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 = User.F_UserId;
            dModel.F_DeleteFlag = 0;

            bool b = new Bll.T_QC_IndexBase().Add(dModel) > 0;
            if (b)
            {
                DB.DbHelperSQL.ExecuteSql("update T_QC_IndexCategory set F_Score=F_Score+" + dModel.F_Score + " where F_CategoryId =" + dModel.F_CategoryId);
                return Success("添加成功");
            }
            else
            {
                return Success("添加失败");
            }
        }

        //编辑指标
        [HttpPost]
        public ActionResult EditIndexBase(int id, int cid, int score, int sort, string title, string remark)
        {
            if (id > 0)
            {
                Bll.T_QC_IndexBase dBLL = new Bll.T_QC_IndexBase();
                Model.T_QC_IndexBase dModel = dBLL.GetModel(id);
                if (dModel != null)
                {
                    int oldscore = dModel.F_Score.Value;
                    dModel.F_CategoryId = cid;
                    dModel.F_Sort = sort;
                    dModel.F_Score = score;
                    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_ModifyBy = User.F_UserId;
                    dModel.F_ModifyOn = DateTime.Now;

                    bool b = dBLL.Update(dModel);
                    if (b)
                    {
                        DB.DbHelperSQL.ExecuteSql("update T_QC_IndexCategory set F_Score=F_Score+" + dModel.F_Score + "-" + oldscore + " where F_CategoryId =" + dModel.F_CategoryId);
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Success("编辑失败");
                    }
                }
                else
                    return Error("获取信息失败");
            }
            else
            {
                return Error("请选择要编辑的指标");
            }

        }

        //删除指标记录
        [HttpPost]
        public ActionResult DelIndexBase(int id)
        {
            if (id > 0)
            {
                Bll.T_QC_IndexBase dBLL = new Bll.T_QC_IndexBase();
                Model.T_QC_IndexBase dModel = dBLL.GetModel(id);
                if (dModel != null)
                {
                    if (dBLL.Delete(id))
                    {
                        DB.DbHelperSQL.ExecuteSql("update T_QC_IndexCategory set F_Score=F_Score-" + dModel.F_Score + " where F_CategoryId =" + dModel.F_CategoryId);
                        return Success("删除成功");
                    }
                    else
                    {
                        return Error("删除失败");
                    }
                }
                else
                {
                    return Error("获取信息失败");
                }
            }
            else
            {
                return Error("请选择要删除的记录");
            }
        }
    }
}