using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Filter;
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();

        /// <summary>
        /// 获取登录记录列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList(FilterLog filter)
        {
            var res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                var sql = "";
                if (!string.IsNullOrWhiteSpace(filter.Name))
                {
                    sql += " and F_LoginName like '%" + filter.Name + "%' ";
                }
                if (filter.LoginDate != null)
                {
                    sql += $" and DATEDIFF(s,'{Convert.ToDateTime(filter.LoginDate).ToShortDateString()} 00:00:01',F_LoginDate)>=0 and DATEDIFF(s,F_LoginDate,'{Convert.ToDateTime(filter.LoginDate).ToShortDateString()} 23:59:59')>0";
                }
                int recordCount = 0;
                var dt = BLL.PagerBLL.GetListPager(
                     "T_Sys_LoginLogs",
                     "F_Id",
                     "*",
                     sql,
                     "ORDER BY F_Id desc",
                     filter.PageSize,
                     filter.PageIndex,
                     true,
                     out recordCount);

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

        /// <summary>
        /// 删除登录记录
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Delete(string[] ids)
        {
            var res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                if (ids != null)
                {
                    var str = string.Join(",", ids);
                    if (loginLogsBLL.DeleteList(str))
                        res = Success("删除成功");
                    else
                        res = Error("删除失败");
                }
                else
                    res = Error("删除失败");
            }
            return res;
        }
    }
}
