using Fake.DependencyInjection; using Oms.Domain.CustomersAggregate.Entities; using Oms.Domain.CustomersAggregate.Repositories; using Oms.SqlSugarCore.Repositories; namespace Oms.SqlSugarCore.Customers; public class CustomerRepository : OmsSqlSugarRepository, ICustomerRepository, IScopedDependency { public Task GetByCustomerCodeAsync(string customerCode, CancellationToken cancellationToken = default) { return FirstOrDefaultAsync(x => x.CustomerCode == customerCode, cancellationToken); } public async Task GetByCustomerCodeIncludingDeletedAsync( string customerCode, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); return await SoftDeleteQuery.FirstIncludingDeletedAsync( db, x => x.CustomerCode == customerCode, cancellationToken); } public Task GetByPhoneAsync(string phone, CancellationToken cancellationToken = default) { return FirstOrDefaultAsync(x => x.Phone == phone, cancellationToken); } public async Task GetByPhoneIncludingDeletedAsync( string phone, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); return await SoftDeleteQuery.FirstIncludingDeletedAsync( db, x => x.Phone == phone, cancellationToken); } public Task GetByEmailAsync(string email, CancellationToken cancellationToken = default) { return FirstOrDefaultAsync(x => x.Email == email, cancellationToken); } public async Task GetByEmailIncludingDeletedAsync( string email, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); return await SoftDeleteQuery.FirstIncludingDeletedAsync( db, x => x.Email == email, cancellationToken); } public Task GetByWechatOpenidAsync(string wechatOpenid, CancellationToken cancellationToken = default) { return FirstOrDefaultAsync(x => x.WechatOpenid == wechatOpenid, cancellationToken); } public Task ExistsPhoneAsync(string phone, CancellationToken cancellationToken = default) { return AnyAsync(x => x.Phone == phone, cancellationToken); } public async Task ExistsPhoneAnyAsync(string phone, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); var hit = await db.Ado.SqlQuerySingleAsync( "SELECT 1 FROM t_customer WHERE Phone = @phone LIMIT 1", new { phone }); return hit.HasValue; } public async Task ExistsEmailAnyAsync(string email, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); var hit = await db.Ado.SqlQuerySingleAsync( "SELECT 1 FROM t_customer WHERE Email = @email LIMIT 1", new { email }); return hit.HasValue; } public async Task ExistsWechatOpenidAnyAsync(string wechatOpenid, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); var hit = await db.Ado.SqlQuerySingleAsync( "SELECT 1 FROM t_customer WHERE WechatOpenid = @wechatOpenid LIMIT 1", new { wechatOpenid }); return hit.HasValue; } public async Task> ListCustomerCodesByPrefixAsync( string prefix, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(prefix)) { return []; } var minLength = prefix.Length + 3; var maxLength = prefix.Length + 4; var db = await GetDbContextAsync(cancellationToken); return await db.Queryable() .Where(x => x.CustomerCode.StartsWith(prefix) && x.CustomerCode.Length >= minLength && x.CustomerCode.Length <= maxLength) .Select(x => x.CustomerCode) .ToListAsync(cancellationToken); } public async Task<(IReadOnlyList<(int ReferrerId, int RefereeCount, DateTime? LatestBindTime)> Items, int Total)> GetReferrerSummariesPagedAsync( string? keyword, int pageIndex, int pageSize, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); var kw = string.IsNullOrWhiteSpace(keyword) ? null : keyword.Trim(); var query = db.Queryable() .Where(x => x.ReferrerCustomerId != null && x.ParentCustomerId == null) .GroupBy(x => x.ReferrerCustomerId) .Select(x => new { ReferrerId = x.ReferrerCustomerId!.Value, RefereeCount = SqlSugar.SqlFunc.AggregateCount(x.Id), LatestBindTime = SqlSugar.SqlFunc.AggregateMax(x.ReferralBindTime) }) .MergeTable() .InnerJoin((g, c) => g.ReferrerId == c.Id) .Where((g, c) => c.ParentCustomerId == null); if (kw != null) { query = query.Where((g, c) => c.CustomerCode.Contains(kw) || c.CustomerName.Contains(kw) || (c.Phone != null && c.Phone.Contains(kw))); } var total = await query.CountAsync(cancellationToken); var rows = await query .OrderByDescending((g, c) => g.LatestBindTime) .OrderByDescending((g, c) => g.RefereeCount) .Select((g, c) => new { g.ReferrerId, g.RefereeCount, g.LatestBindTime }) .ToPageListAsync(pageIndex, pageSize, cancellationToken); return (rows.Select(x => (x.ReferrerId, x.RefereeCount, x.LatestBindTime)).ToList(), total); } }