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.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; namespace SJZY.Track.Application.Services.TrackPush; /// /// 轨迹推送 /// [ApiExplorerSettings(GroupName = "轨迹推送")] [Route("/Track/PushConfig")] public class TrackPushService : ApplicationService { private readonly IStringLocalizer _stringLocalizer; private readonly ITrackPushConfigRepository _trackPushConfigRepository; private readonly ITrackPullConfigRepository _trackPullConfigRepository; private readonly ITrackPushCacheService _trackPushCacheService; public TrackPushService( IStringLocalizer stringLocalizer, ITrackPushConfigRepository trackPushConfigRepository, ITrackTemplateRepository trackTemplateRepository, ITrackPullConfigRepository trackPullConfigRepository, ITrackPushCacheService trackPushCacheService) { _stringLocalizer = stringLocalizer; _trackPushConfigRepository = trackPushConfigRepository; _trackPullConfigRepository = trackPullConfigRepository; _trackPushCacheService = trackPushCacheService; } /// /// 轨迹推送配置列表 /// /// /// [HttpPost("GetPage")] public async Task> GetPageAsync([FromBody] KeywordsInputVO input) { return await _trackPushConfigRepository.GetPageAsync(input); } /// /// 轨迹推送配置内容列表 /// /// /// [HttpPost("GetContentPage")] public async Task> GetContentPageAsync( [FromBody] GetTrackPushContentPageInputVO input) { return await _trackPushConfigRepository.GetContentPageAsync(input); } /// /// 轨迹推送配置详情 /// /// /// [HttpGet("GetDetail")] public async Task GetDetailAsync(int id) { return await _trackPushConfigRepository.GetDetailAsync(id); } /// /// 轨迹推送配置内容详情 /// /// /// [HttpGet("GetContentDetail")] public async Task GetContentDetailAsync(int id) { return await _trackPushConfigRepository.GetContentDetailAsync(id); } /// /// 轨迹推送配置编辑 /// /// /// [HttpPost("Edit")] public async Task EditAsync([FromBody] EditTrackPushConfigInput input) { if (input.Options != OptionTypeNoNull.删除) { Expression> isAnyExp = x => x.SubSystemId == input.SubSystemId && x.CustomId == input.CustomId; if (input.Options == OptionTypeNoNull.修改) isAnyExp = isAnyExp.And(x => x.Id != input.Id); if (await _trackPushConfigRepository.IsAnyAsync(isAnyExp)) throw new BusinessException(message: _stringLocalizer["IsTrackPushConfigAny"]); } switch (input.Options) { case OptionTypeNoNull.新增: { var trackPushEntity = ObjectMapper.Map(input); var trackPushParamsEntity = ObjectMapper.Map(input); using (var ts = TransactionScopeHelper.Create()) { int id = await _trackPushConfigRepository.InsertReturnIdentityAsync(trackPushEntity); trackPushParamsEntity.PullOrPushConfigId = id; await _trackPullConfigRepository.InsertParamsAsync(trackPushParamsEntity); 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); ts.Complete(); } if (input.IsAutoPush!.Value) await _trackPushCacheService.SetPushContentConfigsAsync(input.Id); } 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); ts.Complete(); } } break; default: throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum, nameof(EditTrackPushConfigInput.Options)]); } return true; } /// /// 轨迹推送配置内容编辑 /// /// /// [HttpPost("EditContent")] public async Task EditContentAsync([FromBody] EditTrackPushContentInput input) { if (input.Options != OptionTypeNoNull.删除) { Expression> isContentAnyExp = x => x.TrackPushConfigId == input.TrackPushConfigId; if (input.Options == OptionTypeNoNull.修改) isContentAnyExp = isContentAnyExp.And(x => x.Id != input.Id); if (await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackContentId == input.TrackContentId))) throw new BusinessException(message: _stringLocalizer["IsTrackPushContentAny"]); else if (!input.TrackContentCodeValue.IsNullOrWhiteSpace() && await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackContentCodeValue == input.TrackContentCodeValue))) throw new BusinessException(message: _stringLocalizer["IsTrackPushContentCodeValueAny"]); else if (!input.TrackStatusValue.IsNullOrWhiteSpace() && await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackStatusValue == input.TrackStatusValue))) throw new BusinessException(message: _stringLocalizer["IsTrackPushContentStatusValueAny"]); else if (!input.TrackContentValue.IsNullOrWhiteSpace() && await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackContentValue == input.TrackContentValue))) throw new BusinessException(message: _stringLocalizer["IsTrackPushContentValueAny"]); else if (!input.TrackEnContentValue.IsNullOrWhiteSpace() && await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp.And(x => x.TrackEnContentValue == input.TrackEnContentValue))) 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(input); entity.Sort = maxSort + 1; await _trackPushConfigRepository.InsertContentAsync(entity); await _trackPushCacheService.SetPushContentConfigsAsync(input.TrackPushConfigId); 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); await _trackPushCacheService.SetPushContentConfigsAsync(entity.TrackPushConfigId); 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(); } await _trackPushCacheService.SetPushContentConfigsAsync(entity.TrackPushConfigId); break; } default: throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum, nameof(EditTrackPushContentInput.Options)]); } return true; } /// /// 轨迹推送配置内容排序 /// /// /// [HttpPost("SortContent")] public async Task SortContentAsync([FromBody] SortTrackPushContentInput input) { List 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; }); if (trackPushContentEntities.Count > 0) { await _trackPushConfigRepository.UpdateContentRangeAsync(trackPushContentEntities); await _trackPushCacheService.SetPushContentConfigsAsync(trackPushContentEntities[0].TrackPushConfigId); } return true; } }