namespace SimpleAdmin.Application; public class OnboardingCompanyService : DbRepository, IOnboardingCompanyService { public async Task> Page(OnboardingCompanyPageInput input) { return await Context.Queryable() .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> ListEnabled() { return Context.Queryable() .Where(it => it.Status == "enable") .OrderBy(it => it.CompanyName) .ToListAsync(); } public async Task 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()); } public async Task Edit(OnboardingCompanyEditInput input) { await Detail(new BaseIdInput { Id = input.Id }); await EnsureCodeUnique(input.CompanyCode, input.Id); await UpdateAsync(input.Adapt()); } public async Task Delete(BaseIdListInput input) { if (input.Ids.Count == 0) return; var hasPositions = await Context.Queryable() .AnyAsync(it => input.Ids.Contains(it.CompanyId)); if (hasPositions) throw Oops.Bah("公司下仍有岗位,不能删除"); var hasTasks = await Context.Queryable() .AnyAsync(it => input.Ids.Contains(it.CompanyId)); if (hasTasks) throw Oops.Bah("公司已有入职任务,不能删除"); var hasEmployees = await Context.Queryable() .AnyAsync(it => input.Ids.Contains(it.CompanyId)); if (hasEmployees) throw Oops.Bah("公司已有正式员工,不能删除"); await DeleteByIdsAsync(input.Ids.Cast().ToArray()); } private async Task EnsureCodeUnique(string code, long id) { var exists = await Context.Queryable() .AnyAsync(it => it.CompanyCode == code && it.Id != id); if (exists) throw Oops.Bah("公司编码已存在"); } }