LogInfoVM.cs 15.1 KB
using AutoMapper;
using HHECS.Application.Enums;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Infrastructure.Enums;
using HHECS.Infrastructure.Excel;
using HHECS.Model.Entities;
using HHECS.Model.Excel.Models;
using HHECS.Model.ExcelModels;
using HHECS.WinCommon.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.WinClient.View.SysLogInfo
{
    public class LogInfoVM : VMBase
    {
        #region 属性

        #region InterfaceLog

        public string InterfaceName { get; set; }

        public string InterfaceLogUrl { get; set; }

        public string InterfaceLogRequest { get; set; }

        public string InterfaceLogResponse { get; set; }

        public string InterfaceLogFlag { get; set; }

        public DateTime? InterfaceLogStartTime { get; set; } = DateTime.Now.Date.AddDays(-1);

        public DateTime? InterfaceLogEndTime { get; set; } = DateTime.Now.Date.AddDays(2).AddSeconds(-1);

        public List<InterfaceLog> InterfaceLogs { get; set; }

        public PageInfo PageInterfaceLogInfo { get; set; } = new PageInfo();

        public Dictionary<string, object> InterfaceNames { get; set; }

        public Dictionary<string, object> InterfaceLogFlags { get; set; }

        #endregion

        #region ContentLog
        public string ContentLogTitle { get; set; }

        public string ContentLogContent { get; set; }

        public string ContentLogFlag { get; set; }

        public DateTime? ContentLogStartTime { get; set; } = DateTime.Now.Date.AddDays(-1);

        public DateTime? ContentLogEndTime { get; set; } = DateTime.Now.Date.AddDays(2).AddSeconds(-1);

        public List<ContentLog> ContentLogs { get; set; }

        public PageInfo PageContentLogInfo { get; set; } = new PageInfo();

        public Dictionary<string, object> ContentLogTitles { get; set; }

        public Dictionary<string, object> ContentLogFlags { get; set; }

        #endregion

        #region OperationLog

        public string OperationLogTitle { get; set; }

        public string OperationLogModule { get; set; }

        public string OperationLogContent { get; set; }

        public string OperationLogResult { get; set; }

        public DateTime? OperationLogStartTime { get; set; } = DateTime.Now.Date.AddDays(-1);

        public DateTime? OperationLogEndTime { get; set; } = DateTime.Now.Date.AddDays(2).AddSeconds(-1);

        public List<OperationLog> OperationLogs { get; set; }

        public PageInfo PageOperationLogInfo { get; set; } = new PageInfo();

        public Dictionary<string, object> OperationLogTitles { get; set; }

        public Dictionary<string, object> OperationLogModules { get; set; }

        public Dictionary<string, object> OperationLogResults { get; set; }

        #endregion

        #endregion

        public LogInfoVM()
        {
            var levels = EnumHelper.EnumListDicString<Level>("全部", "");
            //接口日志标志
            InterfaceLogFlags = levels;
            //内容日志标志
            ContentLogFlags = levels;
            //操作日志结果
            OperationLogResults = levels;

            //接口名改为从数据库获取
            string dictStr1 = DictConst.RemoteUrls.ToString();
            string dictStr2 = DictConst.SystemAPIs.ToString();
            var remoteUrls = App.SystemService.GetDictWithDetails(t => t.Code == dictStr1)?.Data?.DictDetails ?? new List<DictDetail>();
            var systemAPIs = App.SystemService.GetDictWithDetails(t => t.Code == dictStr2)?.Data?.DictDetails ?? new List<DictDetail>();
            List<DictDetail> interfaceNameDictDetails = new List<DictDetail>()
            {
                new DictDetail() { Id = 0,Code = "",Name = "全部" }
            };
            interfaceNameDictDetails.AddRange(remoteUrls);
            interfaceNameDictDetails.AddRange(systemAPIs);
            InterfaceNames = interfaceNameDictDetails.ToDictionary(t => t.Name, x => (object)x.Code);

            //内容日志标题
            string dictStr3 = DictConst.ContentLogTitle.ToString();
            var contentLogTitleDictDetails = App.SystemService.GetDictWithDetails(t => t.Code == dictStr3)?.Data?.DictDetails ?? new List<DictDetail>();
            contentLogTitleDictDetails.Insert(0, new DictDetail() { Id = 0, Code = "", Name = "全部" });
            ContentLogTitles = contentLogTitleDictDetails.ToDictionary(t=>t.Name, x => (object)x.Code);

            //操作日志标题
            string dictStr4 = DictConst.OperationLogTitle.ToString();
            var operationLogTitleDictDetails = App.SystemService.GetDictWithDetails(t => t.Code == dictStr4)?.Data?.DictDetails ?? new List<DictDetail>();
            operationLogTitleDictDetails.Insert(0, new DictDetail() { Id = 0, Code = "", Name = "全部" });
            OperationLogTitles = operationLogTitleDictDetails.ToDictionary(t => t.Name, x => (object)x.Code);

            //操作日志模块
            var modules = EnumHelper.EnumListDicString<ModuleConst>("全部", "");
            OperationLogModules = modules;
        }

        #region 方法

        #region InterfaceLog

        private Expression<Func<InterfaceLog, bool>> GetInterfaceLogExpression()
        {
            Expression<Func<InterfaceLog, bool>> filter = t => true;
            if (!string.IsNullOrWhiteSpace(InterfaceName))
            {
                filter = filter.And(t => t.InterfaceName == InterfaceName);
            }
            if (!string.IsNullOrWhiteSpace(InterfaceLogUrl))
            {
                filter = filter.And(t => t.Url.Contains(InterfaceLogUrl));
            }
            if (!string.IsNullOrWhiteSpace(InterfaceLogRequest))
            {
                filter = filter.And(t => t.Request.Contains(InterfaceLogRequest));
            }
            if (!string.IsNullOrWhiteSpace(InterfaceLogResponse))
            {
                filter = filter.And(t => t.Response.Contains(InterfaceLogResponse));
            }
            if (!string.IsNullOrWhiteSpace(InterfaceLogFlag))
            {
                filter = filter.And(t => t.Flag == InterfaceLogFlag);
            }
            if (InterfaceLogStartTime != null)
            {
                filter = filter.And(t => t.Created >= InterfaceLogStartTime);
            }
            if (InterfaceLogEndTime != null)
            {
                filter = filter.And(t => t.Created <= InterfaceLogEndTime);
            }
            return filter;
        }

        public void QueryInterfaceLog()
        {
            Expression<Func<InterfaceLog, bool>> filter = GetInterfaceLogExpression();
            var result = App.LogService.GetInterfaceLogs(filter, PageInterfaceLogInfo.PageIndex, PageInterfaceLogInfo.PageSize, out long totalCount);
            if (result.Success)
            {
                PageInterfaceLogInfo.TotalCount = totalCount;
                InterfaceLogs = result.Data;
            }
            else
            {
                MessageBox.Show($"查询失败:{result.Msg}");
            }
        }

        public async void ExportInterfaceLog()
        {
            try
            {
                if (MessageBox.Show("是否确认导出?", "注意", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }
                Expression<Func<InterfaceLog, bool>> filter = GetInterfaceLogExpression();
                var interfaceLogResult = App.LogService.GetInterfaceLogs(filter);
                if (!interfaceLogResult.Success)
                {
                    MessageBox.Show($"导出失败:{interfaceLogResult.Msg}");
                    return;
                }
                var interfaceLogs = interfaceLogResult.Data;
                var config = new MapperConfiguration(cfg => cfg.CreateMap<InterfaceLog, InterfaceLogExcelModel>());
                var mapper = config.CreateMapper();
                List<InterfaceLogExcelModel> models = new List<InterfaceLogExcelModel>();
                interfaceLogs.ForEach(t =>
                {
                    var temp = mapper.Map<InterfaceLogExcelModel>(t);
                    models.Add(temp);
                });
                string TimeTxt = DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒");
                await NPIOHelper.ExportDTtoExcelAsync(models, "接口日志", string.Format($"{App.ExportPath}接口日志{TimeTxt}.xlsx"), App.ExportPath);
                MessageBox.Show($"接口日志导出成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导出异常:{ex.Message}");
            }
        }

        #endregion

        #region ContentLog

        private Expression<Func<ContentLog, bool>> GetContentLogExpression()
        {
            Expression<Func<ContentLog, bool>> filter = t => true;
            if (!string.IsNullOrWhiteSpace(ContentLogTitle))
            {
                filter = filter.And(t => t.Title == ContentLogTitle);
            }
            if (!string.IsNullOrWhiteSpace(ContentLogContent))
            {
                filter = filter.And(t => t.Content.Contains(ContentLogContent));
            }
            if (!string.IsNullOrWhiteSpace(ContentLogFlag))
            {
                filter = filter.And(t => t.Flag == ContentLogFlag);
            }
            if (ContentLogStartTime != null)
            {
                filter = filter.And(t => t.Created >= ContentLogStartTime);
            }
            if (ContentLogEndTime != null)
            {
                filter = filter.And(t => t.Created <= ContentLogEndTime);
            }
            return filter;
        }

        public void QueryContentLog()
        {
            Expression<Func<ContentLog, bool>> filter = GetContentLogExpression();
            var result = App.LogService.GetContentLogs(filter, PageContentLogInfo.PageIndex, PageContentLogInfo.PageSize, out long totalCount);
            if (result.Success)
            {
                PageContentLogInfo.TotalCount = totalCount;
                ContentLogs = result.Data;
            }
            else
            {
                MessageBox.Show($"查询失败:{result.Msg}");
            }
        }

        public async void ExportContentLog()
        {
            try
            {
                if (MessageBox.Show("是否确认导出?", "注意", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }
                Expression<Func<ContentLog, bool>> filter = GetContentLogExpression();
                var contentLogResult = App.LogService.GetContentLogs(filter);
                if (!contentLogResult.Success)
                {
                    MessageBox.Show($"导出失败:{contentLogResult.Msg}");
                    return;
                }
                var contentLogs = contentLogResult.Data;
                var config = new MapperConfiguration(cfg => cfg.CreateMap<ContentLog, ContentLogExcelModel>());
                var mapper = config.CreateMapper();
                List<ContentLogExcelModel> models = new List<ContentLogExcelModel>();
                contentLogs.ForEach(t =>
                {
                    var temp = mapper.Map<ContentLogExcelModel>(t);
                    models.Add(temp);
                });
                string TimeTxt = DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒");
                await NPIOHelper.ExportDTtoExcelAsync(models, "接口日志", string.Format($"{App.ExportPath}接口日志{TimeTxt}.xlsx"), App.ExportPath);
                MessageBox.Show($"接口日志导出成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导出异常:{ex.Message}");
            }
        }

        #endregion

        #region OperationLog

        private Expression<Func<OperationLog, bool>> GetOperationLogExpression()
        {
            Expression<Func<OperationLog, bool>> filter = t => true;
            if (!string.IsNullOrWhiteSpace(OperationLogTitle))
            {
                filter = filter.And(t => t.Title == OperationLogTitle);
            }
            if (!string.IsNullOrWhiteSpace(OperationLogModule))
            {
                filter = filter.And(t => t.Module == OperationLogModule);
            }
            if (!string.IsNullOrWhiteSpace(OperationLogContent))
            {
                filter = filter.And(t => t.Content.Contains(OperationLogContent));
            }
            if (!string.IsNullOrWhiteSpace(OperationLogResult))
            {
                filter = filter.And(t => t.Result == OperationLogResult);
            }
            if (OperationLogStartTime != null)
            {
                filter = filter.And(t => t.Created >= OperationLogStartTime);
            }
            if (OperationLogEndTime != null)
            {
                filter = filter.And(t => t.Created <= OperationLogEndTime);
            }
            return filter;
        }

        public void QueryOperationLog()
        {
            Expression<Func<OperationLog, bool>> filter = GetOperationLogExpression();
            var result = App.LogService.GetOperationLogs(filter, PageOperationLogInfo.PageIndex, PageOperationLogInfo.PageSize, out long totalCount);
            if (result.Success)
            {
                PageOperationLogInfo.TotalCount = totalCount;
                OperationLogs = result.Data;
            }
            else
            {
                MessageBox.Show($"查询失败:{result.Msg}");
            }
        }

        public async void ExportOperationLog()
        {
            try
            {
                if (MessageBox.Show("是否确认导出?", "注意", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }
                Expression<Func<OperationLog, bool>> filter = GetOperationLogExpression();
                var operationLogResult = App.LogService.GetOperationLogs(filter);
                if (!operationLogResult.Success)
                {
                    MessageBox.Show($"导出失败:{operationLogResult.Msg}");
                    return;
                }
                var operationLogs = operationLogResult.Data;
                var config = new MapperConfiguration(cfg => cfg.CreateMap<OperationLog, OperationLogExcelModel>());
                var mapper = config.CreateMapper();
                List<OperationLogExcelModel> models = new List<OperationLogExcelModel>();
                operationLogs.ForEach(t =>
                {
                    var temp = mapper.Map<OperationLogExcelModel>(t);
                    models.Add(temp);
                });
                string TimeTxt = DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒");
                await NPIOHelper.ExportDTtoExcelAsync(models, "接口日志", string.Format($"{App.ExportPath}接口日志{TimeTxt}.xlsx"), App.ExportPath);
                MessageBox.Show($"接口日志导出成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导出异常:{ex.Message}");
            }
        }

        #endregion

        #endregion

    }
}