using CallCenter.Utility;
using CallCenterApi.DB;
using CallCenterApi.Interface.App_Start;
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.Out
{
    [OutOutActionFilter]
    public class OutApiController : BaseController
    {
        // GET: OutApi
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDeptList(string deptname)
        {
            BLL.T_Sys_Department departmentBLL = new BLL.T_Sys_Department();
            DataTable dt = new DataTable();
            string sql = " and F_State=0";
            if (!string.IsNullOrEmpty(deptname))
            {
                sql += " and F_DeptName like '%" + deptname.Trim() + "%'";
            }
            //dt = departmentBLL.GetList(0, "F_State=0" + sql, " F_Sort").Tables[0];

            dt = DbHelperSQL.Query("select F_DeptId Id,F_DeptName Name,F_PartentId PartentId,F_Sort Sort from T_Sys_Department where 1=1 " + sql).Tables[0];

            return Success("加载成功", dt);
        }

        /// <summary>
        /// 获取知识库列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRepositoryList()
        {
            DataTable dt = new DataTable();
            string sql = " and F_IsDelete=0 ";
            string strkey = HttpUtility.UrlDecode(RequestString.GetQueryString("key"));
            string strstarttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            int deptid = RequestString.GetInt("deptid", 0);
            int type = RequestString.GetInt("type", 1);
            int state = RequestString.GetInt("state", 0);

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

            #region sql 语句相关处理
            if (type == 1)
            {//查看通过审核和紧急事项
                sql += " and F_State in (" + (int)Model.EnumRepositoryState.AuditPass + "," + (int)Model.EnumRepositoryState.Urgent + ")";
            }
            else if (type == 2)
            {//待审核列表
                sql += " and F_State in (" + (int)Model.EnumRepositoryState.Audit + ")";
            }

            if (strkey.Trim() != "" && strkey != "undefined")
            {
                sql += " and (F_Key like '%" + strkey + "%' or F_Title like '%" + strkey + "%') ";
            }
            if (state != 0)
            {
                sql += " and F_State =" + state + " ";
            }

            if (deptid != 0)
            {
                sql += " and F_DeptId ='" + deptid + "'";
            }
            if (strstarttime.Trim() != "" && strstarttime != "undefined")
            {
                sql += " and datediff(day,F_CreateTime,'" + strstarttime + "')<=0 ";
            }
            if (strendtime.Trim() != "" && strendtime != "undefined")
            {
                sql += " and datediff(day,F_CreateTime,'" + strendtime + "')>=0   ";
            }
            #endregion

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

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            string cols = "*,dbo.GetUserName(F_CreateUser) as UserName,dbo.GetDeptName(F_DeptId) as DeptName";

            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Repository_List",
                "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>
        /// <param name="areaId"></param>
        /// <returns></returns>
        public ActionResult GetRepository()
        {
            int id = RequestString.GetInt("id", 0);
            Model.T_Repository_List dModel = new BLL.T_Repository_List().GetModel(id);

            if (dModel != null)
            {
                DataTable dtfile = new DataTable();
                if (!string.IsNullOrEmpty(dModel.F_File))
                {
                    var configfj = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='FileUrlPath' ").FirstOrDefault();

                    dtfile = DbHelperSQL.Query("select * from T_Sys_Accessories where F_FileId in (" + dModel.F_File + ")").Tables[0];

                    foreach (DataRow dr in dtfile.Rows)
                    {
                        dr["F_FileUrl"] = configfj.F_ParamValue + dr["F_FileUrl"].ToString();
                    }
                }

                var obj = new
                {
                    data = dModel,
                    file = dtfile
                };
                return Success("获取信息成功", obj);
            }
            return Error("获取信息失败");
        }
    }
}