临时提交,新功能未 完成

This commit is contained in:
2025-06-30 20:11:21 +08:00
parent a64065fdda
commit a1de03a94e
7 changed files with 119 additions and 90 deletions

View File

@@ -21,4 +21,6 @@ public class DbMenu
[SugarColumn(IsIgnore = true)] [SugarColumn(IsIgnore = true)]
public List<DbMenu> Items { get; set; } public List<DbMenu> Items { get; set; }
public DbMenu? Parent { get; set; }
} }

View File

@@ -24,5 +24,5 @@ public class DbVariableTable
[SugarColumn(IsNullable = true)] public int? DeviceId { get; set; } [SugarColumn(IsNullable = true)] public int? DeviceId { get; set; }
[Navigate(NavigateType.ManyToOne, nameof(DeviceId))] [Navigate(NavigateType.ManyToOne, nameof(DeviceId))]
public Device? Device { get; set; } public DbDevice? Device { get; set; }
} }

View File

@@ -34,7 +34,7 @@ public class DeviceRepository
public async Task<List<Device>> GetAll() public async Task<List<Device>> GetAll()
{ {
var dlist = await _db.Queryable<DbDevice>().Includes(d => d.VariableTables).ToListAsync(); var dlist = await _db.Queryable<DbDevice>().Includes(d => d.VariableTables,dv=>dv.Device).ToListAsync();
var devices = new List<Device>(); var devices = new List<Device>();
foreach (var dbDevice in dlist) foreach (var dbDevice in dlist)
{ {

View File

@@ -24,14 +24,34 @@ public class MenuRepository
{ {
// //无主键用法新:5.1.4.110 // //无主键用法新:5.1.4.110
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载 // db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
List<MenuBean> menus = new(); List<DbMenu> dbMenuList = await _db.Queryable<DbMenu>().ToListAsync();
var dbMenuList = await _db.Queryable<DbMenu>().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
foreach (var item in dbMenuList) List<MenuBean> menuTree = new();
var dbMenuTree = await _db.Queryable<DbMenu>().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
foreach (var dbMenu in dbMenuTree)
{ {
menus.Add(item.CopyTo<MenuBean>()); AddParent(dbMenu);
menuTree.Add(dbMenu.CopyTo<MenuBean>());
} }
return menus; return menuTree;
}
private void AddParent(DbMenu dbMenu)
{
if (dbMenu.Items == null || dbMenu.Items.Count == 0)
return;
foreach (var item in dbMenu.Items)
{
item.Parent = dbMenu;
if (item.Items!=null && item.Items.Count>0)
{
AddParent(item);
}
}
} }
public async Task<int> AddMenu(MenuBean menu) public async Task<int> AddMenu(MenuBean menu)
@@ -62,7 +82,8 @@ public class MenuRepository
if (addDeviceMenuId == 0) if (addDeviceMenuId == 0)
throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!"); throw new InvalidOperationException($"{menu.Name},设备菜单添加失败!!");
var defVarTable= await _db.Queryable<DbVariableTable>().FirstAsync(v=>v.DeviceId==device.Id && v.Name=="默认变量表"); var defVarTable = await _db.Queryable<DbVariableTable>()
.FirstAsync(v => v.DeviceId == device.Id && v.Name == "默认变量表");
if (defVarTable == null) if (defVarTable == null)
throw new InvalidOperationException($"没有找到{device.Name}的默认变量表。"); throw new InvalidOperationException($"没有找到{device.Name}的默认变量表。");
var defVarTableMenu = new MenuBean() var defVarTableMenu = new MenuBean()

View File

@@ -32,7 +32,7 @@ public class VariableTableRepository
public async Task<List<VariableTable>> GetAll() public async Task<List<VariableTable>> GetAll()
{ {
var dbVariableTables = await _db.Queryable<DbVariableTable>().ToListAsync(); var dbVariableTables = await _db.Queryable<DbVariableTable>().Includes(dv=>dv.Device).ToListAsync();
var variableTables = new List<VariableTable>(); var variableTables = new List<VariableTable>();
foreach (var dbVariableTable in dbVariableTables) foreach (var dbVariableTable in dbVariableTables)

View File

@@ -5,7 +5,6 @@ namespace PMSWPF.Extensions;
public static class ObjectExtensions public static class ObjectExtensions
{ {
/// <summary> /// <summary>
/// 复制一个新的对象并返回 /// 复制一个新的对象并返回
/// </summary> /// </summary>
@@ -65,6 +64,11 @@ public static class ObjectExtensions
if (targetProperty != null && targetProperty.CanWrite) if (targetProperty != null && targetProperty.CanWrite)
{ {
object sourceValue = sourceProperty.GetValue(tsource); object sourceValue = sourceProperty.GetValue(tsource);
// 判断源属性和目标属性是否是泛型列表
bool isSourceList = sourceProperty.PropertyType.IsGenericType &&
sourceProperty.PropertyType.GetGenericTypeDefinition() == typeof(List<>);
bool isTargetList = targetProperty.PropertyType.IsGenericType &&
targetProperty.PropertyType.GetGenericTypeDefinition() == typeof(List<>);
// 场景 1: 属性类型完全相同 // 场景 1: 属性类型完全相同
if (targetProperty.PropertyType == sourceProperty.PropertyType) if (targetProperty.PropertyType == sourceProperty.PropertyType)
@@ -72,14 +76,27 @@ public static class ObjectExtensions
targetProperty.SetValue(ttarget, sourceValue); targetProperty.SetValue(ttarget, sourceValue);
} }
// 场景 2: 属性类型不同,但可能是泛型 List<T> 类型 // 场景 2: 属性类型不同,但可能是泛型 List<T> 类型
else if(isTargetList && isSourceList)
{
CopyGenericList(ttarget, sourceProperty, targetProperty, sourceValue);
}
else else
{ {
bool isSourceList = sourceProperty.PropertyType.IsGenericType &&
sourceProperty.PropertyType.GetGenericTypeDefinition() == typeof(List<>);
bool isTargetList = targetProperty.PropertyType.IsGenericType &&
targetProperty.PropertyType.GetGenericTypeDefinition() == typeof(List<>);
if (isSourceList && isTargetList) }
}
}
}
/// <summary>
/// 复制泛型列表,
/// </summary>
/// <param name="ttarget"></param>
/// <param name="sourceProperty"></param>
/// <param name="targetProperty"></param>
/// <param name="sourceValue"></param>
private static void CopyGenericList(object ttarget, PropertyInfo sourceProperty, PropertyInfo targetProperty,
object? sourceValue)
{ {
// 获取源列表的元素类型 // 获取源列表的元素类型
Type sourceListItemType = sourceProperty.PropertyType.GetGenericArguments()[0]; Type sourceListItemType = sourceProperty.PropertyType.GetGenericArguments()[0];
@@ -93,7 +110,7 @@ public static class ObjectExtensions
if (sourceValue == null) if (sourceValue == null)
{ {
targetProperty.SetValue(ttarget, null); targetProperty.SetValue(ttarget, null);
continue; return;
} }
// 将源值强制转换为 IEnumerable 以便遍历 // 将源值强制转换为 IEnumerable 以便遍历
@@ -134,16 +151,6 @@ public static class ObjectExtensions
$"由于列表元素类型不匹配,跳过属性 '{sourceProperty.Name}':源 '{sourceListItemType.Name}',目标 '{targetListItemType.Name}'"); $"由于列表元素类型不匹配,跳过属性 '{sourceProperty.Name}':源 '{sourceListItemType.Name}',目标 '{targetListItemType.Name}'");
} }
} }
else
{
// 类型不同,且都不是泛型 List<T>。此处跳过,或可实现类型转换。
Console.WriteLine(
$"由于类型不匹配,跳过属性 '{sourceProperty.Name}':源 '{sourceProperty.PropertyType.Name}',目标 '{targetProperty.PropertyType.Name}'");
}
}
}
}
}
/// <summary> /// <summary>
/// 辅助方法,用于动态创建泛型 List<T>。 /// 辅助方法,用于动态创建泛型 List<T>。
@@ -155,7 +162,4 @@ public static class ObjectExtensions
Type listType = typeof(List<>).MakeGenericType(itemType); Type listType = typeof(List<>).MakeGenericType(itemType);
return (IList)Activator.CreateInstance(listType); return (IList)Activator.CreateInstance(listType);
} }
} }

View File

@@ -16,4 +16,6 @@ public class MenuBean
public ViewModelBase ViewModel { get; set; } public ViewModelBase ViewModel { get; set; }
public Object? Data { get; set; } public Object? Data { get; set; }
public List<MenuBean> Items { get; set; } public List<MenuBean> Items { get; set; }
public MenuBean Parent { get; set; }
} }