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
{
    public class MsgController : BaseController
    {
        /// <summary>
        /// 获取消息列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            string sql = " and isdel=0 ";
            DataTable dt = new DataTable();

            sql += " and CreateUser='" + CurrentUser.UserData.F_UserCode + "'";

            int state = RequestString.GetInt("state", -1);
            string strkey = HttpUtility.UrlDecode(RequestString.GetQueryString("key"));
            string strstarttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            if (state != -1)
            {
                sql += " and State='" + state + "'";
            }
            if (strkey.Trim() != "" && strkey != "undefined")
            {
                sql += " and Detail like '%" + strkey.Trim() + "%' ";
            }
            if (strstarttime.Trim() != "" && strstarttime != "undefined")
            {
                sql += " and datediff(day,CreateDate,'" + strstarttime + "')<=0 ";
            }
            if (strendtime.Trim() != "" && strendtime != "undefined")
            {
                sql += " and datediff(day,CreateDate,'" + strendtime + "')>=0   ";
            }

            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Msg_List",
                "ID",
                "*,dbo.GetUserName(CreateUser) as CreateUserName, dbo.GetUserName(ToUser) as ToUserName",
                sql,
                "ORDER BY ID desc",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var obj = new
            {
                state = "success",
                message = "成功",
                rows = dt,
                total = recordCount
            };

            return Content(obj.ToJson());

        }

        /// <summary>
        /// 添加/修改信息
        /// </summary>
        /// <returns></returns>
        public ActionResult AddMsg()
        {
            int id = RequestString.GetInt("id", 0);
            int type = RequestString.GetInt("type", 0);
            int toid = RequestString.GetInt("toid", 0);
            string usercode = RequestString.GetFormString("usercode");
            string cont = RequestString.GetFormString("cont");

            Model.T_Msg_List model = new Model.T_Msg_List();

            if (id == 0)
            {
                model.Detail = cont;
                model.Type = type;
                model.ToUser = usercode;
                model.ToID = toid;
                model.State = 0;
                model.IsDel = 0;
                model.CreateDate = DateTime.Now;
                model.CreateUser = CurrentUser.UserData.F_UserCode;

                if (new BLL.T_Msg_List().Add(model) > 0)
                {
                    return Success("新增成功！");
                }
                else
                {
                    return Error("新增失败！");
                }
            }
            else
            {
                model = new BLL.T_Msg_List().GetModel(id);
                if (model != null)
                {
                    model.Detail = cont;
                    model.Type = type;
                    model.ToUser = usercode;
                    model.ToID = toid;

                    if (new BLL.T_Msg_List().Update(model))
                    {
                        return Success("修改成功！");
                    }
                    else
                    {
                        return Error("修改失败！");
                    }
                }
                else
                {
                    return Error("修改失败！");
                }
            }
        }

        /// <summary>
        /// 删除消息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelMsg(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idd = " ";
                foreach (string str in ids)
                {
                    idd += str + ",";
                }

                string sql = "update T_Msg_List set IsDel=1,DelUser='" + CurrentUser.UserData.F_UserCode + "',DelTime=getdate() where ID in(" + idd.TrimEnd(',') + ")";
                if (!string.IsNullOrEmpty(idd.Trim()))
                {
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        return Success("删除成功");
                    }
                    else
                    {
                        return Error("删除失败");
                    }
                }
                else
                {
                    return Error("请选择记录");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ActionResult UpdateState(string[] ids, int state)
        {

            if (ids != null && ids.Length > 0)
            {
                string idd = " ";
                foreach (string str in ids)
                {
                    idd += str + ",";
                }
                string sj = "null";
                if (state == 1)
                {
                    sj = "getdate()";
                }
                string sql = "update T_Msg_List set State=" + state + ",ReadDate=" + sj + " where ID in(" + idd.TrimEnd(',') + ")";
                if (!string.IsNullOrEmpty(idd.Trim()))
                {
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        return Success("更新成功");
                    }
                    else
                    {
                        return Error("更新失败");
                    }
                }
                else
                {
                    return Error("请选择记录");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        /// <summary>
        /// 阅读消息
        /// </summary>
        /// <returns></returns>
        public ActionResult ReadMsg()
        {

            int id = RequestString.GetInt("id", 0);

            Model.T_Msg_List model = new BLL.T_Msg_List().GetModel(id);
            if (model != null)
            {
                model.State = 0;
                model.ReadDate = DateTime.Now;
                if (new BLL.T_Msg_List().Update(model))
                {
                    return Success("成功！");
                }
                else
                {
                    return Error("失败！");
                }
            }
            else
            {
                return Error("失败");
            }
        }
    }
}