using Furion.DependencyInjection;
using GDZZ.Core.Entity;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GDZZ.Core.Service;
///
/// 员工职位服务
///
public class SysEmpPosService : ISysEmpPosService, ITransient
{
private readonly SqlSugarRepository _sysEmpPosRep; // 员工职位表仓储
public SysEmpPosService(SqlSugarRepository sysEmpPosRep)
{
_sysEmpPosRep = sysEmpPosRep;
}
///
/// 增加或编辑员工职位相关信息
///
/// 员工Id(用户Id)
/// 职位id集合
///
public async Task AddOrUpdate(long empId, List posIdList)
{
try
{
_sysEmpPosRep.CurrentBeginTran();
// 先删除
await DeleteEmpPosInfoByUserId(empId);
if (posIdList != null && posIdList.Any())
{
List list = new List();
posIdList.ForEach(u =>
{
list.Add(new SysEmpPos
{
SysEmpId = empId,
SysPosId = u
});
});
await _sysEmpPosRep.InsertAsync(list);
}
_sysEmpPosRep.CurrentCommitTran();
}
catch (System.Exception)
{
_sysEmpPosRep.CurrentRollbackTran();
throw;
}
}
///
/// 获取所属职位信息
///
/// 员工Id(用户Id)
public async Task> GetEmpPosList(long empId)
{
return await _sysEmpPosRep.AsQueryable().InnerJoin((e, p) => e.SysPosId == p.Id)
.Where((e, p) => e.SysEmpId == empId)
.Select((e, p) => new EmpPosOutput
{
PosId = p.Id,
PosCode = p.Code,
PosName = p.Name
}).ToListAsync();
}
///
/// 获取特殊所属职位信息
///
/// 员工Id(用户Id)
public async Task> GetEmpTSPosList(long empId)
{
return await _sysEmpPosRep.AsQueryable().InnerJoin((e, p) => e.SysPosId == p.Id)
.Where((e, p) => e.SysEmpId == empId)
.Where((e,p)=>p.Name == "老师")
.Select((e, p) => new EmpPosOutput
{
PosId = p.Id,
PosCode = p.Code,
PosName = p.Name
}).ToListAsync();
}
public async Task> GetEmpPosList(List empIds)
{
return await _sysEmpPosRep.AsQueryable().InnerJoin((e, p) => e.SysPosId == p.Id)
.Where((e, p) => empIds.Contains(e.SysEmpId))
.Select((e, p) => new EmpPosOutput
{
PosId = p.Id,
PosCode = p.Code,
PosName = p.Name
}).ToListAsync();
}
///
/// 根据职位Id判断该职位下是否有员工
///
///
///
public async Task HasPosEmp(long posId)
{
return await _sysEmpPosRep.AnyAsync(u => u.SysPosId == posId);
}
///
/// 根据员工Id删除对用的员工-职位信息
///
///
///
public async Task DeleteEmpPosInfoByUserId(long empId)
{
await _sysEmpPosRep.DeleteAsync(u => u.SysEmpId == empId);
}
}