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

namespace CallCenterApi.Interface.Controllers.knowledge
{
    public class KnowledgeReportingController : BaseController
    {
        BLL.T_KnowledgeReporting infoBLL = new BLL.T_KnowledgeReporting();
        private BLL.T_Sys_Department departmentBLL = new BLL.T_Sys_Department();
        private BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
        // 获取知识库上报列表
        public ActionResult GetList(string keywords, string pid)
        {
            string strusercode = HttpUtility.UrlDecode(RequestString.GetQueryString("usercode"));
            string seats = HttpUtility.UrlDecode(RequestString.GetQueryString("seats"));//调度员
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
            int state = RequestString.GetInt("state",-1);
            int type = RequestString.GetInt("type", -1);
            int IsReply = RequestString.GetInt("isReply", -1);
            DataTable dt = new DataTable();
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            string sql = "";
            if (state>-1)
            {
                sql += " and F_State=" + state;
            }
            if (type > -1)
            {
                sql += " and F_Type=" + type;
            }
            if (IsReply > -1)
            {
                sql += " and F_IsReply=" + IsReply;
            }
            if (!string .IsNullOrEmpty (pid ))
            {
                sql += " and F_DeptId ='" + pid + "'";
            }
            else
            {
                if (userModel.F_RoleCode == "GLY" || userModel.F_RoleCode == "ZXLD" || userModel.F_RoleCode == "ZXHWY")
                {
                }
                else
                {
                    sql += " and F_Deptid  in  (select  F_DeptId from  T_Sys_Department" +
                         " where F_State=0 and  (F_DeptId='" + userModel.F_DeptId + "'or   F_PartentId='" + userModel.F_DeptId + "'))";
                }
            }
            if (pid != null && pid.Trim() != "")
            {
                sql += " and F_Deptid=" + pid.Trim();
            }
            if (keywords != null && keywords.Trim() != "")
            {
                sql += " and (F_Content like '%" + keywords.Trim() + "%' or F_Title like '%"
                    + keywords.Trim() + "%' or F_ReplyContent like '%" + keywords.Trim() + "%')";
            }
            if (strusercode != null && strusercode.Trim() != "")
            {
                sql += " and F_CreateUser=" + strusercode;
            }
            if (seats != null && seats.Trim() != "")
            {
                sql += " and F_Seats=" + seats;
            }
            sql += " and F_IsDelete=" + 0;
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_KnowledgeReporting",
                "F_ID",
                "*,dbo.GetDeptName(F_Deptid) as DeptName",
                sql,
                "ORDER BY F_CreateTime desc",
                pagesize,
                pageindex,
                true,
                out recordCount);

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

            return Content(obj.ToJson());

        }

        //获取知识库
        public ActionResult GetInfo(int  id)
        {
            Model.T_KnowledgeReporting dModel = infoBLL.GetModel(id);
            BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
       //     Model.T_Sys_UserAccount userModel = new Model.T_Sys_UserAccount();
            if (dModel != null)
            {
                string DeptName = "";
                if (dModel.F_Deptid>0)
                {
                    var dept = departmentBLL.GetModel((int)dModel.F_Deptid);
                    if (dept != null)
                        DeptName = dept.F_DeptName;
                }
                var obj = new
                {
                    F_ID = dModel.F_ID,
                    F_Type = dModel.F_Type,
                    F_Content = dModel.F_Content,
                    F_Deptid = dModel.F_Deptid,
                    DeptName = DeptName,
                    F_CreateTime = dModel.F_CreateTime,
                    F_LimitTime = dModel.F_LimitTime,
                    F_Seats = dModel.F_Seats,
                    F_ReplyContent = dModel.F_ReplyContent,
                    F_CreateUser = dModel.F_CreateUser,
                    F_UpdateTime = dModel.F_UpdateTime,
                    F_UpdateUser = dModel.F_UpdateUser,
                    F_State = dModel.F_State,
                    F_ReportingTime = dModel.F_ReportingTime,
                    F_ReportingUser = dModel.F_ReportingUser,
                    F_ReplyTime = dModel.F_ReplyTime,
                    F_Title = dModel.F_Title,
                    F_IsReply = dModel.F_IsReply,
                    F_AuditTime = dModel .F_AuditTime ,
                    F_AuditOpinion= dModel .F_AuditOpinion 
                };
                return Success("获取知识库信息成功", obj);
            }
            else
            {
                return Error("获取知识库信息失败");
            }

        }
        [Authority]
        //添加知识库上报
        public ActionResult AddInfo(Model.T_KnowledgeReporting info)
        {
            Model.T_KnowledgeReporting dModel = new Model.T_KnowledgeReporting();
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
            if (string.IsNullOrEmpty(info .F_Title ))
                return Error("标题不能为空");
            if (string.IsNullOrEmpty(info.F_Content))
                return Error("内容不能为空");
            if (info.F_Deptid<0)
                return Error("请选择部门");
            if (info.F_Type  < 0)
                return Error("请选择分类");
            if (info .F_Type ==1)
            {
                if (info.F_LimitTime == null)
                    return Error("请选择回复时限");
                dModel.F_LimitTime = info.F_LimitTime;
                dModel.F_Seats = userModel.F_UserCode;
            }
            dModel.F_State = info.F_State;
            if (dModel.F_State == 1)
            {
                if (userModel.F_RoleCode == "ZXLD" || userModel.F_RoleCode == "GLY")
                {
                    dModel.F_AuditTime = DateTime.Now;
                    dModel.F_State = 2;
                }
                dModel.F_ReportingTime = DateTime.Now;
                dModel.F_ReportingUser = userModel.F_UserCode;
            }
            dModel.F_Type = info.F_Type;
            dModel.F_CreateTime = DateTime .Now ;
            dModel.F_CreateUser = userModel.F_UserCode;
            dModel.F_Title = info.F_Title;
            dModel.F_Content = info.F_Content;
            dModel.F_Deptid = info.F_Deptid ;
            dModel.F_IsDelete = 0;
            int   b = infoBLL.Add  (dModel);
            if (b>0 )
            {
                return Success("添加成功");
            }
            else
            {
                return Success("添加失败");
            }

        }
        [Authority]
        //编辑知识库
        public ActionResult EditInfo(Model.T_KnowledgeReporting info)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
            if (info.F_ID <= 0)
                return Error("参数错误");
            
