添加了标识查询
This commit is contained in:
135
Mods/教育平台/Scripts/SearchEngine.cs
Normal file
135
Mods/教育平台/Scripts/SearchEngine.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using BITKit;
|
||||
using Godot;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace BITFactory;
|
||||
|
||||
public interface ISearchEntry
|
||||
{
|
||||
string Key { get; }
|
||||
string Value { get; }
|
||||
}
|
||||
[Serializable]
|
||||
[Keyless]
|
||||
public class SearchResult:ISearchEntry
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
public string Display { get; set; }
|
||||
public string Id { get; set; }
|
||||
public string RegistryRecord { get; set; }
|
||||
public DateTime UpdateDate { get; set; }
|
||||
public DateTime CreateDate { get; set; }=DateTime.Now;
|
||||
}
|
||||
|
||||
public partial class SearchEngine:Node
|
||||
{
|
||||
private static SqlLiteContext<SearchResult> Context;
|
||||
[Export] private Node nodeContainer;
|
||||
[Export] private PackedScene searchEntry;
|
||||
[Export] private LineEdit searchInput;
|
||||
[Export] private Button searchButton;
|
||||
[Export] private MenuButton quickSearchButton;
|
||||
[Export] private ProgressBar searchProgressBar;
|
||||
[Export] private RichTextLabel searchLogLabel;
|
||||
|
||||
private readonly StringBuilder _logBuilder=new StringBuilder();
|
||||
private SearchResult[] _currentSearchResults;
|
||||
public override async void _Ready()
|
||||
{
|
||||
try
|
||||
{
|
||||
QueueContainerFree();
|
||||
|
||||
await UniTask.SwitchToTaskPool();
|
||||
|
||||
Context = new SqlLiteContext<SearchResult>();
|
||||
await Context.Database.EnsureCreatedAsync();
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
searchButton.Pressed+= Search;
|
||||
searchInput.TextSubmitted += Search;
|
||||
quickSearchButton.Pressed+= QuickSearch;
|
||||
quickSearchButton.GetPopup().AllowSearch = true;
|
||||
|
||||
quickSearchButton.GetPopup().IdPressed += OnPressedIndex;
|
||||
|
||||
searchProgressBar.Hide();
|
||||
}
|
||||
private void Search()
|
||||
{
|
||||
Search(searchInput.Text);
|
||||
}
|
||||
|
||||
private async void Search(string key)
|
||||
{
|
||||
_logBuilder.AppendLine("正在释放");
|
||||
QueueContainerFree();
|
||||
searchProgressBar.Show();
|
||||
var list = await Context.context.ToListAsync();
|
||||
Spawn(string.IsNullOrEmpty(key) ? list : list.Where(x => x.Key.Contains(key)));
|
||||
_logBuilder.AppendLine($"已获取到集合");
|
||||
_logBuilder.AppendLine($"{string.Join("\n",list)}");
|
||||
searchProgressBar.Hide();
|
||||
//searchLogLabel.Text = _logBuilder.ToString();
|
||||
}
|
||||
|
||||
private void QuickSearch()
|
||||
{
|
||||
quickSearchButton.GetPopup().Clear();
|
||||
var enumerable = Context.context.Distinct((x,y)=>x.Key == y.Key);
|
||||
var searchResults = enumerable as SearchResult[] ?? enumerable.ToArray();
|
||||
_currentSearchResults = searchResults;
|
||||
for (var i = 0; i < searchResults.Length; i++)
|
||||
{
|
||||
var index = i;
|
||||
var element = searchResults[i];
|
||||
quickSearchButton.GetPopup().AddItem(element.Key);
|
||||
// quickSearchButton.GetPopup().IdPressed = id =>
|
||||
// {
|
||||
// var entry = searchResults[index];
|
||||
// searchInput.Text = entry.Key;
|
||||
// Search(entry.Key);
|
||||
// };
|
||||
}
|
||||
}
|
||||
private void OnPressedIndex(long index)
|
||||
{
|
||||
var entry = _currentSearchResults[index];
|
||||
searchInput.Text = entry.Key;
|
||||
Search(entry.Key);
|
||||
}
|
||||
|
||||
private void Spawn(IEnumerable<SearchResult> entries)
|
||||
{
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
var instance = searchEntry.Instantiate<UXContainer>();
|
||||
nodeContainer.AddChild(instance);
|
||||
|
||||
instance.titleLabel.Text = entry.Display ?? entry.Id ?? entry.Key;
|
||||
instance.Text = entry.Value;
|
||||
}
|
||||
}
|
||||
|
||||
private void QueueContainerFree()
|
||||
{
|
||||
foreach (var x in nodeContainer.GetChildren())
|
||||
{
|
||||
x.QueueFree();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user