把一些项目移动到插件中
This commit is contained in:
1 parent
d0f9f00b76
commit
5ff52dea92
60 files changed
+410
-27
No files matched your search
@@ -0,0 +1,32 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Store.Modules.CloudProduct.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Hangfire 定时任务:把数据库中未同步(IndexStatus=0)的商品写入 OpenSearch 分词索引。
|
||||
/// </summary>
|
||||
public class GoodsIndexJob
|
||||
{
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<GoodsIndexJob> _logger;
|
||||
|
||||
public GoodsIndexJob(IServiceScopeFactory scopeFactory, IConfiguration configuration,ILogger<GoodsIndexJob> logger)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task ProcessPendingAsync()
|
||||
{
|
||||
var batchSize = _configuration.GetValue<int?>("Indexing:BatchSize") ?? 1000;
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var service = scope.ServiceProvider.GetRequiredService<GoodsService>();
|
||||
var count = await service.IndexPendingGoodsAsync(batchSize);
|
||||
_logger.LogInformation($"[Hangfire] 商品分词定时任务完成,本次同步 {count} 条");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
using MAX.Core.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Store.Modules.CloudProduct.Dtos;
|
||||
using Store.Modules.CloudProduct.Entities;
|
||||
using Store.Modules.CloudProduct.Repositorys;
|
||||
|
||||
namespace Store.Modules.CloudProduct.Services
|
||||
{
|
||||
public interface IGoodsService : IScopedDependency
|
||||
{
|
||||
Task<int> DeleteGoodsAsync(IEnumerable<long> ids);
|
||||
Task<List<GoodsEntity>> GetGoodsByGoodsIdsAsync(IEnumerable<long> goodsIds);
|
||||
Task<GoodsImportResult> ImportGoodsAsync(IEnumerable<GoodsImportItem> items);
|
||||
Task<int> IndexPendingGoodsAsync(int batchSize);
|
||||
Task<long> RebuildSearchIndexAsync(IEnumerable<long>? ids = null);
|
||||
Task<(List<GoodsEntity> List, long Total)> SearchGoodsAsync(GoodsSearchRequest request);
|
||||
Task<int> UpdateThumbUrlAsync(IEnumerable<GoodsEntity> goodsList);
|
||||
}
|
||||
|
||||
internal class GoodsService : IGoodsService
|
||||
{
|
||||
private readonly GoodsRepository _repository;
|
||||
private readonly IOpenSearchService _searchService;
|
||||
private readonly IRawDataStorage _rawDataStorage;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public GoodsService(GoodsRepository repository, IOpenSearchService searchService, IRawDataStorage rawDataStorage, IConfiguration configuration)
|
||||
{
|
||||
_repository = repository;
|
||||
_searchService = searchService;
|
||||
_rawDataStorage = rawDataStorage;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task<(List<GoodsEntity> List, long Total)> SearchGoodsAsync(GoodsSearchRequest request)
|
||||
{
|
||||
// 1. 从搜索引擎获取标题匹配的商品
|
||||
var searchResult = await _searchService.SearchByTitleAsync(request);
|
||||
|
||||
var goodsList = searchResult.Documents.Select(a => new GoodsEntity
|
||||
{
|
||||
Platform = a.Platform,
|
||||
GoodsId = a.Id,
|
||||
Title = a.Title,
|
||||
CatId1 = a.CatId1,
|
||||
CatId2 = a.CatId2,
|
||||
CatId3 = a.CatId3,
|
||||
CatId4 = a.CatId4,
|
||||
MinPrice = a.MinPrice,
|
||||
MaxPrice = 0,
|
||||
SoldQuantity = a.SoldQuantity,
|
||||
ThumbUrl = a.ThumbUrl,
|
||||
}).ToList();
|
||||
|
||||
return (goodsList, searchResult.Total);
|
||||
|
||||
//// 2. 构建数据库查询(只查正常状态,排除软删除)
|
||||
//var query = _repository.Select.Where(x => x.Status == 0);
|
||||
|
||||
//// 如果有标题搜索,先用ID过滤
|
||||
//if (goodsIds.Any())
|
||||
//{
|
||||
// query = query.Where(x => goodsIds.Contains(x.GoodsId));
|
||||
//}
|
||||
|
||||
//// 3. 价格区间查询
|
||||
//if (request.MinPrice.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.MinPrice >= request.MinPrice.Value);
|
||||
//}
|
||||
//if (request.MaxPrice.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.MaxPrice <= request.MaxPrice.Value);
|
||||
//}
|
||||
|
||||
//// 4. 类目查询
|
||||
//if (request.CatId1.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.CatId1 == request.CatId1);
|
||||
//}
|
||||
//if (request.CatId2.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.CatId2 == request.CatId2);
|
||||
//}
|
||||
//if (request.CatId3.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.CatId3 == request.CatId3);
|
||||
//}
|
||||
//if (request.CatId4.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.CatId4 == request.CatId4);
|
||||
//}
|
||||
|
||||
//// 5. 销量区间查询
|
||||
//if (request.MinSoldQuantity.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.SoldQuantity >= request.MinSoldQuantity.Value);
|
||||
//}
|
||||
//if (request.MaxSoldQuantity.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.SoldQuantity <= request.MaxSoldQuantity.Value);
|
||||
//}
|
||||
|
||||
//// 6. 平台过滤
|
||||
//if (request.Platform.HasValue)
|
||||
//{
|
||||
// query = query.Where(x => x.Platform == request.Platform);
|
||||
//}
|
||||
|
||||
//// 8. 获取总数
|
||||
//total = await query.CountAsync();
|
||||
|
||||
//// 9. 排序
|
||||
//if (!string.IsNullOrEmpty(request.SortBy))
|
||||
//{
|
||||
// var isAsc = request.SortOrder?.ToLower() == "asc";
|
||||
// query = query.OrderByPropertyName(request.SortBy, isAsc);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// query = query.OrderByDescending(x => x.CreatedTime);
|
||||
//}
|
||||
|
||||
//// 10. 分页查询
|
||||
//var list = await query
|
||||
// //.Skip((request.PageIndex - 1) * request.PageSize)
|
||||
// //.Limit(request.PageSize)
|
||||
// .ToListAsync();
|
||||
////内存排序
|
||||
//var orderMap = goodsIds.Select((id, idx) => new { id, idx })
|
||||
// .ToDictionary(x => x.id, x => x.idx);
|
||||
//list = list.OrderBy(x => orderMap.TryGetValue(x.GoodsId, out var idx) ? idx : int.MaxValue)
|
||||
// .ToList();
|
||||
//return (list, total);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量导入商品:先落库(按平台 + 商品编号去重)+ 保存原始数据,
|
||||
/// 然后根据配置决定“立即分词”还是“标记未同步交给后台任务分词”。
|
||||
/// </summary>
|
||||
public async Task<GoodsImportResult> ImportGoodsAsync(IEnumerable<GoodsImportItem> items)
|
||||
{
|
||||
var result = new GoodsImportResult();
|
||||
if (items == null)
|
||||
return result;
|
||||
|
||||
var indexImmediately = IsImmediateIndexing();
|
||||
var documents = new List<GoodsSearchDocument>();
|
||||
var indexedIds = new List<long>();
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var goods = item?.Goods;
|
||||
if (goods == null)
|
||||
{
|
||||
result.FailedCount++;
|
||||
result.Errors.Add("商品数据为空");
|
||||
continue;
|
||||
}
|
||||
|
||||
var goodsId = goods.GoodsId;
|
||||
if (goodsId == 0)
|
||||
{
|
||||
result.FailedCount++;
|
||||
result.Errors.Add("商品编号(GoodsId)不能为空");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 导入时统一先标记“未同步”;立即分词成功后再置为“已同步”,
|
||||
// 后台模式下保持“未同步”,由 Hangfire 定时任务分词。
|
||||
goods.IsSegmented = 0;
|
||||
var saved = await UpsertGoodsAsync(goods);
|
||||
|
||||
var rawData = item?.RawData ?? string.Empty;
|
||||
if (!string.IsNullOrWhiteSpace(rawData))
|
||||
{
|
||||
await _rawDataStorage.SaveAsync(saved.GoodsId, rawData);
|
||||
}
|
||||
|
||||
if (indexImmediately)
|
||||
{
|
||||
documents.Add(new GoodsSearchDocument
|
||||
{
|
||||
Platform = saved.Platform,
|
||||
Id = saved.GoodsId,
|
||||
Title = saved.Title ?? string.Empty,
|
||||
ThumbUrl = saved.ThumbUrl,
|
||||
CatId1 = saved.CatId1,
|
||||
CatId2 = saved.CatId2,
|
||||
CatId3 = saved.CatId3,
|
||||
CatId4 = saved.CatId4,
|
||||
MinPrice = saved.MinPrice,
|
||||
SoldQuantity = saved.SoldQuantity,
|
||||
});
|
||||
indexedIds.Add(saved.GoodsId);
|
||||
}
|
||||
|
||||
result.SuccessCount++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.FailedCount++;
|
||||
result.Errors.Add($"GoodsId={goodsId}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
if (documents.Count > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _searchService.BulkIndexGoodsAsync(documents);
|
||||
await MarkIndexedAsync(indexedIds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 写入失败时保持“未同步”,由后台定时任务重试
|
||||
result.FailedCount += documents.Count;
|
||||
result.SuccessCount -= documents.Count;
|
||||
result.Errors.Add("写入 OpenSearch 失败: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 后台任务:批量处理未同步(IsSegmented=0)的商品,写入 OpenSearch 后标记为已同步。
|
||||
/// </summary>
|
||||
public async Task<int> IndexPendingGoodsAsync(int batchSize)
|
||||
{
|
||||
if (batchSize <= 0)
|
||||
batchSize = 200;
|
||||
|
||||
var pending = await _repository.Select
|
||||
.Where(x => x.IsSegmented == 0 && x.Status == 0)
|
||||
.OrderBy(x => x.GoodsId)
|
||||
.Limit(batchSize)
|
||||
.ToListAsync();
|
||||
|
||||
if (pending.Count == 0)
|
||||
return 0;
|
||||
|
||||
var documents = pending
|
||||
.Select(x => new GoodsSearchDocument
|
||||
{
|
||||
Platform = x.Platform,
|
||||
Id = x.GoodsId,
|
||||
Title = x.Title ?? string.Empty,
|
||||
ThumbUrl = x.ThumbUrl,
|
||||
CatId1 = x.CatId1,
|
||||
CatId2 = x.CatId2,
|
||||
CatId3 = x.CatId3,
|
||||
CatId4 = x.CatId4,
|
||||
MinPrice = x.MinPrice,
|
||||
SoldQuantity = x.SoldQuantity,
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await _searchService.BulkIndexGoodsAsync(documents);
|
||||
|
||||
await MarkIndexedAsync(pending.Select(x => x.GoodsId));
|
||||
return pending.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按平台 + 商品编号查找已存在记录,存在则更新,不存在则新增。
|
||||
/// </summary>
|
||||
private async Task<GoodsEntity> UpsertGoodsAsync(GoodsEntity goods)
|
||||
{
|
||||
var existing = await _repository.Select
|
||||
.Where(x => x.Platform == goods.Platform && x.GoodsId == goods.GoodsId)
|
||||
.FirstAsync();
|
||||
|
||||
if (existing != null)
|
||||
{
|
||||
goods.Id = existing.Id;
|
||||
goods.GoodsId = existing.GoodsId;
|
||||
goods.CreatedTime = existing.CreatedTime;
|
||||
goods.UpdatedTime = DateTime.Now;
|
||||
await _repository.UpdateAsync(goods);
|
||||
return goods;
|
||||
}
|
||||
|
||||
goods.CreatedTime = DateTime.Now;
|
||||
goods.UpdatedTime = DateTime.Now;
|
||||
return await _repository.InsertAsync(goods);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据商品编号(GoodsId)列表读取商品。
|
||||
/// </summary>
|
||||
public async Task<List<GoodsEntity>> GetGoodsByGoodsIdsAsync(IEnumerable<long> goodsIds)
|
||||
{
|
||||
var idList = goodsIds?.ToList();
|
||||
if (idList == null || idList.Count == 0)
|
||||
return new List<GoodsEntity>();
|
||||
|
||||
return await _repository.Select
|
||||
.Where(x => idList.Contains(x.GoodsId))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重新从数据库读取指定商品,覆盖 OpenSearch 中已有的分词并重新分词。
|
||||
/// ids 为空时重建全部商品。重建成功的记录标记为已同步。
|
||||
/// </summary>
|
||||
public async Task<long> RebuildSearchIndexAsync(IEnumerable<long>? ids = null)
|
||||
{
|
||||
List<GoodsEntity> goodsList;
|
||||
|
||||
var idList = ids?.ToList();
|
||||
if (idList == null || idList.Count == 0)
|
||||
{
|
||||
goodsList = await _repository.Select.ToListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
goodsList = await _repository.Select
|
||||
.Where(x => idList.Contains(x.GoodsId))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
var count = await _searchService.RebuildIndexAsync(goodsList);
|
||||
await MarkIndexedAsync(goodsList.Where(x => x.GoodsId > 0).Select(x => x.GoodsId));
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除商品:按配置决定软删除(Status=1)还是硬删除(物理删除),
|
||||
/// 两种方式都会删除 OpenSearch 中对应的文档。
|
||||
/// </summary>
|
||||
public async Task<int> DeleteGoodsAsync(IEnumerable<long> ids)
|
||||
{
|
||||
var idList = ids?.Distinct().ToList();
|
||||
if (idList == null || idList.Count == 0)
|
||||
return 0;
|
||||
|
||||
if (IsSoftDelete())
|
||||
{
|
||||
// 软删除:仅用 Status 标记(0正常 1已删除)
|
||||
await _repository.Orm.Update<GoodsEntity>()
|
||||
.Set(x => x.Status, (byte)1)
|
||||
.Where(x => idList.Contains(x.GoodsId))
|
||||
.ExecuteAffrowsAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 硬删除:直接删除数据库记录
|
||||
await _repository.Orm.Delete<GoodsEntity>()
|
||||
.Where(x => idList.Contains(x.GoodsId))
|
||||
.ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
// 无论软删除还是硬删除,都要删除 OpenSearch 中的对应文档
|
||||
await _searchService.DeleteDocumentsAsync(idList);
|
||||
return idList.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量更新商品缩略图。
|
||||
/// </summary>
|
||||
public async Task<int> UpdateThumbUrlAsync(IEnumerable<GoodsEntity> goodsList)
|
||||
{
|
||||
//string sql = _repository.Orm.Update<GoodsEntity>().SetSource(goodsList, a => a.GoodsId).UpdateColumns(a => new { a.ThumbUrl }).ToSql();
|
||||
return await _repository.Orm.Update<GoodsEntity>()
|
||||
.SetSource(goodsList, a => a.GoodsId)
|
||||
.UpdateColumns(a => new { a.ThumbUrl })
|
||||
.ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否导入时立即分词(Indexing:Mode = Immediate)。
|
||||
/// </summary>
|
||||
private bool IsImmediateIndexing()
|
||||
{
|
||||
var mode = _configuration.GetValue<string>("Indexing:Mode");
|
||||
return string.Equals(mode, "Immediate", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否软删除(Delete:SoftDelete,默认 true)。
|
||||
/// </summary>
|
||||
private bool IsSoftDelete()
|
||||
{
|
||||
return _configuration.GetValue<bool?>("Delete:SoftDelete") ?? true;
|
||||
}
|
||||
|
||||
private async Task MarkIndexedAsync(IEnumerable<long> ids)
|
||||
{
|
||||
var idList = ids?.ToList();
|
||||
if (idList == null || idList.Count == 0)
|
||||
return;
|
||||
|
||||
await _repository.Orm.Update<GoodsEntity>()
|
||||
.Set(x => x.IsSegmented, (byte)1)
|
||||
.Where(x => idList.Contains(x.GoodsId))
|
||||
.ExecuteAffrowsAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,545 @@
|
||||
using System.Diagnostics;
|
||||
using MAX.Core.DependencyInjection;
|
||||
using MAX.Framework.Commons;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenSearch.Client;
|
||||
using OpenSearch.Net;
|
||||
using Store.Modules.CloudProduct.Dtos;
|
||||
using Store.Modules.CloudProduct.Entities;
|
||||
using Store.Modules.CloudProduct.Enums;
|
||||
|
||||
namespace Store.Modules.CloudProduct.Services
|
||||
{
|
||||
public interface IOpenSearchService : ISingletonDependency
|
||||
{
|
||||
Task AddDocumentAsync(GoodsSearchDocument document);
|
||||
Task BulkIndexGoodsAsync(IEnumerable<GoodsSearchDocument> documents);
|
||||
Task<bool> ClearIndexAsync();
|
||||
Task<long> CountDocumentsAsync();
|
||||
Task<bool> CreateIndexAsync();
|
||||
Task DeleteDocumentsAsync(IEnumerable<long> ids);
|
||||
Task DeleteIndexAsync();
|
||||
Task<OpenSearchStatusDto> GetStatusAsync();
|
||||
Task<bool> IndexExistsAsync();
|
||||
Task<bool> IsIndexMappingHealthyAsync();
|
||||
Task RefreshIndexAsync();
|
||||
Task<long> RebuildIndexAsync(IEnumerable<GoodsEntity> goodsList);
|
||||
Task<(long Total, List<GoodsSearchDocument> Documents)> SearchByTitleAsync(GoodsSearchRequest request, GoodsSearchSortBy sortBy = GoodsSearchSortBy.Relevance);
|
||||
}
|
||||
|
||||
internal class OpenSearchService : IOpenSearchService
|
||||
{
|
||||
private readonly string _nodeUrl;
|
||||
private readonly string _indexName;
|
||||
private readonly IOpenSearchClient _client = null!;
|
||||
|
||||
// 查询类操作(search/count/status/ping)统一超时
|
||||
private static readonly TimeSpan QueryTimeout = TimeSpan.FromSeconds(5);
|
||||
// 批量写入/建索引/删除索引等管理类操作超时(数据量大时需要更长,可挪到配置文件)
|
||||
private static readonly TimeSpan AdminTimeout = TimeSpan.FromSeconds(30);
|
||||
private readonly ILogger<OpenSearchService> _logger;
|
||||
|
||||
public OpenSearchService(IConfiguration configuration,ILogger<OpenSearchService> logger)
|
||||
{
|
||||
var config = configuration.GetSection("Search");
|
||||
_nodeUrl = config.GetValue<string>("Url") ?? "";
|
||||
_indexName = config.GetValue<string>("IndexName") ?? "";
|
||||
_client = CreateClient();
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private IOpenSearchClient CreateClient()
|
||||
{
|
||||
var pool = new SingleNodeConnectionPool(new Uri(_nodeUrl));
|
||||
var settings = new ConnectionSettings(pool)
|
||||
.DefaultIndex(_indexName)
|
||||
.DefaultMappingFor<GoodsSearchDocument>(m => m.IndexName(_indexName))
|
||||
.RequestTimeout(QueryTimeout)
|
||||
.MaxRetryTimeout(QueryTimeout)
|
||||
.PrettyJson()
|
||||
.DisableDirectStreaming(); // 便于调试打印请求/响应报文
|
||||
|
||||
return new OpenSearchClient(settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 1. 创建索引 ============
|
||||
/// </summary>
|
||||
public async Task<bool> CreateIndexAsync()
|
||||
{
|
||||
var existsResponse = await _client.Indices.ExistsAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (existsResponse.Exists)
|
||||
{
|
||||
if (await IsIndexMappingHealthyAsync())
|
||||
{
|
||||
Console.WriteLine($"索引 [{_indexName}] 已存在且映射正确,无需重复创建。");
|
||||
return true;
|
||||
}
|
||||
|
||||
Console.WriteLine($"索引 [{_indexName}] 的映射不完整(可能由清空索引等操作造成),正在重建...");
|
||||
var deleteResponse = await _client.Indices.DeleteAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (!deleteResponse.IsValid)
|
||||
{
|
||||
throw new AppException("删除损坏索引失败: " + deleteResponse.DebugInformation);
|
||||
}
|
||||
}
|
||||
|
||||
// 优先尝试使用 IK 中文分词(需集群安装 analysis-ik 插件)
|
||||
var createResponse = await TryCreateIndexAsync(useIk: true);
|
||||
|
||||
if (!createResponse.IsValid)
|
||||
{
|
||||
Console.WriteLine("IK 分词插件不可用,已回退到 standard 分析器,中文分词将失效,请检查集群是否安装 analysis-ik 插件。");
|
||||
createResponse = await TryCreateIndexAsync(useIk: false);
|
||||
}
|
||||
|
||||
if (createResponse.IsValid)
|
||||
{
|
||||
Console.WriteLine($"索引 [{_indexName}] 创建成功。");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new AppException("索引创建失败: " + createResponse.DebugInformation);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IsIndexMappingHealthyAsync()
|
||||
{
|
||||
var mappingStatus = await GetIndexMappingStatusAsync();
|
||||
return mappingStatus.Healthy;
|
||||
}
|
||||
|
||||
private async Task<(bool Healthy, string? IndexAnalyzer, string? SearchAnalyzer)> GetIndexMappingStatusAsync()
|
||||
{
|
||||
var getResponse = await _client.Indices.GetAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
if (!getResponse.IsValid)
|
||||
{
|
||||
throw new AppException("读取索引映射失败: " + getResponse.DebugInformation);
|
||||
}
|
||||
if (!getResponse.Indices.TryGetValue(_indexName, out var indexState))
|
||||
{
|
||||
return (false, null, null);
|
||||
}
|
||||
|
||||
var mapping = indexState.Mappings;
|
||||
if (mapping?.Properties == null
|
||||
|| !mapping.Properties.TryGetValue(new PropertyName("title"), out var property)
|
||||
|| property is not ITextProperty textProperty)
|
||||
{
|
||||
return (false, null, null);
|
||||
}
|
||||
|
||||
var indexAnalyzer = textProperty.Analyzer;
|
||||
var searchAnalyzer = textProperty.SearchAnalyzer;
|
||||
var usesIk = indexAnalyzer == "ik_max_word" && searchAnalyzer == "ik_smart";
|
||||
var usesStandard = indexAnalyzer == "standard" && searchAnalyzer == "standard";
|
||||
return (usesIk || usesStandard, indexAnalyzer, searchAnalyzer);
|
||||
}
|
||||
|
||||
private async Task<CreateIndexResponse> TryCreateIndexAsync(bool useIk)
|
||||
{
|
||||
var indexAnalyzer = useIk ? "ik_max_word" : "standard";
|
||||
var searchAnalyzer = useIk ? "ik_smart" : "standard";
|
||||
|
||||
return await _client.Indices.CreateAsync(_indexName, c => c
|
||||
.Settings(s => s
|
||||
.NumberOfShards(1)
|
||||
.NumberOfReplicas(0)
|
||||
)
|
||||
.Map<GoodsSearchDocument>(m => m
|
||||
.Properties(p => p
|
||||
.Number(n => n.Name(x => x.Id).Type(NumberType.Long))
|
||||
.Text(t => t
|
||||
.Name(x => x.Title)
|
||||
.Analyzer(indexAnalyzer)
|
||||
.SearchAnalyzer(searchAnalyzer)
|
||||
)
|
||||
.Number(n => n.Name(x => x.CatId1).Type(NumberType.Integer))
|
||||
.Number(n => n.Name(x => x.CatId2).Type(NumberType.Integer))
|
||||
.Number(n => n.Name(x => x.CatId3).Type(NumberType.Integer))
|
||||
.Number(n => n.Name(x => x.CatId4).Type(NumberType.Integer))
|
||||
.Number(n => n.Name(x => x.MinPrice).Type(NumberType.Long))
|
||||
.Number(n => n.Name(x => x.SoldQuantity).Type(NumberType.Integer))
|
||||
)
|
||||
)
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout))
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 2. 删除索引 ============
|
||||
/// </summary>
|
||||
public async Task DeleteIndexAsync()
|
||||
{
|
||||
var response = await _client.Indices.DeleteAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (!response.IsValid)
|
||||
{
|
||||
throw new AppException("删除失败: " + response.DebugInformation);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IndexExistsAsync()
|
||||
{
|
||||
var response = await _client.Indices.ExistsAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
return response.Exists;
|
||||
}
|
||||
|
||||
public async Task RefreshIndexAsync()
|
||||
{
|
||||
var response = await _client.Indices.RefreshAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (!response.IsValid)
|
||||
{
|
||||
throw new AppException("刷新索引失败: " + response.DebugInformation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 3.1 批量写入商品搜索文档(存在则覆盖) ============
|
||||
/// </summary>
|
||||
public async Task BulkIndexGoodsAsync(IEnumerable<GoodsSearchDocument> documents)
|
||||
{
|
||||
if (documents == null) throw new ArgumentNullException(nameof(documents));
|
||||
|
||||
var list = documents.ToList();
|
||||
if (list.Count == 0) return;
|
||||
|
||||
// 确保索引已按 IK 分词映射创建,否则中文分词无效
|
||||
await CreateIndexAsync();
|
||||
|
||||
var bulkResponse = await _client.BulkAsync(b => b
|
||||
.Index(_indexName)
|
||||
.IndexMany(list, (descriptor, doc) => descriptor.Id(doc.Id))
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout))
|
||||
);
|
||||
|
||||
if (!bulkResponse.IsValid)
|
||||
{
|
||||
throw new AppException("写入 OpenSearch 失败: " + bulkResponse.DebugInformation);
|
||||
}
|
||||
|
||||
// 刷新索引,保证数据立即可搜索(仅用于演示,生产环境不建议频繁 Refresh)
|
||||
await RefreshIndexAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 3.2 根据数据库商品重建搜索索引 ============
|
||||
/// </summary>
|
||||
public async Task<long> RebuildIndexAsync(IEnumerable<GoodsEntity> goodsList)
|
||||
{
|
||||
if (goodsList == null) throw new ArgumentNullException(nameof(goodsList));
|
||||
|
||||
var documents = goodsList
|
||||
.Where(x => x != null && x.GoodsId > 0)
|
||||
.Select(x => new GoodsSearchDocument
|
||||
{
|
||||
Id = x.GoodsId,
|
||||
Title = x.Title ?? string.Empty,
|
||||
CatId1 = x.CatId1,
|
||||
CatId2 = x.CatId2,
|
||||
CatId3 = x.CatId3,
|
||||
CatId4 = x.CatId4,
|
||||
MinPrice = x.MinPrice,
|
||||
SoldQuantity = x.SoldQuantity
|
||||
})
|
||||
.ToList();
|
||||
|
||||
if (documents.Count == 0) return 0;
|
||||
|
||||
await BulkIndexGoodsAsync(documents);
|
||||
return documents.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 3.3 按商品编号批量删除 OpenSearch 文档(软/硬删除共用) ============
|
||||
/// </summary>
|
||||
public async Task DeleteDocumentsAsync(IEnumerable<long> ids)
|
||||
{
|
||||
var list = ids?.Distinct().ToList() ?? new List<long>();
|
||||
if (list.Count == 0) return;
|
||||
|
||||
var exists = await _client.Indices.ExistsAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
if (!exists.Exists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var response = await _client.BulkAsync(b =>
|
||||
{
|
||||
foreach (var id in list)
|
||||
{
|
||||
b.Delete<GoodsSearchDocument>(d => d.Index(_indexName).Id(id));
|
||||
}
|
||||
b.RequestConfiguration(r => r.RequestTimeout(AdminTimeout));
|
||||
return b;
|
||||
});
|
||||
|
||||
if (!response.IsValid)
|
||||
{
|
||||
throw new AppException("删除 OpenSearch 文档失败: " + response.DebugInformation);
|
||||
}
|
||||
|
||||
await RefreshIndexAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 4. 手动新增一条搜索文档 ============
|
||||
/// </summary>
|
||||
public async Task AddDocumentAsync(GoodsSearchDocument document)
|
||||
{
|
||||
if (document == null) throw new ArgumentNullException(nameof(document));
|
||||
|
||||
var response = await _client.IndexAsync(document, i => i
|
||||
.Index(_indexName)
|
||||
.Id(document.Id)
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout))
|
||||
);
|
||||
|
||||
if (response.IsValid)
|
||||
{
|
||||
await RefreshIndexAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new AppException("新增失败: " + response.DebugInformation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 5. 分词搜索(分页 + 类目/价格/销量/平台过滤 + 排序) ============
|
||||
/// </summary>
|
||||
/// <param name="request">搜索条件(关键词、类目、价格、销量、平台、分页)</param>
|
||||
/// <param name="sortBy">排序方式</param>
|
||||
public async Task<(long Total, List<GoodsSearchDocument> Documents)> SearchByTitleAsync(GoodsSearchRequest request,
|
||||
GoodsSearchSortBy sortBy = GoodsSearchSortBy.Relevance)
|
||||
{
|
||||
var keyword = request.Title;
|
||||
int? catId1 = request.CatId1;
|
||||
int? catId2 = request.CatId2;
|
||||
int? catId3 = request.CatId3;
|
||||
int? catId4 = request.CatId4;
|
||||
long? minPrice = request.MinPrice;
|
||||
long? maxPrice = request.MaxPrice;
|
||||
int? minSoldQuantity = request.MinSoldQuantity;
|
||||
int? maxSoldQuantity = request.MaxSoldQuantity;
|
||||
int? platform = request.Platform;
|
||||
int pageIndex = request.PageIndex;
|
||||
int pageSize = request.PageSize;
|
||||
|
||||
if (pageIndex < 1) pageIndex = 1;
|
||||
if (pageSize < 1) pageSize = 20;
|
||||
if (pageSize > 2000) pageSize = 2000; // 避免深分页/单页过大拖垮性能,按需调整上限
|
||||
|
||||
var from = (pageIndex - 1) * pageSize;
|
||||
|
||||
ISearchResponse<GoodsSearchDocument> searchResponse;
|
||||
try
|
||||
{
|
||||
searchResponse = await _client.SearchAsync<GoodsSearchDocument>(s => s
|
||||
.Index(_indexName)
|
||||
.Query(q => q
|
||||
.Bool(b =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(keyword) == false)
|
||||
{
|
||||
b.Must(m => m.Match(mm => mm.Field(f => f.Title).Query(keyword)));
|
||||
}
|
||||
|
||||
var filters = new List<Func<QueryContainerDescriptor<GoodsSearchDocument>, QueryContainer>>();
|
||||
if (platform.HasValue && platform!.Value > 0) filters.Add(f => f.Term(t => t.Field(x => x.Platform).Value(platform.Value)));
|
||||
|
||||
if (catId1.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId1).Value(catId1.Value)));
|
||||
if (catId2.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId2).Value(catId2.Value)));
|
||||
if (catId3.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId3).Value(catId3.Value)));
|
||||
if (catId4.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId4).Value(catId4.Value)));
|
||||
|
||||
if (minPrice.HasValue || maxPrice.HasValue)
|
||||
{
|
||||
filters.Add(f => f.Range(r =>
|
||||
{
|
||||
var range = r.Field(x => x.MinPrice);
|
||||
if (minPrice.HasValue) range = range.GreaterThanOrEquals(minPrice.Value);
|
||||
if (maxPrice.HasValue) range = range.LessThanOrEquals(maxPrice.Value);
|
||||
return range;
|
||||
}));
|
||||
}
|
||||
|
||||
if (minSoldQuantity.HasValue || maxSoldQuantity.HasValue)
|
||||
{
|
||||
filters.Add(f => f.Range(r =>
|
||||
{
|
||||
var range = r.Field(x => x.SoldQuantity);
|
||||
if (minSoldQuantity.HasValue) range = range.GreaterThanOrEquals(minSoldQuantity.Value);
|
||||
if (maxSoldQuantity.HasValue) range = range.LessThanOrEquals(maxSoldQuantity.Value);
|
||||
return range;
|
||||
}));
|
||||
}
|
||||
|
||||
if (filters.Count > 0) b.Filter(filters.ToArray());
|
||||
return b;
|
||||
})
|
||||
)
|
||||
.Sort(sort => sortBy switch
|
||||
{
|
||||
GoodsSearchSortBy.PriceAsc => sort.Ascending(f => f.MinPrice),
|
||||
GoodsSearchSortBy.PriceDesc => sort.Descending(f => f.MinPrice),
|
||||
GoodsSearchSortBy.SoldDesc => sort.Descending(f => f.SoldQuantity),
|
||||
_ => sort.Descending(SortSpecialField.Score)
|
||||
})
|
||||
.From(from)
|
||||
.Size(pageSize)
|
||||
.TrackTotalHits(true) // 命中数超过 10000 时默认不精确统计,需要显式开启
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout))
|
||||
);
|
||||
}
|
||||
catch (OpenSearchClientException ex) when (ex.Response?.Success == false)
|
||||
{
|
||||
throw new AppException($"搜索超时或失败({QueryTimeout.TotalSeconds}秒): " + ex.Message);
|
||||
}
|
||||
|
||||
if (!searchResponse.IsValid)
|
||||
{
|
||||
throw new AppException("搜索失败: " + searchResponse.DebugInformation);
|
||||
}
|
||||
|
||||
return (searchResponse.Total, searchResponse.Documents.ToList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 6. 统计文档数 ============
|
||||
/// </summary>
|
||||
public async Task<long> CountDocumentsAsync()
|
||||
{
|
||||
var countResponse = await _client.CountAsync<GoodsSearchDocument>(c => c
|
||||
.Index(_indexName)
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout))
|
||||
);
|
||||
if (countResponse.IsValid)
|
||||
{
|
||||
Console.WriteLine($"索引 [{_indexName}] 当前文档总数: {countResponse.Count}");
|
||||
return countResponse.Count;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("统计失败: " + countResponse.DebugInformation);
|
||||
throw new AppException(countResponse.DebugInformation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 7. 查询 OpenSearch 状态 ============
|
||||
/// </summary>
|
||||
public async Task<OpenSearchStatusDto> GetStatusAsync()
|
||||
{
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
var status = new OpenSearchStatusDto
|
||||
{
|
||||
NodeUrl = _nodeUrl,
|
||||
IndexName = _indexName,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var ping = await _client.PingAsync(p => p
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
status.Connected = ping.IsValid;
|
||||
if (!ping.IsValid)
|
||||
{
|
||||
status.Message = ping.DebugInformation;
|
||||
_logger.LogError(ping.DebugInformation);
|
||||
return status;
|
||||
}
|
||||
_logger.LogInformation("OpenSerach 已连接");
|
||||
var clusterHealth = await _client.Cluster.HealthAsync(Indices.All, h => h
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
if (!clusterHealth.IsValid)
|
||||
{
|
||||
status.Message = clusterHealth.DebugInformation;
|
||||
return status;
|
||||
}
|
||||
|
||||
status.ClusterName = clusterHealth.ClusterName;
|
||||
status.ClusterStatus = clusterHealth.Status.ToString().ToLowerInvariant();
|
||||
status.NumberOfNodes = clusterHealth.NumberOfNodes;
|
||||
status.NumberOfDataNodes = clusterHealth.NumberOfDataNodes;
|
||||
status.ActivePrimaryShards = clusterHealth.ActivePrimaryShards;
|
||||
status.ActiveShards = clusterHealth.ActiveShards;
|
||||
status.RelocatingShards = clusterHealth.RelocatingShards;
|
||||
status.InitializingShards = clusterHealth.InitializingShards;
|
||||
status.UnassignedShards = clusterHealth.UnassignedShards;
|
||||
status.IndexExists = await IndexExistsAsync();
|
||||
|
||||
if (status.IndexExists)
|
||||
{
|
||||
var mappingStatus = await GetIndexMappingStatusAsync();
|
||||
status.MappingHealthy = mappingStatus.Healthy;
|
||||
status.IndexAnalyzer = mappingStatus.IndexAnalyzer;
|
||||
status.SearchAnalyzer = mappingStatus.SearchAnalyzer;
|
||||
|
||||
var count = await _client.CountAsync<GoodsSearchDocument>(c => c
|
||||
.Index(_indexName)
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
status.DocumentCount = count.IsValid ? count.Count : -1;
|
||||
if (!count.IsValid)
|
||||
{
|
||||
status.Message = count.DebugInformation;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
status.Message = $"索引 [{_indexName}] 不存在";
|
||||
}
|
||||
|
||||
status.Healthy = status.IndexExists
|
||||
&& status.MappingHealthy
|
||||
&& status.ClusterStatus != "red"
|
||||
&& status.DocumentCount >= 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
status.Healthy = false;
|
||||
status.Message = ex.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
stopwatch.Stop();
|
||||
status.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空索引:删除后按统一映射重建,重建期间该索引不可查(由后台定时任务负责回填数据,接受短暂空窗)
|
||||
/// </summary>
|
||||
public async Task<bool> ClearIndexAsync()
|
||||
{
|
||||
var existsResponse = await _client.Indices.ExistsAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
if (!existsResponse.Exists)
|
||||
{
|
||||
Console.WriteLine($"索引 {_indexName} 不存在");
|
||||
return false;
|
||||
}
|
||||
|
||||
var deleteResponse = await _client.Indices.DeleteAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (!deleteResponse.IsValid)
|
||||
{
|
||||
Console.WriteLine($"删除索引 {_indexName} 失败: {deleteResponse.DebugInformation}");
|
||||
return false;
|
||||
}
|
||||
Console.WriteLine($"索引 {_indexName} 已删除");
|
||||
|
||||
return await CreateIndexAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using MAX.Core.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
|
||||
namespace Store.Modules.CloudProduct.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 商品原始数据文件存储:按 goodsId % 100 分目录,文件名即商品ID,GZip 压缩保存。
|
||||
/// </summary>
|
||||
public interface IRawDataStorage : IScopedDependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 保存原始数据,返回保存后的完整路径。
|
||||
/// </summary>
|
||||
Task<string> SaveAsync(long goodsId, string rawData);
|
||||
|
||||
/// <summary>
|
||||
/// 读取并解压原始数据;文件不存在时返回 null。
|
||||
/// </summary>
|
||||
Task<string?> ReadAsync(long goodsId);
|
||||
|
||||
/// <summary>
|
||||
/// 判断指定商品的原始数据文件是否存在。
|
||||
/// </summary>
|
||||
bool Exists(long goodsId);
|
||||
}
|
||||
|
||||
internal class RawDataStorage : IRawDataStorage
|
||||
{
|
||||
private readonly string _rootPath;
|
||||
|
||||
public RawDataStorage(IConfiguration configuration)
|
||||
{
|
||||
var configured = configuration.GetValue<string>("RawData:Path");
|
||||
if (string.IsNullOrWhiteSpace(configured))
|
||||
configured = "RawData";
|
||||
|
||||
_rootPath = Path.GetFullPath(configured);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string> SaveAsync(long goodsId, string rawData)
|
||||
{
|
||||
var filePath = GetFilePath(goodsId);
|
||||
var dir = Path.GetDirectoryName(filePath)!;
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
await using var gzip = new GZipStream(fileStream, CompressionLevel.Fastest);
|
||||
await using var writer = new StreamWriter(gzip, new UTF8Encoding(false));
|
||||
await writer.WriteAsync(rawData);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string?> ReadAsync(long goodsId)
|
||||
{
|
||||
var filePath = GetFilePath(goodsId);
|
||||
if (!File.Exists(filePath))
|
||||
return null;
|
||||
|
||||
await using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
await using var gzip = new GZipStream(fileStream, CompressionMode.Decompress);
|
||||
using var reader = new StreamReader(gzip, new UTF8Encoding(false));
|
||||
return await reader.ReadToEndAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Exists(long goodsId)
|
||||
{
|
||||
return File.Exists(GetFilePath(goodsId));
|
||||
}
|
||||
|
||||
private string GetFilePath(long goodsId)
|
||||
{
|
||||
var subDir = (goodsId % 100).ToString();
|
||||
return Path.Combine(_rootPath, subDir, goodsId.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
using MAX.Core.DependencyInjection;
|
||||
using MAX.Framework.Commons;
|
||||
using MAX.Framework.Dtos;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenSearch.Client;
|
||||
using OpenSearch.Net;
|
||||
using Store.Modules.CloudProduct.Dtos;
|
||||
using Store.Modules.CloudProduct.Enums;
|
||||
|
||||
namespace Store.Modules.CloudProduct.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户收藏商品索引服务:负责收藏索引(Search:FavoritesIndexName)的
|
||||
/// 创建、写入、删除、重建与检索,索引文档由 user_goods_favorites 关联 goods 补全而来。
|
||||
/// </summary>
|
||||
public interface IUserFavoritesIndexService : ISingletonDependency
|
||||
{
|
||||
/// <summary>检查索引是否存在</summary>
|
||||
Task<bool> IndexExistsAsync();
|
||||
|
||||
/// <summary>检查索引映射(分析器)是否符合当前服务要求</summary>
|
||||
Task<bool> IsIndexMappingHealthyAsync();
|
||||
|
||||
/// <summary>创建索引(含 IK 分词映射;已存在且映射正常时跳过)</summary>
|
||||
Task<bool> CreateIndexAsync();
|
||||
|
||||
/// <summary>删除并重建索引(映射统一,重建期间该索引不可查)</summary>
|
||||
Task<bool> ClearIndexAsync();
|
||||
|
||||
/// <summary>批量写入收藏文档(存在则覆盖)</summary>
|
||||
Task IndexDocumentsAsync(IEnumerable<UserFavoritesSearchDocument> documents);
|
||||
|
||||
/// <summary>按文档Id批量删除收藏文档</summary>
|
||||
Task DeleteDocumentsAsync(IEnumerable<string> documentIds);
|
||||
|
||||
/// <summary>统计索引文档数</summary>
|
||||
Task<long> CountDocumentsAsync();
|
||||
|
||||
/// <summary>按用户检索收藏商品(标题/销量/价格/类目/分组)</summary>
|
||||
Task<(long Total, List<UserFavoritesSearchDocument> Documents)> SearchAsync(UserFavoritesSearchRequest request, UserFavoritesSortBy sortBy);
|
||||
}
|
||||
|
||||
internal class UserFavoritesIndexService : IUserFavoritesIndexService
|
||||
{
|
||||
private readonly string _nodeUrl;
|
||||
private readonly string _indexName;
|
||||
private readonly IOpenSearchClient _client = null!;
|
||||
private readonly ILogger<UserFavoritesIndexService> _logger;
|
||||
|
||||
// 查询类操作(search/count/exists)统一超时
|
||||
private static readonly TimeSpan QueryTimeout = TimeSpan.FromSeconds(5);
|
||||
// 批量写入/建索引/删除索引等管理类操作超时(数据量大时需要更长)
|
||||
private static readonly TimeSpan AdminTimeout = TimeSpan.FromSeconds(30);
|
||||
|
||||
public UserFavoritesIndexService(IConfiguration configuration, ILogger<UserFavoritesIndexService> logger)
|
||||
{
|
||||
var config = configuration.GetSection("Search");
|
||||
_nodeUrl = config.GetValue<string>("Url") ?? "";
|
||||
_indexName = config.GetValue<string>("FavoritesIndexName") ?? "";
|
||||
_logger = logger;
|
||||
_client = CreateClient();
|
||||
}
|
||||
|
||||
private IOpenSearchClient CreateClient()
|
||||
{
|
||||
var pool = new SingleNodeConnectionPool(new Uri(_nodeUrl));
|
||||
var settings = new ConnectionSettings(pool)
|
||||
.DefaultIndex(_indexName)
|
||||
.DefaultMappingFor<UserFavoritesSearchDocument>(m => m.IndexName(_indexName))
|
||||
.RequestTimeout(QueryTimeout)
|
||||
.MaxRetryTimeout(QueryTimeout)
|
||||
.PrettyJson()
|
||||
.DisableDirectStreaming();
|
||||
|
||||
return new OpenSearchClient(settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 1. 索引是否存在 ============
|
||||
/// </summary>
|
||||
public async Task<bool> IndexExistsAsync()
|
||||
{
|
||||
var response = await _client.Indices.ExistsAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
return response.Exists;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 2. 索引映射是否健康 ============
|
||||
/// </summary>
|
||||
public async Task<bool> IsIndexMappingHealthyAsync()
|
||||
{
|
||||
var getResponse = await _client.Indices.GetAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
if (!getResponse.IsValid)
|
||||
{
|
||||
throw new AppException("读取收藏索引映射失败: " + getResponse.DebugInformation);
|
||||
}
|
||||
if (!getResponse.Indices.TryGetValue(_indexName, out var indexState))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var mapping = indexState.Mappings;
|
||||
if (mapping?.Properties == null
|
||||
|| !mapping.Properties.TryGetValue(new PropertyName("title"), out var property)
|
||||
|| property is not ITextProperty textProperty)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var indexAnalyzer = textProperty.Analyzer;
|
||||
var searchAnalyzer = textProperty.SearchAnalyzer;
|
||||
var usesIk = indexAnalyzer == "ik_max_word" && searchAnalyzer == "ik_smart";
|
||||
var usesStandard = indexAnalyzer == "standard" && searchAnalyzer == "standard";
|
||||
return usesIk || usesStandard;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 3. 创建索引 ============
|
||||
/// </summary>
|
||||
public async Task<bool> CreateIndexAsync()
|
||||
{
|
||||
var existsResponse = await _client.Indices.ExistsAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (existsResponse.Exists)
|
||||
{
|
||||
if (await IsIndexMappingHealthyAsync())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
_logger.LogWarning("收藏索引 [{Index}] 的映射不完整,正在重建...", _indexName);
|
||||
var deleteResponse = await _client.Indices.DeleteAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (!deleteResponse.IsValid)
|
||||
{
|
||||
throw new AppException("删除损坏的收藏索引失败: " + deleteResponse.DebugInformation);
|
||||
}
|
||||
}
|
||||
|
||||
// 优先尝试使用 IK 中文分词(需集群安装 analysis-ik 插件)
|
||||
var createResponse = await TryCreateIndexAsync(useIk: true);
|
||||
|
||||
if (!createResponse.IsValid)
|
||||
{
|
||||
_logger.LogWarning("IK 分词插件不可用,收藏索引已回退到 standard 分析器,中文分词将失效。");
|
||||
createResponse = await TryCreateIndexAsync(useIk: false);
|
||||
}
|
||||
|
||||
if (!createResponse.IsValid)
|
||||
{
|
||||
throw new AppException("收藏索引创建失败: " + createResponse.DebugInformation);
|
||||
}
|
||||
|
||||
_logger.LogInformation("收藏索引 [{Index}] 创建成功。", _indexName);
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<CreateIndexResponse> TryCreateIndexAsync(bool useIk)
|
||||
{
|
||||
var indexAnalyzer = useIk ? "ik_max_word" : "standard";
|
||||
var searchAnalyzer = useIk ? "ik_smart" : "standard";
|
||||
|
||||
return await _client.Indices.CreateAsync(_indexName, c => c
|
||||
.Settings(s => s
|
||||
.NumberOfShards(1)
|
||||
.NumberOfReplicas(0)
|
||||
)
|
||||
.Map<UserFavoritesSearchDocument>(m => m
|
||||
.Properties(p => p
|
||||
.Keyword(k => k.Name(x => x.Id))
|
||||
.Number(n => n.Name(x => x.FavId).Type(NumberType.Long))
|
||||
.Keyword(k => k.Name(x => x.UserName))
|
||||
.Number(n => n.Name(x => x.GroupId).Type(NumberType.Integer))
|
||||
.Keyword(k => k.Name(x => x.GroupName))
|
||||
.Number(n => n.Name(x => x.GoodsId).Type(NumberType.Long))
|
||||
.Number(n => n.Name(x => x.Platform).Type(NumberType.Byte))
|
||||
.Text(t => t
|
||||
.Name(x => x.Title)
|
||||
.Analyzer(indexAnalyzer)
|
||||
.SearchAnalyzer(searchAnalyzer)
|
||||
)
|
||||
.Text(t => t.Name(x => x.ThumbUrl))
|
||||
.Number(n => n.Name(x => x.CatId1).Type(NumberType.Integer))
|
||||
.Number(n => n.Name(x => x.CatId2).Type(NumberType.Integer))
|
||||
.Number(n => n.Name(x => x.CatId3).Type(NumberType.Integer))
|
||||
.Number(n => n.Name(x => x.CatId4).Type(NumberType.Integer))
|
||||
.Number(n => n.Name(x => x.MinPrice).Type(NumberType.Long))
|
||||
.Number(n => n.Name(x => x.SoldQuantity).Type(NumberType.Integer))
|
||||
.Date(d => d.Name(x => x.CreatedAt))
|
||||
)
|
||||
)
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout))
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 4. 删除并重建索引 ============
|
||||
/// </summary>
|
||||
public async Task<bool> ClearIndexAsync()
|
||||
{
|
||||
var existsResponse = await _client.Indices.ExistsAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
|
||||
if (!existsResponse.Exists)
|
||||
{
|
||||
return await CreateIndexAsync();
|
||||
}
|
||||
|
||||
var deleteResponse = await _client.Indices.DeleteAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (!deleteResponse.IsValid)
|
||||
{
|
||||
throw new AppException($"删除收藏索引 {_indexName} 失败: " + deleteResponse.DebugInformation);
|
||||
}
|
||||
|
||||
_logger.LogInformation("收藏索引 [{Index}] 已删除,准备重建。", _indexName);
|
||||
return await CreateIndexAsync();
|
||||
}
|
||||
|
||||
private async Task RefreshIndexAsync()
|
||||
{
|
||||
var response = await _client.Indices.RefreshAsync(_indexName, d => d
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
|
||||
if (!response.IsValid)
|
||||
{
|
||||
throw new AppException("刷新收藏索引失败: " + response.DebugInformation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 5. 批量写入收藏文档(存在则覆盖) ============
|
||||
/// </summary>
|
||||
public async Task IndexDocumentsAsync(IEnumerable<UserFavoritesSearchDocument> documents)
|
||||
{
|
||||
if (documents == null) throw new ArgumentNullException(nameof(documents));
|
||||
|
||||
var list = documents.Where(x => x != null && string.IsNullOrWhiteSpace(x.Id) == false).ToList();
|
||||
if (list.Count == 0) return;
|
||||
|
||||
await CreateIndexAsync();
|
||||
|
||||
var bulkResponse = await _client.BulkAsync(b => b
|
||||
.Index(_indexName)
|
||||
.IndexMany(list, (descriptor, doc) => descriptor.Id(doc.Id))
|
||||
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout))
|
||||
);
|
||||
|
||||
if (!bulkResponse.IsValid)
|
||||
{
|
||||
throw new AppException("批量写入收藏索引失败: " + bulkResponse.DebugInformation);
|
||||
}
|
||||
|
||||
await RefreshIndexAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 6. 按文档Id批量删除收藏文档 ============
|
||||
/// </summary>
|
||||
public async Task DeleteDocumentsAsync(IEnumerable<string> documentIds)
|
||||
{
|
||||
var list = documentIds?.Where(x => string.IsNullOrWhiteSpace(x) == false).Distinct().ToList() ?? new List<string>();
|
||||
if (list.Count == 0) return;
|
||||
|
||||
if (await IndexExistsAsync() == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var response = await _client.BulkAsync(b =>
|
||||
{
|
||||
foreach (var id in list)
|
||||
{
|
||||
b.Delete<UserFavoritesSearchDocument>(d => d.Index(_indexName).Id(id));
|
||||
}
|
||||
b.RequestConfiguration(r => r.RequestTimeout(AdminTimeout));
|
||||
return b;
|
||||
});
|
||||
|
||||
if (!response.IsValid)
|
||||
{
|
||||
throw new AppException("删除收藏索引文档失败: " + response.DebugInformation);
|
||||
}
|
||||
|
||||
await RefreshIndexAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 7. 统计文档数 ============
|
||||
/// </summary>
|
||||
public async Task<long> CountDocumentsAsync()
|
||||
{
|
||||
var countResponse = await _client.CountAsync<UserFavoritesSearchDocument>(c => c
|
||||
.Index(_indexName)
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout))
|
||||
);
|
||||
if (!countResponse.IsValid)
|
||||
{
|
||||
throw new AppException("统计收藏索引失败: " + countResponse.DebugInformation);
|
||||
}
|
||||
|
||||
return countResponse.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ============ 8. 检索用户收藏商品(标题/价格/销量/类目/分组 + 排序 + 分页) ============
|
||||
/// </summary>
|
||||
public async Task<(long Total, List<UserFavoritesSearchDocument> Documents)> SearchAsync(UserFavoritesSearchRequest request, UserFavoritesSortBy sortBy)
|
||||
{
|
||||
if (request == null) throw new ArgumentNullException(nameof(request));
|
||||
if (string.IsNullOrWhiteSpace(request.UserName))
|
||||
{
|
||||
throw new AppException("用户名不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var keyword = request.Title;
|
||||
int? groupId = request.GroupId;
|
||||
var groupName = request.GroupName;
|
||||
int pageIndex = request.PageIndex;
|
||||
int pageSize = request.PageSize;
|
||||
|
||||
if (pageIndex < 1) pageIndex = 1;
|
||||
if (pageSize < 1) pageSize = 20;
|
||||
if (pageSize > 2000) pageSize = 2000; // 避免深分页/单页过大拖垮性能
|
||||
|
||||
var from = (pageIndex - 1) * pageSize;
|
||||
|
||||
ISearchResponse<UserFavoritesSearchDocument> searchResponse;
|
||||
try
|
||||
{
|
||||
searchResponse = await _client.SearchAsync<UserFavoritesSearchDocument>(s => s
|
||||
.Index(_indexName)
|
||||
.Query(q => q
|
||||
.Bool(b =>
|
||||
{
|
||||
var filters = new List<Func<QueryContainerDescriptor<UserFavoritesSearchDocument>, QueryContainer>>();
|
||||
|
||||
// 只检索当前用户自己的收藏 —— 放进 filters,避免被后面的 Filter 覆盖
|
||||
filters.Add(f => f.Term(t => t.Field(x => x.UserName).Value(request.UserName)));
|
||||
|
||||
if (groupId.HasValue)
|
||||
{
|
||||
filters.Add(f => f.Term(t => t.Field(x => x.GroupId).Value(groupId.Value)));
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(groupName) == false)
|
||||
{
|
||||
filters.Add(f => f.Term(t => t.Field(x => x.GroupName).Value(groupName)));
|
||||
}
|
||||
|
||||
if (request.Platform.HasValue && request.Platform.Value > 0)
|
||||
{
|
||||
filters.Add(f => f.Term(t => t.Field(x => x.Platform).Value(request.Platform.Value)));
|
||||
}
|
||||
|
||||
if (request.CatId1.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId1).Value(request.CatId1.Value)));
|
||||
if (request.CatId2.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId2).Value(request.CatId2.Value)));
|
||||
if (request.CatId3.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId3).Value(request.CatId3.Value)));
|
||||
if (request.CatId4.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId4).Value(request.CatId4.Value)));
|
||||
|
||||
if (request.MinPrice.HasValue || request.MaxPrice.HasValue)
|
||||
{
|
||||
filters.Add(f => f.Range(r =>
|
||||
{
|
||||
var range = r.Field(x => x.MinPrice);
|
||||
if (request.MinPrice.HasValue) range = range.GreaterThanOrEquals(request.MinPrice.Value);
|
||||
if (request.MaxPrice.HasValue) range = range.LessThanOrEquals(request.MaxPrice.Value);
|
||||
return range;
|
||||
}));
|
||||
}
|
||||
|
||||
if (request.MinSoldQuantity.HasValue || request.MaxSoldQuantity.HasValue)
|
||||
{
|
||||
filters.Add(f => f.Range(r =>
|
||||
{
|
||||
var range = r.Field(x => x.SoldQuantity);
|
||||
if (request.MinSoldQuantity.HasValue) range = range.GreaterThanOrEquals(request.MinSoldQuantity.Value);
|
||||
if (request.MaxSoldQuantity.HasValue) range = range.LessThanOrEquals(request.MaxSoldQuantity.Value);
|
||||
return range;
|
||||
}));
|
||||
}
|
||||
|
||||
// 统一一次性应用所有 filter,避免覆盖
|
||||
b.Filter(filters.ToArray());
|
||||
|
||||
if (string.IsNullOrWhiteSpace(keyword) == false)
|
||||
{
|
||||
b.Must(m => m.Match(mm => mm.Field(f => f.Title).Query(keyword)));
|
||||
}
|
||||
|
||||
return b;
|
||||
})
|
||||
)
|
||||
.Sort(sort => sortBy switch
|
||||
{
|
||||
UserFavoritesSortBy.CreatedTimeAsc => sort.Ascending(f => f.CreatedAt),
|
||||
UserFavoritesSortBy.PriceAsc => sort.Ascending(f => f.MinPrice),
|
||||
UserFavoritesSortBy.PriceDesc => sort.Descending(f => f.MinPrice),
|
||||
UserFavoritesSortBy.SoldDesc => sort.Descending(f => f.SoldQuantity),
|
||||
_ => sort.Descending(f => f.CreatedAt)
|
||||
})
|
||||
.From(from)
|
||||
.Size(pageSize)
|
||||
.TrackTotalHits(true)
|
||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout))
|
||||
);}
|
||||
catch (OpenSearchClientException ex) when (ex.Response?.Success == false)
|
||||
{
|
||||
throw new AppException($"收藏检索超时或失败({QueryTimeout.TotalSeconds}秒): " + ex.Message);
|
||||
}
|
||||
|
||||
if (!searchResponse.IsValid)
|
||||
{
|
||||
throw new AppException("收藏检索失败: " + searchResponse.DebugInformation);
|
||||
}
|
||||
|
||||
return (searchResponse.Total, searchResponse.Documents.ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,603 @@
|
||||
using MAX.Core.DependencyInjection;
|
||||
using MAX.Framework.Commons;
|
||||
using MAX.Framework.Dtos;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Store.Modules.CloudProduct.Dtos;
|
||||
using Store.Modules.CloudProduct.Entities;
|
||||
using Store.Modules.CloudProduct.Enums;
|
||||
using Store.Modules.CloudProduct.Repositorys;
|
||||
|
||||
namespace Store.Modules.CloudProduct.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户收藏商品业务:收藏/取消收藏(数据库 + 收藏索引同步写入)、收藏分组维护、收藏状态查询、收藏检索与索引重建。
|
||||
/// </summary>
|
||||
public interface IUserFavoritesService : IScopedDependency
|
||||
{
|
||||
/// <summary>批量收藏商品,返回本次成功收藏的商品数量(已收藏的商品视为修改收藏分组)</summary>
|
||||
Task<int> AddFavoritesAsync(AddUserFavoriteRequest request);
|
||||
|
||||
/// <summary>批量取消收藏,返回实际取消的条数</summary>
|
||||
Task<int> RemoveFavoritesAsync(RemoveUserFavoritesRequest request);
|
||||
|
||||
/// <summary>创建收藏分组:创建成功返回 true,同名分组已存在返回 false</summary>
|
||||
Task<bool> CreateGroupAsync(CreateUserFavoriteGroupRequest request);
|
||||
|
||||
/// <summary>查询用户的收藏分组(含默认分组)及分组下收藏数量</summary>
|
||||
Task<List<UserFavoriteGroupDto>> GetGroupsAsync(string userName);
|
||||
|
||||
/// <summary>按 GoodsIds 顺序返回每个商品是否已被该用户收藏</summary>
|
||||
Task<List<bool>> GetFavoriteStatusAsync(UserFavoriteStatusRequest request);
|
||||
|
||||
/// <summary>检索用户收藏的商品(标题/销量/价格/类目/分组)</summary>
|
||||
Task<(List<UserFavoriteItem> List, long Total)> SearchFavoritesAsync(UserFavoritesSearchRequest request);
|
||||
|
||||
/// <summary>移除所有用户收藏索引并按数据库数据全量重建,返回重建的文档数</summary>
|
||||
Task<long> RebuildSearchIndexAsync();
|
||||
}
|
||||
|
||||
internal class UserFavoritesService : IUserFavoritesService
|
||||
{
|
||||
/// <summary>未归属任何分组的收藏所使用的分组名(对应 group_id = 0)</summary>
|
||||
public const string DefaultGroupName = "默认分组";
|
||||
|
||||
/// <summary>重建索引时单次 bulk 的文档数,避免一次请求过大</summary>
|
||||
private const int RebuildBatchSize = 2000;
|
||||
|
||||
private readonly UserGoodsFavoriteRepository _favoriteRepository;
|
||||
private readonly UserGoodsFavoriteGroupRepository _groupRepository;
|
||||
private readonly GoodsRepository _goodsRepository;
|
||||
private readonly IUserFavoritesIndexService _indexService;
|
||||
private readonly ILogger<UserFavoritesService> _logger;
|
||||
|
||||
public UserFavoritesService(
|
||||
UserGoodsFavoriteRepository favoriteRepository,
|
||||
UserGoodsFavoriteGroupRepository groupRepository,
|
||||
GoodsRepository goodsRepository,
|
||||
IUserFavoritesIndexService indexService,
|
||||
ILogger<UserFavoritesService> logger)
|
||||
{
|
||||
_favoriteRepository = favoriteRepository;
|
||||
_groupRepository = groupRepository;
|
||||
_goodsRepository = goodsRepository;
|
||||
_indexService = indexService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量收藏商品:先落库(user_goods_favorites),再同步写入收藏索引;
|
||||
/// 商品不存在/已删除的编号会被跳过,返回本次成功收藏的商品数量。
|
||||
/// 任意一步失败都会回滚本次已写入的数据库记录,保证“数据库中有收藏 = 索引中可搜索到”。
|
||||
/// </summary>
|
||||
public async Task<int> AddFavoritesAsync(AddUserFavoriteRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new AppException("请求参数不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var userName = request.UserName?.Trim() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(userName))
|
||||
{
|
||||
throw new AppException("用户名不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var goodsIds = request.GoodsIds?.Where(x => x > 0).Distinct().ToList() ?? new List<long>();
|
||||
if (goodsIds.Count == 0)
|
||||
{
|
||||
throw new AppException("请提供要收藏的商品编号", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
if (request.GroupId < 0)
|
||||
{
|
||||
throw new AppException("收藏分组无效", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
// 1. 校验分组归属(分组ID > 0 时必须是该用户自己创建的分组)
|
||||
if (request.GroupId > 0)
|
||||
{
|
||||
var group = await _groupRepository.Select
|
||||
.Where(x => x.Id == request.GroupId && x.UserName == userName)
|
||||
.FirstAsync();
|
||||
|
||||
if (group == null)
|
||||
{
|
||||
throw new AppException($"收藏分组不存在: {request.GroupId}", ResultCode.ValidationError);
|
||||
}
|
||||
}
|
||||
|
||||
var groupName = await ResolveGroupNameAsync(userName, request.GroupId);
|
||||
|
||||
// 2. 批量取商品快照(不存在/已删除的商品不收藏,直接跳过)
|
||||
var goodsList = await _goodsRepository.Select
|
||||
.Where(x => goodsIds.Contains(x.GoodsId) && x.Status == 0)
|
||||
.ToListAsync();
|
||||
|
||||
if (goodsList.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var goodsMap = new Dictionary<long, GoodsEntity>();
|
||||
foreach (var goods in goodsList)
|
||||
{
|
||||
goodsMap[goods.GoodsId] = goods;
|
||||
}
|
||||
|
||||
// 3. 批量取已存在的收藏记录
|
||||
var existingList = await _favoriteRepository.Select
|
||||
.Where(x => x.UserName == userName && goodsIds.Contains(x.GoodsId))
|
||||
.ToListAsync();
|
||||
|
||||
var existingMap = new Dictionary<long, UserGoodsFavoriteEntity>();
|
||||
foreach (var favorite in existingList)
|
||||
{
|
||||
existingMap[favorite.GoodsId] = favorite;
|
||||
}
|
||||
|
||||
// 4. 逐条落库:不存在则新增,已存在且分组不同则修改分组
|
||||
var insertedIds = new List<long>();
|
||||
var changedGroupIds = new List<(long Id, int PreviousGroupId)>();
|
||||
var documents = new List<UserFavoritesSearchDocument>();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var goods in goodsMap.Values)
|
||||
{
|
||||
if (existingMap.TryGetValue(goods.GoodsId, out var favorite) == false)
|
||||
{
|
||||
favorite = new UserGoodsFavoriteEntity
|
||||
{
|
||||
UserName = userName,
|
||||
GoodsId = goods.GoodsId,
|
||||
GroupId = request.GroupId,
|
||||
CreatedAt = DateTime.Now,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await _favoriteRepository.InsertAsync(favorite);
|
||||
insertedIds.Add(favorite.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 并发收藏同一商品时可能命中唯一键 uk_user_goods,回读已存在记录后按“修改分组”处理
|
||||
favorite = await _favoriteRepository.Select
|
||||
.Where(x => x.UserName == userName && x.GoodsId == goods.GoodsId)
|
||||
.FirstAsync();
|
||||
|
||||
if (favorite == null)
|
||||
{
|
||||
throw new AppException($"收藏商品失败({goods.GoodsId}): {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (favorite.GroupId != request.GroupId)
|
||||
{
|
||||
changedGroupIds.Add((favorite.Id, favorite.GroupId));
|
||||
await _favoriteRepository.Orm.Update<UserGoodsFavoriteEntity>()
|
||||
.Set(x => x.GroupId, request.GroupId)
|
||||
.Where(x => x.Id == favorite.Id)
|
||||
.ExecuteAffrowsAsync();
|
||||
favorite.GroupId = request.GroupId;
|
||||
}
|
||||
|
||||
documents.Add(BuildDocument(favorite, goods, groupName));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
await CompensateWriteFailureAsync(insertedIds, changedGroupIds);
|
||||
throw;
|
||||
}
|
||||
|
||||
// 5. 批量同步写入索引
|
||||
try
|
||||
{
|
||||
await _indexService.IndexDocumentsAsync(documents);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await CompensateWriteFailureAsync(insertedIds, changedGroupIds);
|
||||
throw;
|
||||
}
|
||||
|
||||
return documents.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量取消收藏:先删除数据库记录,再删除索引文档;
|
||||
/// 索引删除失败时回补数据库记录,避免“库里有收藏但搜索不到”。
|
||||
/// </summary>
|
||||
public async Task<int> RemoveFavoritesAsync(RemoveUserFavoritesRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new AppException("请求参数不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var userName = request.UserName?.Trim() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(userName))
|
||||
{
|
||||
throw new AppException("用户名不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var goodsIds = request.GoodsIds?.Where(x => x > 0).Distinct().ToList() ?? new List<long>();
|
||||
if (goodsIds.Count == 0)
|
||||
{
|
||||
throw new AppException("请提供要取消收藏的商品编号", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var favorites = await _favoriteRepository.Select
|
||||
.Where(x => x.UserName == userName && goodsIds.Contains(x.GoodsId))
|
||||
.ToListAsync();
|
||||
|
||||
if (favorites.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
await _favoriteRepository.Orm.Delete<UserGoodsFavoriteEntity>()
|
||||
.Where(x => x.UserName == userName && goodsIds.Contains(x.GoodsId))
|
||||
.ExecuteAffrowsAsync();
|
||||
|
||||
try
|
||||
{
|
||||
await _indexService.DeleteDocumentsAsync(
|
||||
favorites.Select(x => UserFavoritesSearchDocument.BuildId(x.UserName, x.GoodsId)));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 索引删除失败:回补数据库记录(自增主键会重新生成,检索以“用户名+商品编号”为准,不受影响)
|
||||
_logger.LogError(ex, "删除收藏索引失败,回补数据库收藏记录。UserName={UserName} Count={Count}", userName, favorites.Count);
|
||||
await _favoriteRepository.InsertAsync(favorites);
|
||||
throw;
|
||||
}
|
||||
|
||||
return favorites.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建收藏分组:创建成功返回 true,同一用户同名分组已存在返回 false。
|
||||
/// </summary>
|
||||
public async Task<bool> CreateGroupAsync(CreateUserFavoriteGroupRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new AppException("请求参数不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var userName = request.UserName?.Trim() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(userName))
|
||||
{
|
||||
throw new AppException("用户名不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var groupName = request.GroupName?.Trim() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(groupName))
|
||||
{
|
||||
throw new AppException("分组名不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
if (groupName.Length > 255)
|
||||
{
|
||||
throw new AppException("分组名长度不能超过255个字符", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var existing = await _groupRepository.Select
|
||||
.Where(x => x.UserName == userName && x.GroupName == groupName)
|
||||
.FirstAsync();
|
||||
|
||||
if (existing != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
await _groupRepository.InsertAsync(new UserGoodsFavoriteGroupEntity
|
||||
{
|
||||
UserName = userName,
|
||||
GroupName = groupName,
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询用户的收藏分组及分组下收藏数量,默认分组(分组ID=0)存在收藏时一并返回。
|
||||
/// </summary>
|
||||
public async Task<List<UserFavoriteGroupDto>> GetGroupsAsync(string userName)
|
||||
{
|
||||
userName = userName?.Trim() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(userName))
|
||||
{
|
||||
throw new AppException("用户名不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var groups = await _groupRepository.Select
|
||||
.Where(x => x.UserName == userName)
|
||||
.OrderBy(x => x.Id)
|
||||
.ToListAsync();
|
||||
|
||||
var countList = await _favoriteRepository.Select
|
||||
.Where(x => x.UserName == userName)
|
||||
.GroupBy(x => x.GroupId)
|
||||
.ToListAsync(x => new { GroupId = x.Key, Count = x.Count() });
|
||||
|
||||
var countMap = countList.ToDictionary(x => x.GroupId, x => (long)x.Count);
|
||||
|
||||
var result = new List<UserFavoriteGroupDto>();
|
||||
|
||||
if (countMap.TryGetValue(0, out var defaultCount) && defaultCount > 0)
|
||||
{
|
||||
result.Add(new UserFavoriteGroupDto
|
||||
{
|
||||
Id = 0,
|
||||
UserName = userName,
|
||||
GroupName = DefaultGroupName,
|
||||
FavoriteCount = defaultCount,
|
||||
});
|
||||
}
|
||||
|
||||
result.AddRange(groups.Select(x => new UserFavoriteGroupDto
|
||||
{
|
||||
Id = x.Id,
|
||||
UserName = x.UserName,
|
||||
GroupName = x.GroupName,
|
||||
FavoriteCount = countMap.TryGetValue((int)x.Id, out var count) ? count : 0,
|
||||
}));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按 GoodsIds 顺序返回每个商品是否已被该用户收藏(商品编号无效的返回 false)。
|
||||
/// 以数据库为准,不依赖索引,索引重建期间结果依然准确。
|
||||
/// </summary>
|
||||
public async Task<List<bool>> GetFavoriteStatusAsync(UserFavoriteStatusRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new AppException("请求参数不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var userName = request.UserName?.Trim() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(userName))
|
||||
{
|
||||
throw new AppException("用户名不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var goodsIds = request.GoodsIds ?? new List<long>();
|
||||
if (goodsIds.Count == 0)
|
||||
{
|
||||
return new List<bool>();
|
||||
}
|
||||
|
||||
var validIds = goodsIds.Where(x => x > 0).Distinct().ToList();
|
||||
var favoritedIds = new HashSet<long>();
|
||||
|
||||
if (validIds.Count > 0)
|
||||
{
|
||||
var favorited = await _favoriteRepository.Select
|
||||
.Where(x => x.UserName == userName && validIds.Contains(x.GoodsId))
|
||||
.ToListAsync(x => x.GoodsId);
|
||||
|
||||
favoritedIds = favorited.ToHashSet();
|
||||
}
|
||||
|
||||
// 结果与入参顺序一一对应,前台可直接按下标渲染
|
||||
return goodsIds.Select(x => x > 0 && favoritedIds.Contains(x)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从收藏索引检索当前用户的收藏商品。
|
||||
/// </summary>
|
||||
public async Task<(List<UserFavoriteItem> List, long Total)> SearchFavoritesAsync(UserFavoritesSearchRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new AppException("请求参数不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
var userName = request.UserName?.Trim() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(userName))
|
||||
{
|
||||
throw new AppException("用户名不能为空", ResultCode.ValidationError);
|
||||
}
|
||||
|
||||
request.UserName = userName;
|
||||
|
||||
var searchResult = await _indexService.SearchAsync(request, ParseSortBy(request.SortBy));
|
||||
var list = searchResult.Documents.Select(ToItem).ToList();
|
||||
return (list, searchResult.Total);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除所有用户收藏索引并重新创建,然后从 user_goods_favorites 关联 goods 全量回填。
|
||||
/// </summary>
|
||||
public async Task<long> RebuildSearchIndexAsync()
|
||||
{
|
||||
// 1. 删除并重建索引(清空所有用户收藏索引)
|
||||
await _indexService.ClearIndexAsync();
|
||||
|
||||
// 2. 从数据库读取全部收藏记录
|
||||
var favorites = await _favoriteRepository.Select
|
||||
.OrderBy(x => x.Id)
|
||||
.ToListAsync();
|
||||
|
||||
if (favorites.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 3. 关联 goods 补全商品数据(标题/价格/销量/类目/平台/缩略图)
|
||||
var goodsIds = favorites.Select(x => x.GoodsId).Distinct().ToList();
|
||||
var goodsList = await _goodsRepository.Select
|
||||
.Where(x => goodsIds.Contains(x.GoodsId))
|
||||
.ToListAsync();
|
||||
|
||||
var goodsMap = new Dictionary<long, GoodsEntity>();
|
||||
foreach (var goods in goodsList)
|
||||
{
|
||||
goodsMap[goods.GoodsId] = goods;
|
||||
}
|
||||
|
||||
// 4. 关联分组表补全分组名
|
||||
var groupIds = favorites.Where(x => x.GroupId > 0).Select(x => (long)x.GroupId).Distinct().ToList();
|
||||
var groupMap = new Dictionary<int, string>();
|
||||
|
||||
if (groupIds.Count > 0)
|
||||
{
|
||||
var groups = await _groupRepository.Select
|
||||
.Where(x => groupIds.Contains(x.Id))
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var group in groups)
|
||||
{
|
||||
groupMap[(int)group.Id] = group.GroupName;
|
||||
}
|
||||
}
|
||||
|
||||
var documents = new List<UserFavoritesSearchDocument>();
|
||||
var missingGoodsCount = 0;
|
||||
|
||||
foreach (var favorite in favorites)
|
||||
{
|
||||
if (goodsMap.TryGetValue(favorite.GoodsId, out var goods) == false)
|
||||
{
|
||||
// 商品已被物理删除,跳过该收藏(不写入索引)
|
||||
missingGoodsCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
var groupName = favorite.GroupId <= 0
|
||||
? DefaultGroupName
|
||||
: (groupMap.TryGetValue(favorite.GroupId, out var name) ? name : string.Empty);
|
||||
|
||||
documents.Add(BuildDocument(favorite, goods, groupName));
|
||||
}
|
||||
|
||||
// 5. 分批写入索引
|
||||
for (var offset = 0; offset < documents.Count; offset += RebuildBatchSize)
|
||||
{
|
||||
await _indexService.IndexDocumentsAsync(documents.Skip(offset).Take(RebuildBatchSize));
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"用户收藏索引重建完成:收藏 {FavoriteCount} 条,写入索引 {IndexedCount} 条,商品缺失跳过 {MissingCount} 条。",
|
||||
favorites.Count, documents.Count, missingGoodsCount);
|
||||
|
||||
return documents.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 收藏落库成功但索引写入失败时的补偿:撤销本次新增的收藏记录、还原被修改的收藏分组。
|
||||
/// </summary>
|
||||
private async Task CompensateWriteFailureAsync(List<long> insertedIds, List<(long Id, int PreviousGroupId)> changedGroupIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (insertedIds.Count > 0)
|
||||
{
|
||||
await _favoriteRepository.Orm.Delete<UserGoodsFavoriteEntity>()
|
||||
.Where(x => insertedIds.Contains(x.Id))
|
||||
.ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
foreach (var (id, previousGroupId) in changedGroupIds)
|
||||
{
|
||||
await _favoriteRepository.Orm.Update<UserGoodsFavoriteEntity>()
|
||||
.Set(x => x.GroupId, previousGroupId)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex,
|
||||
"回滚用户收藏记录失败。Inserted={InsertedCount} GroupChanged={GroupChangedCount}",
|
||||
insertedIds.Count, changedGroupIds.Count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取分组名:分组ID <= 0 返回默认分组名,分组不存在返回空串。
|
||||
/// </summary>
|
||||
private async Task<string> ResolveGroupNameAsync(string userName, int groupId)
|
||||
{
|
||||
if (groupId <= 0)
|
||||
{
|
||||
return DefaultGroupName;
|
||||
}
|
||||
|
||||
var group = await _groupRepository.Select
|
||||
.Where(x => x.Id == groupId && x.UserName == userName)
|
||||
.FirstAsync();
|
||||
|
||||
return group?.GroupName ?? string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 组装收藏索引文档:收藏记录 + 商品快照 + 分组名。
|
||||
/// </summary>
|
||||
private static UserFavoritesSearchDocument BuildDocument(UserGoodsFavoriteEntity favorite, GoodsEntity goods, string groupName)
|
||||
{
|
||||
return new UserFavoritesSearchDocument
|
||||
{
|
||||
Id = UserFavoritesSearchDocument.BuildId(favorite.UserName, favorite.GoodsId),
|
||||
FavId = favorite.Id,
|
||||
UserName = favorite.UserName,
|
||||
GroupId = favorite.GroupId,
|
||||
GroupName = groupName,
|
||||
GoodsId = goods.GoodsId,
|
||||
Platform = goods.Platform,
|
||||
Title = goods.Title ?? string.Empty,
|
||||
ThumbUrl = goods.ThumbUrl,
|
||||
CatId1 = goods.CatId1,
|
||||
CatId2 = goods.CatId2,
|
||||
CatId3 = goods.CatId3,
|
||||
CatId4 = goods.CatId4,
|
||||
MinPrice = goods.MinPrice,
|
||||
SoldQuantity = goods.SoldQuantity,
|
||||
CreatedAt = favorite.CreatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
private static UserFavoriteItem ToItem(UserFavoritesSearchDocument document)
|
||||
{
|
||||
return new UserFavoriteItem
|
||||
{
|
||||
Id = document.FavId,
|
||||
UserName = document.UserName,
|
||||
GroupId = document.GroupId,
|
||||
GroupName = document.GroupName,
|
||||
GoodsId = document.GoodsId,
|
||||
Platform = document.Platform,
|
||||
Title = document.Title,
|
||||
ThumbUrl = document.ThumbUrl,
|
||||
CatId1 = document.CatId1,
|
||||
CatId2 = document.CatId2,
|
||||
CatId3 = document.CatId3,
|
||||
CatId4 = document.CatId4,
|
||||
MinPrice = document.MinPrice,
|
||||
SoldQuantity = document.SoldQuantity,
|
||||
CreatedAt = document.CreatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析排序方式,非法值一律按“收藏时间倒序”处理。
|
||||
/// </summary>
|
||||
private static UserFavoritesSortBy ParseSortBy(string? sortBy)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sortBy))
|
||||
{
|
||||
return UserFavoritesSortBy.CreatedTimeDesc;
|
||||
}
|
||||
|
||||
return Enum.TryParse<UserFavoritesSortBy>(sortBy, ignoreCase: true, out var value)
|
||||
? value
|
||||
: UserFavoritesSortBy.CreatedTimeDesc;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user