All checks were successful
Build and Deploy / deploy (push) Successful in 4m34s
Co-authored-by: Cursor <cursoragent@cursor.com>
161 lines
6.0 KiB
C#
161 lines
6.0 KiB
C#
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<Customer>, ICustomerRepository, IScopedDependency
|
|
{
|
|
public Task<Customer?> GetByCustomerCodeAsync(string customerCode, CancellationToken cancellationToken = default)
|
|
{
|
|
return FirstOrDefaultAsync(x => x.CustomerCode == customerCode, cancellationToken);
|
|
}
|
|
|
|
public async Task<Customer?> GetByCustomerCodeIncludingDeletedAsync(
|
|
string customerCode,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var db = await GetDbContextAsync(cancellationToken);
|
|
return await SoftDeleteQuery.FirstIncludingDeletedAsync<Customer>(
|
|
db,
|
|
x => x.CustomerCode == customerCode,
|
|
cancellationToken);
|
|
}
|
|
|
|
public Task<Customer?> GetByPhoneAsync(string phone, CancellationToken cancellationToken = default)
|
|
{
|
|
return FirstOrDefaultAsync(x => x.Phone == phone, cancellationToken);
|
|
}
|
|
|
|
public async Task<Customer?> GetByPhoneIncludingDeletedAsync(
|
|
string phone,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var db = await GetDbContextAsync(cancellationToken);
|
|
return await SoftDeleteQuery.FirstIncludingDeletedAsync<Customer>(
|
|
db,
|
|
x => x.Phone == phone,
|
|
cancellationToken);
|
|
}
|
|
|
|
public Task<Customer?> GetByEmailAsync(string email, CancellationToken cancellationToken = default)
|
|
{
|
|
return FirstOrDefaultAsync(x => x.Email == email, cancellationToken);
|
|
}
|
|
|
|
public async Task<Customer?> GetByEmailIncludingDeletedAsync(
|
|
string email,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var db = await GetDbContextAsync(cancellationToken);
|
|
return await SoftDeleteQuery.FirstIncludingDeletedAsync<Customer>(
|
|
db,
|
|
x => x.Email == email,
|
|
cancellationToken);
|
|
}
|
|
|
|
public Task<Customer?> GetByWechatOpenidAsync(string wechatOpenid, CancellationToken cancellationToken = default)
|
|
{
|
|
return FirstOrDefaultAsync(x => x.WechatOpenid == wechatOpenid, cancellationToken);
|
|
}
|
|
|
|
public Task<bool> ExistsPhoneAsync(string phone, CancellationToken cancellationToken = default)
|
|
{
|
|
return AnyAsync(x => x.Phone == phone, cancellationToken);
|
|
}
|
|
|
|
public async Task<bool> ExistsPhoneAnyAsync(string phone, CancellationToken cancellationToken = default)
|
|
{
|
|
var db = await GetDbContextAsync(cancellationToken);
|
|
var hit = await db.Ado.SqlQuerySingleAsync<int?>(
|
|
"SELECT 1 FROM t_customer WHERE Phone = @phone LIMIT 1",
|
|
new { phone });
|
|
return hit.HasValue;
|
|
}
|
|
|
|
public async Task<bool> ExistsEmailAnyAsync(string email, CancellationToken cancellationToken = default)
|
|
{
|
|
var db = await GetDbContextAsync(cancellationToken);
|
|
var hit = await db.Ado.SqlQuerySingleAsync<int?>(
|
|
"SELECT 1 FROM t_customer WHERE Email = @email LIMIT 1",
|
|
new { email });
|
|
return hit.HasValue;
|
|
}
|
|
|
|
public async Task<bool> ExistsWechatOpenidAnyAsync(string wechatOpenid, CancellationToken cancellationToken = default)
|
|
{
|
|
var db = await GetDbContextAsync(cancellationToken);
|
|
var hit = await db.Ado.SqlQuerySingleAsync<int?>(
|
|
"SELECT 1 FROM t_customer WHERE WechatOpenid = @wechatOpenid LIMIT 1",
|
|
new { wechatOpenid });
|
|
return hit.HasValue;
|
|
}
|
|
|
|
public async Task<IReadOnlyList<string>> 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<Customer>()
|
|
.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<Customer>()
|
|
.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<Customer>((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);
|
|
}
|
|
}
|