2023-07-06 16:32:57 +08:00
|
|
|
using System;
|
2023-07-09 00:48:08 +08:00
|
|
|
using System.Collections.Generic;
|
2023-07-06 16:32:57 +08:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Mime;
|
2023-07-10 00:00:20 +08:00
|
|
|
using System.Reflection.Metadata;
|
2023-07-06 16:32:57 +08:00
|
|
|
using BITKit;
|
2023-07-08 15:32:06 +08:00
|
|
|
using Cysharp.Threading.Tasks;
|
2023-07-06 16:32:57 +08:00
|
|
|
using Godot;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-07-10 00:00:20 +08:00
|
|
|
using Constant = BITKit.Constant;
|
2023-07-06 16:32:57 +08:00
|
|
|
|
|
|
|
namespace BITFactory;
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
[Serializable]
|
|
|
|
public abstract class IDIS_Base
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 创建时间
|
|
|
|
/// </summary>
|
2023-07-10 00:00:20 +08:00
|
|
|
public DateTime CreateTime { get; set; }=DateTime.Now;
|
2023-07-06 16:32:57 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 更新时间
|
|
|
|
/// </summary>
|
2023-07-10 00:00:20 +08:00
|
|
|
public DateTime UpdateTime { get; set; }=DateTime.Now;
|
2023-07-06 16:32:57 +08:00
|
|
|
}
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
/// <summary>
|
|
|
|
/// 标识的值
|
|
|
|
/// </summary>
|
|
|
|
[Serializable]
|
|
|
|
public class IDIS_Value:IDIS_Base
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 标识码
|
|
|
|
/// </summary>
|
|
|
|
[Key]
|
|
|
|
public string Handle { get; set; }
|
|
|
|
}
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
public class IDIS_Query : IDIS_Value
|
|
|
|
{
|
|
|
|
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
/// <summary>
|
|
|
|
/// 关联的数据
|
|
|
|
/// </summary>
|
|
|
|
public IDIS_Data[] Datas { get; internal set; }
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
/// <summary>
|
|
|
|
/// 关联的标识引用
|
|
|
|
/// </summary>
|
|
|
|
public IDIS_Reference[] References { get; internal set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 标识的格式
|
|
|
|
/// </summary>
|
|
|
|
[Serializable]
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
public class IDIS_Data:IDIS_Base
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 不需要处理这个
|
|
|
|
/// </summary>
|
|
|
|
[Key]
|
|
|
|
public int Index { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 标识码
|
|
|
|
/// </summary>
|
|
|
|
public string Handle { get; set; }
|
|
|
|
/// <summary>
|
2023-07-17 04:10:14 +08:00
|
|
|
/// 记录的名称,通常为中文
|
|
|
|
/// </summary>
|
|
|
|
public string Name { get; set; }
|
|
|
|
/// <summary>
|
2023-07-06 16:32:57 +08:00
|
|
|
/// 值类型,例如string,url,site
|
|
|
|
/// </summary>
|
|
|
|
public string Format { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 值
|
|
|
|
/// </summary>
|
|
|
|
public string Value { get; set; }
|
2023-07-10 00:00:20 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 类型,用于解析时对数据条目进行分类
|
|
|
|
/// </summary>
|
|
|
|
public string Category { get; set; }
|
2023-07-06 16:32:57 +08:00
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// 标识的引用或关联
|
|
|
|
/// </summary>
|
|
|
|
[Serializable]
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
public class IDIS_Reference:IDIS_Base
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 不需要处理这个
|
|
|
|
/// </summary>
|
|
|
|
[Key]
|
|
|
|
public int Index{ get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 标识码
|
|
|
|
/// </summary>
|
|
|
|
public string Handle { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 相关联的标识
|
|
|
|
/// </summary>
|
|
|
|
public string RelatedHandle { get; set; }
|
|
|
|
}
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
public class IDIS_DBContext:DbContext
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 标识表
|
|
|
|
/// </summary>
|
|
|
|
private DbSet<IDIS_Value> Values { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 标识数据表
|
|
|
|
/// </summary>
|
|
|
|
private DbSet<IDIS_Data> Datas{ get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 标识引用表
|
|
|
|
/// </summary>
|
|
|
|
private DbSet<IDIS_Reference> References{ get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// 确保创建数据库
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="optionsBuilder"></param>
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
{
|
|
|
|
// ReSharper disable once StringLiteralTypo
|
|
|
|
//var sql = SQLiteContextHelper.GetConnectionString("IDIS");
|
|
|
|
SQLiteContextHelper.GetConnectionSqlAndPath("IDIS",out var sql,out var path);
|
|
|
|
optionsBuilder.UseSqlite(sql);
|
2023-07-12 15:27:27 +08:00
|
|
|
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll);
|
2023-07-06 16:32:57 +08:00
|
|
|
BIT4Log.Log<IDIS_DBContext>($"已创建标识数据库:{path}");
|
|
|
|
}
|
2023-07-09 00:48:08 +08:00
|
|
|
|
2023-07-06 16:32:57 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 查询多个标识
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">模糊标识码</param>
|
|
|
|
/// <param name="queries">查询列表</param>
|
|
|
|
/// <returns>是否查询到了内容</returns>
|
|
|
|
public bool Query(string key, out IDIS_Query[] queries)
|
|
|
|
{
|
2023-07-12 15:27:27 +08:00
|
|
|
ChangeTracker.DetectChanges();
|
|
|
|
ChangeTracker.Clear();
|
2023-07-09 00:48:08 +08:00
|
|
|
queries = Values
|
|
|
|
.Where(x => x.Handle.Contains(key))
|
|
|
|
.Select(x => new IDIS_Query()
|
|
|
|
{
|
|
|
|
Handle = x.Handle,
|
2023-07-10 00:00:20 +08:00
|
|
|
CreateTime = x.CreateTime,
|
|
|
|
UpdateTime = x.UpdateTime,
|
|
|
|
Datas = Datas.Where(data => data.Handle == x.Handle).ToArray(),
|
|
|
|
References = References.Where(reference => reference.Handle == x.Handle).ToArray()
|
2023-07-09 00:48:08 +08:00
|
|
|
}).ToArray();
|
|
|
|
return queries.Any();
|
2023-07-06 16:32:57 +08:00
|
|
|
}
|
2023-07-09 00:48:08 +08:00
|
|
|
|
2023-07-06 16:32:57 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 查询单个标识
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">模糊标识</param>
|
|
|
|
/// <param name="query">标识查询结果</param>
|
|
|
|
/// <returns>是否查询到了标识</returns>
|
|
|
|
public bool Query(string key, out IDIS_Query query)
|
|
|
|
{
|
|
|
|
Query(key, out IDIS_Query[] queries);
|
|
|
|
query = queries.FirstOrDefault();
|
|
|
|
return queries.Any();
|
|
|
|
}
|
2023-07-08 00:02:32 +08:00
|
|
|
public bool Register(string handle)
|
|
|
|
{
|
|
|
|
var handleExists = Values.Any(x => x.Handle == handle);
|
|
|
|
if (handleExists)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var value = new IDIS_Value()
|
|
|
|
{
|
|
|
|
Handle = handle
|
|
|
|
};
|
|
|
|
Values.Add(value);
|
2023-07-12 12:11:10 +08:00
|
|
|
SaveChangesAsync();
|
2023-07-08 00:02:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2023-07-17 04:10:14 +08:00
|
|
|
public void Register(string handle,string name,string format, string value,string category)
|
2023-07-08 00:02:32 +08:00
|
|
|
{
|
|
|
|
var handleExists = Values.Any(x => x.Handle == handle);
|
|
|
|
if (!handleExists)
|
|
|
|
{
|
|
|
|
Register(handle);
|
|
|
|
}
|
|
|
|
var data = new IDIS_Data()
|
|
|
|
{
|
2023-07-17 04:10:14 +08:00
|
|
|
Name = name,
|
2023-07-08 00:02:32 +08:00
|
|
|
Handle = handle,
|
|
|
|
Format = format,
|
2023-07-10 00:00:20 +08:00
|
|
|
Value = value,
|
|
|
|
Category = category,
|
2023-07-08 00:02:32 +08:00
|
|
|
};
|
|
|
|
Datas.Add(data);
|
2023-07-12 12:11:10 +08:00
|
|
|
SaveChangesAsync();
|
2023-07-08 00:02:32 +08:00
|
|
|
}
|
2023-07-08 15:32:06 +08:00
|
|
|
|
|
|
|
public void RegisterReference(string handle, string refenceHandle)
|
|
|
|
{
|
|
|
|
References.Add(new IDIS_Reference()
|
|
|
|
{
|
|
|
|
Handle = handle,
|
|
|
|
RelatedHandle = refenceHandle,
|
|
|
|
});
|
2023-07-12 12:11:10 +08:00
|
|
|
SaveChangesAsync();
|
2023-07-08 15:32:06 +08:00
|
|
|
}
|
2023-07-10 00:00:20 +08:00
|
|
|
|
2023-07-10 15:50:11 +08:00
|
|
|
public bool Update(string handle, string format, string value)
|
2023-07-10 00:00:20 +08:00
|
|
|
{
|
2023-07-10 15:50:11 +08:00
|
|
|
var result = Datas.FirstOrDefault(x => x.Handle == handle && x.Format == format);
|
2023-07-12 12:11:10 +08:00
|
|
|
if (result is null) return false;
|
|
|
|
result.UpdateTime=DateTime.Now;
|
|
|
|
result.Value = value;
|
|
|
|
SaveChangesAsync();
|
|
|
|
return true;
|
2023-07-10 00:00:20 +08:00
|
|
|
}
|
2023-07-06 16:32:57 +08:00
|
|
|
}
|
|
|
|
// ReSharper disable once IdentifierTypo
|
|
|
|
/// <summary>
|
|
|
|
/// 标识码注册与查询服务
|
|
|
|
/// </summary>
|
|
|
|
public partial class IDIS_Service:Node
|
|
|
|
{
|
2023-07-17 04:10:14 +08:00
|
|
|
public static IDIS_Service Singleton { get; private set; }
|
2023-07-06 16:32:57 +08:00
|
|
|
private static IDIS_DBContext Context;
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
Context = new IDIS_DBContext();
|
|
|
|
BIT4Log.Log<IDIS_Service>("已创建标识数据库");
|
2023-07-08 15:32:06 +08:00
|
|
|
UniTask.Run(()=>Context.Database.EnsureCreatedAsync());
|
2023-07-06 16:32:57 +08:00
|
|
|
}
|
2023-07-12 15:27:27 +08:00
|
|
|
|
2023-07-17 04:10:14 +08:00
|
|
|
public override void _EnterTree()
|
|
|
|
{
|
|
|
|
Singleton = this;
|
|
|
|
}
|
|
|
|
|
2023-07-12 15:27:27 +08:00
|
|
|
public override void _ExitTree()
|
|
|
|
{
|
|
|
|
Context.Dispose();
|
|
|
|
Context = null;
|
|
|
|
}
|
|
|
|
|
2023-07-10 00:00:20 +08:00
|
|
|
public bool Query(string word,out IDIS_Query[] queries) => Context.Query(word, out queries);
|
2023-07-08 00:02:32 +08:00
|
|
|
public bool Register(string handle) => Context.Register(handle);
|
2023-07-17 04:10:14 +08:00
|
|
|
public void Register(string handle,string name, string format, string value,string category) => Context.Register(handle,name, format, value,category);
|
2023-07-08 15:32:06 +08:00
|
|
|
public void RegisterReference(string handle,string refenceHandle) => Context.RegisterReference(handle,refenceHandle);
|
|
|
|
public static string GenerateHandle() => $"88.123.99/{Mathf.Abs(Guid.NewGuid().GetHashCode())}";
|
2023-07-10 00:00:20 +08:00
|
|
|
public bool Query(string key, out IDIS_Query query) => Context.Query(key, out query);
|
2023-07-10 15:50:11 +08:00
|
|
|
public bool Update(string handle, string format, string value) => Context.Update(handle, format, value);
|
2023-07-06 16:32:57 +08:00
|
|
|
}
|