track/src/SJZY.Track.Application/Services/TrackTemplates/TrackTemplateService.cs
2024-03-01 10:14:49 +08:00

215 lines
7.4 KiB
C#

using System.Linq.Expressions;
using Microsoft.Extensions.Localization;
using SJZY.Expand.ABP.Core.Localization;
using SJZY.Track.Application.Contract.Dtos.TrackTemplates;
using SJZY.Track.Application.Localization;
using SJZY.Track.Domain.Aggregates.TrackTemplateAggregate;
using SJZY.Track.Domain.Dtos.TrackTemplate;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackTemplates;
/// <summary>
/// 轨迹模版
/// </summary>
[ApiExplorerSettings(GroupName = "轨迹模版")]
[Route("/Track/Template")]
public class TrackTemplateService : ApplicationService, ITransientDependency
{
private readonly IStringLocalizer<TrackLocalizationResource> _stringLocalizer;
private readonly ITrackTemplateRepository _trackTemplateRepository;
/// <inheritdoc/>
public TrackTemplateService(
IStringLocalizer<TrackLocalizationResource> stringLocalizer,
ITrackTemplateRepository trackTemplateRepository)
{
_stringLocalizer = stringLocalizer;
_trackTemplateRepository = trackTemplateRepository;
}
/// <summary>
/// 轨迹模版编辑
/// </summary>
/// <returns></returns>
[HttpPost("Edit")]
public async Task<UnifyResultDto> EditAsync(EditTrackTemplateInput input)
{
return input.Options switch
{
OptionType.新增 => await AddAsync(input),
OptionType.修改 => await UpdateAsync(input),
OptionType.删除 => await DeleteAsync(input),
_ => throw new BusinessException(message: "当前操作动作不合法")
};
}
/// <summary>
/// 根据模版id获取轨迹模版
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("GetDetail")]
public async Task<UnifyResultDto<GetTrackTemplateDetailDto>> GetDetailAsync(int id)
{
var detailInfo = await _trackTemplateRepository.GetDetailAsync(id);
if (detailInfo == null)
{
return new UnifyResultDto<GetTrackTemplateDetailDto>(false, null, "未查询到轨迹模版");
}
var dto = ObjectMapper.Map<GetTrackTemplateDetailVO, GetTrackTemplateDetailDto>(detailInfo);
return new UnifyResultDto<GetTrackTemplateDetailDto>("200", "", dto);
}
/// <summary>
/// 获取分页轨迹模版
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetList")]
public async Task<UnifyPageResultDto<GetTrackTemplateListDto>> GetPageTrackTemplate(GetPageTrackTemplateInput input)
{
var vo = ObjectMapper.Map<GetPageTrackTemplateInput, GetPageTrackTemplateVO>(input);
var resultDto = await _trackTemplateRepository.GetPageTrackTemplate(vo);
var pageResultDto =
ObjectMapper.Map<UnifyPageResultDto<GetTrackTemplateListVO>, UnifyPageResultDto<GetTrackTemplateListDto>>(
resultDto);
return pageResultDto;
}
#region 私有方法
private async Task<UnifyResultDto> AddAsync(EditTrackTemplateInput input)
{
#region 业务校验
//校验模版代码唯一性
var templateEntity =
await _trackTemplateRepository.GetFirstAsync(
a => a.SubSystemId == input.SubSystemId && a.Code == input.Code);
if (templateEntity != null)
{
throw new BusinessException(message: _stringLocalizer["TemplateExist"]);
}
#endregion
var entity = ObjectMapper.Map<EditTrackTemplateInput, TrackTemplateEntity>(input);
var isTrue = await _trackTemplateRepository.InsertAsync(entity);
return new UnifyResultDto("200", "新增成功", isTrue);
}
private async Task<UnifyResultDto> UpdateAsync(EditTrackTemplateInput input)
{
#region 业务校验
var isExist = await _trackTemplateRepository.GetFirstAsync(a =>
a.Id != input.Id && a.SubSystemId == input.SubSystemId && a.Code == input.Code);
if (isExist != null)
{
throw new BusinessException(message: _stringLocalizer["TemplateExist"]);
}
#endregion
var templateEntity = await _trackTemplateRepository.GetFirstAsync(a => a.Id == input.Id);
ObjectMapper.Map(input, templateEntity);
var isTrue = await _trackTemplateRepository.UpdateAsync(templateEntity);
return new UnifyResultDto("200", "修改成功", isTrue);
}
private async Task<UnifyResultDto> DeleteAsync(EditTrackTemplateInput input)
{
#region 业务校验
//到时候,得判断这个模版有没有在轨迹映射里面被使用过,如果被使用就抛出提示
#endregion
var isTrue = await _trackTemplateRepository.SoftDeleteAsync(x => x.Id == input.Id);
return new UnifyResultDto("200", "删除成功", isTrue);
}
#endregion 私有方法
/// <summary>
/// 轨迹模板内容列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetContentPage")]
public async Task<UnifyPageResultDto<GetTrackTemplateContentPageOutputVO>> GetContentPageAsync(
[FromBody] GetTrackTemplateContentPageInputVO input)
{
return await _trackTemplateRepository.GetContentPageAsync(input);
}
/// <summary>
/// 轨迹模板内容详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("GetContentDetail")]
public async Task<GetTrackTemplateContentDetailOutputVO> GetContentDetailAsync(int id)
{
return await _trackTemplateRepository.GetContentDetailAsync(id);
}
/// <summary>
/// 轨迹模板内容编辑
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("EditContent")]
public async Task<bool> EditContentAsync([FromBody] EditTrackTemplateContentInput input)
{
if (input.Options != OptionTypeNoNull.删除)
{
Expression<Func<TrackTemplateContentEntity, bool>> isAnyExp = x =>
x.TrackTemplateId == input.TrackTemplateId && x.TrackContentId == input.TrackContentId;
if (input.Options == OptionTypeNoNull.修改) isAnyExp.And(x => x.Id != input.Id);
if (await _trackTemplateRepository.IsContentAnyAsync(isAnyExp))
throw new BusinessException(message: _stringLocalizer["IsTrackTemplateContentAny"]);
}
switch (input.Options)
{
case OptionTypeNoNull.新增:
var entity = ObjectMapper.Map<EditTrackTemplateContentInput, TrackTemplateContentEntity>(input);
await _trackTemplateRepository.InsertContentAsync(entity);
break;
case OptionTypeNoNull.修改:
var oldEntity = await _trackTemplateRepository.GetContentByIdAsync(input.Id!.Value) ??
throw new BusinessException(message: _stringLocalizer["NotFindData"]);
ObjectMapper.Map(input, oldEntity);
await _trackTemplateRepository.UpdateContentAsync(oldEntity);
break;
case OptionTypeNoNull.删除:
await _trackTemplateRepository.SoftDeleteContentAsync(input.Id!.Value);
break;
default:
throw new BusinessException(message: _stringLocalizer[SjzyBasicLocalization.Validation_IsInEnum,
nameof(EditTrackTemplateContentInput.Options)]);
}
return true;
}
}