using System.Linq.Expressions; using Fake.Application; using Microsoft.AspNetCore.Mvc; using Oms.Application.Dtos.Channels; using Oms.Application.Dtos.Shared; using Oms.Application.Shared; using Oms.Domain.ChannelsAggregate.Entities; using Oms.Domain.ChannelsAggregate.Repositories; namespace Oms.Application.Services.Channels; /// /// 自动触发规则 API 服务。 /// [ApiExplorerSettings(GroupName = "OMS-Channel")] public class AutoTriggerService : ApplicationService { private IAutoTriggerRepository AutoTriggerRepository => LazyServiceProvider.GetRequiredService(); /// /// 查询自动触发规则列表。 /// /// 入参:自动触发规则列表查询条件。 /// 自动触发规则分页结果,包含列表数据和总记录数。 [HttpGet("/channel/auto-trigger/list")] public async Task> GetListAsync([FromQuery] AutoTriggerListInput input) { var predicate = BuildListPredicate(input); var pageIndex = OmsDocHelper.NormalizePage(input.Page); var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize); var entities = await AutoTriggerRepository.GetPagedListAsync( predicate, pageIndex, pageSize, new Dictionary { [nameof(AutoTrigger.UpdateTime)] = false, [nameof(AutoTrigger.Id)] = false }); var total = await AutoTriggerRepository.CountAsync(predicate); var items = entities.Select(MapOutput).ToList(); return new OmsPageResult { Items = items, Total = total }; } /// /// 新增自动触发规则。 /// /// 入参:自动触发规则保存参数。 /// 无返回体,仅完成新增自动触发规则。 [HttpPost("/channel/auto-trigger/create")] public async Task CreateAsync(AutoTriggerSaveInput input) { var entity = new AutoTrigger { Status = input.Status, Operation = input.Operation, TriggerTiming = input.TriggerTiming, TriggerCondition = input.TriggerCondition, PromptMessage = input.PromptMessage, Notifier = input.Notifier }; await AutoTriggerRepository.InsertAsync(entity); } /// /// 更新自动触发规则。 /// /// 入参:自动触发规则保存参数。 /// 无返回体,仅完成更新自动触发规则。 [HttpPost("/channel/auto-trigger/update")] public async Task UpdateAsync(AutoTriggerSaveInput input) { var entity = await GetEntityOrThrowAsync(input.Id); entity.Status = input.Status; entity.Operation = input.Operation; entity.TriggerTiming = input.TriggerTiming; entity.TriggerCondition = input.TriggerCondition; entity.PromptMessage = input.PromptMessage; entity.Notifier = input.Notifier; await AutoTriggerRepository.UpdateAsync(entity); } /// /// 删除自动触发规则。 /// /// 入参:目标主键。 /// 无返回体,仅完成删除自动触发规则。 [HttpPost("/channel/auto-trigger/delete")] public async Task DeleteAsync(IdInput input) { var entity = await GetEntityOrThrowAsync(input.Id); await AutoTriggerRepository.DeleteAsync(entity); } /// /// 切换自动触发规则状态。 /// /// 入参:目标主键和状态值。 /// 无返回体,仅完成切换自动触发规则状态。 [HttpPost("/channel/auto-trigger/toggleStatus")] public async Task ToggleStatusAsync(ToggleStatusInput input) { var entity = await GetEntityOrThrowAsync(input.Id); entity.Status = input.Status; await AutoTriggerRepository.UpdateAsync(entity); } private static Expression> BuildListPredicate(AutoTriggerListInput input) { var operation = input.Operation?.Trim(); var triggerTiming = input.TriggerTiming?.Trim(); return entity => (string.IsNullOrWhiteSpace(operation) || entity.Operation == operation) && (string.IsNullOrWhiteSpace(triggerTiming) || entity.TriggerTiming == triggerTiming); } private async Task GetEntityOrThrowAsync(int? id) { if (!id.HasValue || id.Value <= 0) { throw new BusinessException("自动触发不存在"); } var entity = await AutoTriggerRepository.FirstOrDefaultAsync(x => x.Id == id.Value); if (entity == null) { throw new BusinessException("自动触发不存在"); } return entity; } private static AutoTriggerOutput MapOutput(AutoTrigger entity) { return new AutoTriggerOutput { Id = entity.Id, Status = (byte)entity.Status, Operation = entity.Operation, TriggerTiming = entity.TriggerTiming, TriggerCondition = entity.TriggerCondition, PromptMessage = entity.PromptMessage, Notifier = entity.Notifier, CreateTime = entity.CreateTime, UpdateTime = OmsDocHelper.NullIfDefault(entity.UpdateTime) }; } }