track/src/SJZY.Track.Application/Services/Demo/InterFaceDemoService.cs
2024-02-28 11:40:15 +08:00

251 lines
6.7 KiB
C#

using SJZY.Expand.ABP.Core.Common;
using SJZY.Track.Application.Dtos.Demo;
using SJZY.Track.Domain.Aggregates.DemoAggregate;
using SJZY.Track.Domain.Dtos.Demo;
namespace SJZY.Track.Application.Services.Demo;
/// <summary>
/// 接口请求完整样例
/// </summary>
[ApiExplorerSettings(GroupName = "Demo")]
public class InterFaceDemoService : ApplicationService
{
#region 构造函数
private readonly IDemoRepository _demoRepository;
public InterFaceDemoService(IDemoRepository demoRepository)
{
_demoRepository = demoRepository;
}
#endregion
#region 增删改
/// <summary>
/// 增删改
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("InterFace/Edit")]
public async Task<LongIdBaseDto> Edit(DemoInput input)
{
var res = new LongIdBaseDto();
if (input.Option == OptionType.新增)
{
var entity = ObjectMapper.Map<DemoInput, DemoEntity>(input);
res.Id = await _demoRepository.InsertReturnIdentityAsync(entity);
}
else if (input.Option == OptionType.修改 || input.Option == OptionType.删除)
{
var demoInfo = await _demoRepository.GetById1(input.Id);
if (demoInfo == null)
{
//校验数据是否存在...
throw new BusinessException(message: "未找到指定编号下的数据");
}
res.Id = demoInfo.Id;
if (input.Option == OptionType.删除)
{
//await _demoRepository.DeleteAsync(demoInfo); //真删除
//软删除
demoInfo.IsDeleted = true;
await _demoRepository.UpdateAsync(demoInfo);
}
else
{
var entity = ObjectMapper.Map(input, demoInfo);
await _demoRepository.UpdateAsync(entity);
}
}
return res;
}
#endregion
#region 局部更新最佳实践
/// <summary>
/// 局部更新最佳实践
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="BusinessException"></exception>
[HttpPost("InterFace/UpdatePartColumns")]
public async Task<IdBaseDto> UpdatePartColumns(DemoInput input)
{
var demoInfo = await _demoRepository.GetById1(input.Id);
if (demoInfo == null)
{
//校验数据是否存在...
throw new BusinessException(message: "未找到指定编号下的数据");
}
//更新同时忽略某几项参数
await _demoRepository.UpdatePartColumns(demoInfo);
return new IdBaseDto() { Id = input.Id };
}
#endregion
#region 分页查询接口
/// <summary>
/// 分页查询
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("InterFace/GetListByPage")]
public async Task<UnifyPageResultDto<GetDemoListByPageOutput>> GetListByPage(GetDemoListByPageInput input)
{
//分页时返回值统一用 PageResultDto<> 类型
var list = await _demoRepository.GetListByPage(input.PageSize, input.PageIndex, input.Name);
var res = ObjectMapper.Map<UnifyPageResultDto<DemoEntity>, UnifyPageResultDto<GetDemoListByPageOutput>>(list);
return res;
}
#endregion
#region 入参转换
/// <summary>
/// 入参转换
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("InterFace/InputToValueObject")]
public async Task<List<GetDemoListByPageOutput>> InputToValueObject(GetDemoListByPageInput input)
{
var obj = ObjectMapper.Map<GetDemoListByPageInput, DemoValueObject>(input);
var list = await _demoRepository.InputToValueObject(obj);
var res = ObjectMapper.Map<List<DemoEntity>, List<GetDemoListByPageOutput>>(list);
return res;
}
#endregion
#region 原生sql
/// <summary>
/// 原生sql
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
[HttpPost("InterFace/OriginalSql")]
public async Task<UnifyPageResultDto<DemoValueObject>> OriginalSql(DemoValueObject obj)
{
var list = await _demoRepository.OriginalSql(obj);
return list;
}
#endregion
#region 自定义返回结果格式
/// <summary>
/// 自定义返回结果格式
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Demo/CustomerResult")]
[CustomerResult]
public async Task<MyCustomerResultOutput> GetListAsync()
{
/*
说明:
理论上我们有全公司级别的返回值约定,不管是前端调后端,还是后端微服务间的调用,都应完全遵循该规范。
但不排除有些特殊情况下,必须要返回自定义格式的场景。
*/
var res = new MyCustomerResultOutput();
res.Code = "200";
res.Message = "这是一个自定义返回结果";
return res;
}
#endregion
#region 约定类型下返回null
/// <summary>
/// 约定类型下返回null
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Demo/ReturnNull")]
public async Task<MyCustomerResultOutput> ReturnNull()
{
MyCustomerResultOutput res = null;
return res;
}
#endregion
#region
/// <summary>
/// 没有返回值
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("Demo/WithoutReturnObject")]
public async Task<UnifyResultDto> WithoutReturnObject()
{
return null;
}
#endregion
#region 主从关系实体存储
/// <summary>
/// 主从关系实体存储
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
[HttpPost("InterFace/EntityRelationSave")]
public async Task<UnifyResultDto> EntityRelationSave(DemoDetailSaveInput input)
{
var obj = ObjectMapper.Map<DemoDetailSaveInput, DemoEntity>(input);
var res = await _demoRepository.EntityRelationSave(obj);
return new UnifyResultDto(res);
}
#endregion
#region 数据缓存
/// <summary>
/// 数据缓存
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("InterFace/GetDataCache")]
public async Task<DemoEntity> GetDataCache(IdBaseDto input)
{
var res = await _demoRepository.GetDataCache(input.Id);
return res;
}
#endregion
//[HttpPost("InterFace/AAAA")]
//public async Task<UnifyResultDto> AAAA()
//{
// var result = await _httpClientProxy.Service.GetSystemTokenAsync("Track");
// return result;
//}
}