DigitalFactory/Admin.NET/Admin.NET.Application/Service/Supplier/SupplierService.cs

119 lines
4.1 KiB
C#

using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 供应商服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class SupplierService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<Supplier> _rep;
public SupplierService(SqlSugarRepository<Supplier> rep)
{
_rep = rep;
}
/// <summary>
/// 分页查询供应商
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<SupplierOutput>> Page(SupplierInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Name.Contains(input.SearchKey.Trim())
|| u.Contacts.Contains(input.SearchKey.Trim())
|| u.Tel.Contains(input.SearchKey.Trim())
|| u.CompanyType.Contains(input.SearchKey.Trim())
|| u.Duties.Contains(input.SearchKey.Trim())
|| u.Address.Contains(input.SearchKey.Trim())
|| u.Remarks.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Contacts), u => u.Contacts.Contains(input.Contacts.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CompanyType), u => u.CompanyType.Contains(input.CompanyType.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Duties), u => u.Duties.Contains(input.Duties.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Address), u => u.Address.Contains(input.Address.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim()))
.Select<SupplierOutput>();
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(AddSupplierInput input)
{
var entity = input.Adapt<Supplier>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除供应商
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteSupplierInput input)
{
var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? 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(UpdateSupplierInput input)
{
var entity = input.Adapt<Supplier>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取供应商
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<Supplier> Detail([FromQuery] QueryByIdSupplierInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取供应商列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<SupplierOutput>> List([FromQuery] SupplierInput input)
{
return await _rep.AsQueryable().Where(a => !a.IsDelete).Select<SupplierOutput>().ToListAsync();
}
}