track/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs
2024-03-05 10:50:07 +08:00

291 lines
13 KiB
C#

using Microsoft.Extensions.Localization;
using SJZY.Expand.ABP.Core;
using SJZY.Expand.ABP.Core.Localization;
using SJZY.Expand.ABP.Core.PublicMethords;
using SJZY.Track.Application.Contract.Dtos.TrackPush;
using SJZY.Track.Application.Contract.Services;
using SJZY.Track.Application.Localization;
using SJZY.Track.Domain.Aggregates.TrackPullAggregate;
using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
using SJZY.Track.Domain.Aggregates.TrackTemplateAggregate;
using SJZY.Track.Domain.Dtos.Common;
using SJZY.Track.Domain.Dtos.TrackPush;
using SqlSugar;
using System.Linq.Expressions;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackPush;
/// <summary>
/// 轨迹推送
/// </summary>
[ApiExplorerSettings(GroupName = "轨迹推送")]
[Route("/Track/PushConfig")]
public class TrackPushService : ApplicationService, ITransientDependency
{
private readonly IStringLocalizer<TrackLocalizationResource> _stringLocalizer;
private readonly ITrackPushConfigRepository _trackPushConfigRepository;
private readonly ITrackTemplateRepository _trackTemplateRepository;
private readonly ITrackPullConfigRepository _trackPullConfigRepository;
private readonly ITrackPushCacheService _trackPushCacheService;
public TrackPushService(
IStringLocalizer<TrackLocalizationResource> stringLocalizer,
ITrackPushConfigRepository trackPushConfigRepository,
ITrackTemplateRepository trackTemplateRepository,
ITrackPullConfigRepository trackPullConfigRepository,
ITrackPushCacheService trackPushCacheService)
{
_stringLocalizer = stringLocalizer;
_trackPushConfigRepository = trackPushConfigRepository;
_trackTemplateRepository = trackTemplateRepository;
_trackPullConfigRepository = trackPullConfigRepository;
_trackPushCacheService = trackPushCacheService;
}
/// <summary>
/// 轨迹推送配置列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetPage")]
public async Task<UnifyPageResultDto<GetTrackPushConfigPageOutputVO>> GetPageAsync([FromBody] KeywordsInputVO input)
{
return await _trackPushConfigRepository.GetPageAsync(input);
}
/// <summary>
/// 轨迹推送配置内容列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetContentPage")]
public async Task<UnifyPageResultDto<GetTrackPushContentPageOutputVO>> GetContentPageAsync([FromBody] GetTrackPushContentPageInputVO input)
{
return await _trackPushConfigRepository.GetContentPageAsync(input);
}
/// <summary>
/// 轨迹推送配置详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("GetDetail")]
public async Task<GetTrackPushConfigDetailOutputVO> GetDetailAsync(int id)
{
var res = await _trackPushConfigRepository.GetDetailAsync(id);
if (res is null) return null;
List<int> trackTemplateIds = res.TrackTemplateId.Split(',').Select(int.Parse).ToList();
var trackTemplateEntities = await _trackTemplateRepository.GetListAsync(x => trackTemplateIds.Contains(x.Id));
trackTemplateEntities.ForEach(item => { (res.TrackTemplate ??= new()).Add(new() { Value = item.Id, Key = item.Name }); });
return res;
}
/// <summary>
/// 轨迹推送配置内容详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("GetContentDetail")]
public async Task<GetTrackPushContentDetailOutputVO> GetContentDetailAsync(int id)
{
return await _trackPushConfigRepository.GetContentDetailAsync(id);
}
/// <summary>
/// 轨迹推送配置编辑
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Edit")]
public async Task<bool> EditAsync([FromBody] EditTrackPushConfigInput input)
{
if (input.Options != OptionTypeNoNull.删除)
{
Expression<Func<TrackPushConfigEntity, bool>> isAnyExp = x =>
x.SubSystemId == input.SubSystemId && x.CustomId == input.CustomId && x.ServiceProviderId == input.ServiceProviderId;
if (input.Options == OptionTypeNoNull.修改) isAnyExp = isAnyExp.And(x => x.Id != input.Id);
var trackTemplateIdExp = Expressionable.Create<TrackPushConfigEntity>();
input.TrackTemplateIds?.ForEach(item => { trackTemplateIdExp.Or(x => SqlFunc.SplitIn(x.TrackTemplateId, item.ToString())); });
isAnyExp = isAnyExp.And(trackTemplateIdExp.ToExpression());
if (await _trackPushConfigRepository.IsAnyAsync(isAnyExp))
throw new BusinessException(message: _stringLocalizer["IsTrackPushConfigAny"]);
}
switch (input.Options)
{
case OptionTypeNoNull.新增:
{
var trackPushEntity = ObjectMapper.Map<EditTrackPushConfigInput, TrackPushConfigEntity>(input);
var trackPushParamsEntity = ObjectMapper.Map<EditTrackPushConfigInput, TrackPushPullParamsConfigEntity>(input);
using (var ts = TransactionScopeHelper.Create())
{
int id = await _trackPushConfigRepository.InsertReturnIdentityAsync(trackPushEntity);
trackPushParamsEntity.PullOrPushConfigId = id;
await _trackPullConfigRepository.InsertParamsAsync(trackPushParamsEntity);
if (input.IsAutoPush) await _trackPushCacheService.IsPushAsync(input.CustomId);
ts.Complete();
}
}
break;
case OptionTypeNoNull.修改:
{
var oldTrackPushEntity = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == input.Id) ?? throw new BusinessException(message: _stringLocalizer["NotFindData"]);
int oldCustomId = oldTrackPushEntity.CustomId;
var oldTrackPushParamsEntity =
await _trackPullConfigRepository.GetParamsSingleAsync(x =>
x.PullOrPushConfigId == input.Id && x.IsPush);
ObjectMapper.Map(input, oldTrackPushEntity);
ObjectMapper.Map(input, oldTrackPushParamsEntity);
using (var ts = TransactionScopeHelper.Create())
{
await _trackPushConfigRepository.UpdateAsync(oldTrackPushEntity);
await _trackPullConfigRepository.UpdateParamsAsync(oldTrackPushParamsEntity);
await _trackPushCacheService.UpdateAsync(oldCustomId, false);
if (input.IsAutoPush) await _trackPushCacheService.IsPushAsync(input.CustomId);
ts.Complete();
}
}
break;
case OptionTypeNoNull.删除:
{
var oldTrackPushEntity = await _trackPushConfigRepository.GetFirstAsync(x => x.Id == input.Id) ?? throw new BusinessException(message: _stringLocalizer["NotFindData"]);
using (var ts = TransactionScopeHelper.Create())
{
await _trackPushConfigRepository.SoftDeleteAsync(x => x.Id == input.Id);
await _trackPullConfigRepository.SoftDeleteParamsAsync(x =>
x.PullOrPushConfigId == input.Id && x.IsPush);
await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.TrackPushConfigId == input.Id);
await _trackPushCacheService.UpdateAsync(oldTrackPushEntity.CustomId, false);
ts.Complete();
}
}
break;
default:
throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum,
nameof(EditTrackPushConfigInput.Options)]);
}
return true;
}
/// <summary>
/// 轨迹推送配置内容编辑
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("EditContent")]
public async Task<bool> EditContentAsync([FromBody] EditTrackPushContentInput input)
{
if (input.Options != OptionTypeNoNull.删除)
{
var isContentAnyExp = Expressionable.Create<TrackPushContentConfigEntity>()
.And(x => x.TrackPushConfigId == input.TrackPushConfigId);
if (input.Options == OptionTypeNoNull.修改) isContentAnyExp.And(x => x.Id != input.Id);
if (await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackContentId == input.TrackContentId).ToExpression()))
throw new BusinessException(message: _stringLocalizer["IsTrackPushContentAny"]);
else if (!input.TrackContentCodeValue.IsNullOrWhiteSpace() && await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackContentCodeValue == input.TrackContentCodeValue).ToExpression()))
throw new BusinessException(message: _stringLocalizer["IsTrackPushContentCodeValueAny"]);
else if (!input.TrackStatusValue.IsNullOrWhiteSpace() && await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackStatusValue == input.TrackStatusValue).ToExpression()))
throw new BusinessException(message: _stringLocalizer["IsTrackPushContentStatusValueAny"]);
else if (!input.TrackContentValue.IsNullOrWhiteSpace() && await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackContentValue == input.TrackContentValue).ToExpression()))
throw new BusinessException(message: _stringLocalizer["IsTrackPushContentValueAny"]);
else if (!input.TrackEnContentValue.IsNullOrWhiteSpace() && await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackEnContentValue == input.TrackEnContentValue).ToExpression()))
throw new BusinessException(message: _stringLocalizer["IsTrackPushEnContentValueAny"]);
}
switch (input.Options)
{
case OptionTypeNoNull.新增:
{
int maxSort = await _trackPushConfigRepository.ContentMaxAsync(x => x.Sort, x => x.TrackPushConfigId == input.TrackPushConfigId);
var entity = ObjectMapper.Map<EditTrackPushContentInput, TrackPushContentConfigEntity>(input);
entity.Sort = maxSort + 1;
await _trackPushConfigRepository.InsertContentAsync(entity);
break;
}
case OptionTypeNoNull.修改:
{
var entity = await _trackPushConfigRepository.GetContentByIdAsync(input.Id!.Value) ?? throw new BusinessException(message: _stringLocalizer["NotFindData"]);
ObjectMapper.Map(input, entity);
await _trackPushConfigRepository.UpdateContentAsync(entity);
break;
}
case OptionTypeNoNull.删除:
{
var entity = await _trackPushConfigRepository.GetContentByIdAsync(input.Id!.Value) ?? throw new BusinessException(message: _stringLocalizer["NotFindData"]);
using (var ts = TransactionScopeHelper.Create())
{
await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id);
await _trackPushConfigRepository.UpdateContentAsync(x => x.Sort == x.Sort - 1, x => x.TrackPushConfigId == entity.TrackPushConfigId && x.Sort > entity.Sort);
ts.Complete();
}
break;
}
default:
throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum,
nameof(EditTrackPushContentInput.Options)]);
}
return true;
}
/// <summary>
/// 轨迹推送配置内容排序
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("SortContent")]
public async Task<bool> SortContentAsync([FromBody] SortTrackPushContentInput input)
{
List<int> ids = input.SortInfo.Select(x => int.Parse(x.Key)).ToList();
var trackPushContentEntities = await _trackPushConfigRepository.GetContentListAsync(x => ids.Contains(x.Id));
trackPushContentEntities.ForEach(item =>
{
item.Sort = input.SortInfo.Single(x => x.Key == item.Id.ToString()).Value;
});
await _trackPushConfigRepository.UpdateContentRangeAsync(trackPushContentEntities);
return true;
}
}