using FreeSql; using MAX.Core.DependencyInjection; using Store.Users.Entities; namespace Store.Users.Repositories; public interface IUserRepository : IBaseRepository { /// 获取用户及从主账号继承的有效配置;用户不存在时返回空。 Task GetUser(string userName); /// 获取用户所属主账号(主账号返回自身);用户不存在时返回空,主账号无效时抛出业务异常。 Task GetMainUser(string userName); } /// 集中处理旧系统主子账号的有效计费配置和积分权限继承。 internal class UserRepository(IFreeSql freeSql) : BaseRepository(freeSql), IUserRepository, ITransientDependency { /// public async Task GetUser(string userName) { var user = await Where(u => u.UserName == userName).FirstAsync(); if (user == null || user.IsMainUser) return user; var parent = await GetMainUser(user); user.OemId = parent.OemId; // 子账号的计费配置完全跟随主账号,包括未配置的费用。 user.ShopPointsPerMonth = parent.ShopPointsPerMonth; user.PostagePerOrder = parent.PostagePerOrder; user.RefundPostagePerOrder = parent.RefundPostagePerOrder; user.GiftOemAmountPerOrder = parent.GiftOemAmountPerOrder; user.SyncLogisticsPerOrder = parent.SyncLogisticsPerOrder; var authority = (user.Authority ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToHashSet(); var parentAuthority = (parent.Authority ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); if (parentAuthority.Contains("积分用户")) authority.Add("积分用户"); else authority.Remove("积分用户"); user.Authority = string.Join(",", authority); return user; } /// public async Task GetMainUser(string userName) { var user = await Where(u => u.UserName == userName).FirstAsync(); return user == null ? null : await GetMainUser(user); } /// 统一校验主子账号关系,禁止从其他子账号继承配置。 private async Task GetMainUser(UserEntity user) { if (user.IsMainUser) return user; var parent = await Where(u => u.UserName == user.ParentUserName).FirstAsync(); if (parent == null || !parent.IsMainUser) throw new MAX.Framework.Commons.AppException("主账号不存在或账号关系无效,请联系管理员"); return parent; } }