using Furion.DatabaseAccessor.Extensions;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 监测发布内容管理服务
///
public class ExamDataPublishService : IExamDataPublishService, ITransient
{
private readonly IRepository _rep;
public ExamDataPublishService(IRepository rep)
{
_rep = rep;
}
#region 创建更新
///
/// 添加发布内容
///
///
///
public async Task Add(AddExamDataPublishInput 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(UpdateExamDataPublishInput input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
item.Name = input.Name;
item.Remark = input.Remark;
await item.UpdateIncludeAsync(new[] { nameof(item.Name), 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 != PublishStatus.UNPUBLISH)
{
throw Oops.Oh(ErrorCode.E3001);
}
await item.DeleteAsync();
}
#endregion
#region 状态处理
///
/// 发布
///
///
///
public async Task Publish(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
if (item.Status == PublishStatus.PUBLISHED)
{
throw Oops.Oh(ErrorCode.E2006);
}
item.Status = PublishStatus.PUBLISHED;
item.PublishTime = DateTime.Now;
item.PublishSysUserId = CurrentSysUserInfo.SysUserId;
await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.PublishTime), nameof(item.PublishSysUserId) });
}
///
/// 取消
///
///
///
public async Task Unpublish(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
if (item.Status != PublishStatus.PUBLISHED)
{
throw Oops.Oh(ErrorCode.E2006);
}
item.Status = PublishStatus.UNPUBLISH; // 设置为未发布
await item.UpdateIncludeAsync(new[] { nameof(item.Status) });
}
#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();
return items;
}
#endregion
#region 上传匹配
#endregion
}