trait:支持单条轨迹删除

This commit is contained in:
xiaolipro 2024-08-13 09:39:38 +08:00
parent d3627b7a66
commit a57e58459f
3 changed files with 51 additions and 76 deletions

View File

@ -109,6 +109,25 @@ public class TrackOrderService : ApplicationService
return new List<TrackOrderDetailDto>();
}
/// <summary>
/// 根据轨迹id删除单条轨迹
/// </summary>
/// <param name="trackId">轨迹id</param>
/// <returns></returns>
[HttpPost("GetDetails")]
public async Task<bool> DeleteTrackByIdAsync(string trackId)
{
var track = await _trackDetailsRepository.GetByIdAsync(trackId);
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (track == null)
{
throw new BusinessException(message: "轨迹不存在!");
}
await _trackDetailsRepository.DeleteTrackByIdAsync(trackId, track.IndexName);
return true;
}
#endregion
#region 轨迹订单列表

View File

@ -1,29 +1,28 @@
using SJZY.Expand.ElasticSearch.Services;
using SJZY.Track.Domain.Dtos.TrackDetails;
namespace SJZY.Track.Domain.Aggregates.TrackDetailsAggregate;
public interface ITrackDetailsRepository
public interface ITrackDetailsRepository : IElasticsearchClientService<TrackDetailsIndex>
{
Task<List<TrackDetailsIndex>> GetListAsync(int[] orderIds, string trackCode, bool forUpsert = false);
Task<List<TrackDetailsIndex>> GetLatestListAsync(int[] eventOrderIds, List<string> trackCodes);
Task<(List<TrackDetailsIndex> results, object[]? nextSearchAfter)> GetListAsync(int orderType, List<string> orderNos, object[]? searchAfter = null, int limit = 1000);
Task<bool> AnyAsync(int orderId, DateTime trackTime);
Task<(List<TrackDetailsIndex> results, object[]? nextSearchAfter)> GetListAsync(int orderType,
List<string> orderNos, object[]? searchAfter = null, int limit = 1000);
Task<bool> AnyAsync(int orderId, string originTrackDataHash);
Task<bool> AnyByCodeAsync(int orderId, string trackCode);
Task UpdateAsync(TrackDetailsIndex index);
Task AddManyAsync(List<TrackDetailsIndex> index);
Task AddAsync(TrackDetailsIndex track);
Task<(List<TrackDetailsIndex> list, long total)> GetListAsync(TrackDetailsQueryInput input);
Task<List<TrackDetailsIndex>> GetListAsync(int orderId);
Task DeleteTrackAsync(int[] orderIds, List<string> trackCodes);
Task DeleteTrackByIdAsync(string id, string indexName);
}

View File

