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

namespace CallCenterApi.Interface.Controllers
{
    //[Authority]
    public class LogController : BaseController
    {
        private BLL.T_Sys_LoginLogs loginLogsBLL = new BLL.T_Sys_LoginLogs();

        [Authority]
        /// <summary>
        /// 获取登录记录列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList()
        {

            string deptid = RequestString.GetQueryString("deptid");

            string name = RequestString.GetQueryString("Name");
            string logindate = RequestString.GetQueryString("LoginDate");
            string endlogindate = RequestString.GetQueryString("endLoginDate");
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            var sql = "";

            if (!string.IsNullOrWhiteSpace(deptid))
            {
                sql += " and b.F_DeptId ='"+ deptid + "' ";
            }

            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += " and a.F_LoginName like '%" + name + "%' ";
            }
            if (!string.IsNullOrWhiteSpace(logindate))
            {
                sql += " and CONVERT(varchar , a.F_LoginDate, 120)>=CONVERT(varchar , '" + logindate + " 00:00:01', 120)   ";
            }
            else
            {
                string now= DateTime.Now.ToString("yyyy-MM")+"-01";

                sql += " and CONVERT(varchar , a.F_LoginDate, 120)>=CONVERT(varchar , '" + now + " 00:00:01', 120)   ";
            }
            if (!string.IsNullOrWhiteSpace(endlogindate))
            {
                
                sql += "   and CONVERT(varchar , a.F_LoginDate, 120)<=CONVERT(varchar , '" +endlogindate + " 23:59:59', 120) ";
            }
            else
            {
                string now = DateTime.Now.ToString("yyyy-MM-dd");
                sql += "   and CONVERT(varchar , a.F_LoginDate, 120)<=CONVERT(varchar , '" + now + " 23:59:59', 120) ";
            }

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

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }

            int recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                 "(select dbo.GetDeptName( b.F_DeptId) deptname,COUNT(1) num,MAX(F_LoginDate) logindate from  T_Sys_LoginLogs a left join T_Sys_UserAccount b on a.F_LoginName = b.F_UserName where dbo.GetDeptName( b.F_DeptId) is not null " + sql+ " group by b.F_DeptId ) c",
                 "deptname",
                 "*",
                 "",
                 "",
                 pagesize,
                 pageindex,
                 true,
                 out recordCount);

            var obj = new
            {
                rows = dt,
                total = recordCount
            };
            return Content(obj.ToJson());

        }

        [Authority]
        /// <summary>
        /// 记录前端错误日志
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult ErrorLog(string log)
        {
            return Error(log);
        }

        [Authority]
        /// <summary>
        /// 删除登录记录
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Delete(string[] ids)
        {
            if (ids != null)
            {
                var str = string.Join(",", ids);
                if (loginLogsBLL.DeleteList(str))
                    return Success("删除成功");
                else
                    return Error("删除失败");
            }
            else
                return Error("删除失败");

        }

        /// <summary>
        /// 话务操作日志，写入txt
        /// </summary>
        /// <param name="direct">方向：send,recive</param>
        /// <param name="type"></param>
        /// <param name="cont"></param>
        /// <returns></returns>
        public ActionResult CallOpt(string direct, string type, string cont)
        {
            string usercode = User.UserData["F_UserCode"];
            string strtype = type;
            switch (type.ToLower())
            {
                case "heart": strtype = "心跳"; break;//心跳
                case "login":
                    strtype = "签入";
                    break; //签入
                case "logout":
                    strtype = "签出";
                    break; //签出
                case "dropcall":
                    strtype = "挂断";
                    break; //挂断
                case "makecall":
                    strtype = "外呼";
                    break; //外呼
                case "setstate":
                    strtype = "置忙置闲";
                    break; //置忙置闲
                case "meeting":
                    strtype = "多方通话";
                    break; //多方通话
                case "transfer":
                    strtype = "转移";
                    break; //转移
                case "hold":
                    strtype = "保持";
                    break; //保持
                case "retrieve":
                    strtype = "接回";
                    break; //接回
                case "incoming":
                    strtype = "来电";
                    break; //来电
                case "subscribe":
                    strtype = "监测";
                    break; //监测
                case "subscribecancel":
                    strtype = "停止监测";
                    break; //停止监测
                case "agentstate":
                    strtype = "坐席状态";
                    break; //坐席状态
                case "linestate":
                    strtype = "线路状态";
                    break; //线路状态
                case "motorsetstate":
                    strtype = "班长置闲";
                    break; //班长置闲
                case "linestateagent":
                    strtype = "线路状态通知";
                    break; //线路状态通知
                case "callid":
                    strtype = "获取callid";
                    break; //获取callid
                case "recordpath":
                    strtype = "录音返回";
                    break; //录音返回
                case "close":
                    strtype = "关闭连接";
                    break;
                case "agentstateagent":
                    strtype = "坐席状态变化";
                    break;
                case "logincheck":
                    strtype = "签入检测";
                    break;
                case "sayfree":
                    strtype = "置闲";
                    break;
                case "saybusy":
                    strtype = "置忙";
                    break;
                case "listen":
                    strtype = "监听";
                    break;
                case "insert":
                    strtype = "强插";
                    break;
                case "intercept":
                    strtype = "强截";
                    break;
                case "break":
                    strtype = "强拆";
                    break;
                case "forceagentstate":
                    strtype = "强制控制坐席状态";
                    break;
                case "instead":
                    strtype = "代接";
                    break;
                case "turnivr":
                    strtype = "转IVR";
                    break;
                case "consult":
                    strtype = "协商呼叫";
                    break;
                case "confirmtransfer":
                    strtype = "协商结果";
                    break;
                case "acdconnectnotify":
                    strtype = "ACD连接通知消息";
                    break;
                case "monitor":
                    strtype = "实时数据监控";
                    break;
            }
            var obj = new
            {
                direct = direct,
                optuser = usercode,
                opttype = type,
                content = cont
            };
            return Warn(strtype, obj);
        }
    }
}
