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 WeightRoundingService : ApplicationService { private IWeightRoundingRepository WeightRoundingRepository => LazyServiceProvider.GetRequiredService(); /// /// 查询计重进位规则列表。 /// /// 入参:计重进位规则列表查询条件。 /// 计重进位规则分页结果,包含列表数据和总记录数。 [HttpPost("/channel/weight-rounding/list")] public async Task> GetListAsync(WeightRoundingListInput input) { var predicate = BuildPredicate(input); var pageIndex = OmsDocHelper.NormalizePage(input.Page); var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize); var entities = await WeightRoundingRepository.GetPagedListAsync( predicate, pageIndex, pageSize, new Dictionary { [nameof(WeightRounding.UpdateTime)] = false, [nameof(WeightRounding.Id)] = false }); var total = await WeightRoundingRepository.CountAsync(predicate); return new OmsPageResult { Items = entities.Select(MapOutput).ToList(), Total = total }; } /// /// 新增计重进位规则。 /// /// 入参:计重进位规则保存参数。 /// 无返回体,仅完成新增计重进位规则。 [HttpPost("/channel/weight-rounding/create")] public async Task CreateAsync(WeightRoundingSaveInput input) { await ValidateNameAsync(input.AppKey, input.Name, null); await WeightRoundingRepository.InsertAsync(new WeightRounding { AppKey = input.AppKey, Name = input.Name }); } /// /// 更新计重进位规则。 /// /// 入参:计重进位规则保存参数。 /// 无返回体,仅完成更新计重进位规则。 [HttpPost("/channel/weight-rounding/update")] public async Task UpdateAsync(WeightRoundingSaveInput input) { var entity = await GetEntityOrThrowAsync(input.Id); await ValidateNameAsync(input.AppKey, input.Name, entity.Id); entity.AppKey = input.AppKey; entity.Name = input.Name; await WeightRoundingRepository.UpdateAsync(entity); } /// /// 删除计重进位规则。 /// /// 入参:目标主键。 /// 无返回体,仅完成删除计重进位规则。 [HttpPost("/channel/weight-rounding/delete")] public async Task DeleteAsync(IdInput input) { var entity = await GetEntityOrThrowAsync(input.Id); await WeightRoundingRepository.DeleteAsync(entity); } /// /// 查询计重进位规则明细列表。 /// /// 入参:计重进位规则明细列表查询条件。 /// 计重进位规则明细分页结果,包含列表数据和总记录数。 [HttpPost("/channel/weight-rounding/info/list")] public async Task> GetInfoListAsync(WeightRoundingInfoListInput input) { await GetEntityOrThrowAsync(input.RoundingId); var pageIndex = OmsDocHelper.NormalizePage(input.Page); var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize); var entities = await WeightRoundingRepository.GetInfosAsync(input.RoundingId, pageIndex, pageSize); var total = await WeightRoundingRepository.CountInfosAsync(input.RoundingId); return new OmsPageResult { Items = entities.Select(MapInfoOutput).ToList(), Total = total }; } /// /// 新增计重进位规则明细。 /// /// 入参:计重进位规则明细保存参数。 /// 无返回体,仅完成新增计重进位规则明细。 [HttpPost("/channel/weight-rounding/info/add")] public async Task AddInfoAsync(WeightRoundingInfoSaveInput input) { await GetEntityOrThrowAsync(input.RoundingId); await WeightRoundingRepository.InsertInfoAsync(new WeightRoundingInfo { RoundingId = input.RoundingId, StartValue = input.StartValue, EndValue = input.EndValue, Public = input.Public, CustomerGroup1 = input.CustomerGroup1, CustomerGroup2 = input.CustomerGroup2 }); } /// /// 更新计重进位规则明细。 /// /// 入参:计重进位规则明细保存参数。 /// 无返回体,仅完成更新计重进位规则明细。 [HttpPost("/channel/weight-rounding/info/update")] public async Task UpdateInfoAsync(WeightRoundingInfoSaveInput input) { var entity = await GetInfoOrThrowAsync(input.Id); await GetEntityOrThrowAsync(input.RoundingId); entity.RoundingId = input.RoundingId; entity.StartValue = input.StartValue; entity.EndValue = input.EndValue; entity.Public = input.Public; entity.CustomerGroup1 = input.CustomerGroup1; entity.CustomerGroup2 = input.CustomerGroup2; await WeightRoundingRepository.UpdateInfoAsync(entity); } /// /// 删除计重进位规则明细。 /// /// 入参:目标主键。 /// 无返回体,仅完成删除计重进位规则明细。 [HttpPost("/channel/weight-rounding/info/delete")] public async Task DeleteInfoAsync(IdInput input) { await GetInfoOrThrowAsync(input.Id); await WeightRoundingRepository.DeleteInfoAsync(input.Id); } private static Expression> BuildPredicate(WeightRoundingListInput input) { var appKey = input.AppKey?.Trim(); return entity => string.IsNullOrWhiteSpace(appKey) || entity.AppKey == appKey; } private async Task ValidateNameAsync(string appKey, string name, int? id) { var existed = await WeightRoundingRepository.GetByAppKeyAndNameAsync(appKey, name); if (existed != null && existed.Id != id) { throw new BusinessException("计重进位名称已存在"); } } private async Task GetEntityOrThrowAsync(int? id) { if (!id.HasValue || id.Value <= 0) { throw new BusinessException("计重进位不存在"); } var entity = await WeightRoundingRepository.FirstOrDefaultAsync(x => x.Id == id.Value); if (entity == null) { throw new BusinessException("计重进位不存在"); } return entity; } private async Task GetInfoOrThrowAsync(int? id) { if (!id.HasValue || id.Value <= 0) { throw new BusinessException("计重进位明细不存在"); } var entity = await WeightRoundingRepository.GetInfoByIdAsync(id.Value); if (entity == null) { throw new BusinessException("计重进位明细不存在"); } return entity; } private static WeightRoundingOutput MapOutput(WeightRounding entity) { return new WeightRoundingOutput { Id = entity.Id, AppKey = entity.AppKey, Name = entity.Name, CreateTime = entity.CreateTime, UpdateTime = OmsDocHelper.NullIfDefault(entity.UpdateTime) }; } private static WeightRoundingInfoOutput MapInfoOutput(WeightRoundingInfo entity) { return new WeightRoundingInfoOutput { Id = entity.Id, RoundingId = entity.RoundingId, StartValue = entity.StartValue, EndValue = entity.EndValue, Public = entity.Public, CustomerGroup1 = entity.CustomerGroup1, CustomerGroup2 = entity.CustomerGroup2, CreateTime = entity.CreateTime, UpdateTime = OmsDocHelper.NullIfDefault(entity.UpdateTime) }; } }