@ -3,7 +3,6 @@ using SJZY.Expand.ABP.Core.PublicMethords;
using SJZY.Expand.ElasticSearch.Services;
using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate;
using SJZY.Track.Domain.Consts;
using SJZY.Track.Domain.Dtos.TrackDetails;
namespace SJZY.Track.Repository.Repositories;
@ -50,7 +49,8 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!r.IsValid)
{
throw new Exception($"轨迹查询异常,原因:{r.DebugInformation}。");
throw new InvalidOperationException(
$"轨迹查询失败,orderIds:[{orderIds.JoinAsString(", ")}],trackCode:{trackCode},原因:{r.DebugInformation}。");
}
return r.ToIdList();
@ -101,30 +101,13 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!r.IsValid)
{
throw new Exception($"轨迹查询异常,原因:{r.DebugInformation}。");
throw new InvalidOperationException(
$"最新轨迹查询失败,orderIds:[{orderIds.JoinAsString(", ")}],trackCodes:[{trackCodes.JoinAsString(", ")}],原因:{r.DebugInformation}。");
}
return r.Documents.ToList();
}
public async Task<bool> AnyAsync(int orderId, DateTime trackTime)
{
var r = await Context.SearchAsync<TrackDetailsIndex>(x => x
.Source(s => s.Includes(fd => fd.Field(f => f.TrackCode)))
.Query(q =>
q.Term(f => f.OrderId, orderId)
&& q.Term(f => f.TrackTime, trackTime)
)
);
if (!r.IsValid)
{
throw new Exception($"轨迹查询异常,原因:{r.DebugInformation}。");
}
return r.Hits.Count > 0;
}
public async Task<bool> AnyAsync(int orderId, string originTrackDataHash)
{
var r = await Context.SearchAsync<TrackDetailsIndex>(x => x
@ -137,25 +120,8 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!r.IsValid)
{
throw new Exception($"轨迹查询异常,原因:{r.DebugInformation}。");
}
return r.Hits.Count > 0;
}
public async Task<bool> AnyByCodeAsync(int orderId, string trackCode)
{
var r = await Context.SearchAsync<TrackDetailsIndex>(x => x
.Source(s => s.Includes(fd => fd.Field(f => f.TrackCode)))
.Query(q =>
q.Term(f => f.OrderId, orderId)
&& q.Term(f => f.TrackCode.Suffix("keyword"), trackCode)
)
);
if (!r.IsValid)
{
throw new Exception($"轨迹查询异常,原因:{r.DebugInformation}。");
throw new InvalidOperationException(
$"轨迹查询失败,orderId:{orderId},hash:{originTrackDataHash},原因:{r.DebugInformation}。");
}
return r.Hits.Count > 0;
@ -178,7 +144,7 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!r.IsValid)
{
throw new Exception($"轨迹更新失败{track.ToJson()},原因:{r.DebugInformation}。");
throw new InvalidOperationException($"轨迹更新失败,track:{track.ToJson()},原因:{r.DebugInformation}。");
}
}
@ -193,7 +159,7 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!r.IsValid)
{
throw new Exception($"{tracks.Count}条轨迹新增失败,原因:{r.DebugInformation}。");
throw new InvalidOperationException($"{tracks.Count}条轨迹新增失败,原因:{r.DebugInformation}。");
}
}
@ -206,11 +172,12 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!r.IsValid)
{
throw new Exception($"轨迹新增失败,原因:{r.DebugInformation}。");
throw new InvalidOperationException($"轨迹新增失败,track:{track.ToJson()},原因:{r.DebugInformation}。");
}
}
public async Task<(List<TrackDetailsIndex> results, object[]? nextSearchAfter)> GetListAsync(int orderType, List<string> orderNos, object[]? searchAfter = null, int limit = 1000)
public async Task<(List<TrackDetailsIndex> results, object[]? nextSearchAfter)> GetListAsync(int orderType,
List<string> orderNos, object[]? searchAfter = null, int limit = 1000)
{
if (orderNos.Count < 1) return (new List<TrackDetailsIndex>(), null);
@ -242,7 +209,7 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
)
.Sort(s => s
.Ascending(f => f.TrackTime) // 需要根据某个字段排序
.Ascending(f => f.OrderNo) // 可以添加更多排序字段以确保唯一性
.Ascending(f => f.OrderNo) // 可以添加更多排序字段以确保唯一性
)
.Size(limit)
.SearchAfter(searchAfter)
@ -250,7 +217,8 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!r.IsValid)
{
throw new Exception($"轨迹查询异常,原因:{r.DebugInformation}。");
throw new InvalidOperationException(
$"轨迹查询失败,orderType:{orderType},orderNos:{orderNos},原因:{r.DebugInformation}。");
}
var results = r.Documents.ToList();
@ -270,10 +238,10 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!r.IsValid)
{
throw new Exception($"轨迹查询异常,原因:{r.DebugInformation}。");
throw new InvalidOperationException($"轨迹查询失败,orderId:{orderId},原因:{r.DebugInformation}。");
}
return r.Documents.ToList();
return r.ToIdList();
}
public async Task DeleteTrackAsync(int[] orderIds, List<string> trackCodes)
@ -289,38 +257,27 @@ internal class TrackDetailsRepository : ElasticSearchContextAbstract<TrackDetail
if (!search.IsValid)
{
throw new Exception($"轨迹查询异常,原因:{search.DebugInformation}。");
throw new InvalidOperationException(
$"轨迹查询失败,orderIds:[{orderIds.JoinAsString(", ")}],trackCodes:[{trackCodes.JoinAsString(", ")}],原因:{search.DebugInformation}。");
}
var tracks = search.ToIdList();
foreach (var track in tracks)
{
await Context.DeleteAsync<TrackDetailsIndex>(track.Id, u => u
.Index(track.IndexName)
);
await DeleteTrackByIdAsync(track.Id, track.IndexName);
}
}
public async Task<(List<TrackDetailsIndex> list, long total)> GetListAsync(TrackDetailsQueryInput input)
public async Task DeleteTrackByIdAsync(string id, string indexName)
{
if (input.OrderNos.Count < 1) return default;
var distinctOrderNumbers = input.OrderNos.Distinct();
var finalSize = input.PageSize > 500 ? 500 : input.PageSize;
var data = await Context.SearchAsync<TrackDetailsIndex>(x => x
.Query(q =>
q.Terms(t => t
.Field(f => f.OrderNo)
.Terms(distinctOrderNumbers))
)
.Sort(s => s.Field(f => f.CreateTime, SortOrder.Descending))
.From((input.PageIndex - 1) * input.PageSize)
.Size(finalSize)
.TrackTotalHits() // todo: 聚合获取匹配查询条件的文档总数,而不受"index.max_result_window"的限制
var r = await Context.DeleteAsync<TrackDetailsIndex>(id, u => u
.Index(indexName)
);
return (data.Documents.ToList(), data.Total);
if (!r.IsValid)
{
throw new InvalidOperationException($"轨迹删除失败,id:{indexName},原因:{r.DebugInformation}。");
}
}
}