using DeviceRepair.Models;
using DeviceRepair.Models.History;
using DeviceRepair.Models.Logs;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DeviceRepair.DataAccess
{
    public class OperationAccess : DbContext<UserInfoModel>
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        private static OperationAccess manager;
        public static OperationAccess Instance
        {
            get
            {
                if (manager == null)
                    manager = new OperationAccess();
                return manager;
            }
        }

        /// <summary>
        /// 用户登录登出日志
        /// </summary>
        /// <param name="FilterInfo"></param>
        /// <returns></returns>
        public APIResponseData UserLoginContent(OperationFilterModel FilterInfo)
        {
            APIResponseData apiResponseData = new APIResponseData();
            try
            {
                db.ChangeDatabase("main");
                Dictionary<string, UserInfoModel> userInfos = base.GetList().ToDictionary(x => x.LoginCode, x => x);
                if (userInfos != null && userInfos.Count > 0)
                {
                    db.ChangeDatabase("log");

                    var exp = Expressionable.Create<UserLogin>();
                    if (FilterInfo != null)
                    {
                        exp.AndIF(FilterInfo.sDate != DateTime.MinValue, x => x.OperationDate >= FilterInfo.sDate);
                        exp.AndIF(FilterInfo.eDate != DateTime.MinValue, x => x.OperationDate <= FilterInfo.eDate);
                        exp.AndIF(!string.IsNullOrWhiteSpace(FilterInfo.LoginCode), x => x.LoginCode.Contains(FilterInfo.LoginCode));
                        exp.AndIF(!string.IsNullOrWhiteSpace(FilterInfo.OperationType), x => x.OperationType.Contains(FilterInfo.OperationType));
                        exp.AndIF(!string.IsNullOrWhiteSpace(FilterInfo.ClientName), x => x.OperationComputer.Contains(FilterInfo.ClientName));
                    }

                    List<UserLogin> userLogins = db.Queryable<UserLogin>().With(SqlWith.NoLock).Where(exp.ToExpression()).ToList();
                    foreach (UserLogin item in userLogins)
                    {
                        if (userInfos.ContainsKey(item.LoginCode))
                            item.RealName = userInfos[item.LoginCode].RealName;
                    }
                    apiResponseData.Code = 1;
                    apiResponseData.Data = userLogins;
                }
            }
            catch (SqlSugarException ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
