LogService.cs 9.44 KB
using HHECS.Application.Enums;
using HHECS.Application.Error;
using HHECS.BllModel;
using HHECS.Dal.Repository;
using HHECS.Infrastructure.Enums;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace HHECS.Application.Service
{
    /// <summary>
    /// 日志服务类
    /// </summary>
    public class LogService : BaseService
    {
        #region 日志查询

        public BllResult<List<InterfaceLog>> GetInterfaceLogs(Expression<Func<InterfaceLog, bool>> exp, int pageIndex, int pageSize, out long totalCount)
        {
            try
            {
                InterfaceLogRepository interfaceLogRepository = new InterfaceLogRepository();
                var result = interfaceLogRepository.Where(exp).Count(out totalCount).OrderByDescending(a => a.CreateTime).Page(pageIndex, pageSize).ToList();
                return BllResultFactory.Success(result);
            }
            catch (Exception ex)
            {
                totalCount = 0;
                return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        public BllResult<List<InterfaceLog>> GetInterfaceLogs(Expression<Func<InterfaceLog, bool>> exp)
        {
            try
            {
                InterfaceLogRepository interfaceLogRepository = new InterfaceLogRepository();
                var result = interfaceLogRepository.Where(exp).OrderByDescending(a => a.CreateTime).ToList();
                return BllResultFactory.Success(result);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        public BllResult<List<ContentLog>> GetContentLogs(Expression<Func<ContentLog, bool>> exp, int pageIndex, int pageSize, out long totalCount)
        {
            try
            {
                ContentLogRepository contentLogRepository = new ContentLogRepository();
                var result = contentLogRepository.Where(exp).Count(out totalCount).OrderByDescending(a => a.CreateTime).Page(pageIndex, pageSize).ToList();
                return BllResultFactory.Success(result);
            }
            catch (Exception ex)
            {
                totalCount = 0;
                return BllResultFactory.Error<List<ContentLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        public BllResult<List<ContentLog>> GetContentLogs(Expression<Func<ContentLog, bool>> exp)
        {
            try
            {
                ContentLogRepository contentLogRepository = new ContentLogRepository();
                var result = contentLogRepository.Where(exp).OrderByDescending(a => a.CreateTime).ToList();
                return BllResultFactory.Success(result);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<ContentLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        public BllResult<List<OperationLog>> GetOperationLogs(Expression<Func<OperationLog, bool>> exp, int pageIndex, int pageSize, out long totalCount)
        {
            try
            {
                OperationLogRepository operationLogRepository = new OperationLogRepository();
                var result = operationLogRepository.Where(exp).Count(out totalCount).OrderByDescending(a => a.CreateTime).Page(pageIndex, pageSize).ToList();
                return BllResultFactory.Success(result);
            }
            catch (Exception ex)
            {
                totalCount = 0;
                return BllResultFactory.Error<List<OperationLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        public BllResult<List<OperationLog>> GetOperationLogs(Expression<Func<OperationLog, bool>> exp)
        {
            try
            {
                OperationLogRepository operationLogRepository = new OperationLogRepository();
                var result = operationLogRepository.Where(exp).OrderByDescending(a => a.CreateTime).ToList();
                return BllResultFactory.Success(result);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<OperationLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        #endregion

        #region 日志记录
        /// <summary>
        /// 记录接口日志到数据库
        /// </summary>
        /// <param name="interfaceName"></param>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public BllResult LogInterface(string interfaceName, string request, string response, Level flag, string content, string remark)
        {
            InterfaceLog interfaceLog = new InterfaceLog();
            interfaceLog.InterfaceName = interfaceName;
            interfaceLog.Request = request;
            interfaceLog.Response = response;
            interfaceLog.Flag = flag.ToString();
            interfaceLog.Content = content.Length > 300 ? content.Substring(0,300) : content;
            interfaceLog.Remark = remark;
            interfaceLog.CreateBy = Accounts.WCSInterface.ToString();
            interfaceLog.CreateTime = DateTime.Now;
            InterfaceLogRepository interfaceLogRepository = new InterfaceLogRepository();
            interfaceLogRepository.Insert(interfaceLog);
            return  BllResultFactory.Success(); 
        }

        /// <summary>
        /// 记录接口日志到数据库
        /// </summary>
        public BllResult LogInterface(string interfaceName, string url, string request, string response, Level flag, string content, string remark, string createdBy, DateTime? startTime, DateTime? endTime)
        {
            try
            {
                InterfaceLog interfaceLog = new InterfaceLog()
                {
                    InterfaceName = interfaceName,
                    Url = url,
                    Request = request,
                    Response = response,
                    Flag = flag.ToString(),
                    Content = content.Length > 300 ? content.Substring(0, 300) : content,
                    Remark = remark,
                    CreateBy = createdBy,
                    UpdateBy = createdBy,
                    CreateTime = startTime,
                    UpdateTime = endTime
                };
                InterfaceLogRepository interfaceLogRepository = new InterfaceLogRepository();
                interfaceLogRepository.Insert(interfaceLog);
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        /// <summary>
        /// 记录普通日志到数据库
        /// </summary>
        public BllResult LogContent(Title logTitle, string log, string userCode, Level flag)
        {
            try
            {
                ContentLog contentLog = new ContentLog()
                {
                    Title = logTitle.ToString(),
                    Content = log,
                    Flag = flag.ToString(),
                    CreateTime = DateTime.Now,
                    CreateBy = userCode
                };
                ContentLogRepository contentLogRepository = new ContentLogRepository();
                contentLogRepository.Insert(contentLog);
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        /// <summary>
        /// 记录操作日志
        /// </summary>
        public BllResult LogOperation(Title noticeTitle, ModuleConst moduleConst, string content, string result, string userCode)
        {
            try
            {
                OperationLog operationLog = new OperationLog()
                {
                    Title = noticeTitle.ToString(),
                    Module = moduleConst.ToString(),
                    Content = content,
                    Result = result,
                    CreateTime = DateTime.Now,
                    CreateBy = userCode
                };
                OperationLogRepository operationLogRepository = new OperationLogRepository();
                operationLogRepository.Insert(operationLog);
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<InterfaceLog>>($"异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        /// <summary>
        /// 记录业务返回日志
        /// </summary>
        public void LogBllResult(BllResult bllResult, Title title, string userCode)
        {
            LogContent(title, bllResult.Msg, userCode, bllResult.Success ? Level.Success : Level.Failure);
        }

        /// <summary>
        /// 记录业务返回日志
        /// </summary>
        public void LogBllResult<T>(BllResult<T> bllResult, Title title, string userCode)
        {
            LogContent(title, bllResult.Msg, userCode, bllResult.Success ? Level.Success : Level.Failure);
        }

        #endregion
    }
}