iFactory.Godot/Mods/教育平台/Scripts/SearchEngine.cs

171 lines
4.4 KiB
C#
Raw Normal View History

2023-07-03 02:34:01 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
2023-07-04 04:08:18 +08:00
using System.Globalization;
2023-07-03 02:34:01 +08:00
using System.Linq;
using System.Text;
using BITKit;
using Godot;
using Microsoft.EntityFrameworkCore;
using Cysharp.Threading.Tasks;
2023-07-03 03:02:14 +08:00
using Microsoft.Extensions.DependencyInjection;
2023-07-03 02:34:01 +08:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BITFactory;
public interface ISearchEntry
{
string Key { get; }
string Value { get; }
}
[Serializable]
public class SearchResult:ISearchEntry
{
2023-07-03 03:02:14 +08:00
[Key]
public Guid Guid { get; set; }= Guid.NewGuid();
2023-07-03 02:34:01 +08:00
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;
2023-07-05 10:20:08 +08:00
public string Correlation { get; set; }
2023-07-03 02:34:01 +08:00
}
2023-07-03 03:02:14 +08:00
public interface ISearchEngine
{
SearchResult[] GetSearchResults(string key);
2023-07-04 04:08:18 +08:00
void SaveChanges();
2023-07-03 03:02:14 +08:00
void Add(SearchResult result);
void Remove(string key, string id);
}
public partial class SearchEngine:Node,ISearchEngine
2023-07-03 02:34:01 +08:00
{
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;
2023-07-03 03:02:14 +08:00
2023-07-03 02:34:01 +08:00
public override async void _Ready()
{
2023-07-03 03:02:14 +08:00
BITApp.ServiceCollection.AddSingleton<ISearchEngine>(this);
DI.Register<ISearchEngine>(this);
2023-07-03 02:34:01 +08:00
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();
2023-07-04 04:08:18 +08:00
var result = (string.IsNullOrEmpty(key) ? list : list.Where(x => x.Key.Contains(key))).ToArray();
//Fallback
if (result.Any() is false)
{
result = list.Where(x => x.Display.Contains(key!)).ToArray();
}
if (result.Any() is false)
{
result = list.Where(x => x.Id.Contains(key!)).ToArray();
}
Spawn(result);
2023-07-03 02:34:01 +08:00
_logBuilder.AppendLine($"已获取到集合");
_logBuilder.AppendLine($"{string.Join("\n",list)}");
searchProgressBar.Hide();
}
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);
}
}
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;
2023-07-04 04:08:18 +08:00
instance.updateTimeLabel.Text = entry.UpdateDate.ToString(CultureInfo.InvariantCulture);
instance.createTimeLabel.Text = entry.CreateDate.ToString(CultureInfo.InvariantCulture);
instance.headerLabel.Text = entry.Key;
2023-07-03 02:34:01 +08:00
}
}
private void QueueContainerFree()
{
foreach (var x in nodeContainer.GetChildren())
{
x.QueueFree();
}
}
2023-07-03 03:02:14 +08:00
public SearchResult[] GetSearchResults(string key)=>Context.context.Where(x=>x.Key.Contains(key)).ToArray();
2023-07-04 04:08:18 +08:00
public void SaveChanges() => Context.SaveChanges();
2023-07-03 03:02:14 +08:00
public void Add(SearchResult result)
{
Context.context.Add(result);
Context.SaveChanges();
}
public void Remove(string key, string id)
{
throw new NotImplementedException();
}
2023-07-03 02:34:01 +08:00
}