namespace SimpleAdmin.Application; /// /// 实现入职业务公司业务能力。 /// public class OnboardingCompanyService : DbRepository, IOnboardingCompanyService { private readonly IOnboardingDataPermissionService _dataPermission; public OnboardingCompanyService(IOnboardingDataPermissionService dataPermission) { _dataPermission = dataPermission; } /// /// 分页查询入职业务公司。 /// public async Task> Page(OnboardingCompanyPageInput input) { return await Context.Queryable() .WhereIF(!string.IsNullOrWhiteSpace(input.CompanyCode), it => it.CompanyCode.Contains(input.CompanyCode)) .WhereIF(!string.IsNullOrWhiteSpace(input.CompanyName), it => it.CompanyName.Contains(input.CompanyName)) .WhereIF(!string.IsNullOrWhiteSpace(input.Status), it => it.Status == input.Status) .OrderByIF(!string.IsNullOrWhiteSpace(input.SortField), input.GetSafeOrderBy("Id", "Status", "CreateTime", "UpdateTime", "CreateUser", "CreateUserId", "CompanyCode", "CompanyName", "LegalName", "ContactPhone")) .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) { NormalizeAndValidateQiyuesuoConfig(input); await EnsureCodeUnique(input.CompanyCode, 0); await InsertAsync(input.Adapt()); } /// /// 编辑入职业务公司。 /// public async Task Edit(OnboardingCompanyEditInput input) { NormalizeAndValidateQiyuesuoConfig(input); var company = await Detail(new BaseIdInput { Id = input.Id }); await _dataPermission.EnsureCanManage( company.CreateUserId, OnboardingPermissionCodes.SetupManageAll, "公司"); await EnsureCodeUnique(input.CompanyCode, input.Id); await UpdateAsync(input.Adapt()); } /// /// 删除未被岗位、任务或员工引用的公司。 /// public async Task Delete(BaseIdListInput input) { if (input.Ids.Count == 0) return; var companies = await Context.Queryable() .Where(item => input.Ids.Contains(item.Id)) .ToListAsync(); if (companies.Count != input.Ids.Distinct().Count()) throw Oops.Bah("公司不存在"); await _dataPermission.EnsureCanManage( companies.Select(item => item.CreateUserId), OnboardingPermissionCodes.SetupManageAll, "公司"); var hasPositions = await Context.Queryable() .AnyAsync(it => input.Ids.Contains(it.CompanyId)); if (hasPositions) throw Oops.Bah("公司下仍有岗位,不能删除"); var hasDepartments = await Context.Queryable() .AnyAsync(item => input.Ids.Contains(item.CompanyId)); if (hasDepartments) throw Oops.Bah("公司下仍有部门,不能删除"); var hasOnboardingTemplates = await Context.Queryable() .AnyAsync(item => input.Ids.Contains(item.CompanyId)); if (hasOnboardingTemplates) throw Oops.Bah("公司已被入职模板引用,不能删除"); var hasContractTemplates = await Context.Queryable() .AnyAsync(item => item.CompanyId.HasValue && input.Ids.Contains(item.CompanyId.Value)); if (hasContractTemplates) 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 static void NormalizeAndValidateQiyuesuoConfig(ObCompany company) { company.CompanyCode = OnboardingCodeValidator.Normalize(company.CompanyCode, "公司编码", 50); company.CompanyName = company.CompanyName?.Trim(); company.LegalName = company.LegalName?.Trim(); if (string.IsNullOrWhiteSpace(company.CompanyName)) throw Oops.Bah("公司名称不能为空"); company.QiyuesuoCategoryId = company.QiyuesuoCategoryId?.Trim(); company.QiyuesuoSealId = company.QiyuesuoSealId?.Trim(); var hasCategoryId = !string.IsNullOrWhiteSpace(company.QiyuesuoCategoryId); var hasSealId = !string.IsNullOrWhiteSpace(company.QiyuesuoSealId); if (hasCategoryId != hasSealId) throw Oops.Bah("契约锁默认业务分类 Id 和企业印章 Id 必须同时填写"); if (!hasCategoryId) return; if (!long.TryParse(company.QiyuesuoCategoryId, out _) || !long.TryParse(company.QiyuesuoSealId, out _)) { throw Oops.Bah("契约锁默认业务分类 Id 和企业印章 Id 只能填写数字"); } } 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("公司编码已存在"); } }