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.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; /// /// 轨迹推送 /// [ApiExplorerSettings(GroupName = "轨迹推送")] [Route("/Track/PushConfig")] public class TrackPushService : ApplicationService, ITransientDependency { private readonly IStringLocalizer _stringLocalizer; private readonly ITrackPushConfigRepository _trackPushConfigRepository; private readonly ITrackTemplateRepository _trackTemplateRepository; private readonly ITrackPullConfigRepository _trackPullConfigRepository; public TrackPushService( IStringLocalizer stringLocalizer, ITrackPushConfigRepository trackPushConfigRepository, ITrackTemplateRepository trackTemplateRepository, ITrackPullConfigRepository trackPullConfigRepository) { _stringLocalizer = stringLocalizer; _trackPushConfigRepository = trackPushConfigRepository; _trackTemplateRepository = trackTemplateRepository; _trackPullConfigRepository = trackPullConfigRepository; } /// /// 轨迹推送配置列表 /// /// /// [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) { var res = await _trackPushConfigRepository.GetDetailAsync(id); if (res is null) return null; List 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; } /// /// 轨迹推送配置内容详情 /// /// /// [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 && x.ServiceProviderId == input.ServiceProviderId; if (input.Options == OptionTypeNoNull.修改) isAnyExp.And(x => x.Id != input.Id); var trackTemplateIdExp = Expressionable.Create(); foreach (var trackTemplateId in input.TrackTemplateIds) { trackTemplateIdExp.Or(x => SqlFunc.SplitIn(x.TrackTemplateId, trackTemplateId.ToString())); } if (await _trackPushConfigRepository.IsAnyAsync(isAnyExp.And(trackTemplateIdExp.ToExpression()))) 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"]); 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(); } } break; case OptionTypeNoNull.删除: 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 && x.TrackContentId == input.TrackContentId; if (input.Options == OptionTypeNoNull.修改) isContentAnyExp.And(x => x.Id != input.Id); if (await _trackPushConfigRepository.IsContentAnyAsync(isContentAnyExp)) throw new BusinessException(message: _stringLocalizer["IsTrackPushContentAny"]); } switch (input.Options) { case OptionTypeNoNull.新增: { var trackPushContentEntity = ObjectMapper.Map(input); await _trackPushConfigRepository.InsertContentAsync(trackPushContentEntity); } break; case OptionTypeNoNull.修改: { var oldTrackPushContentEntity = await _trackPushConfigRepository.GetContentByIdAsync(input.Id!.Value) ?? throw new BusinessException(message: _stringLocalizer["NotFindData"]); ObjectMapper.Map(input, oldTrackPushContentEntity); await _trackPushConfigRepository.UpdateContentAsync(oldTrackPushContentEntity); } break; case OptionTypeNoNull.删除: await _trackPushConfigRepository.SoftDeleteContentAsync(x => x.Id == input.Id); break; default: throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum, nameof(EditTrackPushContentInput.Options)]); } return true; } }