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.information
{
    //[Authority]
    public class NoticeController : BaseController
    {
        BLL.T_Msg_NoticeInfo dBLL = new BLL.T_Msg_NoticeInfo();
        private BLL.T_Sys_RoleInfo rolebll = new BLL.T_Sys_RoleInfo();
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="code"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult GetRoleList()
        {
            DataTable dt = new DataTable();
            dt = rolebll.GetAllList().Tables[0];
            return Success("获取成功", dt);
        }
        /// <summary>
        /// 获取坐席列表
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult GetUserListByRoleId(int roleid)
        {
            DataTable dt = new DataTable();
            var sql = " F_DeleteFlag=0 ";
            if (roleid != 0)
            {
                sql += " and F_RoleId = " + roleid + "";
            }
            string sqldt = "SELECT F_UserId,F_UserCode,F_RoleId,F_UserName FROM T_Sys_UserAccount WHERE " + sql + " ";
            dt = DbHelperSQL.Query(sqldt).Tables[0];
            return Success("获取成功", dt);
        }

        // 获取公告列表
        public ActionResult GetList(string datetime)
        {
            DataTable dt = new DataTable();
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            string sql = " ";
            if (datetime != null && datetime.Trim() != "")
            {
                sql += " and Datediff(day,F_Startdate,'" + datetime.Trim() + "')>=0";
                sql += " and Datediff(day,F_Enddate,'" + datetime.Trim() + "')<=0";
            }
            //else
            //{
            //    sql += " and Datediff(day,F_Startdate,getdate())>=0";
            //    sql += " and Datediff(day,F_Enddate,getdate())<=0";
            //}

            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }
            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Msg_NoticeInfo",
                "F_NoticeId",
                "*",
                sql,
                "ORDER BY F_NoticeId desc",
                pagesize,
                pageindex,
                true,
                out recordCount);
            List<Model.T_Msg_NoticeInfo> msgList = new BLL.T_Msg_NoticeInfo().DataTableToList(dt);
            List<Model.T_Sys_UserAccount> userList = new BLL.T_Sys_UserAccount().GetModelList("");
            var obj = new
            {
                rows = msgList.Select(m =>
                {
                    Model.T_Sys_UserAccount usermodel = userList.Where(u => u.F_UserId == m.F_CreateBy.Value).FirstOrDefault();
                    var uname = ""; var sdate = ""; var edate = "";
                    if (usermodel != null)
                        uname = usermodel.F_UserName;
                    if (m.F_StartDate != null)
                        sdate = m.F_StartDate.Value.ToString("yyyy-MM-dd");
                    if (m.F_EndDate != null)
                        edate = m.F_EndDate.Value.ToString("yyyy-MM-dd");
                    return new
                    {
                        F_NoticeId = m.F_NoticeId,
                        F_Title = m.F_Title,
                        F_Content = m.F_Content,
                        F_StartDate = sdate,
                        F_EndDate = edate,
                        F_CreateOn = m.F_CreateOn,
                        F_CreateBy = m.F_CreateBy,
                        F_CreateByName = uname
                    };
                }),
                total = recordCount
            };

            return Content(obj.ToJson());

        }

        //首页获取公告
        public ActionResult GetListTop(int top)
        {
            int userID = 0, roleId = 0;
            int.TryParse(User.UserData["F_UserID"], out userID);
            int.TryParse(User.UserData["F_RoleID"], out roleId);
            DataTable dt = new DataTable();
            string sql = " 1 = 1 and (ISNULL(F_RoleId,0) = 0 or ISNULL(F_RoleId,0) = " + roleId + ") and (ISNULL(F_UserId,0) = 0 or ISNULL(F_UserId,0) = " + userID + ") ";

            sql += " and Datediff(day,F_Startdate,getdate())>=0";
            sql += " and Datediff(day,F_Enddate,getdate())<=0";
            dt = new BLL.T_Msg_NoticeInfo().GetList(top, sql, " F_NoticeId desc ").Tables[0];
            int recordCount = dt.Rows.Count;
            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }        

        //获取公告
        public ActionResult GetNotice(string noticeid)
        {
            Model.T_Sys_UserAccount userModel = new Model.T_Sys_UserAccount();
            Model.T_Msg_NoticeInfo dModel = dBLL.GetModel(int.Parse(noticeid.Trim()));
            if (dModel != null)
            {
                if (dModel.F_CreateBy != null)
                    userModel = new BLL.T_Sys_UserAccount().GetModel(dModel.F_CreateBy.Value);
                var uname = ""; var sdate = ""; var edate = "";
                if (userModel != null)
                    uname = userModel.F_UserName;
                if (dModel.F_StartDate != null)
                    sdate = dModel.F_StartDate.Value.ToString("yyyy-MM-dd");
                if (dModel.F_EndDate != null)
                    edate = dModel.F_EndDate.Value.ToString("yyyy-MM-dd");
                var obj = new
                {
                    F_NoticeId = dModel.F_NoticeId,
                    F_Title = dModel.F_Title,
                    F_Content = dModel.F_Content,
                    F_StartDate = sdate,
                    F_EndDate = edate,
                    F_CreateOn = dModel.F_CreateOn,
                    F_CreateBy = dModel.F_CreateBy,
                    F_CreateByName = uname,
                    F_UserId = dModel.F_UserId == null ? 0 : dModel.F_UserId,
                    F_RoleId = dModel.F_RoleId == null ? 0 : dModel.F_RoleId,
                };
                return Success("获取公告信息成功", obj);
            }
            else
            {
                return Error("获取公告信息失败");
            }

        }
        [Authority]
        //添加公告
        public ActionResult AddNotice(string title, string content, string stime, string etime, string userid, string roleid)
        {
            Model.T_Msg_NoticeInfo dModel = new Model.T_Msg_NoticeInfo();
            int userId = 0, roleId = 0;
            int.TryParse(userid, out userId);
            int.TryParse(roleid, out roleId);

            if (title != null)
                dModel.F_Title = title.Trim();
            if (content != null)
                dModel.F_Content = content.Trim();
            if (stime != null)
                dModel.F_StartDate = DateTime.Parse(stime);
            if (etime != null)
                dModel.F_EndDate = DateTime.Parse(etime);
            //添加权限
            dModel.F_RoleId = roleId;
            dModel.F_UserId = userId;
            dModel.F_CreateOn = DateTime.Now;
            dModel.F_CreateBy = int.Parse(User.UserData["F_UserID"]);

            int b = dBLL.Add(dModel);
            if (b > 0)
            {
                return Success("添加成功");
            }
            else
            {
                return Success("添加失败");
            }
        }

        //编辑公告
        public ActionResult EditNotice(string nid, string title, string content, string stime, string etime, string userid, string roleid)
        {
            int userId = 0, roleId = 0;
            int.TryParse(userid, out userId);
            int.TryParse(roleid, out roleId);

            if (nid != null && nid.Trim() != "")
            {
                Model.T_Msg_NoticeInfo dModel = dBLL.GetModel(int.Parse(nid.Trim()));
                if (dModel != null)
                {
                    if (title != null)
                        dModel.F_Title = title.Trim();
                    if (content != null)
                        dModel.F_Content = content.Trim();
                    if (stime != null)
                        dModel.F_StartDate = DateTime.Parse(stime);
                    if (etime != null)
                        dModel.F_EndDate = DateTime.Parse(etime);

                    //添加权限
                    dModel.F_RoleId = roleId;
                    dModel.F_UserId = userId;
                    dModel.F_ModifyDate = DateTime.Now;
                    dModel.F_ModifyBy = int.Parse(User.UserData["F_UserID"]);

                    bool b = dBLL.Update(dModel);
                    if (b)
                    {
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Success("编辑失败");
                    }
                }
                else
                    return Error("获取信息失败");
            }
            else
            {
                return Error("请选择要编辑的知识库分类");
            }

        }
        [Authority]
        //删除公告
        public ActionResult DelNotice(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idds = "";
                for (int i = 0; i < ids.Length; i++)
                {
                    idds += ids[i] + ",";
                }
                bool r = dBLL.DeleteList(idds.Trim(','));

                if (r)
                    return Success("删除成功");
                else
                {
                    return Error("删除失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        #region 官网接口
        //首页获取公告
        public ActionResult GetWebListTop(int top)
        {
            DataTable dt = new DataTable();
            string sql = " 1=1 ";
            sql += " and Datediff(day,F_Startdate,getdate())>=0";
            sql += " and Datediff(day,F_Enddate,getdate())<=0";
            dt = new BLL.T_Msg_NoticeInfo().GetList(top, sql, " F_NoticeId desc ").Tables[0];
            int recordCount = dt.Rows.Count;
            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());
        }
        #endregion
    }
}