Merge pull request '过滤重复种子数据' (#175) from koy07555/Admin.NET.Pro:种子数据加过滤功能 into main

Reviewed-on: http://101.43.53.74:3000/Admin.NET/Admin.NET.Pro/pulls/175
This commit is contained in:
zuohuaijun 2024-11-14 17:49:22 +08:00
commit 90fc6d10ed
6 changed files with 106 additions and 5 deletions

View File

@ -1,4 +1,4 @@
{
{
"$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json",
"Urls": "http://*:5005", //
@ -21,6 +21,13 @@
"DefaultErrorMessage": "系统异常,请联系管理员",
"ThrowBah": true, // Oops.Oh
"LogError": false //
},
// (访)
"StaticContentTypeMappings": {
".dll": "application/octet-stream",
".exe": "application/octet-stream",
".pdb": "application/octet-stream",
".so": "application/octet-stream"
},
"LocalizationSettings": {
"SupportedCultures": [ "zh-CN", "en" ], //

View File

@ -41,5 +41,14 @@ public class CreateSeedDataInput
/// 后缀
/// </summary>
/// <example>Web.Application</example>
public string Suffix { get; set; }
public string Suffix { get; set; }
/// <summary>
/// 过滤已有数据
/// </summary>
/// <remarks>
/// 如果数据在其它不同名的已有的种子类型的数据中出现过,就不生成这个数据
/// 主要用于生成菜单功能,菜单功能往往与子项目绑定,如果生成完整数据就会导致菜单项多处理重复。
/// </remarks>
public bool FilterExistingData { get; set; }
}

View File

@ -4,9 +4,11 @@
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Mapster.Adapters;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Npgsql;
using System.Linq;
namespace Admin.NET.Core.Service;
@ -354,7 +356,67 @@ public class SysDatabaseService : IDynamicApiController, ITransient
orderField = dbColumnInfos.Where(u => u.DbColumnName.ToLower() == "id").FirstOrDefault();
if (orderField != null)
query.OrderBy(orderField.DbColumnName);
object records = query.ToList();
IEnumerable recordsTmp = (IEnumerable)query.ToList();
List<dynamic> records = recordsTmp.ToDynamicList();
//这里要过滤已存在的数据
if (input.FilterExistingData && records.Count() > 0)
{
//获取实体类型
//获取所有种数据数据类型
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false) && u.FullName.EndsWith("." + input.EntityName))
.Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
.ToList();
if (entityTypes.Count == 1) //只有一个实体匹配才能过滤
{
//获取实体的主键对应的属性名称
var pkInfo = entityTypes[0].GetProperties().Where(u => u.GetCustomAttribute<SugarColumn>() != null && u.GetCustomAttribute<SugarColumn>().IsPrimaryKey).First();
if (pkInfo != null)
{
var seedDataTypes = App.EffectiveTypes
.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(
i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>)) && i.GenericTypeArguments[0] == entityTypes[0]
)
)
.ToList();
//可能会重名的种子数据不作为过滤项
string doNotFilterfullName1 = $"{input.Position}.SeedData.{input.SeedDataName}";
string doNotFilterfullName2 = $"{input.Position}.{input.SeedDataName}"; //Core中的命名空间没有SeedData
PropertyInfo idPropertySeedData = records[0].GetType().GetProperty("Id");
for (int i = seedDataTypes.Count - 1; i >= 0; i--)
{
string fullName = seedDataTypes[i].FullName;
if ((fullName == doNotFilterfullName1) || (fullName == doNotFilterfullName2))
continue;
//开始删除重复数据
var instance = Activator.CreateInstance(seedDataTypes[i]);
var hasDataMethod = seedDataTypes[i].GetMethod("HasData");
var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
if (seedData == null) continue;
List<object> recordsToRemove = new List<object>();
foreach (var record in records)
{
object recordId = pkInfo.GetValue(record);
foreach (var d1 in seedData)
{
object dataId = idPropertySeedData.GetValue(d1);
if (recordId != null && dataId != null && recordId.Equals(dataId))
{
recordsToRemove.Add(record);
break;
}
}
}
foreach (var itemToRemove in recordsToRemove)
{
records.Remove(itemToRemove);
}
}
}
}
}
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
var recordsJSON = JsonConvert.SerializeObject(records, Formatting.Indented, timeConverter);

View File

@ -1,4 +1,4 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
@ -25,6 +25,7 @@ using Newtonsoft.Json;
using OnceMi.AspNetCore.OSS;
using SixLabors.ImageSharp.Web.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Unicode;
@ -273,7 +274,15 @@ public class Startup : AppStartup
// 特定文件类型(文件后缀)处理
var contentTypeProvider = FS.GetFileExtensionContentTypeProvider();
// contentTypeProvider.Mappings[".文件后缀"] = "MIME 类型";
// contentTypeProvider.Mappings[".文件后缀"] = "MIME 类型";
var cpMappings = App.GetConfig<Dictionary<string, string>>("StaticContentTypeMappings");
if (cpMappings != null)
{
foreach (var key in cpMappings.Keys)
{
contentTypeProvider.Mappings[key] = cpMappings[key];
}
}
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = contentTypeProvider

View File

@ -72,4 +72,12 @@ export interface CreateSeedDataInput {
* @example Web.Application
*/
suffix?: string | null;
/**
*
*
* @type {boolean}
* @memberof CreateSeedDataInput
*/
filterExistingData?: boolean;
}

View File

@ -27,6 +27,11 @@
</el-select>
</el-form-item>
</el-col>
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
<el-form-item label="过滤重复数据" prop="filterExistingData">
<el-switch v-model="state.ruleForm.filterExistingData"></el-switch>
</el-form-item>
</el-col>
</el-row>
</el-form>
<template #footer>
@ -69,6 +74,7 @@ const openDialog = (row: any) => {
state.ruleForm.configId = row.configId;
state.ruleForm.tableName = row.tableName;
state.ruleForm.position = row.position;
state.ruleForm.filterExistingData = false;
state.isShowDialog = true;
};