DigitalFactory/Admin.NET/Admin.NET.Application/Service/Employee/EmployeeService.cs

122 lines
4.4 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 EmployeeService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<Employee> _rep;
public EmployeeService(SqlSugarRepository<Employee> rep)
{
_rep = rep;
}
/// <summary>
/// 分页查询员工管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<EmployeeOutput>> Page(EmployeeInput input)
{
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Name.Contains(input.SearchKey.Trim())
|| u.Job.Contains(input.SearchKey.Trim())
|| u.Sex.Contains(input.SearchKey.Trim())
|| u.Tel.Contains(input.SearchKey.Trim())
|| u.Email.Contains(input.SearchKey.Trim())
|| u.Address.Contains(input.SearchKey.Trim())
|| u.IDNumber.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.Job), u => u.Job.Contains(input.Job.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Sex), u => u.Sex.Contains(input.Sex.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Email), u => u.Email.Contains(input.Email.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Address), u => u.Address.Contains(input.Address.Trim()))
.WhereIF(input.Age>0, u => u.Age == input.Age)
.WhereIF(!string.IsNullOrWhiteSpace(input.IDNumber), u => u.IDNumber.Contains(input.IDNumber.Trim()))
.WhereIF(input.DepartmentId>0, u => u.DepartmentId == input.DepartmentId)
.WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim()))
.Select<EmployeeOutput>();
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(AddEmployeeInput input)
{
var entity = input.Adapt<Employee>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除员工管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteEmployeeInput 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(UpdateEmployeeInput input)
{
var entity = input.Adapt<Employee>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取员工管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<Employee> Detail([FromQuery] QueryByIdEmployeeInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取员工管理列表
/// </summary>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<EmployeeOutput>> List()
{
return await _rep.AsQueryable().Select<EmployeeOutput>().Where(a => !a.IsDelete).ToListAsync();
}
}