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

namespace CallCenterApi.Interface.Controllers
{
    public class SeatGroupController : BaseController
    {
        BLL.T_Sys_SeatGroup seatBLL = new BLL.T_Sys_SeatGroup();
        /// <summary>
        /// 获取坐席组列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            DataTable dt = new DataTable();
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            string sql = "";
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_SeatGroup",
                "F_ZXZID",
                "*",
                sql,
                "ORDER BY F_ZXZID desc",
                pagesize,
                pageindex,
                true,
                out recordCount);

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

            return Content(obj.ToJson());
        }

        //获取坐席组列表
        public ActionResult GetSeatList()
        {
            List<Model.T_Sys_SeatGroup> seatList = seatBLL.GetModelList(" 1=1 order by F_ZXZID desc ");
            if (seatList.Count > 0)
                return Success("坐席组列表加载成功", seatList);
            else
                return Error("坐席组列表加载失败");
        }

        /// <summary>
        /// 获取坐席组
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public ActionResult GetSeatGroup(int ZXZID = 0)
        {
            Model.T_Sys_SeatGroup sModel = seatBLL.GetModel(ZXZID);
            if (sModel != null)
                return Success("获取坐席组信息成功", sModel);
            return Error("获取坐席组信息失败");
        }

        /// <summary>
        /// 添加坐席组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddSeatGroup(SeatGroupInput input)
        {
            if (GetExistByCode(input.ZXZCode, 0) || GetExistByName(input.ZXZName, 0))
            {
                return Error("坐席组编号和名称必须唯一，请重新输入！");
            }

            Model.T_Sys_SeatGroup sModel = new Model.T_Sys_SeatGroup();
            if (input.ZXZCode != null)
                sModel.F_ZXZCode = input.ZXZCode.Trim();
            if (input.ZXZName != null)
                sModel.F_ZXZName = input.ZXZName.Trim();
            if (input.ZXAtt != null)
                sModel.F_ZXAtt = input.ZXAtt.Trim();
            if (input.WHWDKey != null)
                sModel.F_WHWDKey = input.WHWDKey.Trim();
            if (input.WHBDKey != null)
                sModel.F_WHBDKey = input.WHBDKey.Trim();
            if (input.IVRKey != null)
                sModel.F_IVRKey = input.IVRKey;
            if (input.Des != null)
                sModel.F_Des = input.Des.Trim();

            sModel.F_CreateTime = DateTime.Now;
            sModel.F_CreateByName = CurrentUser.UserData.F_UserName;
            sModel.F_CreateBy = CurrentUser.UserData.F_UserId;

            if (seatBLL.Add(sModel) > 0)
                return Success("添加成功");
            else
                return Success("添加失败");
        }

        /// <summary>
        /// 编辑坐席组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditSeatGroup(SeatGroupInput input)
        {
            if (input.ZXZID <= 0)
                return Error("请选择要编辑的坐席组");

            if (GetExistByCode(input.ZXZCode, input.ZXZID) || GetExistByName(input.ZXZName, input.ZXZID))
            {
                return Error("坐席组编号和名称必须唯一，请重新输入！");
            }

            Model.T_Sys_SeatGroup sModel = seatBLL.GetModel(input.ZXZID);
            if (sModel == null)
                return Error("获取信息失败");

            if (input.ZXZCode != null)
                sModel.F_ZXZCode = input.ZXZCode.Trim();
            if (input.ZXZName != null)
                sModel.F_ZXZName = input.ZXZName.Trim();
            if (input.ZXAtt != null)
                sModel.F_ZXAtt = input.ZXAtt.Trim();
            if (input.WHWDKey != null)
                sModel.F_WHWDKey = input.WHWDKey.Trim();
            if (input.WHBDKey != null)
                sModel.F_WHBDKey = input.WHBDKey.Trim();
            if (input.IVRKey != null)
                sModel.F_IVRKey = input.IVRKey;
            if (input.Des != null)
                sModel.F_Des = input.Des.Trim();

            if (seatBLL.Update(sModel))
                return Success("编辑成功");
            return Error("编辑失败");
        }

        /// <summary>
        /// 删除坐席组
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelSeatGroup(string[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return Error("请选择要删除的坐席组");

            var idStr = string.Join(",", ids);
            if (string.IsNullOrEmpty(idStr.Trim()))
                return Error("请选择要删除的坐席组");

            if (seatBLL.DeleteList(idStr))
                return Success("删除成功");
            return Error("删除失败");
        }

        #region
        /// <summary>
        /// 根据坐席组编号查询是否存在此坐席组编号
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private bool GetExistByCode(string code, int? id)
        {
            string sql = " 1=1 ";
            if (code != null)
            {
                sql += " and F_ZXZCode='" + code.Trim() + "' ";
            }
            if (id != null)
            {
                sql += " and F_ZXZID <>" + id;
            }
            var ss = seatBLL.GetRecordCount(sql);
            if (ss > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 根据坐席组名称查询是否存在此坐席组编号
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private bool GetExistByName(string name, int? id)
        {
            string sql = " 1=1 ";
            if (name != null)
            {
                sql += " and F_ZXZName='" + name.Trim() + "' ";
            }
            if (id != null)
            {
                sql += " and F_ZXZID <>" + id;
            }
            var ss = seatBLL.GetRecordCount(sql);
            if (ss > 0)
                return true;
            else
                return false;
        }
        #endregion

    }
}