1.05 #7

Merged
zzh merged 19 commits from test into master 2026-10-15 13:59:09 +08:00
16 changed files with 142 additions and 136 deletions
Showing only changes of commit 09ca1c96d7 - Show all commits

View File

@ -3,6 +3,6 @@
<PropertyGroup> <PropertyGroup>
<!-- All Fake packages --> <!-- All Fake packages -->
<FakePackageVersion>8.0.5.21</FakePackageVersion> <FakePackageVersion>8.0.5.22</FakePackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -6,8 +6,8 @@
<PropertyGroup> <PropertyGroup>
<FakeRepoRoot Condition="'$(FakeRepoRoot)' == ''">C:\Users\BSI\RiderProjects\fake2</FakeRepoRoot> <FakeRepoRoot Condition="'$(FakeRepoRoot)' == ''">C:\Users\BSI\RiderProjects\fake2</FakeRepoRoot>
<!-- EventBus / Dashboard / IntegrationEventLog --> <!-- EventBus / Dashboard / IntegrationEventLog -->
<UseLocalFakeEventBus Condition="'$(UseLocalFakeEventBus)' == ''">false</UseLocalFakeEventBus> <UseLocalFakeEventBus Condition="'$(UseLocalFakeEventBus)' == ''">true</UseLocalFakeEventBus>
<!-- RBAC / FileManagement(表前缀 fk_ 等框架改动需同源) --> <!-- RBAC / FileManagement(表前缀 fk_ 等框架改动需同源) -->
<UseLocalFakeModules Condition="'$(UseLocalFakeModules)' == ''">false</UseLocalFakeModules> <UseLocalFakeModules Condition="'$(UseLocalFakeModules)' == ''">true</UseLocalFakeModules>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -1,5 +1,6 @@
using Fake.DependencyInjection; using Fake.DependencyInjection;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Oms.Application.Dtos.DataPerm; using Oms.Application.Dtos.DataPerm;
@ -11,7 +12,7 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
public async Task<RbacUserSnapshot?> GetUserSnapshotAsync(Guid userId, CancellationToken cancellationToken = default) public async Task<RbacUserSnapshot?> GetUserSnapshotAsync(Guid userId, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
SELECT SELECT
u.`Id` AS `Id`, u.`Id` AS `Id`,
u.`userid` AS `MapId`, u.`userid` AS `MapId`,
@ -19,22 +20,21 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
u.`Name` AS `Name`, u.`Name` AS `Name`,
u.`OrganizationId` AS `OrganizationId`, u.`OrganizationId` AS `OrganizationId`,
o.`Name` AS `OrganizationName` o.`Name` AS `OrganizationName`
FROM `user` u FROM `{{FakeTables.User}}` u
LEFT JOIN `organization` o ON o.`Id` = u.`OrganizationId` AND o.`IsDeleted` = 0 LEFT JOIN `{{FakeTables.Organization}}` o ON o.`Id` = u.`OrganizationId` AND o.`IsDeleted` = 0
WHERE u.`Id` = {0} AND u.`IsDeleted` = 0 WHERE u.`Id` = {0} AND u.`IsDeleted` = 0
LIMIT 1 LIMIT 1
"""; """;
return await Db.Database.SqlQueryRaw<RbacUserSnapshot>(sql, userId.ToString()) return await Db.Database.SqlQueryRaw<RbacUserSnapshot>(sql, userId.ToString())
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
} }
public async Task<bool> IsPureCustomerUserAsync(Guid userId, CancellationToken cancellationToken = default) public async Task<bool> IsPureCustomerUserAsync(Guid userId, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
SELECT COUNT(*) AS `Value` SELECT COUNT(*) AS `Value`
FROM `user_role` ur FROM `{{FakeTables.UserRole}}` ur
INNER JOIN `role` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0 INNER JOIN `{{FakeTables.Role}}` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0
WHERE ur.`UserId` = {0} WHERE ur.`UserId` = {0}
AND r.`Code` <> 'Customer' AND r.`Code` <> 'Customer'
AND r.`Code` NOT LIKE 'CustomerSub_%' AND r.`Code` NOT LIKE 'CustomerSub_%'
@ -47,12 +47,11 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
return false; return false;
} }
const string hasRoleSql = """ var hasRoleSql = $$"""
SELECT COUNT(*) AS `Value` SELECT COUNT(*) AS `Value`
FROM `user_role` ur FROM `{{FakeTables.UserRole}}` ur
WHERE ur.`UserId` = {0} WHERE ur.`UserId` = {0}
"""; """;
var roleCount = await Db.Database.SqlQueryRaw<int>(hasRoleSql, userId.ToString()) var roleCount = await Db.Database.SqlQueryRaw<int>(hasRoleSql, userId.ToString())
.FirstAsync(cancellationToken); .FirstAsync(cancellationToken);
return roleCount > 0; return roleCount > 0;
@ -60,10 +59,10 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
public async Task<bool> IsAdminUserAsync(Guid userId, CancellationToken cancellationToken = default) public async Task<bool> IsAdminUserAsync(Guid userId, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
SELECT COUNT(*) AS `Value` SELECT COUNT(*) AS `Value`
FROM `user_role` ur FROM `{{FakeTables.UserRole}}` ur
INNER JOIN `role` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0 INNER JOIN `{{FakeTables.Role}}` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0
WHERE ur.`UserId` = {0} WHERE ur.`UserId` = {0}
AND r.`Code` = 'Admin' AND r.`Code` = 'Admin'
"""; """;
@ -74,21 +73,20 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
public async Task<List<Guid>> GetUserRoleIdsAsync(Guid userId, CancellationToken cancellationToken = default) public async Task<List<Guid>> GetUserRoleIdsAsync(Guid userId, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
SELECT ur.`RoleId` AS `Value` SELECT ur.`RoleId` AS `Value`
FROM `user_role` ur FROM `{{FakeTables.UserRole}}` ur
WHERE ur.`UserId` = {0} WHERE ur.`UserId` = {0}
"""; """;
return await Db.Database.SqlQueryRaw<Guid>(sql, userId.ToString()) return await Db.Database.SqlQueryRaw<Guid>(sql, userId.ToString())
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
public async Task<List<Guid>> GetUserIdsByRoleAsync(Guid roleId, CancellationToken cancellationToken = default) public async Task<List<Guid>> GetUserIdsByRoleAsync(Guid roleId, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
SELECT ur.`UserId` AS `Value` SELECT ur.`UserId` AS `Value`
FROM `user_role` ur FROM `{{FakeTables.UserRole}}` ur
WHERE ur.`RoleId` = {0} WHERE ur.`RoleId` = {0}
"""; """;
@ -115,7 +113,6 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
WHERE p.`IsDeleted` = 0 WHERE p.`IsDeleted` = 0
AND p.`RoleId` IN ({inClause}) AND p.`RoleId` IN ({inClause})
"""; """;
return await Db.Database.SqlQueryRaw<OrgPermRow>(sql).ToListAsync(cancellationToken); return await Db.Database.SqlQueryRaw<OrgPermRow>(sql).ToListAsync(cancellationToken);
} }
@ -385,14 +382,14 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
public async Task<List<Guid>> GetOrganizationSubtreeIdsAsync(Guid rootOrganizationId, CancellationToken cancellationToken = default) public async Task<List<Guid>> GetOrganizationSubtreeIdsAsync(Guid rootOrganizationId, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
WITH RECURSIVE org_tree AS ( WITH RECURSIVE org_tree AS (
SELECT o.`Id` SELECT o.`Id`
FROM `organization` o FROM `{{FakeTables.Organization}}` o
WHERE o.`Id` = {0} AND o.`IsDeleted` = 0 WHERE o.`Id` = {0} AND o.`IsDeleted` = 0
UNION ALL UNION ALL
SELECT c.`Id` SELECT c.`Id`
FROM `organization` c FROM `{{FakeTables.Organization}}` c
INNER JOIN org_tree t ON c.`ParentId` = t.`Id` INNER JOIN org_tree t ON c.`ParentId` = t.`Id`
WHERE c.`IsDeleted` = 0 WHERE c.`IsDeleted` = 0
) )
@ -414,22 +411,21 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
} }
var inClause = string.Join(",", ids.Select(x => $"'{x}'")); var inClause = string.Join(",", ids.Select(x => $"'{x}'"));
var sql = $""" var sql = $$"""
SELECT o.`Id` AS `Id`, o.`Name` AS `Name` SELECT o.`Id` AS `Id`, o.`Name` AS `Name`
FROM `organization` o FROM `{{FakeTables.Organization}}` o
WHERE o.`IsDeleted` = 0 WHERE o.`IsDeleted` = 0
AND o.`Id` IN ({inClause}) AND o.`Id` IN ({inClause})
"""; """;
var rows = await Db.Database.SqlQueryRaw<OrganizationNameRow>(sql).ToListAsync(cancellationToken); var rows = await Db.Database.SqlQueryRaw<OrganizationNameRow>(sql).ToListAsync(cancellationToken);
return rows.ToDictionary(x => x.Id, x => x.Name ?? x.Id.ToString()); return rows.ToDictionary(x => x.Id, x => x.Name ?? x.Id.ToString());
} }
public async Task<bool> OrganizationExistsAndEnabledAsync(Guid organizationId, CancellationToken cancellationToken = default) public async Task<bool> OrganizationExistsAndEnabledAsync(Guid organizationId, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
SELECT COUNT(*) AS `Value` SELECT COUNT(*) AS `Value`
FROM `organization` o FROM `{{FakeTables.Organization}}` o
WHERE o.`Id` = {0} WHERE o.`Id` = {0}
AND o.`IsDeleted` = 0 AND o.`IsDeleted` = 0
"""; """;
@ -450,13 +446,12 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
if (dataScope == DataPermConstants.DataScopeTeam) if (dataScope == DataPermConstants.DataScopeTeam)
{ {
const string sql = """ var sql = $$"""
SELECT u.`userid` AS `Value` SELECT u.`userid` AS `Value`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`IsDeleted` = 0 WHERE u.`IsDeleted` = 0
AND u.`OrganizationId` = {0} AND u.`OrganizationId` = {0}
"""; """;
return await Db.Database.SqlQueryRaw<int>(sql, organizationId.ToString()) return await Db.Database.SqlQueryRaw<int>(sql, organizationId.ToString())
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
@ -470,9 +465,9 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
} }
var inClause = string.Join(",", orgIds.Select(x => $"'{x}'")); var inClause = string.Join(",", orgIds.Select(x => $"'{x}'"));
var sql = $""" var sql = $$"""
SELECT u.`userid` AS `Value` SELECT u.`userid` AS `Value`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`IsDeleted` = 0 WHERE u.`IsDeleted` = 0
AND u.`OrganizationId` IN ({inClause}) AND u.`OrganizationId` IN ({inClause})
"""; """;
@ -485,7 +480,7 @@ public class DataPermRepository(FakeRbacDbContext dbContext) : IScopedDependency
public async Task<List<DataResourceMetaDto>> GetDataResourcesAsync(CancellationToken cancellationToken = default) public async Task<List<DataResourceMetaDto>> GetDataResourcesAsync(CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $"""
SELECT SELECT
r.`Code` AS `Code`, r.`Code` AS `Code`,
r.`Name` AS `Name`, r.`Name` AS `Name`,

View File

@ -2,6 +2,7 @@ using System.Text.Json;
using Fake.Application; using Fake.Application;
using Fake.DependencyInjection; using Fake.DependencyInjection;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Storage;
using Oms.Application.Dtos.DataPerm; using Oms.Application.Dtos.DataPerm;
@ -20,21 +21,21 @@ public class DataPermStore(FakeRbacDbContext dbContext, DataPermContextCache cac
public async Task<UserDataPermDto> GetUserDataPermAsync(Guid userId, CancellationToken cancellationToken = default) public async Task<UserDataPermDto> GetUserDataPermAsync(Guid userId, CancellationToken cancellationToken = default)
{ {
var user = await dbContext.Database.SqlQueryRaw<UserHeaderRow>(""" var user = await dbContext.Database.SqlQueryRaw<UserHeaderRow>($$"""
SELECT SELECT
u.`Id` AS `UserId`, u.`Id` AS `UserId`,
u.`Name` AS `UserName`, u.`Name` AS `UserName`,
u.`OrganizationId` AS `OrganizationId`, u.`OrganizationId` AS `OrganizationId`,
o.`Name` AS `OrganizationName` o.`Name` AS `OrganizationName`
FROM `user` u FROM `{{FakeTables.User}}` u
LEFT JOIN `organization` o ON o.`Id` = u.`OrganizationId` AND o.`IsDeleted` = 0 LEFT JOIN `{{FakeTables.Organization}}` o ON o.`Id` = u.`OrganizationId` AND o.`IsDeleted` = 0
WHERE u.`Id` = {0} AND u.`IsDeleted` = 0 WHERE u.`Id` = {0} AND u.`IsDeleted` = 0
LIMIT 1 LIMIT 1
""", userId.ToString()).FirstOrDefaultAsync(cancellationToken) """, userId.ToString()).FirstOrDefaultAsync(cancellationToken)
?? throw new BusinessException("用户不存在"); ?? throw new BusinessException("用户不存在");
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var orgRows = await dbContext.Database.SqlQueryRaw<UserOrgPermRow>(""" var orgRows = await dbContext.Database.SqlQueryRaw<UserOrgPermRow>($$"""
SELECT SELECT
p.`Id` AS `Id`, p.`Id` AS `Id`,
p.`OrganizationId` AS `OrganizationId`, p.`OrganizationId` AS `OrganizationId`,
@ -44,11 +45,11 @@ public class DataPermStore(FakeRbacDbContext dbContext, DataPermContextCache cac
p.`ExpireTime` AS `ExpireTime`, p.`ExpireTime` AS `ExpireTime`,
p.`IsTemporary` AS `IsTemporary` p.`IsTemporary` AS `IsTemporary`
FROM `user_org_perm` p FROM `user_org_perm` p
LEFT JOIN `organization` o ON o.`Id` = p.`OrganizationId` AND o.`IsDeleted` = 0 LEFT JOIN `{{FakeTables.Organization}}` o ON o.`Id` = p.`OrganizationId` AND o.`IsDeleted` = 0
WHERE p.`IsDeleted` = 0 AND p.`UserId` = {0} WHERE p.`IsDeleted` = 0 AND p.`UserId` = {0}
""", userId.ToString()).ToListAsync(cancellationToken); """, userId.ToString()).ToListAsync(cancellationToken);
var scopeRows = await dbContext.Database.SqlQueryRaw<UserDataScopeRow>(""" var scopeRows = await dbContext.Database.SqlQueryRaw<UserDataScopeRow>($"""
SELECT p.`Id` AS `Id`, p.`DataScope` AS `DataScope`, p.`DataResourceCode` AS `DataResourceCode`, p.`ExpireTime` AS `ExpireTime` SELECT p.`Id` AS `Id`, p.`DataScope` AS `DataScope`, p.`DataResourceCode` AS `DataResourceCode`, p.`ExpireTime` AS `ExpireTime`
FROM `user_data_scope` p FROM `user_data_scope` p
WHERE p.`IsDeleted` = 0 AND p.`UserId` = {0} WHERE p.`IsDeleted` = 0 AND p.`UserId` = {0}
@ -129,16 +130,16 @@ public class DataPermStore(FakeRbacDbContext dbContext, DataPermContextCache cac
{ {
var role = await dbContext.Database.SqlQueryRaw<RoleHeaderRow>(""" var role = await dbContext.Database.SqlQueryRaw<RoleHeaderRow>("""
SELECT r.`Id` AS `RoleId`, r.`Name` AS `RoleName` SELECT r.`Id` AS `RoleId`, r.`Name` AS `RoleName`
FROM `role` r FROM `{{FakeTables.Role}}` r
WHERE r.`Id` = {0} AND r.`IsDeleted` = 0 WHERE r.`Id` = {0} AND r.`IsDeleted` = 0
LIMIT 1 LIMIT 1
""", roleId.ToString()).FirstOrDefaultAsync(cancellationToken) """, roleId.ToString()).FirstOrDefaultAsync(cancellationToken)
?? throw new BusinessException("角色不存在"); ?? throw new BusinessException("角色不存在");
var orgRows = await dbContext.Database.SqlQueryRaw<RoleOrgPermRow>(""" var orgRows = await dbContext.Database.SqlQueryRaw<RoleOrgPermRow>($$"""
SELECT p.`Id` AS `Id`, p.`OrganizationId` AS `OrganizationId`, o.`Name` AS `OrganizationName`, p.`OrgScope` AS `OrgScope` SELECT p.`Id` AS `Id`, p.`OrganizationId` AS `OrganizationId`, o.`Name` AS `OrganizationName`, p.`OrgScope` AS `OrgScope`
FROM `role_org_perm` p FROM `role_org_perm` p
LEFT JOIN `organization` o ON o.`Id` = p.`OrganizationId` AND o.`IsDeleted` = 0 LEFT JOIN `{{FakeTables.Organization}}` o ON o.`Id` = p.`OrganizationId` AND o.`IsDeleted` = 0
WHERE p.`IsDeleted` = 0 AND p.`RoleId` = {0} WHERE p.`IsDeleted` = 0 AND p.`RoleId` = {0}
""", roleId.ToString()).ToListAsync(cancellationToken); """, roleId.ToString()).ToListAsync(cancellationToken);
@ -520,7 +521,7 @@ public class DataPermStore(FakeRbacDbContext dbContext, DataPermContextCache cac
public async Task<List<FieldMaskItemDto>> GetFieldMasksAsync(string dataResourceCode, CancellationToken cancellationToken = default) public async Task<List<FieldMaskItemDto>> GetFieldMasksAsync(string dataResourceCode, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $"""
SELECT m.`FieldName` AS `FieldName`, m.`SecretLevel` AS `SecretLevel`, m.`MaskType` AS `MaskType`, m.`MaskPattern` AS `MaskPattern` SELECT m.`FieldName` AS `FieldName`, m.`SecretLevel` AS `SecretLevel`, m.`MaskType` AS `MaskType`, m.`MaskPattern` AS `MaskPattern`
FROM `data_perm_field_mask` m FROM `data_perm_field_mask` m
WHERE m.`IsDeleted` = 0 AND m.`IsEnabled` = 1 AND m.`DataResourceCode` = {0} WHERE m.`IsDeleted` = 0 AND m.`IsEnabled` = 1 AND m.`DataResourceCode` = {0}
@ -640,7 +641,7 @@ public class DataPermStore(FakeRbacDbContext dbContext, DataPermContextCache cac
public async Task<DataPermTemplateDetailDto> GetTemplateDetailAsync(Guid templateId, CancellationToken cancellationToken = default) public async Task<DataPermTemplateDetailDto> GetTemplateDetailAsync(Guid templateId, CancellationToken cancellationToken = default)
{ {
const string headerSql = """ var headerSql = $"""
SELECT t.`Id` AS `Id`, t.`Name` AS `Name`, t.`Code` AS `Code`, t.`Description` AS `Description`, SELECT t.`Id` AS `Id`, t.`Name` AS `Name`, t.`Code` AS `Code`, t.`Description` AS `Description`,
t.`RoleCode` AS `RoleCode`, t.`IsBuiltIn` AS `IsBuiltIn` t.`RoleCode` AS `RoleCode`, t.`IsBuiltIn` AS `IsBuiltIn`
FROM `data_perm_template` t FROM `data_perm_template` t
@ -652,7 +653,7 @@ public class DataPermStore(FakeRbacDbContext dbContext, DataPermContextCache cac
.FirstOrDefaultAsync(cancellationToken) .FirstOrDefaultAsync(cancellationToken)
?? throw new BusinessException("模板不存在"); ?? throw new BusinessException("模板不存在");
const string itemSql = """ var itemSql = $"""
SELECT i.`Id` AS `Id`, i.`DimensionType` AS `DimensionType`, CAST(i.`ItemContent` AS CHAR) AS `ItemContentJson`, i.`SortOrder` AS `SortOrder` SELECT i.`Id` AS `Id`, i.`DimensionType` AS `DimensionType`, CAST(i.`ItemContent` AS CHAR) AS `ItemContentJson`, i.`SortOrder` AS `SortOrder`
FROM `data_perm_template_item` i FROM `data_perm_template_item` i
WHERE i.`TemplateId` = {0} AND i.`IsDeleted` = 0 WHERE i.`TemplateId` = {0} AND i.`IsDeleted` = 0

View File

@ -1,5 +1,6 @@
using Fake.DependencyInjection; using Fake.DependencyInjection;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Oms.Application.Dtos.Customers; using Oms.Application.Dtos.Customers;
@ -41,18 +42,17 @@ public class CustomerServiceAdvisorResolver(FakeRbacDbContext dbContext) : IScop
return null; return null;
} }
const string sql = """ var sql = $$"""
SELECT SELECT
u.`Name` AS `Name`, u.`Name` AS `Name`,
u.`WechatId` AS `WechatId`, u.`WechatId` AS `WechatId`,
u.`WechatQrCode` AS `WechatQrCode`, u.`WechatQrCode` AS `WechatQrCode`,
u.`WecomKfUrl` AS `WecomKfUrl` u.`WecomKfUrl` AS `WecomKfUrl`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`IsDeleted` = 0 WHERE u.`IsDeleted` = 0
AND u.`userid` = {0} AND u.`userid` = {0}
LIMIT 1 LIMIT 1
"""; """;
return await dbContext.Database return await dbContext.Database
.SqlQueryRaw<AdvisorRow>(sql, userId) .SqlQueryRaw<AdvisorRow>(sql, userId)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
@ -60,13 +60,13 @@ public class CustomerServiceAdvisorResolver(FakeRbacDbContext dbContext) : IScop
private async Task<AdvisorRow?> FindByNameOrAccountAsync(string key, CancellationToken cancellationToken) private async Task<AdvisorRow?> FindByNameOrAccountAsync(string key, CancellationToken cancellationToken)
{ {
const string sql = """ var sql = $$"""
SELECT SELECT
u.`Name` AS `Name`, u.`Name` AS `Name`,
u.`WechatId` AS `WechatId`, u.`WechatId` AS `WechatId`,
u.`WechatQrCode` AS `WechatQrCode`, u.`WechatQrCode` AS `WechatQrCode`,
u.`WecomKfUrl` AS `WecomKfUrl` u.`WecomKfUrl` AS `WecomKfUrl`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`IsDeleted` = 0 WHERE u.`IsDeleted` = 0
AND (u.`Name` = {0} OR u.`Account` = {0}) AND (u.`Name` = {0} OR u.`Account` = {0})
ORDER BY u.`CreateTime` ASC ORDER BY u.`CreateTime` ASC

View File

@ -1,5 +1,6 @@
using Fake.DependencyInjection; using Fake.DependencyInjection;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Oms.Application.Dtos.Rbac; using Oms.Application.Dtos.Rbac;
@ -7,20 +8,19 @@ namespace Oms.Application.Rbac;
public class InternalUserRepository(FakeRbacDbContext dbContext) : IScopedDependency public class InternalUserRepository(FakeRbacDbContext dbContext) : IScopedDependency
{ {
private const string InternalUserFilter = """ private static readonly string InternalUserFilter = $$"""
( (
NOT EXISTS (SELECT 1 FROM `user_role` ur0 WHERE ur0.`UserId` = u.`Id`) NOT EXISTS (SELECT 1 FROM `{{FakeTables.UserRole}}` ur0 WHERE ur0.`UserId` = u.`Id`)
OR EXISTS ( OR EXISTS (
SELECT 1 SELECT 1
FROM `user_role` ur FROM `{{FakeTables.UserRole}}` ur
INNER JOIN `role` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0 INNER JOIN `{{FakeTables.Role}}` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0
WHERE ur.`UserId` = u.`Id` WHERE ur.`UserId` = u.`Id`
AND r.`Code` <> 'Customer' AND r.`Code` <> 'Customer'
AND r.`Code` NOT LIKE 'CustomerSub\_%' AND r.`Code` NOT LIKE 'CustomerSub\_%'
) )
) )
"""; """;
public async Task<InternalUserListResultDto> GetInternalUsersAsync( public async Task<InternalUserListResultDto> GetInternalUsersAsync(
string? keyword, string? keyword,
Guid? organizationId, Guid? organizationId,
@ -45,16 +45,16 @@ public class InternalUserRepository(FakeRbacDbContext dbContext) : IScopedDepend
args.Add(organizationId.Value.ToString()); args.Add(organizationId.Value.ToString());
} }
var countSql = $""" var countSql = $$"""
SELECT COUNT(*) AS `Value` SELECT COUNT(*) AS `Value`
FROM `user` u FROM `{{FakeTables.User}}` u
{where} {where}
"""; """;
var total = await dbContext.Database.SqlQueryRaw<long>(countSql, args.ToArray()) var total = await dbContext.Database.SqlQueryRaw<long>(countSql, args.ToArray())
.FirstAsync(cancellationToken); .FirstAsync(cancellationToken);
var offset = Math.Max(page - 1, 0) * pageSize; var offset = Math.Max(page - 1, 0) * pageSize;
var listSql = $""" var listSql = $$"""
SELECT SELECT
u.`Id` AS `Id`, u.`Id` AS `Id`,
u.`Name` AS `Name`, u.`Name` AS `Name`,
@ -67,13 +67,12 @@ public class InternalUserRepository(FakeRbacDbContext dbContext) : IScopedDepend
u.`OrganizationId` AS `OrganizationId`, u.`OrganizationId` AS `OrganizationId`,
o.`Name` AS `OrganizationName`, o.`Name` AS `OrganizationName`,
u.`CreateTime` AS `CreatedAt` u.`CreateTime` AS `CreatedAt`
FROM `user` u FROM `{{FakeTables.User}}` u
LEFT JOIN `organization` o ON o.`Id` = u.`OrganizationId` AND o.`IsDeleted` = 0 LEFT JOIN `{{FakeTables.Organization}}` o ON o.`Id` = u.`OrganizationId` AND o.`IsDeleted` = 0
{where} {where}
ORDER BY u.`CreateTime` DESC ORDER BY u.`CreateTime` DESC
LIMIT {pageSize} OFFSET {offset} LIMIT {pageSize} OFFSET {offset}
"""; """;
var rows = await dbContext.Database.SqlQueryRaw<InternalUserRow>(listSql, args.ToArray()) var rows = await dbContext.Database.SqlQueryRaw<InternalUserRow>(listSql, args.ToArray())
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
if (rows.Count == 0) if (rows.Count == 0)
@ -112,14 +111,14 @@ public class InternalUserRepository(FakeRbacDbContext dbContext) : IScopedDepend
} }
var inClause = string.Join(",", userIds.Select(x => $"'{x}'")); var inClause = string.Join(",", userIds.Select(x => $"'{x}'"));
var sql = $""" var sql = $$"""
SELECT SELECT
ur.`UserId` AS `UserId`, ur.`UserId` AS `UserId`,
r.`Id` AS `RoleId`, r.`Id` AS `RoleId`,
r.`Name` AS `RoleName`, r.`Name` AS `RoleName`,
r.`Code` AS `RoleCode` r.`Code` AS `RoleCode`
FROM `user_role` ur FROM `{{FakeTables.UserRole}}` ur
INNER JOIN `role` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0 INNER JOIN `{{FakeTables.Role}}` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0
WHERE ur.`UserId` IN ({inClause}) WHERE ur.`UserId` IN ({inClause})
ORDER BY r.`Name` ORDER BY r.`Name`
"""; """;

View File

@ -1,6 +1,7 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Fake.DependencyInjection; using Fake.DependencyInjection;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Oms.Application.Dtos.Rbac; using Oms.Application.Dtos.Rbac;
@ -15,18 +16,17 @@ public class UserProfileRepository(FakeRbacDbContext dbContext) : IScopedDepende
public async Task<UserProfileData?> GetAsync(Guid userId, CancellationToken cancellationToken = default) public async Task<UserProfileData?> GetAsync(Guid userId, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
SELECT SELECT
u.`Phone` AS `Phone`, u.`Phone` AS `Phone`,
u.`EmployeeNo` AS `EmployeeNo`, u.`EmployeeNo` AS `EmployeeNo`,
u.`WechatId` AS `WechatId`, u.`WechatId` AS `WechatId`,
u.`WechatQrCode` AS `WechatQrCode`, u.`WechatQrCode` AS `WechatQrCode`,
u.`WecomKfUrl` AS `WecomKfUrl` u.`WecomKfUrl` AS `WecomKfUrl`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`Id` = {0} AND u.`IsDeleted` = 0 WHERE u.`Id` = {0} AND u.`IsDeleted` = 0
LIMIT 1 LIMIT 1
"""; """;
var row = await dbContext.Database var row = await dbContext.Database
.SqlQueryRaw<UserProfileRow>(sql, userId.ToString()) .SqlQueryRaw<UserProfileRow>(sql, userId.ToString())
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
@ -56,7 +56,7 @@ public class UserProfileRepository(FakeRbacDbContext dbContext) : IScopedDepende
} }
var inClause = string.Join(",", userIds.Select(x => $"'{x}'")); var inClause = string.Join(",", userIds.Select(x => $"'{x}'"));
var sql = $""" var sql = $$"""
SELECT SELECT
u.`Id` AS `UserId`, u.`Id` AS `UserId`,
u.`Phone` AS `Phone`, u.`Phone` AS `Phone`,
@ -64,7 +64,7 @@ public class UserProfileRepository(FakeRbacDbContext dbContext) : IScopedDepende
u.`WechatId` AS `WechatId`, u.`WechatId` AS `WechatId`,
u.`WechatQrCode` AS `WechatQrCode`, u.`WechatQrCode` AS `WechatQrCode`,
u.`WecomKfUrl` AS `WecomKfUrl` u.`WecomKfUrl` AS `WecomKfUrl`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`Id` IN ({inClause}) AND u.`IsDeleted` = 0 WHERE u.`Id` IN ({inClause}) AND u.`IsDeleted` = 0
"""; """;
@ -83,8 +83,8 @@ public class UserProfileRepository(FakeRbacDbContext dbContext) : IScopedDepende
public async Task SaveAsync(Guid userId, UserProfileData profile, CancellationToken cancellationToken = default) public async Task SaveAsync(Guid userId, UserProfileData profile, CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
UPDATE `user` UPDATE `{{FakeTables.User}}`
SET SET
`Phone` = {1}, `Phone` = {1},
`EmployeeNo` = {2}, `EmployeeNo` = {2},
@ -93,7 +93,6 @@ public class UserProfileRepository(FakeRbacDbContext dbContext) : IScopedDepende
`WecomKfUrl` = {5} `WecomKfUrl` = {5}
WHERE `Id` = {0} AND `IsDeleted` = 0 WHERE `Id` = {0} AND `IsDeleted` = 0
"""; """;
await dbContext.Database.ExecuteSqlRawAsync( await dbContext.Database.ExecuteSqlRawAsync(
sql, sql,
[ [
@ -113,8 +112,8 @@ public class UserProfileRepository(FakeRbacDbContext dbContext) : IScopedDepende
UserProfileData profile, UserProfileData profile,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
UPDATE `user` UPDATE `{{FakeTables.User}}`
SET SET
`Phone` = {1}, `Phone` = {1},
`WechatId` = {2}, `WechatId` = {2},
@ -142,16 +141,16 @@ public class UserProfileRepository(FakeRbacDbContext dbContext) : IScopedDepende
{ {
var normalized = employeeNo.Trim().ToUpperInvariant(); var normalized = employeeNo.Trim().ToUpperInvariant();
var sql = excludeUserId.HasValue var sql = excludeUserId.HasValue
? """ ? $$"""
SELECT COUNT(*) AS `Value` SELECT COUNT(*) AS `Value`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`IsDeleted` = 0 WHERE u.`IsDeleted` = 0
AND u.`EmployeeNo` = {0} AND u.`EmployeeNo` = {0}
AND u.`Id` <> {1} AND u.`Id` <> {1}
""" """
: """ : $$"""
SELECT COUNT(*) AS `Value` SELECT COUNT(*) AS `Value`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`IsDeleted` = 0 WHERE u.`IsDeleted` = 0
AND u.`EmployeeNo` = {0} AND u.`EmployeeNo` = {0}
"""; """;
@ -166,9 +165,9 @@ public class UserProfileRepository(FakeRbacDbContext dbContext) : IScopedDepende
public async Task<string> GenerateNextEmployeeNoAsync(CancellationToken cancellationToken = default) public async Task<string> GenerateNextEmployeeNoAsync(CancellationToken cancellationToken = default)
{ {
const string sql = """ var sql = $$"""
SELECT u.`EmployeeNo` AS `EmployeeNo` SELECT u.`EmployeeNo` AS `EmployeeNo`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`IsDeleted` = 0 WHERE u.`IsDeleted` = 0
AND u.`EmployeeNo` IS NOT NULL AND u.`EmployeeNo` IS NOT NULL
AND u.`EmployeeNo` <> '' AND u.`EmployeeNo` <> ''

View File

@ -5,6 +5,7 @@ using Fake.Rbac.Application.Dtos.User;
using Fake.Rbac.Application.Services; using Fake.Rbac.Application.Services;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.Security.Claims; using Fake.Security.Claims;
using Fake.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Oms.Application.Dtos.Customers; using Oms.Application.Dtos.Customers;
@ -203,8 +204,9 @@ public class CustomerService : ApplicationService
// 同步 RBAC 登录邮箱,覆盖注册时的 @noemail.local 占位 // 同步 RBAC 登录邮箱,覆盖注册时的 @noemail.local 占位
try try
{ {
await RbacDbContext.Database.ExecuteSqlInterpolatedAsync( await RbacDbContext.Database.ExecuteSqlRawAsync(
$"UPDATE `user` SET `Email` = {email} WHERE `userid` = {customer.UserId} AND `IsDeleted` = 0"); $"UPDATE `{FakeTables.User}` SET `Email` = {{0}} WHERE `userid` = {{1}} AND `IsDeleted` = 0",
email, customer.UserId);
} }
catch catch
{ {
@ -390,8 +392,8 @@ public class CustomerService : ApplicationService
private async Task<Guid?> GetRbacUserGuidByMapIdAsync(int mapId) private async Task<Guid?> GetRbacUserGuidByMapIdAsync(int mapId)
{ {
return await RbacDbContext.Database.SqlQuery<Guid?>( return await RbacDbContext.Database.SqlQueryRaw<Guid?>(
$"select `Id` as `Value` from `user` where `userid` = {mapId} and `IsDeleted` = 0") $"select `Id` as `Value` from `{FakeTables.User}` where `userid` = {{0}} and `IsDeleted` = 0", mapId)
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
} }
@ -495,8 +497,8 @@ public class CustomerService : ApplicationService
private async Task<int> GetRbacUserMapIdOrThrowAsync(Guid id) private async Task<int> GetRbacUserMapIdOrThrowAsync(Guid id)
{ {
var userId = await RbacDbContext.Database.SqlQuery<int?>( var userId = await RbacDbContext.Database.SqlQueryRaw<int?>(
$"select `userid` as `Value` from `user` where `Id` = {id}") $"select `userid` as `Value` from `{FakeTables.User}` where `Id` = {{0}}", id)
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
if (!userId.HasValue || userId.Value <= 0) if (!userId.HasValue || userId.Value <= 0)
{ {

View File

@ -1,6 +1,7 @@
using System.Linq.Expressions; using System.Linq.Expressions;
using Fake.Application; using Fake.Application;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Oms.Application.Dtos.Shared; using Oms.Application.Dtos.Shared;
@ -24,20 +25,19 @@ public class NoticeService : ApplicationService
private const string ReceiverTypeUser = "user"; private const string ReceiverTypeUser = "user";
private const string ReceiverTypeCustomer = "customer"; private const string ReceiverTypeCustomer = "customer";
private const string InternalUserFilter = """ private static readonly string InternalUserFilter = $$"""
( (
NOT EXISTS (SELECT 1 FROM `user_role` ur0 WHERE ur0.`UserId` = u.`Id`) NOT EXISTS (SELECT 1 FROM `{{FakeTables.UserRole}}` ur0 WHERE ur0.`UserId` = u.`Id`)
OR EXISTS ( OR EXISTS (
SELECT 1 SELECT 1
FROM `user_role` ur FROM `{{FakeTables.UserRole}}` ur
INNER JOIN `role` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0 INNER JOIN `{{FakeTables.Role}}` r ON r.`Id` = ur.`RoleId` AND r.`IsDeleted` = 0
WHERE ur.`UserId` = u.`Id` WHERE ur.`UserId` = u.`Id`
AND r.`Code` <> 'Customer' AND r.`Code` <> 'Customer'
AND r.`Code` NOT LIKE 'CustomerSub\_%' AND r.`Code` NOT LIKE 'CustomerSub\_%'
) )
) )
"""; """;
private INoticeRepository NoticeRepository => LazyServiceProvider.GetRequiredService<INoticeRepository>(); private INoticeRepository NoticeRepository => LazyServiceProvider.GetRequiredService<INoticeRepository>();
private INoticeReadRepository NoticeReadRepository => LazyServiceProvider.GetRequiredService<INoticeReadRepository>(); private INoticeReadRepository NoticeReadRepository => LazyServiceProvider.GetRequiredService<INoticeReadRepository>();
private ICustomerRepository CustomerRepository => LazyServiceProvider.GetRequiredService<ICustomerRepository>(); private ICustomerRepository CustomerRepository => LazyServiceProvider.GetRequiredService<ICustomerRepository>();
@ -239,15 +239,15 @@ public class NoticeService : ApplicationService
args.Add(pattern); args.Add(pattern);
} }
var countSql = $"SELECT COUNT(*) AS `Value` FROM `user` u {where}"; var countSql = $"SELECT COUNT(*) AS `Value` FROM `{{FakeTables.User}}` u {where}";
var total = await RbacDbContext.Database.SqlQueryRaw<long>(countSql, args.ToArray()).FirstAsync(); var total = await RbacDbContext.Database.SqlQueryRaw<long>(countSql, args.ToArray()).FirstAsync();
var offset = Math.Max(pageIndex - 1, 0) * pageSize; var offset = Math.Max(pageIndex - 1, 0) * pageSize;
var listSql = $""" var listSql = $$"""
SELECT SELECT
u.`userid` AS `Value`, u.`userid` AS `Value`,
u.`Name` AS `Label`, u.`Name` AS `Label`,
u.`Account` AS `Extra` u.`Account` AS `Extra`
FROM `user` u FROM `{{FakeTables.User}}` u
{where} {where}
ORDER BY u.`Name`, u.`userid` ORDER BY u.`Name`, u.`userid`
LIMIT {pageSize} OFFSET {offset} LIMIT {pageSize} OFFSET {offset}
@ -465,7 +465,7 @@ public class NoticeService : ApplicationService
return []; return [];
} }
var sql = $"SELECT `userid` AS `UserId` FROM `user` WHERE `IsDeleted` = 0 AND `userid` IN ({string.Join(",", userIds)})"; var sql = $"SELECT `userid` AS `UserId` FROM `{{FakeTables.User}}` WHERE `IsDeleted` = 0 AND `userid` IN ({string.Join(",", userIds)})";
return await RbacDbContext.Database.SqlQueryRaw<UserIdQueryResult>(sql) return await RbacDbContext.Database.SqlQueryRaw<UserIdQueryResult>(sql)
.Select(x => x.UserId) .Select(x => x.UserId)
.ToListAsync(); .ToListAsync();

View File

@ -2,6 +2,7 @@ using System.Net.Mail;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Fake.Application; using Fake.Application;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Oms.Application.Shared; namespace Oms.Application.Shared;
@ -167,13 +168,13 @@ public static class OmsRbacRegistrationGuard
} }
var hit = await rbacDbContext.Database var hit = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$""" $$"""
SELECT 1 AS Value SELECT 1 AS Value
FROM `organization` FROM `{{FakeTables.Organization}}`
WHERE `Id` = {organizationId.Value.ToString()} AND `IsDeleted` = 0 WHERE `Id` = {0} AND `IsDeleted` = 0
LIMIT 1 LIMIT 1
""") """, organizationId.Value.ToString())
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
if (!hit.HasValue) if (!hit.HasValue)
@ -201,13 +202,13 @@ public static class OmsRbacRegistrationGuard
foreach (var roleId in ids) foreach (var roleId in ids)
{ {
var hit = await rbacDbContext.Database var hit = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$""" $$"""
SELECT 1 AS Value SELECT 1 AS Value
FROM `role` FROM `{{FakeTables.Role}}`
WHERE `Id` = {roleId.ToString()} AND `IsDeleted` = 0 WHERE `Id` = {0} AND `IsDeleted` = 0
LIMIT 1 LIMIT 1
""") """, roleId.ToString())
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
if (!hit.HasValue) if (!hit.HasValue)
@ -492,15 +493,16 @@ public static class OmsRbacRegistrationGuard
if (excludeUserId is Guid id && id != Guid.Empty) if (excludeUserId is Guid id && id != Guid.Empty)
{ {
var hit = await rbacDbContext.Database var hit = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$"SELECT 1 AS Value FROM `user` WHERE `Account` = {account} AND `Id` <> {id.ToString()} LIMIT 1") $"SELECT 1 AS Value FROM `{{FakeTables.User}}` WHERE `Account` = {0} AND `Id` <> {1} LIMIT 1",
account, id.ToString())
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
return hit.HasValue; return hit.HasValue;
} }
var any = await rbacDbContext.Database var any = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$"SELECT 1 AS Value FROM `user` WHERE `Account` = {account} LIMIT 1") $"SELECT 1 AS Value FROM `{{FakeTables.User}}` WHERE `Account` = {0} LIMIT 1", account)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
return any.HasValue; return any.HasValue;
} }
@ -514,15 +516,16 @@ public static class OmsRbacRegistrationGuard
if (excludeUserId is Guid id && id != Guid.Empty) if (excludeUserId is Guid id && id != Guid.Empty)
{ {
var hit = await rbacDbContext.Database var hit = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$"SELECT 1 AS Value FROM `user` WHERE `Email` = {email} AND `Id` <> {id.ToString()} LIMIT 1") $"SELECT 1 AS Value FROM `{{FakeTables.User}}` WHERE `Email` = {0} AND `Id` <> {1} LIMIT 1",
email, id.ToString())
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
return hit.HasValue; return hit.HasValue;
} }
var any = await rbacDbContext.Database var any = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$"SELECT 1 AS Value FROM `user` WHERE `Email` = {email} LIMIT 1") $"SELECT 1 AS Value FROM `{{FakeTables.User}}` WHERE `Email` = {0} LIMIT 1", email)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
return any.HasValue; return any.HasValue;
} }
@ -536,15 +539,16 @@ public static class OmsRbacRegistrationGuard
if (excludeUserId is Guid id && id != Guid.Empty) if (excludeUserId is Guid id && id != Guid.Empty)
{ {
var hit = await rbacDbContext.Database var hit = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$"SELECT 1 AS Value FROM `user` WHERE `Phone` = {phone} AND `Id` <> {id.ToString()} LIMIT 1") $"SELECT 1 AS Value FROM `{{FakeTables.User}}` WHERE `Phone` = {0} AND `Id` <> {1} LIMIT 1",
phone, id.ToString())
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
return hit.HasValue; return hit.HasValue;
} }
var any = await rbacDbContext.Database var any = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$"SELECT 1 AS Value FROM `user` WHERE `Phone` = {phone} LIMIT 1") $"SELECT 1 AS Value FROM `{{FakeTables.User}}` WHERE `Phone` = {0} LIMIT 1", phone)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
return any.HasValue; return any.HasValue;
} }
@ -558,15 +562,16 @@ public static class OmsRbacRegistrationGuard
if (excludeUserId is Guid id && id != Guid.Empty) if (excludeUserId is Guid id && id != Guid.Empty)
{ {
var hit = await rbacDbContext.Database var hit = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$"SELECT 1 AS Value FROM `user` WHERE `WechatId` = {wechatId} AND `Id` <> {id.ToString()} LIMIT 1") $"SELECT 1 AS Value FROM `{{FakeTables.User}}` WHERE `WechatId` = {0} AND `Id` <> {1} LIMIT 1",
wechatId, id.ToString())
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
return hit.HasValue; return hit.HasValue;
} }
var any = await rbacDbContext.Database var any = await rbacDbContext.Database
.SqlQuery<int?>( .SqlQueryRaw<int?>(
$"SELECT 1 AS Value FROM `user` WHERE `WechatId` = {wechatId} LIMIT 1") $"SELECT 1 AS Value FROM `{{FakeTables.User}}` WHERE `WechatId` = {0} LIMIT 1", wechatId)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
return any.HasValue; return any.HasValue;
} }

