180 lines
4.4 KiB
C#
180 lines
4.4 KiB
C#
|
using System;
|
||
|
using System.ComponentModel.DataAnnotations;
|
||
|
using System.Linq;
|
||
|
using System.Net.Mime;
|
||
|
using BITKit;
|
||
|
using Godot;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
||
|
namespace BITFactory;
|
||
|
// ReSharper disable once IdentifierTypo
|
||
|
[Serializable]
|
||
|
public abstract class IDIS_Base
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 创建时间
|
||
|
/// </summary>
|
||
|
public DateTime CreateDate { get; set; }=DateTime.Now;
|
||
|
/// <summary>
|
||
|
/// 更新时间
|
||
|
/// </summary>
|
||
|
public DateTime UpdateDate { get; set; }=DateTime.Now;
|
||
|
}
|
||
|
// 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>
|
||
|
/// 值类型,例如string,url,site
|
||
|
/// </summary>
|
||
|
public string Format { get; set; }
|
||
|
/// <summary>
|
||
|
/// 值
|
||
|
/// </summary>
|
||
|
public string Value { get; set; }
|
||
|
}
|
||
|
/// <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);
|
||
|
BIT4Log.Log<IDIS_DBContext>($"已创建标识数据库:{path}");
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 查询多个标识
|
||
|
/// </summary>
|
||
|
/// <param name="key">模糊标识码</param>
|
||
|
/// <param name="queries">查询列表</param>
|
||
|
/// <returns>是否查询到了内容</returns>
|
||
|
public bool Query(string key, out IDIS_Query[] queries)
|
||
|
{
|
||
|
var _query = Values.Where(x => x.Handle.Contains(key));
|
||
|
var result = _query.Select(x=>new IDIS_Query()
|
||
|
{
|
||
|
Handle = x.Handle,
|
||
|
CreateDate = x.CreateDate,
|
||
|
UpdateDate = x.UpdateDate,
|
||
|
Datas = Datas.Where(data=>data.Handle == key).ToArray(),
|
||
|
References = References.Where(reference=>reference.Handle == key).ToArray()
|
||
|
});
|
||
|
queries = result.ToArray();
|
||
|
return result.Any();
|
||
|
}
|
||
|
/// <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();
|
||
|
|
||
|
var 温度 = Datas
|
||
|
//查询相关的标识
|
||
|
.Where(x => x.Handle == "MyHandle")
|
||
|
.OrderBy(x=>x.CreateDate)
|
||
|
//从标识中查找"MyFormat"
|
||
|
.First(x => x.Format == "Temperature").Value;
|
||
|
|
||
|
|
||
|
return queries.Any();
|
||
|
}
|
||
|
}
|
||
|
// ReSharper disable once IdentifierTypo
|
||
|
/// <summary>
|
||
|
/// 标识码注册与查询服务
|
||
|
/// </summary>
|
||
|
public partial class IDIS_Service:Node
|
||
|
{
|
||
|
private static IDIS_DBContext Context;
|
||
|
public override void _Ready()
|
||
|
{
|
||
|
Context = new IDIS_DBContext();
|
||
|
BIT4Log.Log<IDIS_Service>("已创建标识数据库");
|
||
|
Context.Database.EnsureCreated();
|
||
|
}
|
||
|
}
|