feat: 程序异常增加高级搜索功能

This commit is contained in:
Argo-Tianyi 2022-01-02 19:29:14 +08:00
parent 6bd7303d82
commit 86522a81a4
9 changed files with 220 additions and 8 deletions

View File

@ -19,15 +19,32 @@ class ExceptionService : BaseDatabase, IException
return true; return true;
} }
public (IEnumerable<Error> Items, int ItemsCount) GetAll(string? searchText, int pageIndex, int pageItems, string? sortName, string sortOrder) public (IEnumerable<Error> Items, int ItemsCount) GetAll(string? searchText, ExceptionFilter filter, int pageIndex, int pageItems, string? sortName, string sortOrder)
{ {
var sql = new Sql(); var sql = new Sql();
if (!string.IsNullOrEmpty(searchText)) if (!string.IsNullOrEmpty(searchText))
{ {
sql.Append("WHERE ErrorPage Like @0 or Message Like @0 or StackTrace Like @0", $"%{searchText}%"); sql.Where("ErrorPage Like @0 or Message Like @0 or StackTrace Like @0", $"%{searchText}%");
} }
if (!string.IsNullOrEmpty(filter.Category))
{
sql.Where("Category = @0", filter.Category);
}
if (!string.IsNullOrEmpty(filter.UserId))
{
sql.Where("UserId Like @0", $"%{filter.UserId}%");
}
if (!string.IsNullOrEmpty(filter.ErrorPage))
{
sql.Where("ErrorPage Like @0", $"%{filter.ErrorPage}%");
}
sql.Where("LogTime >= @0 and LogTime <= @1", filter.Star, filter.End);
if (sortOrder == "Unset") if (sortOrder == "Unset")
{ {
sortOrder = "desc"; sortOrder = "desc";

View File

@ -0,0 +1,14 @@
namespace BootstrapAdmin.Web.Core;
public class ExceptionFilter
{
public DateTime Star { get; set; }
public DateTime End { get; set; }
public string? UserId { get; set; }
public string? ErrorPage { get; set; }
public string? Category { get; set; }
}

View File

@ -6,5 +6,5 @@ public interface IException
{ {
bool Log(Error exception); bool Log(Error exception);
(IEnumerable<Error> Items, int ItemsCount) GetAll(string? searchText, int pageIndex, int pageItems, string?sortName, string sortOrder); (IEnumerable<Error> Items, int ItemsCount) GetAll(string? searchText, ExceptionFilter filter, int pageIndex, int pageItems, string? sortName, string sortOrder);
} }

View File

@ -0,0 +1,14 @@
<div class="row g-3 form-inline">
<div class="col-sm-6 col-md-6">
<DateTimeRange @bind-Value="Value.LogTime" ShowLabel="true" AllowNull="false" />
</div>
<div class="col-sm-6 col-md-6">
<BootstrapInput @bind-Value="Value.ErrorPage" ShowLabel="true" />
</div>
<div class="col-sm-6 col-md-6">
<BootstrapInput @bind-Value="Value.UserId" ShowLabel="true" />
</div>
<div class="col-sm-6 col-md-6">
<Select Items="@Items" @bind-Value="Value.Category" ShowLabel="true" />
</div>
</div>

View File

@ -0,0 +1,38 @@
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Models;
using BootstrapAdmin.Web.Utils;
namespace BootstrapAdmin.Web.Components
{
/// <summary>
///
/// </summary>
public partial class ErrorSearch
{
private List<SelectedItem>? Items { get; set; }
/// <summary>
///
/// </summary>
[Parameter]
[NotNull]
public ErrorSearchModel? Value { get; set; }
/// <summary>
///
/// </summary>
[Parameter]
public EventCallback<ErrorSearchModel> ValueChanged { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
Items = new List<SelectedItem>
{
new SelectedItem("", "全部")
};
Items.AddRange(LookupHelper.GetExceptionCategory());
}
}
}

View File

@ -0,0 +1,92 @@
using BootstrapAdmin.DataAccess.Models;
using System.ComponentModel.DataAnnotations;
namespace BootstrapAdmin.Web.Models
{
/// <summary>
/// 字典维护自定义高级搜索模型
/// </summary>
public class ErrorSearchModel : ITableSearchModel
{
/// <summary>
/// 获得/设置 字典标签
/// </summary>
[Display(Name = "异常类型")]
public string? Category { get; set; }
/// <summary>
/// 获得/设置 字典名称
/// </summary>
[Display(Name = "用户名")]
public string? UserId { get; set; }
/// <summary>
/// 获得/设置 字典代码
/// </summary>
[Display(Name = "请求网址")]
public string? ErrorPage { get; set; }
/// <summary>
/// 获得/设置 字典类型
/// </summary>
[Display(Name = "记录时间")]
[NotNull]
public DateTimeRangeValue? LogTime { get; set; }
/// <summary>
///
/// </summary>
public ErrorSearchModel()
{
Reset();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public IEnumerable<IFilterAction> GetSearchs()
{
var ret = new List<IFilterAction>();
if (!string.IsNullOrEmpty(Category))
{
ret.Add(new SearchFilterAction(nameof(Error.Category), Category));
}
if (!string.IsNullOrEmpty(ErrorPage))
{
ret.Add(new SearchFilterAction(nameof(Error.ErrorPage), ErrorPage));
}
if (LogTime != null)
{
ret.Add(new SearchFilterAction(nameof(Error.LogTime), LogTime.Start, FilterAction.GreaterThanOrEqual));
ret.Add(new SearchFilterAction(nameof(Error.LogTime), LogTime.End, FilterAction.LessThanOrEqual));
}
if (!string.IsNullOrEmpty(UserId))
{
ret.Add(new SearchFilterAction(nameof(Error.UserId), UserId));
}
return ret;
}
/// <summary>
///
/// </summary>
/// <exception cref="NotImplementedException"></exception>
public void Reset()
{
Category = null;
UserId = null;
ErrorPage = null;
LogTime = new DateTimeRangeValue
{
Start = DateTime.Now.AddDays(-7),
End = DateTime.Now
};
}
}
}

View File

@ -2,7 +2,7 @@
<AdminTable TItem="DataAccess.Models.Error" <AdminTable TItem="DataAccess.Models.Error"
IsPagination="true" PageItemsSource="PageItemsSource" IsPagination="true" PageItemsSource="PageItemsSource"
ShowDefaultButtons="false" ShowExtendButtons="false" ShowAdvancedSearch="false" ShowDefaultButtons="false" ShowExtendButtons="false" TableSearchModel="ErrorSearchModel"
OnQueryAsync="OnQueryAsync"> OnQueryAsync="OnQueryAsync">
<ColumnsTemplete> <ColumnsTemplete>
<TableColumn @bind-Field="context.LogTime" Filterable="true" Searchable="true" Sortable="true"></TableColumn> <TableColumn @bind-Field="context.LogTime" Filterable="true" Searchable="true" Sortable="true"></TableColumn>
@ -11,5 +11,12 @@
<TableColumn @bind-Field="context.UserIp" Filterable="true" Searchable="true" Sortable="true"></TableColumn> <TableColumn @bind-Field="context.UserIp" Filterable="true" Searchable="true" Sortable="true"></TableColumn>
<TableColumn @bind-Field="context.ExceptionType" Filterable="true" Searchable="true" Sortable="true"></TableColumn> <TableColumn @bind-Field="context.ExceptionType" Filterable="true" Searchable="true" Sortable="true"></TableColumn>
<TableColumn @bind-Field="context.Message" Filterable="true" Searchable="true" Sortable="true"></TableColumn> <TableColumn @bind-Field="context.Message" Filterable="true" Searchable="true" Sortable="true"></TableColumn>
<TableColumn @bind-Field="context.Category" Lookup="CategroyLookup" Filterable="true" Searchable="true" Sortable="true"></TableColumn>
</ColumnsTemplete> </ColumnsTemplete>
<CustomerSearchTemplate>
@if (context is ErrorSearchModel searchModel)
{
<ErrorSearch @bind-Value="@searchModel"></ErrorSearch>
}
</CustomerSearchTemplate>
</AdminTable> </AdminTable>

View File

@ -1,5 +1,7 @@
using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Models;
using BootstrapAdmin.Web.Utils;
namespace BootstrapAdmin.Web.Pages.Admin; namespace BootstrapAdmin.Web.Pages.Admin;
@ -7,10 +9,22 @@ public partial class Exceptions
{ {
private List<int> PageItemsSource { get; } = new List<int> { 5, 20, 40, 80, 100, 200 }; private List<int> PageItemsSource { get; } = new List<int> { 5, 20, 40, 80, 100, 200 };
private ErrorSearchModel ErrorSearchModel { get; set; } = new ErrorSearchModel();
[Inject] [Inject]
[NotNull] [NotNull]
private IException? ExceptionService { get; set; } private IException? ExceptionService { get; set; }
[NotNull]
private List<SelectedItem>? CategroyLookup { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
CategroyLookup = LookupHelper.GetExceptionCategory();
}
private Task<QueryData<Error>> OnQueryAsync(QueryPageOptions options) private Task<QueryData<Error>> OnQueryAsync(QueryPageOptions options)
{ {
var ret = new QueryData<Error>() var ret = new QueryData<Error>()
@ -20,10 +34,20 @@ public partial class Exceptions
IsSearch = true IsSearch = true
}; };
var items = ExceptionService.GetAll(options.SearchText, options.PageIndex, options.PageItems, options.SortName, options.SortOrder.ToString()); var filter = new ExceptionFilter
{
Category = ErrorSearchModel.Category,
UserId = ErrorSearchModel.UserId,
ErrorPage = ErrorSearchModel.ErrorPage,
Star = ErrorSearchModel.LogTime.Start,
End = ErrorSearchModel.LogTime.End,
};
ret.TotalCount = items.ItemsCount; var (Items, ItemsCount) = ExceptionService.GetAll(options.SearchText, filter, options.PageIndex, options.PageItems, options.SortName, options.SortOrder.ToString());
ret.Items = items.Items;
ret.TotalCount = ItemsCount;
ret.Items = Items;
ret.IsAdvanceSearch = true;
return Task.FromResult(ret); return Task.FromResult(ret);
} }
} }

View File

@ -2,11 +2,17 @@
static class LookupHelper static class LookupHelper
{ {
public static List<SelectedItem> GetTargets() => new List<SelectedItem> public static List<SelectedItem> GetTargets() => new()
{ {
new SelectedItem("_self", "本窗口"), new SelectedItem("_self", "本窗口"),
new SelectedItem("_blank", "新窗口"), new SelectedItem("_blank", "新窗口"),
new SelectedItem("_parent", "父级窗口"), new SelectedItem("_parent", "父级窗口"),
new SelectedItem("_top", "顶级窗口"), new SelectedItem("_top", "顶级窗口"),
}; };
public static List<SelectedItem> GetExceptionCategory() => new()
{
new SelectedItem("App", "应用程序"),
new SelectedItem("DB", "数据库")
};
} }