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.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    public class GroupController : BaseController
    {
        // GET: Group

        /// <summary>
        /// 获取班组列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetList(int isdc = 0)
        {
            DataTable dt = new DataTable();
            string sql = " and F_IsDelete=0 ";
            string strcode = HttpUtility.UrlDecode(RequestString.GetQueryString("code"));
            string strname = HttpUtility.UrlDecode(RequestString.GetQueryString("name"));

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;


            if (strcode.Trim() != "" && strcode != "undefined")
            {
                sql += " and F_GroupCode = '" + strcode.Trim() + "' ";
            }
            if (strname.Trim() != "" && strname != "undefined")
            {
                sql += " and F_GroupName like '%" + strname + "%' ";
            }


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

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            string cols = "*,dbo.GetUserName(F_CreateUser) as UserName";

            if (isdc > 0)
            {
                var dtdc = DbHelperSQL.Query(" select " + cols + " from T_Sys_Group where 1=1 " + sql).Tables[0];
                var msg = new NPOIHelper().ExportToExcel("班组列表", dtdc);
                if (msg == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }

            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Sys_Group",
                "F_Id",
                cols,
                sql,
                "ORDER BY F_CreateTime DESC",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        /// <summary>
        /// 获取班组列表
        /// </summary>
        /// <returns></returns>
        [Authority]
        public ActionResult GetGroupList()
        {
            DataTable dt = new BLL.T_Sys_Group().GetList(" F_State=0 and F_IsDelete=0 ").Tables[0];
            dt.Columns.Add("Users", typeof(object));
            foreach (DataRow dr in dt.Rows)
            {
                dr["Users"] = new BLL.T_Sys_UserAccount().GetList(" F_GroupCode='"+dr["F_GroupCode"].ToString()+"' ").Tables[0];
            }
            return Success("加载成功", dt);
        }

        /// <summary>
        /// 获取班组
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetGroup(int id = 0)
        {
            if (id != 0)
            {
                return Success("成功", new BLL.T_Sys_Group().GetModel(id));
            }
            else
            {
                return Error("参数错误");
            }
        }

        /// <summary>
        /// 获取班组
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult GetGroupByCode(string code)
        {
            if (!string.IsNullOrEmpty(code))
            {
                return Success("成功", new BLL.T_Sys_Group().GetModelList(" F_GroupCode='" + code + "' ").FirstOrDefault());
            }
            else
            {
                return Error("参数错误");
            }
        }

        /// <summary>
        /// 添加班组
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Authority]
        public ActionResult AddGroup()
        {
            

            
            int id = RequestString.GetInt("id", 0);
            string name = RequestString.GetFormString("name");
            string code = RequestString.GetFormString("code");
            string remark = RequestString.GetFormString("remark");

            Model.T_Sys_Group dModel = new Model.T_Sys_Group();
            if (id == 0)
            {
                var list = new BLL.T_Sys_Group().GetModelList(" F_GroupCode='" + code + "' and F_IsDelete=0 ");
                if (list.Count > 0)
                {
                    return Error("已经存在此班组");
                }
                else
                {
                    dModel.F_GroupCode = code;
                    dModel.F_GroupName = name;
                    dModel.F_Remark = remark;
                    dModel.F_State = 0;
                    dModel.F_IsDelete = 0;
                    dModel.F_CreateUser = User.F_UserCode;
                    dModel.F_CreateTime = DateTime.Now;
                    int n = new BLL.T_Sys_Group().Add(dModel);
                    if (n > 0)
                        return Success("添加成功", n);
                    else
                        return Error("添加失败");
                }
            }
            else
            {
                dModel = new BLL.T_Sys_Group().GetModel(id);
                if (dModel != null)
                {
                    var list = new BLL.T_Sys_Group().GetModelList(" F_GroupCode='" + code + "' and F_IsDelete=0 and F_Id!='" + id + "' ");
                    if (list.Count > 0)
                    {
                        return Error("已经存在此班组");
                    }
                    else
                    {
                        dModel.F_GroupCode = code;
                        dModel.F_GroupName = name;
                        dModel.F_Remark = remark;
                        if (new BLL.T_Sys_Group().Update(dModel))
                            return Success("修改成功");
                        else
                            return Error("修改失败");
                    }
                }
                else
                {
                    return Error("修改失败");
                }
            }
        }

        /// <summary>
        /// 删除班组
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Authority]
        public ActionResult DelGroup(string[] ids)
        {
            
            

            if (ids == null || ids.Length <= 0)
            {
                return Error("请选择要删除的班组");
            }

            var idStr = string.Join(",", ids);
            if (string.IsNullOrEmpty(idStr.Trim()))
            {
                return Error("请选择要删除的班组");
            }
            int n = DbHelperSQL.ExecuteSql(" update T_Sys_Group set F_IsDelete=1,F_DeleteUser='" + User.F_UserCode + "',F_DeleteTime=getdate() where F_Id in (" + idStr + ")");
            if (n > 0)
            {
                return Success("删除成功");
            }
            return Error("删除失败");
        }
    }
}