using Furion.DatabaseAccessor.Extensions;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 机构信息服务
///
public class SysOrgService : ISysOrgService, ITransient
{
private readonly IRepository _rep;
public SysOrgService(IRepository rep)
{
_rep = rep;
}
///
/// 添加机构
///
///
///
public async Task Add(AddSysOrgInput input)
{
if (await _rep.AnyAsync(t => t.FullName.ToUpper() == input.FullName.ToUpper() || t.Name.ToUpper() == input.Name.ToUpper() || t.ShortName.ToUpper() == input.ShortName.ToUpper()))
{
throw Oops.Oh(ErrorCode.E2003, "", "全称、名称或简称");
}
if (await _rep.AnyAsync(t => t.OrgType == input.OrgType && t.EducationStage == input.EducationStage && t.Code.ToUpper() == input.Code.ToUpper()))
{
throw Oops.Oh(ErrorCode.E2003, "", "代码");
}
var item = input.Adapt();
await item.InsertAsync();
}
///
/// 更新机构
///
///
///
public async Task Update(UpdateSysOrgInput input)
{
if (await _rep.AnyAsync(t => t.Id != input.Id && (t.FullName.ToUpper() == input.FullName.ToUpper() || t.Name.ToUpper() == input.Name.ToUpper() || t.ShortName.ToUpper() == input.ShortName.ToUpper())))
{
throw Oops.Oh(ErrorCode.E2003, "其他机构", "的全称、名称或简称");
}
if (await _rep.AnyAsync(t => t.Id != input.Id && t.OrgType == input.OrgType && t.EducationStage == input.EducationStage && t.Code.ToUpper() == input.Code.ToUpper()))
{
throw Oops.Oh(ErrorCode.E2003, "其他机构", "的代码");
}
var item = input.Adapt();
await item.UpdateAsync();
}
///
/// 删除机构
///
///
///
public async Task Del(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
item.Status = CommonStatus.DELETED;
await item.UpdateIncludeAsync(new[] { nameof(item.Status) });
await item.FakeDeleteAsync();
}
///
/// 启用禁用机构
///
///
///
public async Task ChangeStatus(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
item.Status = item.Status == CommonStatus.ENABLE ? CommonStatus.DISABLE : CommonStatus.ENABLE;
await item.UpdateIncludeAsync(new[] { nameof(item.Status) });
}
///
/// 根据ID获取机构
///
///
///
public async Task GetById(short id)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
return item.Adapt();
}
///
/// 查询机构列表
///
///
///
public async Task> QueryList(QuerySysOrgInput input)
{
var name = !string.IsNullOrEmpty(input.Name?.Trim());
var code = !string.IsNullOrEmpty(input.Code?.Trim());
var items = await _rep.DetachedEntities
.Where((name, u => EF.Functions.Like(u.ShortName, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.FullName, $"%{input.Name.Trim()}%")))
.Where((code, u => EF.Functions.Like(u.Code, $"%{input.Code.Trim()}%")))
.Where(input.OrgType.HasValue, u => u.OrgType == input.OrgType)
.Where(input.EducationStage.HasValue, u => u.EducationStage == input.EducationStage)
.Where(input.OrgFundSource.HasValue, u => u.OrgFundSource == input.OrgFundSource)
.Where(input.UrbanRuralType.HasValue, u => u.UrbanRuralType == input.UrbanRuralType)
.Where(input.Status.HasValue, u => u.Status == input.Status)
.Where(t => t.IsDeleted == false)
.OrderBy(u => u.OrgType).ThenBy(t => t.EducationStage).ThenBy(t => t.Code)
.ProjectToType()
.ToListAsync();
return items;
}
///
/// 分页查询机构列表
///
///
///
public async Task> QueryPageList(SysOrgPageInput input)
{
var name = !string.IsNullOrEmpty(input.Name?.Trim());
var code = !string.IsNullOrEmpty(input.Code?.Trim());
var ret = await _rep.DetachedEntities
.Where((name, u => EF.Functions.Like(u.ShortName, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.FullName, $"%{input.Name.Trim()}%")))
.Where((code, u => EF.Functions.Like(u.Code, $"%{input.Code.Trim()}%")))
.Where(input.OrgType.HasValue, u => u.OrgType == input.OrgType)
.Where(input.EducationStage.HasValue, u => u.EducationStage == input.EducationStage)
.Where(input.OrgFundSource.HasValue, u => u.OrgFundSource == input.OrgFundSource)
.Where(input.UrbanRuralType.HasValue, u => u.UrbanRuralType == input.UrbanRuralType)
.Where(input.Status.HasValue, u => u.Status == input.Status)
.Where(t => t.IsDeleted == false)
.OrderBy(u => u.OrgType).ThenBy(t => t.EducationStage).ThenBy(t => t.Code)
.ProjectToType()
.ToADPagedListAsync(input.PageIndex, input.PageSize);
return ret;
}
///
/// 获取全部机构列表
///
///
public async Task> GetAllList()
{
var items = await _rep.DetachedEntities.Where(t => t.IsDeleted == false).ProjectToType().ToListAsync();
return items;
}
///
/// 根据学校ID获取校区列表
///
///
///
public async Task> GetOrgBranchByOrgId(short orgId)
{
var items = await _rep.DetachedEntities.Where(t => t.OrgType == OrgType.SCHOOL_BRANCH && t.Pid == orgId).ProjectToType().ToListAsync();
return items;
}
}