admin/src/Oms.Application/Services/Channels/WeightRoundingService.cs
2026-04-21 17:54:52 +08:00

278 lines
10 KiB
C#

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 WeightRoundingService : ApplicationService
{
private IWeightRoundingRepository WeightRoundingRepository => LazyServiceProvider.GetRequiredService<IWeightRoundingRepository>();
/// <summary>
/// 查询计重进位规则列表。
/// </summary>
/// <param name="input">入参:计重进位规则列表查询条件。</param>
/// <returns>计重进位规则分页结果,包含列表数据和总记录数。</returns>
/// <remarks>
/// 核心业务逻辑:
/// 1. 根据查询条件构造筛选规则。
/// 2. 查询目标数据并补齐关联展示字段。
/// 3. 按接口约定返回列表结果。
/// </remarks>
[HttpPost("/channel/weight-rounding/list")]
public async Task<OmsPageResult<WeightRoundingOutput>> 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<string, bool> { [nameof(WeightRounding.UpdateTime)] = false, [nameof(WeightRounding.Id)] = false });
var total = await WeightRoundingRepository.CountAsync(predicate);
return new OmsPageResult<WeightRoundingOutput>
{
Items = entities.Select(MapOutput).ToList(),
Total = total
};
}
/// <summary>
/// 新增计重进位规则。
/// </summary>
/// <param name="input">入参:计重进位规则保存参数。</param>
/// <returns>无返回体,仅完成新增计重进位规则。</returns>
/// <remarks>
/// 核心业务逻辑:
/// 1. 校验入参与关联对象、唯一性等前置条件。
/// 2. 组装目标实体及相关明细数据。
/// 3. 写入审计字段并持久化结果。
/// </remarks>
[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
});
}
/// <summary>
/// 更新计重进位规则。
/// </summary>
/// <param name="input">入参:计重进位规则保存参数。</param>
/// <returns>无返回体,仅完成更新计重进位规则。</returns>
/// <remarks>
/// 核心业务逻辑:
/// 1. 查询并校验目标对象。
/// 2. 按入参更新主体数据和关联配置。
/// 3. 写入更新审计字段并保存结果。
/// </remarks>
[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);
}
/// <summary>
/// 删除计重进位规则。
/// </summary>
/// <param name="input">入参:目标主键。</param>
/// <returns>无返回体,仅完成删除计重进位规则。</returns>
/// <remarks>
/// 核心业务逻辑:
/// 1. 查询并校验目标对象。
/// 2. 执行软删除或删除关联明细。
/// 3. 持久化删除结果。
/// </remarks>
[HttpPost("/channel/weight-rounding/delete")]
public async Task DeleteAsync(IdInput input)
{
var entity = await GetEntityOrThrowAsync(input.Id);
await WeightRoundingRepository.DeleteAsync(entity);
}
/// <summary>
/// 查询计重进位规则明细列表。
/// </summary>
/// <param name="input">入参:计重进位规则明细列表查询条件。</param>
/// <returns>计重进位规则明细分页结果,包含列表数据和总记录数。</returns>
/// <remarks>
/// 核心业务逻辑:
/// 1. 根据查询条件构造筛选规则。
/// 2. 查询目标数据并补齐关联展示字段。
/// 3. 按接口约定返回列表结果。
/// </remarks>
[HttpPost("/channel/weight-rounding/info/list")]
public async Task<OmsPageResult<WeightRoundingInfoOutput>> 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<WeightRoundingInfoOutput>
{
Items = entities.Select(MapInfoOutput).ToList(),
Total = total
};
}
/// <summary>
/// 新增计重进位规则明细。
/// </summary>
/// <param name="input">入参:计重进位规则明细保存参数。</param>
/// <returns>无返回体,仅完成新增计重进位规则明细。</returns>
/// <remarks>
/// 核心业务逻辑:
/// 1. 校验入参与关联对象、唯一性等前置条件。
/// 2. 组装目标实体及相关明细数据。
/// 3. 写入审计字段并持久化结果。
/// </remarks>
[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
});
}
/// <summary>
/// 更新计重进位规则明细。
/// </summary>
/// <param name="input">入参:计重进位规则明细保存参数。</param>
/// <returns>无返回体,仅完成更新计重进位规则明细。</returns>
/// <remarks>
/// 核心业务逻辑:
/// 1. 查询并校验目标对象。
/// 2. 按入参更新主体数据和关联配置。
/// 3. 写入更新审计字段并保存结果。
/// </remarks>
[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);
}
/// <summary>
/// 删除计重进位规则明细。
/// </summary>
/// <param name="input">入参:目标主键。</param>
/// <returns>无返回体,仅完成删除计重进位规则明细。</returns>
/// <remarks>
/// 核心业务逻辑:
/// 1. 查询并校验目标对象。
/// 2. 执行软删除或删除关联明细。
/// 3. 持久化删除结果。
/// </remarks>
[HttpPost("/channel/weight-rounding/info/delete")]
public async Task DeleteInfoAsync(IdInput input)
{
await GetInfoOrThrowAsync(input.Id);
await WeightRoundingRepository.DeleteInfoAsync(input.Id);
}
private static Expression<Func<WeightRounding, bool>> BuildPredicate(WeightRoundingListInput input)
{
var appKey = input.AppKey?.Trim();
return entity => string.IsNullOrWhiteSpace(appKey) || entity.AppKey == appKey;
}
private async Task ValidateNameAsync(string appKey, string name, long? id)
{
var existed = await WeightRoundingRepository.GetByAppKeyAndNameAsync(appKey, name);
if (existed != null && existed.Id != id)
{
throw new BusinessException("计重进位名称已存在");
}
}
private async Task<WeightRounding> GetEntityOrThrowAsync(long? 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<WeightRoundingInfo> GetInfoOrThrowAsync(long? 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)
};
}
}