DigitalFactory/Admin.NET/Admin.NET.Application/Service/CodeElementProp/CodeElementPropService.cs

150 lines
5.4 KiB
C#
Raw Normal View History

2024-05-24 10:22:01 +00:00
using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using Admin.NET.Application.Utils;
namespace Admin.NET.Application;
/// <summary>
/// 码元素内容服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class CodeElementPropService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<CodeElementProp> _rep;
public CodeElementPropService(SqlSugarRepository<CodeElementProp> rep)
{
_rep = rep;
}
/// <summary>
/// 分页查询码元素内容
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<CodeElementPropOutput>> Page(CodeElementPropInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.CodeValue.Contains(input.SearchKey.Trim())
|| u.CodeType.Contains(input.SearchKey.Trim())
|| u.Remark.Contains(input.SearchKey.Trim())
)
.WhereIF(input.Index>0, u => u.Index == input.Index)
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeValue), u => u.CodeValue.Contains(input.CodeValue.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeType), u => u.CodeType.Contains(input.CodeType.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remark), u => u.Remark.Contains(input.Remark.Trim()))
.Select<CodeElementPropOutput>();
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
/// <summary>
/// 增加码元素内容
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task<long> Add(AddCodeElementPropInput input)
{
var entity = input.Adapt<CodeElementProp>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除码元素内容
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteCodeElementPropInput input)
{
var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
//var entity = await _rep.GetFirstAsync(u => u.CodeElementId == input.CodeElementId) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
//var entity = await _rep.GetFirstAsync(u => u.Length == input.Length) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
//await _rep.FakeDeleteAsync(entity); //假删除
await _rep.DeleteAsync(entity); //真删除
}
/// <summary>
/// 更新码元素内容
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdateCodeElementPropInput input)
{
var entity = input.Adapt<CodeElementProp>();
if (input.Id > 0)
{
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
else
{
await _rep.InsertAsync(entity);
}
}
/// <summary>
/// 获取码元素内容
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<CodeElementProp> Detail([FromQuery] QueryByIdCodeElementPropInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
//return await _rep.GetFirstAsync(u => u.CodeElementId == input.CodeElementId);
//return await _rep.GetFirstAsync(u => u.Length == input.Length);
}
/// <summary>
/// 获取码元素内容列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<CodeElementPropOutput>> List()
{
return await _rep.AsQueryable().Select<CodeElementPropOutput>().ToListAsync();
}
/// <summary>
/// 获取码元素内容列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "CodePropByElement")]
public async Task<List<CodeElementPropOutput>> CodePropByElement(CodeElementOutput input)
{
var list = await _rep.AsQueryable().Where(a => a.CodeElementId == input.Id).Select<CodeElementPropOutput>().ToListAsync();
if (list == null || list.Count < 1)
{
var result = new List<CodeElementPropOutput>();
result.Add(new CodeElementPropOutput() { CodeElementId = input.Id, Index = 1, CodeValue = "110", Length = 3, Remark = "110" });
if (input.CodeLength > 7)
{
var len = input.CodeLength - 7;
var value = CodeHelper.GetCodeRandom(len);
result.Add(new CodeElementPropOutput() { CodeElementId = input.Id, Index = 2, CodeValue = value, Length = len, CodeType = "随机数", Remark = value });
}
result.Add(new CodeElementPropOutput() { CodeElementId = input.Id, Index = 3, CodeValue = "0000", Length = 4, CodeType = "序号", Remark = "0001" });
return result;
}
return list;
}
}