289 lines
10 KiB
C#
289 lines
10 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using ZhonTai.Admin.Core.Dto;
|
||
using ZhonTai.Admin.Services;
|
||
using NPP.SmartSchedue.Api.Contracts.Services.Personnel;
|
||
using NPP.SmartSchedue.Api.Contracts.Services.Personnel.Input;
|
||
using NPP.SmartSchedue.Api.Contracts.Services.Personnel.Output;
|
||
using NPP.SmartSchedue.Api.Repositories.Personnel;
|
||
using NPP.SmartSchedue.Api.Contracts.Domain.Personnel;
|
||
using ZhonTai.Admin.Core.Consts;
|
||
using ZhonTai.DynamicApi;
|
||
using ZhonTai.DynamicApi.Attributes;
|
||
using ZhonTai.Admin.Domain.User;
|
||
|
||
namespace NPP.SmartSchedue.Api.Services.Personnel;
|
||
|
||
/// <summary>
|
||
/// 人员资质绑定服务
|
||
/// </summary>
|
||
[DynamicApi(Area = "app")]
|
||
public class PersonnelQualificationService : BaseService, IPersonnelQualificationService, IDynamicApi
|
||
{
|
||
private readonly PersonnelQualificationRepository _personnelQualificationRepository;
|
||
|
||
public PersonnelQualificationService(PersonnelQualificationRepository personnelQualificationRepository)
|
||
{
|
||
_personnelQualificationRepository = personnelQualificationRepository;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public async Task<PersonnelQualificationGetOutput> GetAsync(long id)
|
||
{
|
||
var output = await _personnelQualificationRepository.Select
|
||
.WhereDynamic(id)
|
||
.ToOneAsync<PersonnelQualificationGetOutput>();
|
||
|
||
return output;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询分页
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public async Task<PageOutput<PersonnelQualificationGetPageOutput>> GetPageAsync(PageInput<PersonnelQualificationGetPageInput> input)
|
||
{
|
||
var list = await _personnelQualificationRepository.Select
|
||
.WhereIf(input.Filter?.PersonnelId.HasValue == true, a => a.PersonnelId == input.Filter.PersonnelId)
|
||
.WhereIf(input.Filter?.QualificationId.HasValue == true, a => a.QualificationId == input.Filter.QualificationId)
|
||
.WhereIf(input.Filter?.IsActive.HasValue == true, a => a.IsActive == input.Filter.IsActive)
|
||
.Count(out var total)
|
||
.OrderByDescending(a => a.Id)
|
||
.Page(input.CurrentPage, input.PageSize)
|
||
.ToListAsync<PersonnelQualificationGetPageOutput>();
|
||
|
||
var data = new PageOutput<PersonnelQualificationGetPageOutput>()
|
||
{
|
||
List = list,
|
||
Total = total
|
||
};
|
||
|
||
return data;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public async Task<long> AddAsync(PersonnelQualificationAddInput input)
|
||
{
|
||
var entity = Mapper.Map<PersonnelQualificationEntity>(input);
|
||
var result = await _personnelQualificationRepository.InsertAsync(entity);
|
||
return result.Id;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public async Task UpdateAsync(PersonnelQualificationUpdateInput input)
|
||
{
|
||
var entity = await _personnelQualificationRepository.GetAsync(input.Id);
|
||
Mapper.Map(input, entity);
|
||
|
||
await _personnelQualificationRepository.UpdateAsync(entity);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public async Task DeleteAsync(long id)
|
||
{
|
||
await _personnelQualificationRepository.DeleteAsync(id);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 软删除
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public async Task SoftDeleteAsync(long id)
|
||
{
|
||
await _personnelQualificationRepository.SoftDeleteAsync(id);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 批量软删除
|
||
/// </summary>
|
||
/// <param name="ids"></param>
|
||
/// <returns></returns>
|
||
public async Task BatchSoftDeleteAsync(long[] ids)
|
||
{
|
||
await _personnelQualificationRepository.SoftDeleteAsync(ids);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据资质ID列表获取具有这些资质的人员基础信息列表(ID + 姓名)
|
||
/// </summary>
|
||
/// <param name="qualificationIds">资质ID列表</param>
|
||
/// <returns></returns>
|
||
public async Task<List<PersonnelBasicInfo>> GetPersonnelIdsByQualificationIdsAsync(long[] qualificationIds)
|
||
{
|
||
var personnelInfoMap = new Dictionary<long, PersonnelBasicInfo>();
|
||
|
||
var qualifiedPersonnel = await _personnelQualificationRepository.Select
|
||
.Where(pq => qualificationIds.Contains(pq.QualificationId) && pq.IsActive)
|
||
.ToListAsync(p => new PersonnelBasicInfo
|
||
{
|
||
Id = p.PersonnelId,
|
||
Name = p.PersonnelName // 临时使用默认格式
|
||
});
|
||
|
||
// 去重处理
|
||
foreach (var personnel in qualifiedPersonnel)
|
||
{
|
||
personnelInfoMap[personnel.Id] = personnel;
|
||
}
|
||
|
||
return personnelInfoMap.Values.ToList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据资质ID字符串(逗号分隔)获取具有这些资质的人员基础信息列表(ID + 姓名)
|
||
/// </summary>
|
||
/// <param name="qualificationIds">资质ID字符串,使用逗号分隔</param>
|
||
/// <returns></returns>
|
||
public async Task<List<PersonnelBasicInfo>> GetPersonnelIdsByQualificationIdsAsync(string qualificationIds)
|
||
{
|
||
// 检查输入参数的有效性
|
||
if (string.IsNullOrWhiteSpace(qualificationIds))
|
||
{
|
||
return new List<PersonnelBasicInfo>();
|
||
}
|
||
|
||
try
|
||
{
|
||
// 解析逗号分隔的字符串为long数组
|
||
var qualificationIdArray = qualificationIds
|
||
.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
||
.Select(id => id.Trim())
|
||
.Where(id => !string.IsNullOrWhiteSpace(id))
|
||
.Select(long.Parse)
|
||
.ToArray();
|
||
|
||
// 调用原有的数组版本方法
|
||
return await GetPersonnelIdsByQualificationIdsAsync(qualificationIdArray);
|
||
}
|
||
catch (FormatException)
|
||
{
|
||
// 如果解析失败,返回空列表而不是抛出异常
|
||
return new List<PersonnelBasicInfo>();
|
||
}
|
||
catch (OverflowException)
|
||
{
|
||
// 如果数值超出范围,返回空列表而不是抛出异常
|
||
return new List<PersonnelBasicInfo>();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据人员ID获取其所有有效资质信息
|
||
/// </summary>
|
||
/// <param name="personnelId">人员ID</param>
|
||
/// <returns></returns>
|
||
public async Task<List<PersonnelQualificationGetPageOutput>> GetActiveQualificationsByPersonnelIdAsync(long personnelId)
|
||
{
|
||
var input = new PageInput<PersonnelQualificationGetPageInput>
|
||
{
|
||
Filter = new PersonnelQualificationGetPageInput { PersonnelId = personnelId, IsActive = true },
|
||
PageSize = 1000,
|
||
CurrentPage = 1
|
||
};
|
||
|
||
var result = await GetPageAsync(input);
|
||
return result.List?.ToList() ?? new List<PersonnelQualificationGetPageOutput>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取所有有资质记录的人员列表(用于人员池构建)
|
||
/// 深度业务思考:从资质表获取所有关联人员,确保智能分配系统的人员池完整性
|
||
/// </summary>
|
||
/// <returns>所有有资质记录的人员资质信息</returns>
|
||
public async Task<List<PersonnelQualificationGetPageOutput>> GetAllPersonnelWithQualificationsAsync()
|
||
{
|
||
try
|
||
{
|
||
// 获取所有有效的人员资质记录
|
||
// 这里不分页,获取全量数据用于构建人员池
|
||
var allQualifications = await _personnelQualificationRepository.Select
|
||
.Where(pq => pq.PersonnelId > 0) // 确保有有效的人员ID
|
||
.OrderBy(pq => pq.PersonnelId)
|
||
.ToListAsync<PersonnelQualificationGetPageOutput>();
|
||
|
||
return allQualifications ?? new List<PersonnelQualificationGetPageOutput>();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return new List<PersonnelQualificationGetPageOutput>();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据人员ID获取其所有资质记录(包括无效的)
|
||
/// </summary>
|
||
/// <param name="personnelId">人员ID</param>
|
||
/// <returns>人员的所有资质记录</returns>
|
||
public async Task<List<PersonnelQualificationGetPageOutput>> GetByPersonnelIdAsync(long personnelId)
|
||
{
|
||
try
|
||
{
|
||
// 获取指定人员的所有资质记录,不论是否有效
|
||
var personnelQualifications = await _personnelQualificationRepository.Select
|
||
.Where(pq => pq.PersonnelId == personnelId)
|
||
.OrderBy(pq => pq.QualificationId)
|
||
.ToListAsync<PersonnelQualificationGetPageOutput>();
|
||
|
||
return personnelQualifications ?? new List<PersonnelQualificationGetPageOutput>();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return new List<PersonnelQualificationGetPageOutput>();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据人员ID获取其所有有效的资质实体列表(用于智能分配系统资质匹配)
|
||
/// 深度业务思考:智能分配系统需要完整的资质实体信息进行精确匹配计算
|
||
/// 1. 只获取有效状态的资质记录(IsActive = true)
|
||
/// 2. 检查资质有效期,排除已过期资质
|
||
/// 3. 返回完整实体信息供复杂业务逻辑使用
|
||
/// 4. 异常处理确保系统稳定性
|
||
/// </summary>
|
||
/// <param name="personnelId">人员ID</param>
|
||
/// <returns>人员有效资质实体列表</returns>
|
||
public async Task<List<PersonnelQualificationEntity>> GetPersonnelQualificationsAsync(long personnelId)
|
||
{
|
||
try
|
||
{
|
||
// 深度思考:智能分配系统需要精确的资质匹配
|
||
// 必须同时满足:1. 状态有效 2. 未过期 3. 有效的人员关联
|
||
var currentTime = DateTime.Now;
|
||
|
||
var qualificationEntities = await _personnelQualificationRepository.Select
|
||
.Where(pq => pq.PersonnelId == personnelId &&
|
||
pq.IsActive && // 必须是激活状态
|
||
(pq.ExpiryDate == null || pq.ExpiryDate > currentTime)) // 未过期或永久有效
|
||
.OrderBy(pq => pq.QualificationId)
|
||
.ToListAsync();
|
||
|
||
return qualificationEntities ?? new List<PersonnelQualificationEntity>();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return new List<PersonnelQualificationEntity>();
|
||
}
|
||
}
|
||
} |