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; [ApiExplorerSettings(GroupName = "OMS-Channel")] public class AutoTriggerService : ApplicationService { private IAutoTriggerRepository AutoTriggerRepository => LazyServiceProvider.GetRequiredService(); /// /// 查询自动触发规则列表。 /// /// 入参:自动触发规则列表查询条件。 /// 自动触发规则分页结果,包含列表数据和总记录数。 /// /// 核心业务逻辑: /// 1. 根据查询条件构造筛选规则。 /// 2. 查询目标数据并补齐关联展示字段。 /// 3. 按接口约定返回列表结果。 /// [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 }; } /// /// 新增自动触发规则。 /// /// 入参:自动触发规则保存参数。 /// 无返回体,仅完成新增自动触发规则。 /// /// 核心业务逻辑: /// 1. 校验入参与关联对象、唯一性等前置条件。 /// 2. 组装目标实体及相关明细数据。 /// 3. 写入审计字段并持久化结果。 /// [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); } /// /// 更新自动触发规则。 /// /// 入参:自动触发规则保存参数。 /// 无返回体,仅完成更新自动触发规则。 /// /// 核心业务逻辑: /// 1. 查询并校验目标对象。 /// 2. 按入参更新主体数据和关联配置。 /// 3. 写入更新审计字段并保存结果。 /// [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); } /// /// 删除自动触发规则。 /// /// 入参:目标主键。 /// 无返回体,仅完成删除自动触发规则。 /// /// 核心业务逻辑: /// 1. 查询并校验目标对象。 /// 2. 执行软删除或删除关联明细。 /// 3. 持久化删除结果。 /// [HttpPost("/channel/auto-trigger/delete")] public async Task DeleteAsync(IdInput input) { var entity = await GetEntityOrThrowAsync(input.Id); await AutoTriggerRepository.DeleteAsync(entity); } /// /// 切换自动触发规则状态。 /// /// 入参:目标主键和状态值。 /// 无返回体,仅完成切换自动触发规则状态。 /// /// 核心业务逻辑: /// 1. 查询并校验目标对象。 /// 2. 更新启停状态。 /// 3. 保存状态变更结果。 /// [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) }; } }