using Furion.DatabaseAccessor.Extensions; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using YBEE.EQM.Core; namespace YBEE.EQM.Application; /// /// 监测机构反馈结果管理服务 /// public class ExamOrgResultService : IExamOrgResultService, ITransient { private readonly IRepository _rep; private readonly EqmSiteOptions _eqmSiteOptions; private readonly IHttpContextAccessor _httpContextAccessor; public ExamOrgResultService(IRepository rep, IOptions options, IHttpContextAccessor httpContextAccessor) { _rep = rep; _eqmSiteOptions = options.Value; _httpContextAccessor = httpContextAccessor; } /// /// 添加结果文件 /// /// /// public async Task Add(AddExamOrgResultInput input) { var item = input.Adapt(); await item.InsertNowAsync(); } /// /// 删除文件 /// /// /// public async Task Del(BaseId input) { var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001); var fpath = Path.Combine(_eqmSiteOptions.ExamResultRoot, item.FilePath); if (File.Exists(fpath)) { File.Delete(fpath); } await item.DeleteAsync(); } /// /// 保存下载记录 /// /// /// public async Task SaveDownloadRecord(int id) { var httpContext = _httpContextAccessor.HttpContext; ExamOrgResultDownload item = new() { ExamOrgResultId = id, SysUserId = CurrentSysUserInfo.SysUserId, Account = CurrentSysUserInfo.Account, Ip = httpContext.GetRequestIPv4(), DownloadTime = DateTime.Now, }; await _rep.Change().InsertNowAsync(item); } /// /// 获取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(); } /// /// 根据监测计划获取机构反馈文件列表 /// /// /// public async Task> GetByExamPlanId(int examPlanId) { var pubItems = await _rep.Change().DetachedEntities.Where(t => t.ExamPlanId == examPlanId && t.Status == PublishStatus.PUBLISHED).ProjectToType().ToListAsync(); foreach (var pubItem in pubItems) { if (pubItem.Status == PublishStatus.PUBLISHED) { var items = await _rep.DetachedEntities.Where(t => t.SysOrgId == CurrentSysUserInfo.SysOrgId && t.ExamPlanId == examPlanId && t.ExamDataPublishId == pubItem.Id).ProjectToType().ToListAsync(); pubItem.ExamOrgResultList = items; } } return pubItems; } /// /// 根据发布内容ID获取机构文件列表 /// /// /// public async Task> GetByPublishId(int publishId) { var pub = await _rep.Change().FirstOrDefaultAsync(t => t.Id == publishId) ?? throw Oops.Oh(ErrorCode.E2001); var items = await _rep.DetachedEntities.Where(t => t.ExamDataPublishId == publishId).ProjectToType().OrderBy(t => t.SysOrgId).ToListAsync(); var norgs = await (from o in _rep.Change().AsQueryable() where o.ExamPlanId == pub.ExamPlanId && !(from r in items select r.ExamOrgId).Contains(o.Id) select o).ProjectToType().ToListAsync(); int maxId = 1; if (items.Any()) { maxId = items.Max(t => t.Id) + 1; } items.AddRange(norgs.Select(t => new ExamOrgResultOutput() { Id = maxId++, ExamDataPublishId = publishId, ExamOrgId = t.Id, ExamOrg = t, })); return items; } }