
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.Model;
using ZXDT.CallCenter.MVCWeb.Models;
//using ZXDT.Common;
//using CallCenter.Utility;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class RepositoryCategoryController : BaseController
    {

        #region 知识库分类管理

        BLL.T_RepositoryCategory reposcategoryBLL = new BLL.T_RepositoryCategory();

        public ActionResult RepositoryCategoryList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();

            return View(model);
        }
        ///通过dic编码获取dic值
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetrepositorycategoryJsonModel(int parentId)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(reposcategoryBLL.GetRepositoryCategoryJsonModel(parentId));
        }
        /// <summary>
        /// 获取知识库分类数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("RepositoryCategoryDate")]
        [HttpGet]
        public string RepositoryCategoryDate(DateTime? NowDateTime, int page, int limit, int? parentId)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = " and  F_DeleteFlag=0";

            if (parentId != null)
            {
                sql += " and F_ParentId=" + parentId;
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_RepositoryCategory",
                "F_CategoryId",
                "*",
                sql,
                "ORDER BY F_Sort asc ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }
        /// <summary>
        /// 编辑分类
        /// </summary>
        /// <param name="F_WorkOrderTypeId">当前选中id</param>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// <returns></returns>
        public ActionResult RepositoryCategoryEdit(int categoryId, int editType)
        {

            Model.T_RepositoryCategory viewModel = new Model.T_RepositoryCategory();
            //当前对象实体
            Model.T_RepositoryCategory categoryModel = reposcategoryBLL.GetModel(categoryId);

            if (editType == 1)
            {
                viewModel.F_ParentId = categoryModel.F_CategoryId;
                viewModel.F_Sort = 1;
                viewModel.F_CategoryType = categoryModel.F_CategoryType;
            }
            else
            {
                viewModel = categoryModel;
            }


            return View(viewModel);
        }



        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveRepositoryCategoryData(T_RepositoryCategory categoryModel)
        {
            // 获取当前电脑名：
            string a = System.Net.Dns.GetHostName();

            // 根据电脑名取出全部IP地址：
            System.Net.IPAddress[] addressList = System.Net.Dns.Resolve(a).AddressList;
            string b = Convert.ToString(addressList[0]);

            if (categoryModel.F_CategoryId > 0)
            {
                //int id = Common.CommonRequest.dtUserId;
                //string usercode = Common.CommonRequest.dtUserCode;
                //string pp = Common.DTRequest.GetIP();

                

                return reposcategoryBLL.Update(categoryModel);
            }
            else
            {
                return reposcategoryBLL.Add(categoryModel) > 0;
            }
        }


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteCategoryData(int categoryId)
        {
            return reposcategoryBLL.Delete(categoryId);

        }
        #endregion

        #region 知识库内容管理
        BLL.T_RepositoryInformation reposCategoryContentBLL = new BLL.T_RepositoryInformation();

        //知识库导出模板
        //public string ToExcel()
        //{
        //    NPOIHelper npoi = new NPOIHelper();

        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("标题", typeof(string));//一列
        //    dt.Columns.Add("内容", typeof(string));//二列          


        //    npoi.ExportToExcel("导出模板",dt);
        //    return Success("导出成功");

        //}


        //public string importKnowledgeBase()
        //{
           
        //  //  int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
        //    HttpPostedFile _upfile = RequestString.GetFile("upFile");
        //    int headrow = 0;
        //    NPOIHelper npoi = new NPOIHelper();
        //    DataTable dt = npoi.ExcelToTable(_upfile, headrow);
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        headrow = headrow + 1;
        //        if (dr[0].ToString() != "" && dr[1].ToString() != "")
        //        {
        //            Model.T_Cus_CustomerBase model = new Model.T_Cus_CustomerBase();
        //            model.F_CustomerName = dr[0].ToString();    //客户姓名
        //            //model.F_CustomerEName = dr[1].ToString();   //联系人
        //            model.F_Sex = dr[1].ToString();   //性别
        //            model.F_Mobile = dr[2].ToString();          //手机号码
        //            model.F_Telephone = dr[3].ToString();       //单位电话
        //            model.F_ChargeTelephone = dr[4].ToString(); //其它号码
        //            model.F_PostCode = dr[5].ToString();        //邮编
        //            model.F_Email = dr[6].ToString();           //Email
        //            model.F_ChargeDept = dr[7].ToString();      //所在单位
        //            model.F_ServiceDept = dr[8].ToString();      //部门
        //            model.F_CustomerIndustry = dr[9].ToString();      //职务
        //            model.F_CreateBy = userId;
        //            model.F_CreatedOn = DateTime.Now;
        //            model.F_DeleteFlag = 0;

        //            if (new BLL.T_Cus_CustomerBase().Add(model) <= 0)
        //            {
        //                return Error("第" + headrow + "行，导入失败!");
        //            }
        //        }
        //        else
        //        {
        //            return Error("第" + headrow + "行，第1，2列信息不能为空，未导入");
        //        }
        //    }
        //    return Success("导入成功");

        //}


        public ActionResult RepositoryCategoryContentList(int dType)
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            model.DType = dType;
            return View(model);
        }
        /// <summary>
        /// 获取知识库内容数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("RepositoryCategoryContentDate")]
        [HttpGet]
        public string RepositoryCategoryContentDate(DateTime? NowDateTime, int page,
            int limit,int dType, int? parentId, string keyWord)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = " and  t.F_DeleteFlag=0   ";
            //and F_CreateBy="+F_UserID+"
            if (!string.IsNullOrEmpty(keyWord))
            {
                sql += " and ( t.F_Description like '%" + keyWord + "%' or F_Title like '%" + keyWord + "%')";
            }

            if (parentId != null && parentId > 1)
            {
                sql += " and t.F_CategoryId=" + parentId;
            }

          
            string tableName = "";
            if(dType>1)
            {
                tableName = " (SELECT m.*  FROM T_RepositoryCategory t INNER JOIN T_RepositoryInformation m ON t.F_CategoryId=m.F_CategoryId WHERE t.F_DeleteFlag=0 and m.F_DeleteFlag=0 and t.F_CategoryType=" + dType + ") as t";
                
            }
            else
            {
                tableName = " (SELECT m.*  FROM T_RepositoryCategory t INNER JOIN T_RepositoryInformation m ON t.F_CategoryId=m.F_CategoryId WHERE t.F_DeleteFlag=0 and m.F_DeleteFlag=0 ) as t";
               
            }



            DataTable dt = new DataTable();
            int recordCount = 0;
            //Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                tableName,
                "F_RepositoryId",
                "*",
                sql,
                "ORDER BY F_RepositoryId desc ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }
        /// <summary>
        /// 编辑内容
        /// </summary>
        /// <param name="F_WorkOrderTypeId">当前选中id</param>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// <returns></returns>
        public ActionResult RepositoryCategoryContentEdit(int categoryContentId, int editType)
        {

            Model.T_RepositoryInformation viewModel = new Model.T_RepositoryInformation();
            //当前对象实体
            Model.T_RepositoryInformation categoryContentModel = reposCategoryContentBLL.GetModel(categoryContentId);

            if (editType == 1)
            {
                viewModel.F_CategoryId = categoryContentId;

            }
            else
            {
                viewModel = categoryContentModel;
            }


            return View(viewModel);
        }


        /// <summary>
        /// 查看内容
        /// </summary>
        /// <param name="F_WorkOrderTypeId">当前选中id</param>

        /// <returns></returns>
        public ActionResult RepositoryCategoryContentView(int categoryContentId)
        {
            return View(reposCategoryContentBLL.GetModel(categoryContentId));
        }


        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveRepositoryCategoryContentData(T_RepositoryInformation categoryContentModel)
        {

            categoryContentModel.F_CreateBy = F_UserID;
            if (categoryContentModel.F_RepositoryId > 0)
            {
                return reposCategoryContentBLL.Update(categoryContentModel);
            }
            else
            {
               
                return reposCategoryContentBLL.Add(categoryContentModel) > 0;
            }
        }


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteCategoryContentData(int categoryContentId)
        {
            return reposCategoryContentBLL.Delete(categoryContentId);

        }


        #endregion


    }
}
