track/src/TrackCenter.Application/Services/TrackPull/TrackPullService.cs

384 lines
15 KiB
C#

using System.ComponentModel;
using System.Linq.Expressions;
using Fake.Application.Dtos;
using Microsoft.Extensions.Options;
using TrackCenter.Application.Dtos.TrackPull;
using TrackCenter.Domain.Aggregates.TrackPullAggregate;
using TrackCenter.Domain.Dtos.TrackPull;
using SqlSugar;
using TrackCenter.Application.Helpers;
using TrackCenter.Application.Options;
namespace TrackCenter.Application.Services.TrackPull;
/// <summary>
/// 轨迹拉取
/// </summary>
[ApiExplorerSettings(GroupName = "轨迹拉取")]
[Route("/Track/PullConfig")]
public class TrackPullService(
ITrackPullConfigRepository trackPullConfigRepository,
TrackPullCacheService trackPullCacheService,
IOptionsMonitor<InterfaceRelayParamsConfigOptions> options)
: ApplicationServiceBase
{
/// <summary>
/// 轨迹拉取配置列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetPage")]
public async Task<PagedResult<GetTrackPullConfigPageOutputVO>> GetPageAsync([FromBody] KeywordsInputVO input)
{
return await trackPullConfigRepository.GetPageAsync(input);
}
/// <summary>
/// 轨迹拉取配置内容列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetContentPage")]
public async Task<PagedResult<GetTrackPullContentPageOutputVO>> GetContentPageAsync(
[FromBody] GetTrackPullContentPageInputVO input)
{
return await trackPullConfigRepository.GetContentPageAsync(input);
}
/// <summary>
/// 轨迹拉取配置详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("GetDetail")]
public async Task<GetTrackPullConfigDetailOutputVO> GetDetailAsync(int id)
{
var res = await trackPullConfigRepository.GetDetailAsync(id);
if (res == null)
{
throw new BusinessException(message: "轨迹拉取配置不存在");
}
return res;
}
/// <summary>
/// 轨迹拉取配置内容详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("GetContentDetail")]
public async Task<GetTrackPullContentDetailOutputVO> GetContentDetailAsync(int id)
{
return await trackPullConfigRepository.GetContentDetailAsync(id);
}
/// <summary>
/// 轨迹拉取配置编辑
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Edit")]
public async Task<bool> EditAsync([FromBody] EditTrackPullConfigInput input)
{
//if (input.PullInterval < 60) input.PullInterval = 60;
if (input.Options != OptionTypeNoNull.删除)
{
Expression<Func<TrackPullConfigEntity, bool>> isAnyExp = x =>
x.AppKey == input.AppKey &&
x.ServiceProviderCode == input.ServiceProviderCode;
if (input.Options == OptionTypeNoNull.修改) isAnyExp = isAnyExp.And(x => x.Id != input.Id);
if (await trackPullConfigRepository.IsAnyAsync(isAnyExp))
throw new BusinessException("配置已经存在");
}
switch (input.Options)
{
case OptionTypeNoNull.新增:
{
var trackPullEntity = ObjectMapper.Map<EditTrackPullConfigInput, TrackPullConfigEntity>(input);
if (input.ParseMethod == (int) ParseMethod.InterfaceRelay)
{
var paramsConfig = options.CurrentValue;
if (paramsConfig == null)
{
throw new BusinessException(message: "未找到接口通参数配置,联系管理员添加");
}
trackPullEntity.OrderPath = paramsConfig.OrderPath;
trackPullEntity.OrderNoName = paramsConfig.OrderNoName;
trackPullEntity.TrackDetailsPath = paramsConfig.TrackDetailsPath;
trackPullEntity.TrackContentCodeName = paramsConfig.TrackContentCodeName;
trackPullEntity.TrackStatusName = paramsConfig.TrackStatusName;
trackPullEntity.TrackContentName = paramsConfig.TrackContentName;
trackPullEntity.TrackPlaceName = paramsConfig.TrackPlaceName;
trackPullEntity.TrackIsoTimeName = paramsConfig.TrackIsoTimeName;
trackPullEntity.TrackTimeName = paramsConfig.TrackTimeName;
trackPullEntity.TrackTimeZoneName = paramsConfig.TrackTimeZoneName;
}
using (var ts = TransacationHelper.GetReadCommitted())
{
var id = await trackPullConfigRepository.InsertReturnIdentityAsync(trackPullEntity);
// 新增拉取配置的时候默认加一条所有客户的拉取优先级,兜底,该记录无法被删除
await trackPullConfigRepository.InsertPriorityAsync(new TrackPullPriorityEntity
{
TrackPullConfigId = id,
Customer = GlobalConst.All,
OrderType = TrackOrderType.小包,
OrderNoType = TrackOrderNoType.跟踪单号,
Priority = 999,
PullInterval = 12 * 60 // 12小时
});
await trackPullCacheService.SetPullConfigAsync(id);
ts.Complete();
}
}
break;
case OptionTypeNoNull.修改:
{
var oldTrackPullEntity = await trackPullConfigRepository.GetFirstAsync(x => x.Id == input.Id) ??
throw new BusinessException("配置不存在");
ObjectMapper.Map(input, oldTrackPullEntity);
if (input.ParseMethod == (int) ParseMethod.InterfaceRelay)
{
var paramsConfig = options.CurrentValue;
if (paramsConfig == null)
{
throw new BusinessException(message: "未找到接口通参数配置,联系管理员添加");
}
oldTrackPullEntity.OrderPath = paramsConfig.OrderPath;
oldTrackPullEntity.OrderNoName = paramsConfig.OrderNoName;
oldTrackPullEntity.TrackDetailsPath = paramsConfig.TrackDetailsPath;
oldTrackPullEntity.TrackContentCodeName = paramsConfig.TrackContentCodeName;
oldTrackPullEntity.TrackStatusName = paramsConfig.TrackStatusName;
oldTrackPullEntity.TrackContentName = paramsConfig.TrackContentName;
oldTrackPullEntity.TrackPlaceName = paramsConfig.TrackPlaceName;
oldTrackPullEntity.TrackIsoTimeName = paramsConfig.TrackIsoTimeName;
oldTrackPullEntity.TrackTimeName = paramsConfig.TrackTimeName;
oldTrackPullEntity.TrackTimeZoneName = paramsConfig.TrackTimeZoneName;
}
await trackPullConfigRepository.UpdateAsync(oldTrackPullEntity);
await trackPullCacheService.SetPullConfigAsync(input.Id!.Value);
}
break;
case OptionTypeNoNull.删除:
using (var ts = TransacationHelper.GetReadCommitted())
{
await trackPullConfigRepository.SoftDeleteAsync(x => x.Id == input.Id);
await trackPullConfigRepository.SoftDeleteContentAsync(x => x.TrackPullConfigId == input.Id);
await trackPullConfigRepository.SoftDeletePriorityAsync(x => x.TrackPullConfigId == input.Id);
await trackPullCacheService.SetPullConfigAsync(input.Id!.Value);
ts.Complete();
}
break;
default:
throw new InvalidEnumArgumentException();
}
return true;
}
/// <summary>
/// 轨迹拉取配置内容编辑
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("EditContent")]
public async Task<bool> EditContentAsync([FromBody] EditTrackPullContentInput input)
{
if (input.Options != OptionTypeNoNull.删除)
{
Expression<Func<TrackPullContentConfigEntity, bool>> isContentAnyExp = x =>
x.TrackPullConfigId == input.TrackPullConfigId;
if (input.Options == OptionTypeNoNull.修改)
{
isContentAnyExp = isContentAnyExp.And(x => x.Id != input.Id);
}
if (await trackPullConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x =>
x.TrackContentId == input.TrackContentId)))
throw new BusinessException("配置已存在");
if (!input.TrackStatusValues.IsNullOrEmpty())
{
Expression<Func<TrackPullContentConfigEntity, bool>> trackStatusValuesExp = _ => false;
input.TrackStatusValues!.ForEach(item =>
{
trackStatusValuesExp = trackStatusValuesExp.Or(x => SqlFunc.SplitIn(x.TrackStatusValue, item));
});
if (await trackPullConfigRepository.IsContentAnyAsync(isContentAnyExp.And(trackStatusValuesExp)))
throw new BusinessException(message: "配置已存在");
}
if (!input.TrackKeywords.IsNullOrEmpty())
{
Expression<Func<TrackPullContentConfigEntity, bool>> trackKeywordExp = _ => false;
input.TrackKeywords!.ForEach(item =>
{
trackKeywordExp = trackKeywordExp.Or(x => SqlFunc.SplitIn(x.TrackKeyword, item));
});
if (await trackPullConfigRepository.IsContentAnyAsync(isContentAnyExp.And(trackKeywordExp)))
throw new BusinessException(message: "配置已存在");
}
}
switch (input.Options)
{
case OptionTypeNoNull.新增:
{
var entity = ObjectMapper.Map<EditTrackPullContentInput, TrackPullContentConfigEntity>(input);
await trackPullConfigRepository.InsertContentAsync(entity);
await trackPullCacheService.SetPullConfigAsync(input.TrackPullConfigId!.Value);
break;
}
case OptionTypeNoNull.修改:
{
var oldTrackPullContentEntity =
await trackPullConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
throw new BusinessException(message: "找不到数据");
ObjectMapper.Map(input, oldTrackPullContentEntity);
await trackPullConfigRepository.UpdateContentAsync(oldTrackPullContentEntity);
await trackPullCacheService.SetPullConfigAsync(oldTrackPullContentEntity.TrackPullConfigId);
break;
}
case OptionTypeNoNull.删除:
{
var oldTrackPullContentEntity =
await trackPullConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
throw new BusinessException(message: "找不到数据");
await trackPullConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
await trackPullCacheService.SetPullConfigAsync(oldTrackPullContentEntity.TrackPullConfigId);
break;
}
default:
throw new ArgumentOutOfRangeException();
}
return true;
}
/// <summary>
/// 轨迹拉取优先级配置列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetPriorityPage")]
public async Task<PagedResult<GetTrackPullPriorityPageOutputVO>> GetPriorityPageAsync(
[FromBody] GetTrackPullPriorityPageInputVO input)
{
return await trackPullConfigRepository.GetPriorityPageAsync(input);
}
/// <summary>
/// 轨迹拉取优先级配置详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("GetPriorityDetail")]
public async Task<GetTrackPullPriorityDetailOutputVO> GetPriorityDetailAsync(int id)
{
return await trackPullConfigRepository.GetPriorityDetailAsync(id);
}
/// <summary>
/// 轨迹拉取优先级配置编辑
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("EditPriority")]
public async Task<bool> EditPriorityAsync([FromBody] EditTrackPullPriorityInput input)
{
if (input.Options != OptionTypeNoNull.删除)
{
Expression<Func<TrackPullPriorityEntity, bool>> isPriorityAnyExp = x =>
x.TrackPullConfigId == input.TrackPullConfigId &&
x.Customer == input.Customer &&
x.OrderType == input.OrderType &&
x.OrderNoType == input.OrderNoType;
if (input.Options == OptionTypeNoNull.修改)
{
isPriorityAnyExp = isPriorityAnyExp.And(x => x.Id != input.Id);
}
if (await trackPullConfigRepository.IsPriorityAnyAsync(isPriorityAnyExp))
throw new BusinessException("配置已存在");
if (input.PullInterval < 60) input.PullInterval = 60;
}
switch (input.Options)
{
case OptionTypeNoNull.新增:
{
var entity = ObjectMapper.Map<EditTrackPullPriorityInput, TrackPullPriorityEntity>(input);
await trackPullConfigRepository.InsertPriorityAsync(entity);
break;
}
case OptionTypeNoNull.修改:
{
var oldTrackPullPriorityEntity =
await trackPullConfigRepository.GetPriorityByIdAsync(input.Id!.Value) ??
throw new BusinessException(message: "找不到数据");
ObjectMapper.Map(input, oldTrackPullPriorityEntity);
await trackPullConfigRepository.UpdatePriorityAsync(oldTrackPullPriorityEntity);
break;
}
case OptionTypeNoNull.删除:
{
var priority = await trackPullConfigRepository.GetPriorityByIdAsync(input.Id!.Value) ??
throw new BusinessException(message: "找不到数据");
if (priority.Customer == GlobalConst.All) throw new BusinessException(message: "兜底配置禁止被删除");
await trackPullConfigRepository.SoftDeletePriorityAsync(x => x.Id == input.Id);
break;
}
default:
throw new ArgumentOutOfRangeException();
}
return true;
}
}