using Furion.DatabaseAccessor.Extensions;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 监测数据上报类型管理服务
///
public class ExamDataReportService : IExamDataReportService, ITransient
{
private readonly IRepository _rep;
public ExamDataReportService(IRepository rep)
{
_rep = rep;
}
#region 创建更新
///
/// 添加上报类型
///
///
///
public async Task Add(AddExamDataReportInput input)
{
if (await _rep.AnyAsync(t => t.ExamPlanId == input.ExamPlanId && t.Type == input.Type))
{
throw Oops.Oh(ErrorCode.E2002, "该类型");
}
var item = input.Adapt();
await item.InsertAsync();
}
///
/// 更新上报类型
///
///
///
public async Task Update(UpdateExamDataReportInput input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
item.Remark = input.Remark;
await item.UpdateIncludeAsync(new[] { nameof(item.BeginTime), nameof(item.EndTime), nameof(item.Remark) });
}
///
/// 删除上报类型
///
///
///
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.READY)
{
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.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
return item.Adapt();
}
///
/// 根据监测计划ID获取数据上报类型列表
///
///
///
public async Task> GetListByExamPlanId(int examPlanId)
{
var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId).ProjectToType().OrderBy(t => t.Type).ToListAsync();
var orgCount = await _rep.Change().DetachedEntities.Where(t => t.ExamPlanId == examPlanId).CountAsync();
foreach (var item in items)
{
var reportedCount = await _rep.Change().DetachedEntities.Where(t => t.Type == item.Type && t.ExamOrg.ExamPlanId == examPlanId && t.Status == DataReportStatus.REPORTED).CountAsync();
item.ReportedCount = reportedCount;
item.Count = orgCount;
}
return items;
}
#endregion
}