using Furion.DatabaseAccessor.Extensions;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 监测年级管理服务
///
public class ExamGradeService : IExamGradeService, ITransient
{
private readonly IRepository _rep;
public ExamGradeService(IRepository rep)
{
_rep = rep;
}
///
/// 批量添加监测年级
///
///
///
public async Task AddList(AddExamGradeListInput input)
{
var egradeids = await _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId).Select(t => t.GradeId).ToListAsync();
var newgradeids = input.GradeList.Where(t => !egradeids.Contains(t.GradeId));
if (!newgradeids.Any())
{
return;
}
List items = new();
foreach (var ng in newgradeids)
{
var item = ng.Adapt();
item.ExamPlanId = input.ExamPlanId;
items.Add(item);
}
await _rep.InsertAsync(items);
}
///
/// 移出监测年级
///
///
///
public async Task Remove(DelExamGradeInput input)
{
var roleUsers = await _rep.Where(t => input.IdList.Contains(t.Id)).ToListAsync();
await _rep.DeleteAsync(roleUsers);
}
///
/// 保存监测年级设置
///
///
///
public async Task SaveSetting(ExamGradeSettingInput input)
{
if (!await _rep.AnyAsync(t => t.Id == input.Id))
{
throw Oops.Oh(ErrorCode.E2001);
}
var item = input.Adapt();
await item.UpdateIncludeAsync(new[] {
nameof(item.IsRequiredSample),
nameof(item.IsRequiredSelfExamNumber),
nameof(item.SelfExamNumberLength),
nameof(item.Remark),
});
}
///
/// 根据ID获取监测年级
///
///
///
public async Task GetById(int id)
{
var item = await _rep.DetachedEntities.Include(t => t.Grade).FirstOrDefaultAsync(t => t.Id == id);
return item.Adapt();
}
///
/// 根据监测计划ID获取全部监测年级
///
///
///
public async Task> GetListByExamPlanId(int examPlanId)
{
var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId).ProjectToType().ToListAsync();
return items;
}
///
/// 根据监测计划和年级ID获取监测年级信息
///
///
///
///
public async Task GetByExamPlanGradeId(int examPlanId, int gradeId)
{
var item = await _rep.DetachedEntities.Include(t => t.Grade).FirstOrDefaultAsync(t => t.ExamPlanId == examPlanId && t.GradeId == gradeId);
return item.Adapt();
}
}