using IRaCIS.Application.Contracts; using IRaCIS.Application.Interfaces; using MassTransit; using Microsoft.AspNetCore.Mvc; namespace IRaCIS.Core.Application.Service { [ApiExplorerSettings(GroupName = "Reviewer")] public class TrialExperienceService(IRepository _trialExperienceRepository, IRepository _doctorRepository, IRepository _doctorSummarizeRepository, IRepository _trialExperienceCriteriaRepository, IRepository _attachmentRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ITrialExperienceService { /// /// 根据医生Id,获取临床试验经历 界面所有数据 获取其他相关经历 /// [HttpPost] public async Task GetTrialExperience(TrialExperienceModelIndto indto) { var trialExperience = new TrialExperienceModel(); var doctor = await _doctorRepository.Where(o => o.Id == indto.DoctorId) .ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync(); trialExperience.ClinicalTrialExperienceList = await GetTrialExperienceList(new GetTrialExperienceListInDto() { DoctorId = indto.DoctorId, TrialId = indto.TrialId, }); if (doctor != null) { var trialDoctor = new Doctor(); if (indto.TrialId != null) { trialDoctor = await GetTrialDoctorInfo(new GetTrialDoctorInfoInDto() { DoctorId = indto.DoctorId, TrialId = indto.TrialId.Value, }); } trialExperience.GCP = doctor.GCP; trialExperience.Id = doctor.Id; trialExperience.GCPTime = doctor.GCPTime; trialExperience.GCPAgencies = doctor.GCPAgencies; trialExperience.OtherClinicalExperience =(indto.TrialId != null? trialDoctor.OtherClinicalExperience: doctor.OtherClinicalExperience) ?? ""; trialExperience.OtherClinicalExperienceCN = (indto.TrialId != null ? trialDoctor.OtherClinicalExperienceCN : doctor.OtherClinicalExperienceCN) ?? ""; var attachment = await _attachmentRepository.FirstOrDefaultAsync(t => t.Id == doctor.GCPId); if (attachment != null) { trialExperience.ExpiryDateStr = attachment.ExpiryDate == null ? "" : attachment.ExpiryDate.Value.ToString("yyyy-MM-dd HH:mm"); trialExperience.Path = attachment.Path; trialExperience.GCPFullPath = attachment.Path + "?access_token=" + _userInfo.UserToken; trialExperience.Type = attachment.Type; trialExperience.FileName = attachment.FileName; trialExperience.GCPId = attachment.Id; } } return trialExperience; } /// /// 获取项目编辑的医生信息 /// /// /// public async Task GetTrialDoctorInfo(GetTrialDoctorInfoInDto inDto) { var doctorInfo = await _doctorRepository.Where(x => x.DoctorId == inDto.DoctorId && x.TrialId == inDto.TrialId).FirstOrDefaultAsync(); if (doctorInfo == null) { var systemInfoDcotor = await _doctorRepository.Where(x => x.Id == inDto.DoctorId).FirstNotNullAsync(); Doctor doctor = new Doctor() { DoctorId = inDto.DoctorId, TrialId = inDto.TrialId, OtherClinicalExperience = systemInfoDcotor.OtherClinicalExperience, OtherClinicalExperienceCN = systemInfoDcotor.OtherClinicalExperienceCN, }; ///没有就加 var summarizeCount = await _doctorSummarizeRepository.Where(x => x.DoctorId == inDto.DoctorId && x.TrialId == inDto.TrialId).CountAsync(); if (summarizeCount == 0) { var main = await _doctorSummarizeRepository.Where(x => x.DoctorId == inDto.DoctorId && x.IsMain && x.TrialId == null).ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync(); if (main != null) { main.Id=null; main.TrialId = inDto.TrialId; main.IsMain = false; var entity = await _doctorSummarizeRepository.InsertOrUpdateAsync(main, true); } } doctorInfo = await _doctorRepository.AddAsync(doctor, true); } return doctorInfo; } private async Task> GetTrialExperienceList(GetTrialExperienceListInDto inDto) { if (inDto.TrialId == null) { var doctorClinicalTrialExperienceList = await _trialExperienceRepository .Where(o => o.DoctorId == inDto.DoctorId) .Where(x => x.ExperienceDataType == ExperienceDataType.System || x.ExperienceDataType == ExperienceDataType.SystemAuto) .OrderBy(t => t.CreateTime) .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); return doctorClinicalTrialExperienceList; } else { // 当前项目没有则复制 if (!(await _trialExperienceRepository.AnyAsync(x => x.TrialId == inDto.TrialId &&x.ExperienceDataType== ExperienceDataType.Trial))) { var trialExperienceList = await _trialExperienceRepository.Where(o => o.DoctorId == inDto.DoctorId) .Where(x => x.ExperienceDataType == ExperienceDataType.System || x.ExperienceDataType == ExperienceDataType.SystemAuto).ToListAsync(); foreach (var item in trialExperienceList) { item.Trial = null; item.TrialId = inDto.TrialId; item.ExperienceDataType = ExperienceDataType.Trial; item.Id = NewId.NextGuid(); } await _trialExperienceRepository.AddRangeAsync(trialExperienceList); await _trialExperienceRepository.SaveChangesAsync(); } var doctorClinicalTrialExperienceList = await _trialExperienceRepository .Where(o => o.DoctorId == inDto.DoctorId) .Where(x => x.ExperienceDataType == ExperienceDataType.Trial|| x.ExperienceDataType == ExperienceDataType.TrialAuto) .Where(x => x.TrialId == inDto.TrialId.Value) .OrderBy(t => t.CreateTime) .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); return doctorClinicalTrialExperienceList; } } /// 添加或更新医生临床经验列表项 [HttpPost] public async Task AddOrUpdateTrialExperience(TrialExperienceCommand trialExperienceViewModel) { if (trialExperienceViewModel.TrialId == null) { trialExperienceViewModel.ExperienceDataType = ExperienceDataType.System; } else { trialExperienceViewModel.ExperienceDataType = ExperienceDataType.Trial; } if (trialExperienceViewModel.Id == Guid.Empty || trialExperienceViewModel.Id == null) { var trialExperience = _mapper.Map(trialExperienceViewModel); trialExperience = await _trialExperienceRepository.AddAsync(trialExperience); List criteriaList = new List(); trialExperienceViewModel.EvaluationCriteriaIdList.ForEach(t => criteriaList.Add(new TrialExperienceCriteria() { DoctorId = trialExperienceViewModel.DoctorId, //EvaluationCriteria = t.EvaluationCriteria, EvaluationCriteriaId = t, TrialExperienceId = trialExperience.Id })); await _trialExperienceCriteriaRepository.AddRangeAsync(criteriaList); var success = await _trialExperienceCriteriaRepository.SaveChangesAsync(); return ResponseOutput.Result(success, trialExperience.Id); } else { var needUpdate = trialExperienceViewModel; await _trialExperienceRepository.UpdateFromDTOAsync(trialExperienceViewModel); await _trialExperienceCriteriaRepository.BatchDeleteNoTrackingAsync(t => t.TrialExperienceId == needUpdate.Id); List criteriaList = new List(); trialExperienceViewModel.EvaluationCriteriaIdList.ForEach(t => criteriaList.Add(new TrialExperienceCriteria() { DoctorId = trialExperienceViewModel.DoctorId, EvaluationCriteriaId = t, TrialExperienceId = needUpdate.Id.Value })); await _trialExperienceCriteriaRepository.AddRangeAsync(criteriaList); var success = await _trialExperienceCriteriaRepository.SaveChangesAsync(); return ResponseOutput.Result(success, trialExperienceViewModel.Id); } } /// /// 删除临床经验 /// [HttpDelete, Route("{doctorId:guid}")] public async Task DeleteTrialExperience(Guid doctorId) { var success = await _trialExperienceRepository.BatchDeleteNoTrackingAsync(o => o.Id == doctorId); return ResponseOutput.Result(success); } /// /// 更新-GCP和其他临床经验 /// /// /// [HttpPost] public async Task UpdateGcpExperience(GCPExperienceCommand updateGCPExperienceParam) { //_attachmentRepository.Delete(t => t.DoctorId == updateGCPExperienceParam.Id && t.Type == StaticData.GCP); var successs = await _doctorRepository.BatchUpdateNoTrackingAsync(o => o.Id == updateGCPExperienceParam.Id, u => new Doctor() { GCP = updateGCPExperienceParam.GCP, GCPAgencies= updateGCPExperienceParam.GCPAgencies, GCPTime= updateGCPExperienceParam.GCPTime, GCPId = (updateGCPExperienceParam.GCP == 0 || updateGCPExperienceParam.GCPId == null) ? Guid.Empty : updateGCPExperienceParam.GCPId!.Value }); if (updateGCPExperienceParam.GCP == 0) { await _attachmentRepository.BatchDeleteNoTrackingAsync(a => a.DoctorId == updateGCPExperienceParam.Id && a.Type == "GCP"); } return ResponseOutput.Result(successs, updateGCPExperienceParam.GCPId.ToString()); } /// /// 更新其他技能经验 /// [HttpPost] public async Task UpdateOtherExperience(ClinicalExperienceCommand inDto) { if (inDto.TrialId != null) { var success = await _doctorRepository.BatchUpdateNoTrackingAsync(o => o.DoctorId == inDto.DoctorId&&o.TrialId==inDto.TrialId.Value, u => new Doctor() { OtherClinicalExperience = inDto.OtherClinicalExperience ?? string.Empty, OtherClinicalExperienceCN = inDto.OtherClinicalExperienceCN ?? string.Empty }); return ResponseOutput.Result(success); } else { var success = await _doctorRepository.BatchUpdateNoTrackingAsync(o => o.Id == inDto.DoctorId, u => new Doctor() { OtherClinicalExperience = inDto.OtherClinicalExperience ?? string.Empty, OtherClinicalExperienceCN = inDto.OtherClinicalExperienceCN ?? string.Empty }); return ResponseOutput.Result(success); } } } }