            if (string.IsNullOrEmpty(info.F_Title))
                return Error("标题不能为空");
            if (string.IsNullOrEmpty(info.F_Content))
                return Error("内容不能为空");
            if (info.F_Deptid < 0)
                return Error("请选择部门");
            if (info.F_Type < 0)
                return Error("请选择分类");
            Model.T_KnowledgeReporting dModel = infoBLL.GetModel(info.F_ID);
            if (dModel == null)
                return Error("参数错误");
            if (dModel.F_State == 1)
                return Error("该知识库已经上报");
            if (info.F_Type == 1)
            {
                if (info.F_LimitTime == null)
                    return Error("请选择回复时限");
                dModel.F_LimitTime = info.F_LimitTime;
                dModel.F_Seats = userModel.F_UserCode;
            }
            dModel.F_State = info.F_State;
            if (dModel.F_State == 1)
            {
                if (userModel.F_RoleCode == "ZXLD" || userModel.F_RoleCode == "GLY")
                {
                    dModel.F_AuditTime = DateTime.Now;
                    dModel.F_State = 2;
                }
                dModel.F_ReportingTime = DateTime.Now;
                dModel.F_ReportingUser = userModel.F_UserCode;
            }
            dModel.F_Type = info.F_Type;
            dModel.F_CreateUser  = userModel.F_UserCode;
            dModel.F_UpdateTime = DateTime.Now;
            dModel.F_Title = info.F_Title;
            dModel.F_Content = info.F_Content;
            dModel.F_Deptid = info.F_Deptid;
            dModel.F_IsDelete = 0;
            int b = infoBLL.Add(dModel);
            if (b > 0)
            {
                return Success("添加成功");
            }
            else
            {
                return Success("添加失败");
            }
        }
        [Authority]
        //删除知识库
        public ActionResult DelInfos(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idds = "";
                for (int i = 0; i < ids.Length; i++)
                {
                    idds += ids[i] + ",";
                }
                bool r = infoBLL.DeleteList(idds.Trim(','));

                if (r)
                    return Success("删除成功");
                else
                {
                    return Error("删除失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }
        [Authority]
        //提交知识库
        public ActionResult SubAudit(Model.T_KnowledgeReporting info)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
            if (info.F_ID <= 0)
                return Error("参数错误");
            if (info.F_Deptid < 0)
                return Error("请选择部门");
            Model.T_KnowledgeReporting dModel = infoBLL.GetModel(info.F_ID);
            if (dModel == null)
                return Error("参数错误");
            if (dModel.F_State == 1)
                return Error("该知识库已经上报");
            if (info.F_Type == 1)
            {
                if (info.F_LimitTime == null)
                    return Error("请选择回复时限");
                dModel.F_LimitTime = info.F_LimitTime;
                dModel.F_Seats = userModel.F_UserCode;
            }
            dModel.F_State = info.F_State;
            if (dModel.F_State == 1)
            {
                if (userModel.F_RoleCode == "ZXLD" || userModel.F_RoleCode == "GLY")
                {
                    dModel.F_AuditTime = DateTime.Now;
                    dModel.F_State = 2;
                }
                dModel.F_ReportingTime = DateTime.Now;
                dModel.F_ReportingUser = userModel.F_UserCode;
            }
            bool  b = infoBLL.Update (dModel);
            if (b)
            {
                return Success("提交成功"); 
            }
            else
            {
                return Success("提交失败");
            }

        }

        [Authority]
        //知识库审核
        public ActionResult Audit(int id, string content,int type=0)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
            if (id <= 0)
                return Error("参数错误");
            if (type <=0)
                return Error("请选择是否通过");
            if (string.IsNullOrEmpty(content))
                return Error("请输入审核意见");
            Model.T_KnowledgeReporting dModel = infoBLL.GetModel(id);
            if (dModel == null)
                return Error("参数错误");
            dModel.F_AuditOpinion  = content;
            dModel.F_AuditTime  = DateTime.Now;
            if (type == 1)
                dModel.F_State = 2;
            else
                dModel.F_State = 3;
            dModel.F_Seats = userModel.F_UserCode;
            bool b = infoBLL.Update(dModel);
            if (b)
            {
                return Success("审核成功");
            }
            else
            {
                return Success("审核失败");
            }
        }


        [Authority]
        //回复坐席
        public ActionResult Reply(int id,string content)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount userModel = userBLL.GetModel(userId);
            if (id <= 0)
                return Error("参数错误");
            if (string.IsNullOrEmpty(content))
                return Error("请输入回复内容");
            Model.T_KnowledgeReporting dModel = infoBLL.GetModel(id);
            if (dModel == null)
                return Error("参数错误");
            dModel.F_ReplyContent = content;
            dModel.F_ReplyTime =DateTime .Now ;
            dModel.F_IsReply =1;
            bool  b = infoBLL.Update (dModel);
            if (b)
            {
                return Success("回复成功");
            }
            else
            {
                return Success("回复失败");
            }
        }

    }
}