Files
OnboardXAdmin/SimpleAdmin.Application/Services/Onboarding/Company/OnboardingCompanyService.cs
T
2026-09-17 11:37:39 +08:00

162 lines
6.4 KiB
C#

namespace SimpleAdmin.Application;
/// <summary>
/// 实现入职业务公司业务能力。
/// </summary>
public class OnboardingCompanyService : DbRepository<ObCompany>, IOnboardingCompanyService
{
private readonly IOnboardingDataPermissionService _dataPermission;
public OnboardingCompanyService(IOnboardingDataPermissionService dataPermission)
{
_dataPermission = dataPermission;
}
/// <summary>
/// 分页查询入职业务公司。
/// </summary>
public async Task<SqlSugarPagedList<ObCompany>> Page(OnboardingCompanyPageInput input)
{
return await Context.Queryable<ObCompany>()
.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);
}
/// <summary>
/// 查询全部已启用的入职业务公司。
/// </summary>
public Task<List<ObCompany>> ListEnabled()
{
return Context.Queryable<ObCompany>()
.Where(it => it.Status == "enable")
.OrderBy(it => it.CompanyName)
.ToListAsync();
}
/// <summary>
/// 查询公司详情。
/// </summary>
public async Task<ObCompany> Detail(BaseIdInput input)
{
return await GetByIdAsync(input.Id) ?? throw Oops.Bah("公司不存在");
}
/// <summary>
/// 新增入职业务公司。
/// </summary>
public async Task Add(OnboardingCompanyAddInput input)
{
NormalizeAndValidateQiyuesuoConfig(input);
await EnsureCodeUnique(input.CompanyCode, 0);
await InsertAsync(input.Adapt<ObCompany>());
}
/// <summary>
/// 编辑入职业务公司。
/// </summary>
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<ObCompany>());
}
/// <summary>
/// 删除未被岗位、任务或员工引用的公司。
/// </summary>
public async Task Delete(BaseIdListInput input)
{
if (input.Ids.Count == 0)
return;
var companies = await Context.Queryable<ObCompany>()
.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<ObPosition>()
.AnyAsync(it => input.Ids.Contains(it.CompanyId));
if (hasPositions)
throw Oops.Bah("公司下仍有岗位,不能删除");
var hasDepartments = await Context.Queryable<ObDepartment>()
.AnyAsync(item => input.Ids.Contains(item.CompanyId));
if (hasDepartments)
throw Oops.Bah("公司下仍有部门,不能删除");
var hasOnboardingTemplates = await Context.Queryable<ObOnboardingTemplate>()
.AnyAsync(item => input.Ids.Contains(item.CompanyId));
if (hasOnboardingTemplates)
throw Oops.Bah("公司已被入职模板引用,不能删除");
var hasContractTemplates = await Context.Queryable<ObContractTemplate>()
.AnyAsync(item => item.CompanyId.HasValue && input.Ids.Contains(item.CompanyId.Value));
if (hasContractTemplates)
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 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 (!company.QiyuesuoCategoryId.All(char.IsDigit)
|| !company.QiyuesuoSealId.All(char.IsDigit))
{
throw Oops.Bah("契约锁默认业务分类 Id 和企业印章 Id 只能填写纯数字");
}
if (!long.TryParse(company.QiyuesuoCategoryId, out _)
|| !long.TryParse(company.QiyuesuoSealId, out _))
{
throw Oops.Bah("契约锁默认业务分类 Id 和企业印章 Id 超出有效数字范围(最大支持19位长整数)");
}
}
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("公司编码已存在");
}
}