74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
namespace SimpleAdmin.Application;
|
|
|
|
public class OnboardingCompanyService : DbRepository<ObCompany>, IOnboardingCompanyService
|
|
{
|
|
public async Task<SqlSugarPagedList<ObCompany>> Page(OnboardingCompanyPageInput input)
|
|
{
|
|
return await Context.Queryable<ObCompany>()
|
|
.WhereIF(
|
|
!string.IsNullOrWhiteSpace(input.CompanyName),
|
|
it => it.CompanyName.Contains(input.CompanyName) || it.CompanyCode.Contains(input.CompanyName))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Status), it => it.Status == input.Status)
|
|
.OrderByIF(!string.IsNullOrWhiteSpace(input.SortField), $"{input.SortField} {input.SortOrder}")
|
|
.OrderBy(it => it.CreateTime)
|
|
.ToPagedListAsync(input.PageNum, input.PageSize);
|
|
}
|
|
|
|
public Task<List<ObCompany>> ListEnabled()
|
|
{
|
|
return Context.Queryable<ObCompany>()
|
|
.Where(it => it.Status == "enable")
|
|
.OrderBy(it => it.CompanyName)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<ObCompany> Detail(BaseIdInput input)
|
|
{
|
|
return await GetByIdAsync(input.Id) ?? throw Oops.Bah("公司不存在");
|
|
}
|
|
|
|
public async Task Add(OnboardingCompanyAddInput input)
|
|
{
|
|
await EnsureCodeUnique(input.CompanyCode, 0);
|
|
await InsertAsync(input.Adapt<ObCompany>());
|
|
}
|
|
|
|
public async Task Edit(OnboardingCompanyEditInput input)
|
|
{
|
|
await Detail(new BaseIdInput { Id = input.Id });
|
|
await EnsureCodeUnique(input.CompanyCode, input.Id);
|
|
await UpdateAsync(input.Adapt<ObCompany>());
|
|
}
|
|
|
|
public async Task Delete(BaseIdListInput input)
|
|
{
|
|
if (input.Ids.Count == 0)
|
|
return;
|
|
|
|
var hasPositions = await Context.Queryable<ObPosition>()
|
|
.AnyAsync(it => input.Ids.Contains(it.CompanyId));
|
|
if (hasPositions)
|
|
throw Oops.Bah("公司下仍有岗位,不能删除");
|
|
|
|
var hasTasks = await Context.Queryable<ObOnboardingTask>()
|
|
.AnyAsync(it => input.Ids.Contains(it.CompanyId));
|
|
if (hasTasks)
|
|
throw Oops.Bah("公司已有入职任务,不能删除");
|
|
|
|
var hasEmployees = await Context.Queryable<ObEmployee>()
|
|
.AnyAsync(it => input.Ids.Contains(it.CompanyId));
|
|
if (hasEmployees)
|
|
throw Oops.Bah("公司已有正式员工,不能删除");
|
|
|
|
await DeleteByIdsAsync(input.Ids.Cast<object>().ToArray());
|
|
}
|
|
|
|
private async Task EnsureCodeUnique(string code, long id)
|
|
{
|
|
var exists = await Context.Queryable<ObCompany>()
|
|
.AnyAsync(it => it.CompanyCode == code && it.Id != id);
|
|
if (exists)
|
|
throw Oops.Bah("公司编码已存在");
|
|
}
|
|
}
|