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;
using CallCenterApi.DB;

namespace CallCenterApi.Interface.Controllers.Information
{
    public class NoticeController : BaseController
    {
        BLL.T_Msg_NoticeInfo dBLL = new BLL.T_Msg_NoticeInfo();

        BLL.T_Sys_AutoCallTime calltimebll = new BLL.T_Sys_AutoCallTime();


        // 获取公告列表
        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";
            }

            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)
        {
                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());

        }

        //获取公告
        public ActionResult GetNotice(string noticeid)
        {
            if (noticeid != null && noticeid.Trim() != "")
            {
                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
                    };
                    return Success("获取公告信息成功", obj);
                }
                else
                {
                    return Error("获取公告信息失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }
        }

        //添加公告
        public ActionResult AddNotice(string title, string content, string stime, string etime)
        {
            if (string.IsNullOrEmpty(title))
                return Error("标题不能为空");
            if (string.IsNullOrEmpty(content))
                return Error("内容不能为空");
            Model.T_Msg_NoticeInfo dModel = new Model.T_Msg_NoticeInfo();

            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_CreateOn = DateTime.Now;
            dModel.F_CreateBy = CurrentUser.UserData.F_UserId;

            int b = dBLL.Add(dModel);
            if (b > 0)
            {
                BLL.T_Sys_UserAccount userbll = new BLL.T_Sys_UserAccount();
                BLL.T_Msg_NoticeUsers nubll = new BLL.T_Msg_NoticeUsers();
                List<Model.T_Sys_UserAccount> userlist = userbll.GetModelList("");
                foreach (var item in userlist)
                {
                    Model.T_Msg_NoticeUsers numodel = new Model.T_Msg_NoticeUsers();

                    numodel.F_NoticeId = b;
                    numodel.F_UserId = item.F_UserId;
                    numodel.F_UserName = item.F_UserName;
                    numodel.F_State = 0;
                    nubll.Add(numodel);

                }

                return Success("添加成功");
            }
            else
            {
                return Success("添加失败");
            }

        }

        //编辑公告
        public ActionResult EditNotice(string nid, string title, string content, string stime, string etime)
        {
                if (string.IsNullOrEmpty(nid))
                    return Error("id不能为空");
                if (string.IsNullOrEmpty(title))
                    return Error("标题不能为空");
                if (string.IsNullOrEmpty(content))
                    return Error("内容不能为空");
                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_ModifyDate = DateTime.Now;
                        dModel.F_ModifyBy = CurrentUser.UserData.F_UserId;

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

        }
        
        //删除公告
        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("获取参数失败");
            }
        }



        //获取登录人未读的公告数量
        public int getUnReadNoticeNum(int? userid)
        {
            int s = 0;
            if (userid != null)
            {

                string sql = "select COUNT(1) num from T_Msg_NoticeUsers where F_state = 0 and F_userid = '" + userid.Value + "'";
                s = Convert.ToInt16(DbHelperSQL.GetSingle(sql));
            }
            return s;
        }

        //获取登录人未读的公告xinxi
        public ActionResult getUnReadNotice(int userid)
        {
            string sql = "  select a.F_noticeid,b.* from T_Msg_NoticeUsers a left   join T_Msg_NoticeInfo b   on a.F_noticeid = b.F_NoticeId  where a.F_state = 0 and a.F_userid = '" + userid + "'";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            return Success("获取未读的公告成功",dt);
        }

        //修改公告为已读

        public ActionResult readNotice(int noticeid,int userid)
        {
            BLL.T_Msg_NoticeUsers bll = new BLL.T_Msg_NoticeUsers();
            Model.T_Msg_NoticeUsers model = bll.GetModel(noticeid, userid);
            model.F_State = 1;
            model.F_ReadDate = DateTime.Now;            
            bool b = bll.Update(model);
            if (b)
            {
                return Success("已读成功");
            }
            else
            {
                return Success("已读失败");
            }

        }



        // 获取自动外呼时间的列表
        public ActionResult getAutoCallTimeList()
        {        
            string sql = "select * from   dbo.T_Sys_AutoCallTime";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            return Success("获取成功",dt);
        }

        public ActionResult GetModel()
        {
            Model.T_Sys_AutoCallTime dModel = calltimebll.GetModel();
           
            return Success("获取成功", dModel);

        }
        //修改autocalltime
        public ActionResult  editAutoCallTime(string F_amStartTime, string F_amEndTme, string F_pmStatTime, string F_pmEndTme)
        {
           
             
                Model.T_Sys_AutoCallTime dModel = calltimebll.GetModel();
                if (dModel != null)
                {
                    if (F_amStartTime != null)
                        dModel.F_amStartTime = F_amStartTime;
                    if (F_amEndTme != null)
                        dModel.F_amEndTme = F_amEndTme.Trim();
                    if (F_pmStatTime != null)
                        dModel.F_pmStatTime = F_pmStatTime;
                    if (F_pmEndTme != null)
                        dModel.F_pmEndTme = F_pmEndTme;

                    dModel.F_ModifyDate = DateTime.Now;
                    dModel.F_ModifyBy = CurrentUser.UserData.F_UserCode;

                    bool b = calltimebll.Update(dModel);
                    if (b)
                    {
                        return Success("编辑成功");
                    }
                    else
                    {
                        return Success("编辑失败");
                    }
                }
                else
                    return Error("获取信息失败");
           

        }

    }
}