using Furion.DatabaseAccessor.Extensions;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 高考划线计划管理服务
///
public class NceePlanService : INceePlanService, ITransient
{
private readonly IRepository _rep;
public NceePlanService(IRepository rep)
{
_rep = rep;
}
#region 创建更新
///
/// 添加计划
///
///
///
public async Task Add(AddNceePlanInput input)
{
var maxPlan = await _rep.Where(t => t.SemesterId == input.SemesterId && t.GradeId == input.GradeId).OrderByDescending(t => t.Sequence).FirstOrDefaultAsync();
short seq = maxPlan?.Sequence ?? 0;
var item = input.Adapt();
item.Sequence = (short)(seq + 1);
await item.InsertAsync();
}
///
/// 更新计划
///
///
///
public async Task Update(UpdateNceePlanInput 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.Name),
nameof(item.FullName),
nameof(item.ShortName),
nameof(item.Remark),
nameof(item.Config),
});
}
///
/// 删除计划
///
///
///
public async Task Del(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
if (item.Status == ExamStatus.ACTIVE || item.Status == ExamStatus.STOPPED)
{
throw Oops.Oh(ErrorCode.E3001, "已使用");
}
await item.DeleteAsync();
}
#endregion
#region 状态变更
///
/// 开始监测
///
///
///
public async Task Start(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
if (item.Status != ExamStatus.READY)
{
throw Oops.Oh(ErrorCode.E2006);
}
item.Status = ExamStatus.ACTIVE;
item.BeginTime = DateTime.Now;
await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.BeginTime) });
}
///
/// 结束监测
///
///
///
public async Task Stop(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
if (item.Status != ExamStatus.ACTIVE)
{
throw Oops.Oh(ErrorCode.E2006);
}
item.Status = ExamStatus.STOPPED;
item.EndTime = DateTime.Now;
await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.EndTime) });
}
///
/// 取消监测
///
///
///
public async Task Cancel(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
if (item.Status == ExamStatus.ACTIVE)
{
throw Oops.Oh(ErrorCode.E2006);
}
item.Status = ExamStatus.CANCELLED;
item.EndTime = DateTime.Now;
await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.EndTime) });
}
#endregion
#region 查询统计
///
/// 根据ID获取计划
///
///
///
public async Task GetById(int id)
{
var item = await _rep.DetachedEntities.ProjectToType()
.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
return item;
}
///
/// 分页查询计划列表
///
///
///
public async Task> QueryPageList(NceePlanPageInput input)
{
var query = GetQueryBase(input);
var ret = await query.OrderByDescending(t => t.CreateTime).ProjectToType().ToADPagedListAsync(input.PageIndex, input.PageSize);
return ret;
}
///
/// 获取我的单据状态数量
///
///
public async Task> QueryStatusCount(NceePlanPageInput input)
{
var query = GetQueryBase(input);
if (query == null)
{
return new List();
}
var counts = await query.GroupBy(t => t.Status).Select(t => new StatusCount { Status = (int)t.Key, Count = t.Count() }).ToListAsync();
return counts;
}
#endregion
#region 私有方法
///
/// 构建查询
///
///
///
private IQueryable GetQueryBase(NceePlanPageInput input)
{
var name = !string.IsNullOrEmpty(input.Name?.Trim());
var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
var searchEndTime = !string.IsNullOrEmpty(input.SearchEndTime?.Trim());
var query = _rep.DetachedEntities.Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.FullName, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.ShortName, $"%{input.Name.Trim()}%")))
.Where(input.SemesterId.HasValue, t => t.SemesterId == input.SemesterId)
.Where(input.GradeId.HasValue, t => t.GradeId == input.GradeId)
.Where(input.Status.HasValue, t => t.Status == input.Status)
.Where((searchBeginTime, u => u.CreateTime >= DateTime.Parse(input.SearchBeginTime)))
.Where((searchEndTime, u => u.CreateTime <= DateTime.Parse(input.SearchEndTime)));
return query;
}
#endregion
}