using DatabaseCore;
using DatabaseCore.Models.Department;
using DepartmentBusinessLogic.BindingModels;
using DepartmentBusinessLogic.Interfaces;
using DepartmentBusinessLogic.ViewModels;
using Microsoft.EntityFrameworkCore;
using ModuleTools.Models;
using System;
using System.Linq;
namespace DepartmentDatabaseImplementation.Implementations
{
///
/// Реализация IAcademicPlanRecordService
///
public class AcademicPlanRecordService :
AbstractGenerticEntityService,
IAcademicPlanRecordService
{
protected override OperationResultModel AdditionalCheckingWhenAdding(DbContext context, AcademicPlanRecordSetBindingModel model) => OperationResultModel.Success(null);
protected override OperationResultModel AdditionalCheckingWhenDeleting(DbContext context, AcademicPlanRecord entity, AcademicPlanRecordGetBindingModel model) => OperationResultModel.Success(null);
protected override IQueryable AdditionalCheckingWhenReadingList(IQueryable query, AcademicPlanRecordGetBindingModel model)
{
if (model.AcademicPlanId.HasValue)
{
query = query.Where(x => x.AcademicPlanId == model.AcademicPlanId.Value);
}
if (model.DisciplineId.HasValue)
{
query = query.Where(x => x.DisciplineId == model.DisciplineId.Value);
}
if (model.Semester.HasValue)
{
query = query.Where(x => x.Semester == model.Semester.Value);
}
return query;
}
protected override OperationResultModel AdditionalCheckingWhenUpdateing(DbContext context, AcademicPlanRecordSetBindingModel model) => OperationResultModel.Success(null);
protected override void AdditionalDeleting(DbContext context, AcademicPlanRecord entity, AcademicPlanRecordGetBindingModel model)
{
var hours = context.Set().Where(x => x.AcademicPlanRecordId == model.Id);
foreach (var hour in hours)
{
hour.IsDeleted = true;
hour.DateDelete = DateTime.Now;
}
context.SaveChanges();
}
protected override AcademicPlanRecord GetUniqueEntity(AcademicPlanRecordSetBindingModel model, DbContext context) => context.Set().FirstOrDefault(x => x.AcademicPlanId == model.AcademicPlanId && x.DisciplineId == model.DisciplineId && x.Semester == model.Semester && x.Id != model.Id);
protected override IQueryable IncludingWhenReading(IQueryable query) => query.Include(x => x.AcademicPlan).Include(x => x.Discipline);
protected override IQueryable OrderingWhenReading(IQueryable query) =>
query.OrderBy(x => x.AcademicPlanId).ThenBy(x => x.Semester).ThenBy(x => x.AcademicPlanRecordParentId.HasValue || x.IsParent).ThenBy(x => x.AcademicPlanRecordParentId.HasValue).ThenBy(x => x.Discipline.DisciplineName);
}
}