View File

@ -1,5 +1,6 @@
using Fake.DependencyInjection; using Fake.DependencyInjection;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Oms.Application.Shared; namespace Oms.Application.Shared;
@ -40,7 +41,7 @@ public class OmsRbacUserDisplayNameResolver : IScopedDependency
try try
{ {
var sql = var sql =
$"SELECT `userid` AS `{nameof(RbacUserNameRow.UserId)}`, `Name` AS `{nameof(RbacUserNameRow.DisplayName)}` FROM `user` WHERE `IsDeleted` = 0 AND `userid` IN ({string.Join(",", ids)})"; $"SELECT `userid` AS `{nameof(RbacUserNameRow.UserId)}`, `Name` AS `{nameof(RbacUserNameRow.DisplayName)}` FROM `{FakeTables.User}` WHERE `IsDeleted` = 0 AND `userid` IN ({string.Join(",", ids)})";
var rows = await _rbacDbContext.Database var rows = await _rbacDbContext.Database
.SqlQueryRaw<RbacUserNameRow>(sql) .SqlQueryRaw<RbacUserNameRow>(sql)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);

View File

@ -1,4 +1,5 @@
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Oms.Domain.CustomersAggregate.Entities; using Oms.Domain.CustomersAggregate.Entities;
@ -44,9 +45,9 @@ public static class StaffAdvisorBinder
string key, string key,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
const string sql = """ var sql = $$"""
SELECT u.`Account` AS `Account` SELECT u.`Account` AS `Account`
FROM `user` u FROM `{{FakeTables.User}}` u
WHERE u.`IsDeleted` = 0 WHERE u.`IsDeleted` = 0
AND u.`Account` = {0} AND u.`Account` = {0}
ORDER BY u.`CreateTime` ASC ORDER BY u.`CreateTime` ASC

View File

@ -12,6 +12,7 @@ using Fake.Rbac.Domain.UserAggregate;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.Security.Claims; using Fake.Security.Claims;
using Fake.Users; using Fake.Users;
using Fake.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -904,15 +905,15 @@ public partial class AuthService(
private async Task<Guid?> GetRbacUserGuidByMapIdAsync(int userId, CancellationToken cancellationToken) private async Task<Guid?> GetRbacUserGuidByMapIdAsync(int userId, CancellationToken cancellationToken)
{ {
return await rbacDbContext.Database.SqlQuery<Guid?>( return await rbacDbContext.Database.SqlQueryRaw<Guid?>(
$"select `Id` as `Value` from `user` where `IsDeleted` = 0 and `userid` = {userId}") $"select `Id` as `Value` from `{FakeTables.User}` where `IsDeleted` = 0 and `userid` = {{0}}", userId)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
} }
private async Task<int?> GetMapIdByUserGuidAsync(Guid userId, CancellationToken cancellationToken) private async Task<int?> GetMapIdByUserGuidAsync(Guid userId, CancellationToken cancellationToken)
{ {
return await rbacDbContext.Database.SqlQuery<int?>( return await rbacDbContext.Database.SqlQueryRaw<int?>(
$"select `userid` as `Value` from `user` where `Id` = {userId}") $"select `userid` as `Value` from `{FakeTables.User}` where `Id` = {{0}}", userId)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
} }

View File

@ -187,7 +187,7 @@ public class SimpleAdminApiModule : FakeModule
// 路由 - 必须在认证和授权之前 // 路由 - 必须在认证和授权之前
app.UseRouting(); app.UseRouting();
// EventBus Dashboard(CAP 风格;也可依赖 IStartupFilter 自动挂载,这里显式保证顺序) // EventBus Dashboard:必须在 UsePathBase 之后,否则 /admin/eventbus 会 404
app.UseFakeEventBusDashboard(); app.UseFakeEventBusDashboard();
// OpenAPI 验签需读取原始 Body,允许二次读流 // OpenAPI 验签需读取原始 Body,允许二次读流

View File

@ -5,6 +5,7 @@ using Fake.Rbac.Application.Jwt;
using Fake.Rbac.Application.Services; using Fake.Rbac.Application.Services;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.Security.Claims; using Fake.Security.Claims;
using Fake.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -21,8 +22,8 @@ public class SimpleJwtService(IOptions<FakeJwtOptions> jwtOptions, UserService u
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var claims = await base.GenerateClaimsByUserIdAsync(userId, cancellationToken); var claims = await base.GenerateClaimsByUserIdAsync(userId, cancellationToken);
var mapId = await rbacDbContext.Database.SqlQuery<int?>( var mapId = await rbacDbContext.Database.SqlQueryRaw<int?>(
$"select `userid` as `Value` from `user` where `Id` = {userId}") $"select `userid` as `Value` from `{FakeTables.User}` where `Id` = {{0}}", userId)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
if (mapId.HasValue) if (mapId.HasValue)
{ {

View File

@ -4,6 +4,7 @@ using Fake.Rbac.Application.Dtos.Role;
using Fake.Rbac.Application.Dtos.User; using Fake.Rbac.Application.Dtos.User;
using Fake.Rbac.Application.Services; using Fake.Rbac.Application.Services;
using Fake.Rbac.Infrastructure; using Fake.Rbac.Infrastructure;
using Fake.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -323,8 +324,8 @@ public class WeChatCustomerService(
private async Task<int> GetRbacUserMapIdOrThrowAsync(Guid id, CancellationToken cancellationToken) private async Task<int> GetRbacUserMapIdOrThrowAsync(Guid id, CancellationToken cancellationToken)
{ {
var userId = await rbacDbContext.Database.SqlQuery<int?>( var userId = await rbacDbContext.Database.SqlQueryRaw<int?>(
$"select `userid` as `Value` from `user` where `Id` = {id}") $"select `userid` as `Value` from `{FakeTables.User}` where `Id` = {{0}}", id)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
if (!userId.HasValue || userId.Value <= 0) if (!userId.HasValue || userId.Value <= 0)
{ {