using CallCenter.Utility;
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.knowledge
{
    [Authority]
    public class KnowledgeClassController : BaseController
    {
        private BLL.T_Sys_UserAccount userbll = new BLL.T_Sys_UserAccount();
        private BLL.T_Sys_RoleInfo rolebll = new BLL.T_Sys_RoleInfo();
        // 获取知识库分类列表
        public ActionResult GetAllList()
        {
            DataTable dt = new DataTable();
            string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
            string sql = "";

            dt = new BLL.T_RepositoryCategory().GetList(sql).Tables[0];

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

        // 二级网络单位知识库索要录添加知识库 获取知识库分类列表
        public ActionResult GetAllListEJ()
        {
            DataTable dt = new DataTable();
           
           

            dt = new BLL.T_RepositoryCategory().GetList("F_ParentId=0").Tables[0];

            List<Model.TreeModel> modelList = BindTree(dt, "0");

            if (modelList.Count > 0)
            {
                return Success("加载成功", modelList);
            }
            else
                return Error("加载失败");
        }


        // 获取知识库分类列表
        public ActionResult GetList()
        {
            DataTable dt = new DataTable();
            string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
            string sql = "";
            if (pid.Trim() != "")
            {
                sql += " and F_ParentId=" + pid.Trim();
            }
            else
                sql += " and F_ParentId=0";

            Model.T_Sys_RoleInfo role = rolebll.GetModel("WLDW");
            if (User.F_RoleId == role.F_RoleId)
            {
               sql = " and F_ParentId=0 and F_CategoryId not  in " +
                    "( select F_CategoryId from T_RepositoryCategory where F_CategoryName in('表彰批评类','信息类'))"; 
             
            }
        




            dt = new BLL.T_RepositoryCategory().GetList(" 1=1 " + sql).Tables[0];
            List<Model.TreeModel> modelList = BindTree(dt, "0");

            if (modelList.Count > 0)
            {
                return Success("加载成功", modelList);
            }
            else
                return Error("加载失败");
        }
        //tree  树形知识库分类
        private List<Model.TreeModel> BindTree(DataTable tab, string parentid)
        {
            DataTable tab2 = new DataTable();

            if (tab != null && tab.Rows.Count > 0)
            {
                List<Model.T_RepositoryCategory> categorylist = new BLL.T_RepositoryCategory().DataTableToList(tab);
                List<Model.TreeModel> modelList = new List<Model.TreeModel>(categorylist.Count);
                for (int i = 0; i < categorylist.Count; i++)
                {
                    Model.TreeModel model = new Model.TreeModel();
                    string currentID = categorylist[i].F_CategoryId.ToString();//当前功能ID
                    model.id = currentID;
                    model.IconCls = "";//图标
                    model.text = categorylist[i].F_CategoryName;
                    tab2 = new BLL.T_RepositoryCategory().GetList(" F_ParentId=" + currentID + " ").Tables[0];
                    if (tab2 != null && tab2.Rows.Count > 0)
                    {
                        model.children = BindTree(tab2, currentID);
                    }
                    modelList.Add(model);
                }
                return modelList;

            }
            else
            {
                return null;
            }

        }

        //获取知识库分类
        public ActionResult GetClass(string categoryid)
        {
            if (categoryid != null && categoryid.Trim() != "")
            {
                BLL.T_RepositoryCategory dBLL = new BLL.T_RepositoryCategory();
                Model.T_RepositoryCategory dModel = dBLL.GetModel(int.Parse(categoryid.Trim()));
                if (dModel != null)
                {
                    return Success("获取知识库分类信息成功", dModel);
                }
                else
                {
                    return Error("获取知识库分类信息失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }
        //[Authority]
        //添加知识库分类
        public ActionResult AddClass(string cname, string pid)
        {
            Model.T_RepositoryCategory dModel = new Model.T_RepositoryCategory();
            if (pid != "")
            {
                dModel.F_ParentId = int.Parse(pid.Trim());
            }
            else
                dModel.F_ParentId = 0;

            dModel.F_CategoryName = cname.Trim();

            dModel.F_CreateOn = DateTime.Now;
            dModel.F_CreateBy = User.F_UserId;

            int b = new BLL.T_RepositoryCategory().Add(dModel);
            if (b > 0)
            {
                string content = $"{User.F_UserName}({User.F_UserCode })添加知识库分类{ dModel.F_CategoryName}";
                string Operation = OperationLogController
                    .AddOperationList(content, User.F_UserCode, Common.DTRequest.GetIP(),0, 4594, b);
                return Success("添加成功");
            }
            else
            {
                return Success("添加失败");
            }

        }
        //[Authority]
        //编辑知识库分类
        public ActionResult EditClass(string cid, string cname, string pid)
        {
            if (cid != null && cid.Trim() != "")
            {
                BLL.T_RepositoryCategory dBLL = new BLL.T_RepositoryCategory();
                Model.T_RepositoryCategory dModel = dBLL.GetModel(int.Parse(cid.Trim()));
                if (dModel != null)
                {
                    if (pid.Trim() != "")
                    {
                        dModel.F_ParentId = int.Parse(pid.Trim());
                    }

                    if (cname.Trim() != "")
                    {
                        dModel.F_CategoryName = cname.Trim();
                    }
                    bool b = dBLL.Update(dModel);
                    if (b)
                    {
                        string content = $"{User.F_UserName}({User.F_UserCode })修改知识库分类{ dModel.F_CategoryName}";
                        string Operation = OperationLogController
                            .AddOperationList(content, User.F_UserCode, Common.DTRequest.GetIP(), 1, 4594, dModel.F_CategoryId );
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Success("编辑失败");
                    }
                }
                else
                    return Error("获取信息失败");
            }
            else
            {
                return Error("请选择要编辑的知识库分类");
            }

        }
        //[Authority]
        //删除知识库分类
        public ActionResult DelClass(string ids)
        {
            if (ids != null && ids.Length > 0)
            {
                DataTable dt = new BLL.T_RepositoryInformation().GetList("F_CategoryId=" + Convert.ToInt32(ids)).Tables[0];
                string rid = string.Empty;
                if (dt.Rows.Count > 0)
                {
                    //先删除知识库信息再删除知识库分类
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        rid = dt.Rows[i]["F_RepositoryId"].ToString();
                        if (!string.IsNullOrEmpty(rid))
                        {
                        
                            try
                            {
                                Model.T_RepositoryInformation Model = new BLL.T_RepositoryInformation().GetModel(int.Parse(ids.Trim()));
                                string content1 = $"{User.F_UserName}({User.F_UserCode })删除知识库{ Model.F_Title }";
                                string Operation1 = OperationLogController
                                    .AddOperationList(content1, User.F_UserCode, Common.DTRequest.GetIP(), 2, 4594, Model.F_RepositoryId);
                            }
                            catch
                            {

                            }
                         
                            new BLL.T_RepositoryInformation().Delete(Convert.ToInt32(rid));
                        }
                    }
                }

                try
                {
                    BLL.T_RepositoryCategory dBLL = new BLL.T_RepositoryCategory();
                    Model.T_RepositoryCategory dModel = dBLL.GetModel(int.Parse(ids.Trim()));
                    string content = $"{User.F_UserName}({User.F_UserCode })删除知识库分类{ dModel.F_CategoryName}";
                    string Operation = OperationLogController
                        .AddOperationList(content, User.F_UserCode, Common.DTRequest.GetIP(), 2, 4594, dModel.F_CategoryId);
                }
                catch
                {

                }
              
                bool r = new BLL.T_RepositoryCategory().Delete(Convert.ToInt32(ids));
                if (r)
                {
                 
                    return Success("删除成功");
                }
                else
                {
                    return Error("删除失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }
    }
}