admin/src/Oms.Application/Shared/StaffAdvisorBinder.cs
zzh eba1fa4867 feat: 注册绑定所属顾问与岗位职级字典种子
支持 AdvisorCode 写入 CustomerService=Account;补充 employee_position/employee_job_level 初始 SQL。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-03 12:45:13 +08:00

68 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Fake.Rbac.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Oms.Domain.CustomersAggregate.Entities;
namespace Oms.Application.Shared;
/// <summary>
/// 顾问邀请绑定:注册时把所属顾问(CustomerService)写成员工登录账号。
/// </summary>
public static class StaffAdvisorBinder
{
/// <summary>
/// 仅在 CustomerService 为空时绑定;advisorCode 为员工 Account(来自 ?advisor=)。
/// 无效账号静默跳过,不阻断注册。
/// </summary>
public static async Task BindAdvisorOnCreateAsync(
Customer entity,
string? advisorCode,
FakeRbacDbContext rbacDbContext,
CancellationToken cancellationToken = default)
{
if (!string.IsNullOrWhiteSpace(entity.CustomerService))
{
return;
}
var key = advisorCode?.Trim();
if (string.IsNullOrWhiteSpace(key))
{
return;
}
var account = await ResolveStaffAccountAsync(rbacDbContext, key, cancellationToken);
if (string.IsNullOrWhiteSpace(account))
{
return;
}
entity.CustomerService = account;
}
private static async Task<string?> ResolveStaffAccountAsync(
FakeRbacDbContext rbacDbContext,
string key,
CancellationToken cancellationToken)
{
const string sql = """
SELECT u.`Account` AS `Account`
FROM `user` u
WHERE u.`IsDeleted` = 0
AND u.`Account` = {0}
ORDER BY u.`CreateTime` ASC
LIMIT 1
""";
var row = await rbacDbContext.Database
.SqlQueryRaw<AccountRow>(sql, key)
.FirstOrDefaultAsync(cancellationToken);
return string.IsNullOrWhiteSpace(row?.Account) ? null : row.Account.Trim();
}
private sealed class AccountRow
{
public string? Account { get; set; }
}
}