using System.Linq.Expressions;
using Microsoft.Extensions.Localization;
using SJZY.Expand.ABP.Core.Localization;
using SJZY.Expand.ABP.Core.PublicMethords;
using SJZY.Track.Application.Contract.Dtos.TrackPull;
using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Application.Localization;
using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
using SJZY.Track.Domain.Dtos.Common;
using SJZY.Track.Domain.Dtos.TrackPull;
using SqlSugar;
namespace SJZY.Track.Application.Services.TrackPull;
///
/// 轨迹拉取
///
[ApiExplorerSettings(GroupName = "轨迹拉取")]
[Route("/Track/PullConfig")]
public class TrackPullService : ApplicationService
{
private readonly IStringLocalizer _stringLocalizer;
private readonly ITrackPullCacheService _trackPullCacheService;
private readonly ITrackPullConfigRepository _trackPullConfigRepository;
public TrackPullService(
IStringLocalizer stringLocalizer,
ITrackPullConfigRepository trackPullConfigRepository,
ITrackPullCacheService trackPullCacheService)
{
_stringLocalizer = stringLocalizer;
_trackPullConfigRepository = trackPullConfigRepository;
_trackPullCacheService = trackPullCacheService;
}
///
/// 轨迹拉取配置列表
///
///
///
[HttpPost("GetPage")]
public async Task> GetPageAsync([FromBody] KeywordsInputVO input)
{
return await _trackPullConfigRepository.GetPageAsync(input);
}
///
/// 轨迹拉取配置内容列表
///
///
///
[HttpPost("GetContentPage")]
public async Task> GetContentPageAsync(
[FromBody] GetTrackPullContentPageInputVO input)
{
return await _trackPullConfigRepository.GetContentPageAsync(input);
}
///
/// 轨迹拉取配置详情
///
///
///
[HttpGet("GetDetail")]
public async Task GetDetailAsync(int id)
{
return await _trackPullConfigRepository.GetDetailAsync(id);
}
///
/// 轨迹拉取配置内容详情
///
///
///
[HttpGet("GetContentDetail")]
public async Task GetContentDetailAsync(int id)
{
return await _trackPullConfigRepository.GetContentDetailAsync(id);
}
///
/// 轨迹拉取配置编辑
///
///
///
[HttpPost("Edit")]
public async Task EditAsync([FromBody] EditTrackPullConfigInput input)
{
if (input.PullInterval < 60) input.PullInterval = 60;
if (input.Options != OptionTypeNoNull.删除)
{
Expression> isAnyExp = x =>
x.SubSystemId == input.SubSystemId && x.ServiceProviderId == input.ServiceProviderId;
if (input.Options == OptionTypeNoNull.修改) isAnyExp = isAnyExp.And(x => x.Id != input.Id);
if (await _trackPullConfigRepository.IsAnyAsync(isAnyExp))
throw new BusinessException(message: _stringLocalizer["IsTrackPullConfigAny"]);
}
switch (input.Options)
{
case OptionTypeNoNull.新增:
{
var trackPullEntity = ObjectMapper.Map(input);
var trackPullParamsEntity =
ObjectMapper.Map(input);
int id;
using (var ts = TransactionScopeHelper.Create())
{
id = await _trackPullConfigRepository.InsertReturnIdentityAsync(trackPullEntity);
trackPullParamsEntity.PullOrPushConfigId = id;
await _trackPullConfigRepository.InsertParamsAsync(trackPullParamsEntity);
ts.Complete();
}
await _trackPullCacheService.SetPullConfigAsync(id);
}
break;
case OptionTypeNoNull.修改:
{
var oldTrackPullEntity = await _trackPullConfigRepository.GetFirstAsync(x => x.Id == input.Id) ??
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
var oldTrackPullParamsEntity =
await _trackPullConfigRepository.GetParamsSingleAsync(x =>
x.PullOrPushConfigId == input.Id && !x.IsPush);
ObjectMapper.Map(input, oldTrackPullEntity);
ObjectMapper.Map(input, oldTrackPullParamsEntity);
using (var ts = TransactionScopeHelper.Create())
{
await _trackPullConfigRepository.UpdateAsync(oldTrackPullEntity);
await _trackPullConfigRepository.UpdateParamsAsync(oldTrackPullParamsEntity);
ts.Complete();
}
await _trackPullCacheService.SetPullConfigAsync(input.Id);
}
break;
case OptionTypeNoNull.删除:
using (var ts = TransactionScopeHelper.Create())
{
await _trackPullConfigRepository.SoftDeleteAsync(x => x.Id == input.Id);
await _trackPullConfigRepository.SoftDeleteParamsAsync(x =>
x.PullOrPushConfigId == input.Id && !x.IsPush);
await _trackPullConfigRepository.SoftDeleteContentAsync(x => x.TrackPullConfigId == input.Id);
await _trackPullCacheService.SetPullConfigAsync(input.Id);
ts.Complete();
}
break;
default:
throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum,
nameof(EditTrackPullConfigInput.Options)]);
}
return true;
}
///
/// 轨迹拉取配置内容编辑
///
///
///
[HttpPost("EditContent")]
public async Task EditContentAsync([FromBody] EditTrackPullContentInput input)
{
if (input.Options != OptionTypeNoNull.删除)
{
Expression> 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(message: _stringLocalizer["IsTrackPullContentAny"]);
if (!input.TrackContentCodeValue.IsNullOrWhiteSpace() &&
await _trackPullConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x =>
x.TrackContentCodeValue == input.TrackContentCodeValue)))
throw new BusinessException(message: _stringLocalizer["IsTrackPullContentCodeValueAny"]);
if (!input.TrackStatusValue.IsNullOrWhiteSpace() &&
await _trackPullConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x =>
x.TrackStatusValue == input.TrackStatusValue)))
throw new BusinessException(message: _stringLocalizer["IsTrackPullContentStatusValueAny"]);
if (!input.TrackContentValue.IsNullOrWhiteSpace() &&
await _trackPullConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x =>
x.TrackContentValue == input.TrackContentValue)))
throw new BusinessException(message: _stringLocalizer["IsTrackPullContentValueAny"]);
if (!input.TrackKeywords.IsNullOrEmpty())
{
Expression> 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: _stringLocalizer["IsTrackPullContentKeywordsAny"]);
}
}
switch (input.Options)
{
case OptionTypeNoNull.新增:
{
var entity = ObjectMapper.Map(input);
await _trackPullConfigRepository.InsertContentAsync(entity);
await _trackPullCacheService.SetPullConfigAsync(input.TrackPullConfigId);
break;
}
case OptionTypeNoNull.修改:
{
var oldTrackPullContentEntity = await _trackPullConfigRepository.GetContentByIdAsync(input.Id!.Value) ??
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
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: _stringLocalizer["NotFindData"]);
await _trackPullConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
await _trackPullCacheService.SetPullConfigAsync(oldTrackPullContentEntity.TrackPullConfigId);
break;
}
default:
throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum,
nameof(EditTrackPullContentInput.Options)]);
}
return true;
}
}