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

namespace CallCenterApi.Interface.Controllers
{
    [Authority]
    public class CheckPointController : BaseController
    {
        // GET: CheckPoint

        /// <summary>
        /// 获取标准列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCheckBaseList()
        {
            int type = RequestString.GetInt("type", 0);
            string key = RequestString.GetQueryString("key");
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            var sql = " and 1=1 ";
            if (type != 0)
            {
                sql += " and F_Type = '" + type + "'";
            }
            if (!string.IsNullOrEmpty(key))
            {
                sql += " and F_Name like '%" + key + "%'";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
              "T_Check_Base",
              "F_Id",
              "*",
              sql,
              "ORDER BY F_CreateTime desc ",
              pagesize,
              pageindex,
              true,
              out recordCount);

            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取标准
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public ActionResult GetCheckBase()
        {
            int id = RequestString.GetInt("id", 0);

            Model.T_Check_Base baseModel = new BLL.T_Check_Base().GetModel(id);
            return Success("加载成功", baseModel);

        }

        /// <summary>
        /// 修改分值和类型
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateCheckPoint()
        {
            int id = RequestString.GetInt("id", 0);
            int point = RequestString.GetInt("point", 0);
            int pointtype = RequestString.GetInt("pointtype", 0);

            Model.T_Check_Base baseModel = new BLL.T_Check_Base().GetModel(id);
            if (baseModel != null)
            {
                baseModel.F_Point = point;
                baseModel.F_PointType = pointtype;
                if (new BLL.T_Check_Base().Update(baseModel))
                {
                    return Success("修改成功");
                }
                else
                {
                    return Error("修改失败");
                }
            }
            else
            {
                return Error("修改失败");
            }
        }

        /// <summary>
        /// 添加/编辑标准
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult AddCheckBase()
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = new BLL.T_Sys_UserAccount().GetModel(userId);
            int id = RequestString.GetInt("id", 0);
            int type = RequestString.GetInt("type", 0);
            string name = RequestString.GetFormString("name");
            string remark = RequestString.GetFormString("remark");
            int point = RequestString.GetInt("point", 0);
            int pointtype = RequestString.GetInt("pointtype", 0);

            Model.T_Check_Base orderModel = new Model.T_Check_Base();
            if (id == 0)
            {
                orderModel.F_Type = type;
                orderModel.F_Name = name;
                orderModel.F_Remark = remark;
                orderModel.F_PointType = pointtype;
                orderModel.F_Point = point;
                orderModel.F_CreateTime = DateTime.Now;
                orderModel.F_CreateUser = userModel.F_UserCode;
                if (new BLL.T_Check_Base().Add(orderModel) > 0)
                {
                    return Success("标准添加成功");
                }
                else
                    return Error("标准添加失败");

            }
            else
            {
                orderModel = new BLL.T_Check_Base().GetModel(id);
                if (orderModel != null)
                {
                    orderModel.F_Type = type;
                    orderModel.F_Name = name;
                    orderModel.F_Remark = remark;
                    orderModel.F_PointType = pointtype;
                    orderModel.F_Point = point;
                    if (new BLL.T_Check_Base().Update(orderModel))
                    {
                        return Success("标准修改成功");
                    }
                    else
                        return Error("标准修改失败");

                }
                else
                {
                    return Error("标准修改失败");
                }
            }
        }

        /// <summary>
        /// 删除标准
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelCheckBase(string[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("获取参数失败");
            StringBuilder sb = new StringBuilder();
            foreach (var item in ids)
            {
                sb.Append("'" + item + "',");
            }
            if (new BLL.T_Check_Base().DeleteList(sb.ToString().Trim(',')))
            {
                return Success("删除成功");
            }
            else
                return Error("删除失败");

        }

        /// <summary>
        /// 获取考核列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCheckPointList()
        {
            string year = RequestString.GetQueryString("year");
            string month = RequestString.GetQueryString("month");
            int deptid = RequestString.GetInt("deptid",0);
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            var sql = " and 1=1 ";
            if (deptid != 0)
            {
                sql += " and F_DeptId = '" + deptid + "'";
            }
            if (!string.IsNullOrEmpty(year))
            {
                sql += " and F_Year = '" + year + "'";
            }
            if (!string.IsNullOrEmpty(month))
            {
                sql += " and F_Month = '" + month + "'";
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
              "T_Check_List",
              "F_Id",
              "*,dbo.GetDeptName(F_DeptId) DeptName",
              sql,
              "ORDER BY F_CreateTime desc ",
              pagesize,
              pageindex,
              true,
              out recordCount);

            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 当月考核分数
        /// </summary>
        /// <param name="sdate"></param>
        /// <param name="edate"></param>
        /// <param name="isdc"></param>
        /// <returns></returns>
        public ActionResult GetCheckPoint(string sdate, string edate, int isdc = 0)
        {
            DateTime stime = DateTime.Now;
            DateTime etime = DateTime.Now;

            if (!string.IsNullOrEmpty(sdate))
            {
                stime = DateTime.Parse(sdate);
            }
            if (!string.IsNullOrEmpty(edate))
            {
                etime = DateTime.Parse(edate);
            }
            if (string.IsNullOrEmpty(sdate) && string.IsNullOrEmpty(edate))
            {
                stime = new DateTime(stime.Year, stime.Month, 1); ;
            }
            Dictionary<string, string> paras = new Dictionary<string, string>();
            paras.Add("@sdate", stime.ToString("yyyy-MM-dd"));
            paras.Add("@edate", etime.ToString("yyyy-MM-dd"));
            var obj = DbHelperSQL.RunProcedure("P_DeptPointReport", paras, "DeptPointReport");
            return Success("成功", obj);
        }
    }
}