330 lines
13 KiB
C#
330 lines
13 KiB
C#
using AutoMapper;
|
|
using IRaCIS.Application.ViewModels;
|
|
using IRaCIS.Core.Application.Contracts;
|
|
using IRaCIS.Core.Application.Contracts.DTO;
|
|
using IRaCIS.Core.Application.Contracts.RequestAndResponse;
|
|
using IRaCIS.Core.Domain.Interfaces;
|
|
using IRaCIS.Core.Domain.Models;
|
|
using IRaCIS.Core.Domain.Share;
|
|
using IRaCIS.Core.Domain.Share.AuthUser;
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace IRaCIS.Application.Services.QA
|
|
{
|
|
public class QARecordService : IQARecordService
|
|
{
|
|
private readonly IQARecordRepository _qaRecordRepository;
|
|
private readonly IQADictionaryRepository _qaDictionaryRepository;
|
|
private readonly IMapper _mapper;
|
|
private readonly IDictionaryRepository _dictionaryRepository;
|
|
private readonly IQADialogRepository _qaDialogRepository;
|
|
private readonly IUserRepository _userRepository;
|
|
|
|
private readonly IDicomStudyRepository _studyRepository;
|
|
private readonly ISubjectRepository _subjectRepository;
|
|
private readonly ISubjectVisitRepository _subjectVisitRepository;
|
|
private readonly IAcquisitionSpecificationRepository _acquisitionSpecificationRepository;
|
|
private readonly IUserInfo _userInfo;
|
|
private readonly IStudyStatusDetailRepository _studyStatusDetailRepository;
|
|
|
|
public QARecordService(IQARecordRepository qaRecordRepository,
|
|
IQADictionaryRepository qaDictionaryRepository, IMapper mapper,
|
|
IDictionaryRepository dictionaryRepository, IQADialogRepository qaDialogRepository,
|
|
IUserRepository userRepository,
|
|
IDicomStudyRepository studyRepository,
|
|
ISubjectRepository subjectRepository,
|
|
IAcquisitionSpecificationRepository acquisitionSpecificationRepository,
|
|
IStudyStatusDetailRepository studyStatusDetailRepository,
|
|
ISubjectVisitRepository subjectVisitRepository,
|
|
IUserInfo userInfo)
|
|
{
|
|
_qaRecordRepository = qaRecordRepository;
|
|
_qaDictionaryRepository = qaDictionaryRepository;
|
|
_mapper = mapper;
|
|
_dictionaryRepository = dictionaryRepository;
|
|
_qaDialogRepository = qaDialogRepository;
|
|
_userRepository = userRepository;
|
|
_studyRepository = studyRepository;
|
|
_subjectRepository = subjectRepository;
|
|
_acquisitionSpecificationRepository = acquisitionSpecificationRepository;
|
|
_studyStatusDetailRepository = studyStatusDetailRepository;
|
|
_subjectVisitRepository = subjectVisitRepository;
|
|
_userInfo = userInfo;
|
|
}
|
|
public IResponseOutput AddOrUpdateQARecord(QAToalCommand qaTotalCommand)
|
|
{
|
|
if (qaTotalCommand.QARecord.Id == null)
|
|
{
|
|
|
|
var qaRecord = _mapper.Map<QARecord>(qaTotalCommand.QARecord);
|
|
|
|
var qaDictionaryRecords = _mapper.Map<List<QADictionary>>(qaTotalCommand.QADictionaryList);
|
|
|
|
qaDictionaryRecords.ForEach(t => t.QARecordId = qaRecord.Id);
|
|
|
|
//_qaDictionaryRepository.AddRange(qaDictionaryRecords);
|
|
|
|
qaDictionaryRecords.ForEach(t =>
|
|
{
|
|
qaRecord.QaDictionaryList.Add(t);
|
|
t.TrialId = qaTotalCommand.TrialId;
|
|
t.SiteId = qaTotalCommand.SiteId;
|
|
});
|
|
|
|
_qaRecordRepository.Add(qaRecord);
|
|
|
|
var success = _qaRecordRepository.SaveChanges();
|
|
|
|
var study = _studyRepository.GetAll().FirstOrDefault(u => u.Id == qaTotalCommand.StudyId);
|
|
if (study.Status != (int)StudyStatus.QAing)
|
|
{
|
|
_studyStatusDetailRepository.Add(new StudyStatusDetail
|
|
{
|
|
Status = (int)StudyStatus.QAing,
|
|
StudyId = qaTotalCommand.StudyId,
|
|
Note = string.Empty,
|
|
OptUserName = _userInfo.RealName,
|
|
OptTime = DateTime.Now
|
|
});
|
|
_studyStatusDetailRepository.SaveChanges();
|
|
_studyRepository.Update(u => u.Id == qaTotalCommand.StudyId, t => new DicomStudy
|
|
{
|
|
Status = (int)StudyStatus.QAing
|
|
});
|
|
}
|
|
return ResponseOutput.Result(success, qaRecord.Id);
|
|
|
|
}
|
|
else
|
|
{
|
|
var qARecordId = qaTotalCommand.QARecord.Id;
|
|
var qaRecord = _qaRecordRepository.GetAll().FirstOrDefault(t => t.Id == qARecordId);
|
|
|
|
_mapper.Map(qaTotalCommand.QARecord, qaRecord);
|
|
|
|
|
|
//删除中间表数据
|
|
_qaDictionaryRepository.Delete(t => t.QARecordId == qaTotalCommand.QARecord.Id);
|
|
|
|
var qaDictionaryRecords = _mapper.Map<List<QADictionary>>(qaTotalCommand.QADictionaryList);
|
|
|
|
qaDictionaryRecords.ForEach(t =>
|
|
{
|
|
t.QARecordId = qaRecord.Id;
|
|
t.TrialId = qaTotalCommand.TrialId;
|
|
t.SiteId = qaTotalCommand.SiteId;
|
|
});
|
|
|
|
_qaDictionaryRepository.AddRange(qaDictionaryRecords);
|
|
//qaDictionaryRecords.ForEach(t => qaRecord.QaDictionaryList.Add(t));
|
|
|
|
_qaRecordRepository.Update(qaRecord);
|
|
|
|
var success = _qaRecordRepository.SaveChanges();
|
|
|
|
return ResponseOutput.Result(success);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public IResponseOutput DeleteQARecord(Guid qaQARecordId)
|
|
{
|
|
var success1 = _qaRecordRepository.Delete(t => t.Id == qaQARecordId);
|
|
|
|
var success2 = _qaDictionaryRepository.Delete(t => t.QARecordId == qaQARecordId);
|
|
|
|
return ResponseOutput.Result(success1 || success2);
|
|
}
|
|
|
|
public QADTO GetQARecordList(Guid studyId, Guid trialId)
|
|
{
|
|
var qaRecordsQuery = from qaRecord in _qaRecordRepository.GetAll().Where(t => t.StudyId == studyId)
|
|
join user in _userRepository.GetAll() on qaRecord.CreateUserId equals user.Id
|
|
select new QARecordWithUser
|
|
{
|
|
Id = qaRecord.Id,
|
|
CreateTime = qaRecord.CreateTime,
|
|
Note = qaRecord.Note,
|
|
DeadlineTime = qaRecord.DeadlineTime,
|
|
QATemplateId = qaRecord.QATemplateId,
|
|
StudyId = qaRecord.StudyId,
|
|
UserName = user.RealName
|
|
};
|
|
var qaRecords = qaRecordsQuery.ToList();
|
|
|
|
var qaRecordIds = qaRecords.Select(t => t.Id).ToList();
|
|
|
|
var qaDictionaryQuery =
|
|
from qadic in _qaDictionaryRepository.GetAll().Where(t => qaRecordIds.Contains(t.QARecordId))
|
|
join dictionary in _dictionaryRepository.GetAll() on qadic.DictionaryId equals dictionary.Id
|
|
select new QADicView()
|
|
{
|
|
QARecordId = qadic.QARecordId,
|
|
DictionaryId = qadic.DictionaryId,
|
|
Note = qadic.Note,
|
|
DictionaryValue = dictionary.Value
|
|
};
|
|
|
|
var qaReplyCountList = _qaDialogRepository.GetAll().Where(t => qaRecordIds.Contains(t.QARecordId))
|
|
.GroupBy(t => t.QARecordId).Select(g => new
|
|
{
|
|
QARecordId = g.Key,
|
|
ReplyCount = g.Count()
|
|
}).ToList();
|
|
|
|
var qaDictionaryList = qaDictionaryQuery.ToList();
|
|
|
|
var recordList = new List<QARecordDTO>();
|
|
|
|
qaRecords.ForEach(t =>
|
|
{
|
|
var count = qaReplyCountList.FirstOrDefault(u => u.QARecordId == t.Id)?.ReplyCount;
|
|
t.ReplyCount = count ?? 0;
|
|
|
|
recordList.Add(new QARecordDTO() { QARecord = t });
|
|
});
|
|
|
|
recordList.ForEach(t =>
|
|
{
|
|
var tt = qaDictionaryList.Where(u => u.QARecordId == t.QARecord.Id).ToList();
|
|
t.QADictionaryList.AddRange(tt);
|
|
});
|
|
|
|
var studySubjectInfo = from study in _studyRepository.Find(s => s.Id == studyId)
|
|
join subject in _subjectRepository.GetAll() on study.SubjectId equals subject.Id
|
|
join subjectVisit in _subjectVisitRepository.GetAll() on study.SubjectVisitId equals subjectVisit.Id
|
|
select new StudySubjectInfoDTO
|
|
{
|
|
PatientAge = study.PatientAge,
|
|
PatientName = study.PatientName,
|
|
PatientSex = study.PatientSex,
|
|
SubjectName = subject.Name,
|
|
SubjectCode = subject.Code,
|
|
SubjectAge = subject.Age,
|
|
SubjectSex = subject.Sex,
|
|
StudyStatus = study.Status,
|
|
Modalities = study.Modalities,
|
|
InstanceCount = study.InstanceCount,
|
|
SeriesCount = study.SeriesCount,
|
|
Comment = study.Comment,
|
|
QAComment = study.QAComment,
|
|
|
|
VisitNum = subjectVisit.VisitNum,
|
|
VisitName = subjectVisit.VisitName,
|
|
InPlan = subjectVisit.InPlan,
|
|
SVSTDTC = subjectVisit.SVSTDTC,
|
|
SVENDTC = subjectVisit.SVENDTC
|
|
|
|
};
|
|
var specification = _acquisitionSpecificationRepository.Find(u => u.TrialId == trialId && u.Type == "AcquisitionSpecification").FirstOrDefault();
|
|
return new QADTO
|
|
{
|
|
QARecordList = recordList,
|
|
StudySubjectInfo = studySubjectInfo.SingleOrDefault(),
|
|
Specification = _mapper.Map<AcquisitionSpecificationDTO>(specification)
|
|
};
|
|
|
|
}
|
|
|
|
|
|
public IResponseOutput AddQAReply(QADialogCommand qaDialogCommand)
|
|
{
|
|
var qaReply = _mapper.Map<QADialog>(qaDialogCommand);
|
|
_qaDialogRepository.Add(qaReply);
|
|
|
|
var success = _qaDialogRepository.SaveChanges();
|
|
|
|
return ResponseOutput.Result(success, qaReply.Id);
|
|
}
|
|
|
|
public IResponseOutput DeleteQAReply(Guid qaReplyId)
|
|
{
|
|
var success = _qaDialogRepository.Delete(t => t.Id == qaReplyId);
|
|
|
|
return ResponseOutput.Result(success);
|
|
|
|
}
|
|
|
|
public List<DialogDTO> GetQaRecordDialogList(Guid qaRecordId)
|
|
{
|
|
var query = from qaDialog in _qaDialogRepository.GetAll()
|
|
.Where(t => t.QARecordId == qaRecordId)
|
|
join user in _userRepository.GetAll() on qaDialog.CreateUserId equals user.Id
|
|
select new DialogNode
|
|
{
|
|
Id = qaDialog.Id,
|
|
TalkContent = qaDialog.TalkContent,
|
|
UserName = user.RealName,
|
|
UserId = user.Id,
|
|
ParentId = qaDialog.ParentId,
|
|
CreateTime = qaDialog.CreateTime
|
|
};
|
|
|
|
var qaDialogList = query.OrderBy(t => t.CreateTime).ToList();
|
|
|
|
var result = new List<DialogDTO>();
|
|
|
|
if (qaDialogList.Count > 0)
|
|
{
|
|
|
|
var treeHeads = qaDialogList.Where(t => t.ParentId == Guid.Empty).OrderBy(t => t.CreateTime).ToList();
|
|
|
|
treeHeads.ForEach(dialogHead =>
|
|
{
|
|
result.Add(new DialogDTO()
|
|
{
|
|
Id = dialogHead.Id,
|
|
CreateTime = dialogHead.CreateTime,
|
|
From = dialogHead.UserName,
|
|
UserId = dialogHead.UserId,
|
|
TalkContent = dialogHead.TalkContent,
|
|
To = "",
|
|
PreviousContent = ""
|
|
|
|
});
|
|
|
|
|
|
var list = GetDialogList(dialogHead, qaDialogList);
|
|
|
|
result.AddRange(list);
|
|
|
|
});
|
|
|
|
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
private List<DialogDTO> GetDialogList(DialogNode firstNode, List<DialogNode> qaDialogList)
|
|
{
|
|
var result = new List<DialogDTO>();
|
|
|
|
var chirdren = qaDialogList.Where(t => t.ParentId == firstNode.Id)
|
|
.OrderBy(t => t.CreateTime).ToList();
|
|
|
|
chirdren.ForEach(t =>
|
|
{
|
|
result.Add(new DialogDTO()
|
|
{
|
|
Id = t.Id,
|
|
From = t.UserName,
|
|
TalkContent = t.TalkContent,
|
|
UserId = t.UserId,
|
|
CreateTime = t.CreateTime,
|
|
To = firstNode.UserName,
|
|
PreviousContent = firstNode.TalkContent
|
|
});
|
|
|
|
result.AddRange(GetDialogList(t, qaDialogList));
|
|
});
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |