From 366ca0d237544544792c76357002dbf0dcb0a507 Mon Sep 17 00:00:00 2001 From: nsnail Date: Fri, 7 Jun 2024 11:05:05 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=94=A8=20=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=BF=BD=E7=95=A5=E7=89=88=E6=9C=AC=E9=94=81?= =?UTF-8?q?=20(#138)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [skip ci] Co-authored-by: tk --- assets/res/Enums.ln | 1 - assets/res/Statements.ln | 5 -- .../NetAdmin.AdmServer.Application.csproj | 3 + .../NetAdmin.Application.csproj | 3 + .../Services/RepositoryService.cs | 61 +++++++++++++------ .../Dto/Sys/Role/EditRoleReq.cs | 2 - .../NetAdmin.SysComponent.Application.csproj | 3 + .../Services/Sys/ConfigService.cs | 2 +- .../Services/Sys/DeptService.cs | 2 +- .../Services/Sys/DicContentService.cs | 3 +- .../Services/Sys/JobService.cs | 2 +- .../Services/Sys/MenuService.cs | 2 +- 12 files changed, 58 insertions(+), 31 deletions(-) diff --git a/assets/res/Enums.ln b/assets/res/Enums.ln index fb0ef7c6..a62df084 100644 --- a/assets/res/Enums.ln +++ b/assets/res/Enums.ln @@ -65,7 +65,6 @@ 登录 硕士 离异 -私信 空闲 等于 等待发送 diff --git a/assets/res/Statements.ln b/assets/res/Statements.ln index 42998046..1138bfd3 100644 --- a/assets/res/Statements.ln +++ b/assets/res/Statements.ln @@ -34,18 +34,15 @@ XML注释文件不存在 数据库同步开始 数据库服务器时钟偏移 数据库结构同步完成 -数据版本不能为空 文件不能为空 新密码不能为空 新手机号码验证码不正确 无效端口号 无效证件号码 -日志长度超过限制 旧密码不正确 旧密码不能为空 旧手机号码不正确 旧手机号码验证码不正确 -时间戳缺失或误差过大 时间表达式 时间计划不能为空 未指定部门 @@ -62,7 +59,6 @@ XML注释文件不存在 用户名或密码错误 用户名长度4位以上 用户头像不能为空 -用户档案不能为空 用户编号不存在 用户编号不能为空 目标设备不能为空 @@ -70,7 +66,6 @@ XML注释文件不存在 站内信不存在 站内信状态不正确 站内信类型不正确 -签名缺失 网络地址不正确 菜单名称不能为空 菜单标题不能为空 diff --git a/src/backend/NetAdmin.AdmServer.Application/NetAdmin.AdmServer.Application.csproj b/src/backend/NetAdmin.AdmServer.Application/NetAdmin.AdmServer.Application.csproj index dfce2b16..b00e0fa0 100644 --- a/src/backend/NetAdmin.AdmServer.Application/NetAdmin.AdmServer.Application.csproj +++ b/src/backend/NetAdmin.AdmServer.Application/NetAdmin.AdmServer.Application.csproj @@ -1,4 +1,7 @@ + + DBTYPE_SQLITE + diff --git a/src/backend/NetAdmin.Application/NetAdmin.Application.csproj b/src/backend/NetAdmin.Application/NetAdmin.Application.csproj index c21da26e..ce1aa961 100644 --- a/src/backend/NetAdmin.Application/NetAdmin.Application.csproj +++ b/src/backend/NetAdmin.Application/NetAdmin.Application.csproj @@ -1,4 +1,7 @@ + + DBTYPE_SQLITE + diff --git a/src/backend/NetAdmin.Application/Services/RepositoryService.cs b/src/backend/NetAdmin.Application/Services/RepositoryService.cs index 7d1fbe48..2fbf4476 100644 --- a/src/backend/NetAdmin.Application/Services/RepositoryService.cs +++ b/src/backend/NetAdmin.Application/Services/RepositoryService.cs @@ -31,43 +31,68 @@ public abstract class RepositoryService(BasicReposit /// /// 更新实体 /// - protected Task UpdateAsync(TEntity dto, IEnumerable includeFields, string[] excludeFields = null - , Expression> whereExp = null) + /// 新的值 + /// 包含的属性 + /// 排除的属性 + /// 查询表达式 + /// 是否忽略版本锁 + /// 更新行数 + protected Task UpdateAsync( // + TEntity newValue // + , IEnumerable includeFields // + , string[] excludeFields = null // + , Expression> whereExp = null // + , bool ignoreVersion = false) { - whereExp ??= a => a.Id.Equals(dto.Id); - var update = BuildUpdate(dto, includeFields, excludeFields).Where(whereExp); - + // 默认匹配主键 + whereExp ??= a => a.Id.Equals(newValue.Id); + var update = BuildUpdate(newValue, includeFields, excludeFields, ignoreVersion).Where(whereExp); return update.ExecuteAffrowsAsync(); } + #if DBTYPE_SQLSERVER /// /// 更新实体 /// - protected Task> UpdateEntityAsync(TEntity dto, IEnumerable includeFields - , string[] excludeFields = null - , Expression> whereExp = null) + /// 新的值 + /// 包含的属性 + /// 排除的属性 + /// 查询表达式 + /// 是否忽略版本锁 + /// 更新后的实体列表 + protected Task> UpdateReturnListAsync( // + TEntity newValue // + , IEnumerable includeFields // + , string[] excludeFields = null // + , Expression> whereExp = null // + , bool ignoreVersion = false) { - whereExp ??= a => a.Id.Equals(dto.Id); - return BuildUpdate(dto, includeFields, excludeFields).Where(whereExp).ExecuteUpdatedAsync(); + // 默认匹配主键 + whereExp ??= a => a.Id.Equals(newValue.Id); + return BuildUpdate(newValue, includeFields, excludeFields, ignoreVersion).Where(whereExp).ExecuteUpdatedAsync(); } #endif - private IUpdate BuildUpdate(TEntity dto, IEnumerable includeFields, string[] excludeFields = null) + private IUpdate BuildUpdate( // + TEntity entity // + , IEnumerable includeFields // + , string[] excludeFields = null // + , bool ignoreVersion = false) { - var ret = includeFields == null - ? Rpo.UpdateDiy.SetSource(dto) + var updateExp = includeFields == null + ? Rpo.UpdateDiy.SetSource(entity) : Rpo.UpdateDiy.SetDto(includeFields!.ToDictionary( x => x , x => typeof(TEntity).GetProperty(x, BindingFlags.Public | BindingFlags.Instance)! - .GetValue(dto))); + .GetValue(entity))); if (excludeFields != null) { - ret = ret.IgnoreColumns(excludeFields); + updateExp = updateExp.IgnoreColumns(excludeFields); } - if (dto is IFieldVersion version) { - ret = ret.Where($"{nameof(IFieldVersion.Version)} = @version", new { version = version.Version }); + if (!ignoreVersion && entity is IFieldVersion ver) { + updateExp = updateExp.Where($"{nameof(IFieldVersion.Version)} = @version", new { version = ver.Version }); } - return ret; + return updateExp; } } \ No newline at end of file diff --git a/src/backend/NetAdmin.Domain/Dto/Sys/Role/EditRoleReq.cs b/src/backend/NetAdmin.Domain/Dto/Sys/Role/EditRoleReq.cs index b12e1f5d..53e8bb7e 100644 --- a/src/backend/NetAdmin.Domain/Dto/Sys/Role/EditRoleReq.cs +++ b/src/backend/NetAdmin.Domain/Dto/Sys/Role/EditRoleReq.cs @@ -10,11 +10,9 @@ public sealed record EditRoleReq : CreateRoleReq { /// [JsonIgnore(Condition = JsonIgnoreCondition.Never)] - [Required(ErrorMessageResourceType = typeof(Ln), ErrorMessageResourceName = nameof(Ln.唯一编码不能为空))] public override long Id { get; init; } /// [JsonIgnore(Condition = JsonIgnoreCondition.Never)] - [Required(ErrorMessageResourceType = typeof(Ln), ErrorMessageResourceName = nameof(Ln.数据版本不能为空))] public override long Version { get; init; } } \ No newline at end of file diff --git a/src/backend/NetAdmin.SysComponent.Application/NetAdmin.SysComponent.Application.csproj b/src/backend/NetAdmin.SysComponent.Application/NetAdmin.SysComponent.Application.csproj index 6138b0be..ecbae60d 100644 --- a/src/backend/NetAdmin.SysComponent.Application/NetAdmin.SysComponent.Application.csproj +++ b/src/backend/NetAdmin.SysComponent.Application/NetAdmin.SysComponent.Application.csproj @@ -1,4 +1,7 @@ + + DBTYPE_SQLITE + diff --git a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/ConfigService.cs b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/ConfigService.cs index 6eb590ba..c02c0ce1 100644 --- a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/ConfigService.cs +++ b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/ConfigService.cs @@ -55,7 +55,7 @@ public sealed class ConfigService(BasicRepository rpo) // public async Task EditAsync(EditConfigReq req) { #if DBTYPE_SQLSERVER - return (await UpdateEntityAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt(); + return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt(); #else return await UpdateAsync(req, null).ConfigureAwait(false) > 0 ? await GetAsync(new QueryConfigReq { Id = req.Id }).ConfigureAwait(false) diff --git a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/DeptService.cs b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/DeptService.cs index ef91e015..1c029ce4 100644 --- a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/DeptService.cs +++ b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/DeptService.cs @@ -73,7 +73,7 @@ public sealed class DeptService(BasicRepository rpo) // public async Task EditAsync(EditDeptReq req) { #if DBTYPE_SQLSERVER - return (await UpdateEntityAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt(); + return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt(); #else return await UpdateAsync(req, null).ConfigureAwait(false) > 0 ? await GetAsync(new QueryDeptReq { Id = req.Id }).ConfigureAwait(false) diff --git a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/DicContentService.cs b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/DicContentService.cs index bcd2b6d3..e5687b23 100644 --- a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/DicContentService.cs +++ b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/DicContentService.cs @@ -74,7 +74,8 @@ public sealed class DicContentService(BasicRepository rpo) } #if DBTYPE_SQLSERVER - return (await UpdateEntityAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt(); + return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault() + ?.Adapt(); #else return await UpdateAsync(req, null).ConfigureAwait(false) > 0 ? await GetAsync(new QueryDicContentReq { Id = req.Id }).ConfigureAwait(false) diff --git a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/JobService.cs b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/JobService.cs index 59421c01..24f7f7be 100644 --- a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/JobService.cs +++ b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/JobService.cs @@ -193,7 +193,7 @@ public sealed class JobService(BasicRepository rpo, IJobRecordSer } #if DBTYPE_SQLSERVER - var ret = await UpdateEntityAsync( // + var ret = await UpdateReturnListAsync( // job with { Status = JobStatues.Running, LastExecTime = DateTime.Now } , [nameof(job.Status), nameof(job.LastExecTime)]) .ConfigureAwait(false); diff --git a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/MenuService.cs b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/MenuService.cs index 665ce3ed..09d90eab 100644 --- a/src/backend/NetAdmin.SysComponent.Application/Services/Sys/MenuService.cs +++ b/src/backend/NetAdmin.SysComponent.Application/Services/Sys/MenuService.cs @@ -60,7 +60,7 @@ public sealed class MenuService(BasicRepository rpo, IUserServic public async Task EditAsync(EditMenuReq req) { #if DBTYPE_SQLSERVER - return (await UpdateEntityAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt(); + return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt(); #else return await UpdateAsync(req, null).ConfigureAwait(false) > 0 ? await GetAsync(new QueryMenuReq { Id = req.Id }).ConfigureAwait(false)