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
{
    //
    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 sql = "";
            if (!string.IsNullOrWhiteSpace(filter.Name))
            {
                sql += " and F_LoginName like '%" + filter.Name + "%' ";
            }
            if (filter.LoginDate != null)
            {
                DateTime ctime = Convert.ToDateTime(filter.LoginDate);
                string checkdate = ctime.ToString("yyyy-MM-dd");
                sql += " and CONVERT(varchar , F_LoginDate, 120)>=CONVERT(varchar , '" + checkdate + " 00:00:01', 120) and CONVERT(varchar , F_LoginDate, 120)<=CONVERT(varchar , '" + checkdate + " 23:59:59', 120) ";
            }
            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
            };
            return Content(obj.ToJson());

        }

        /// <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("删除失败");

        }
    }
}
