admin/src/Oms.SqlSugarCore/Customers/CustomerRepository.cs
zzh 9d57d221af fix: 微信注册软删除占坑与字段过长导致保存失败
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-11 10:15:38 +08:00

91 lines
3.3 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 Task<Customer?> GetByPhoneAsync(string phone, CancellationToken cancellationToken = default)
{
return FirstOrDefaultAsync(x => x.Phone == phone, cancellationToken);
}
public Task<Customer?> GetByEmailAsync(string email, CancellationToken cancellationToken = default)
{
return FirstOrDefaultAsync(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<int> GetMaxCustomerCodeSequenceForPrefixAsync(
string prefix,
CancellationToken cancellationToken = default)
{
var expectedLength = prefix.Length + 3;
var db = await GetDbContextAsync(cancellationToken);
var codes = await db.Queryable<Customer>()
.Where(x => x.CustomerCode.StartsWith(prefix) && x.CustomerCode.Length == expectedLength)
.Select(x => x.CustomerCode)
.ToListAsync(cancellationToken);
var max = 0;
foreach (var code in codes)
{
if (code.Length <= prefix.Length)
{
continue;
}
var suffix = code[prefix.Length..];
if (int.TryParse(suffix, out var seq) && seq > max)
{
max = seq;
}
}
return max;
}
}