using Castle.Core.Internal; using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Application.Service.Inspection.DTO; using IRaCIS.Core.Application.Service.Inspection.Interface; using IRaCIS.Core.Domain.Share; using Panda.DynamicWebApi.Attributes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IRaCIS.Core.Application.Service.Inspection { [NonDynamicWebApi] public class InspectionService : BaseService, IInspectionService { public InspectionService() { } public async Task> GetInspectionData(GetDataInspectionDto dto) { //_repository.GetQueryable.GetQueryable < DataInspection > #region 逻辑代码 var query = from data in _repository.GetQueryable() join trial in _repository.GetQueryable() on data.TrialId equals trial.Id into trialtemp from leftrial in trialtemp.DefaultIfEmpty() join site in _repository.GetQueryable() on data.SiteId equals site.Id into sitetemp from leftsite in sitetemp.DefaultIfEmpty() join subject in _repository.GetQueryable() on data.SubjectId equals subject.Id into subtemp from leftsubject in subtemp.DefaultIfEmpty() join subjectVisit in _repository.GetQueryable() on data.SubjectVisitId equals subjectVisit.Id into subjectVisittemp from leftsubjectVisit in subjectVisittemp.DefaultIfEmpty() join parent in _repository.GetQueryable() on data.ParentId equals parent.Id into parenttemp from leftparent in parenttemp.DefaultIfEmpty() join user in _repository.GetQueryable() on data.CreateUserId equals user.Id into usertemp from leftuser in usertemp.DefaultIfEmpty() select new GetDataInspectionOutDto() { CreateTime = data.CreateTime, CreateUserId = data.CreateUserId, ModuleType = data.ModuleType, BlindName = data.BlindName, TrialId = data.TrialId, SiteId = data.SiteId, SubjectId = data.SubjectId, SubjectVisitId = data.SubjectVisitId, OptType = data.OptType, IP = data.IP, Reason = data.Reason, IsSign = data.IsSign, SignId = data.SignId, ParentId = data.ParentId, ChildrenType = data.ChildrenType, JsonDetail = data.JsonDetail, SiteName = leftsite.SiteName, ExperimentName = leftrial.ExperimentName, FirstName = leftsubject.FirstName, LastName = leftsubject.LastName, Id = data.Id, ParentJson = leftparent.JsonDetail, VisitName = leftsubjectVisit.VisitName, CreateUser = leftuser.UserName, UserFirstName = leftuser.FirstName, UserLastName = leftuser.LastName, }; query = query.WhereIf(!dto.BlindName.IsNullOrEmpty(), x => x.BlindName == dto.BlindName) .WhereIf(dto.IsSign != null, x => x.IsSign == dto.IsSign) .WhereIf(dto.ChildrenType != null, x => x.ChildrenType == dto.ChildrenType) .WhereIf(!dto.ModuleType.IsNullOrEmpty(), x => x.ModuleType == dto.ModuleType) .WhereIf(!dto.OptType.IsNullOrEmpty(), x => x.OptType == dto.OptType) .WhereIf(!dto.Reason.IsNullOrEmpty(), x => x.Reason == dto.Reason) .WhereIf(dto.TrialId != null, x => x.TrialId == dto.TrialId) .WhereIf(dto.SubjectId != null, x => x.SubjectId == dto.SubjectId) .WhereIf(dto.SubjectVisitId != null, x => x.SubjectVisitId == dto.SubjectVisitId) .WhereIf(dto.SiteId != null, x => x.SiteId == dto.SiteId); #endregion #region 测试 //var query = from data in _repository.GetQueryable() // join parent in _repository.GetQueryable() on data.ParentId equals parent.Id into parenttemp // from leftparent in parenttemp.DefaultIfEmpty() // join subjectVisit in _repository.GetQueryable() on data.SubjectVisitId equals subjectVisit.Id into subjectVisittemp // from leftsubjectVisit in subjectVisittemp.DefaultIfEmpty() // select new GetDataInspectionOutDto() // { // CreateTime = data.CreateTime, // CreateUserId = data.CreateUserId, // ModuleType = data.ModuleType, // BlindName = data.BlindName, // TrialId = data.TrialId, // SiteId = data.SiteId, // SubjectId = data.SubjectId, // SubjectVisitId = data.SubjectVisitId, // OptType = data.OptType, // IP = data.IP, // Reason = data.Reason, // IsSign = data.IsSign, // SignId = data.SignId, // ParentId = data.ParentId, // ChildrenType = data.ChildrenType, // JsonDetail = data.JsonDetail, // VisitName = leftsubjectVisit.VisitName, // Id = data.Id, // ParentJson = leftparent.JsonDetail, // }; #endregion dto.Asc = false; return await query.ToPagedListAsync(dto.PageIndex, dto.PageSize, "CreateTime", dto.Asc); } /// /// 通用逻辑封装 /// /// 方法参数 /// 添加稽查 /// 用户签名 /// 委托 /// public async Task Enforcement(dynamic OptCommand, DataInspectionAddDTO AuditInfo, SignDTO SignInfo, dynamic fun, IResponseOutput response=null) { Guid? signId = null; MapData(OptCommand, AuditInfo); if (AuditInfo.IsSign) { // 验证用户签名信息 var verifyResult = await VerifySignatureAsync(SignInfo); signId = await AddSignRecordAsync(SignInfo); if (verifyResult.IsSuccess == false) { return verifyResult; } await AddSignRecordAsync(SignInfo); } IResponseOutput bResult; if (response != null) { bResult = response; } else { bResult = await fun(OptCommand); } // 用户 签名某个文档 if (bResult.IsSuccess == false) { return bResult; } // 判断是否需要前面 await AddInspectionRecordAsync(AuditInfo, signId); if (bResult.IsSuccess == false) { return bResult; } return bResult; } /// /// 映射 SiteId SubjectId SubjectVisitId TrialId 最开始没有 需要特殊处理 /// /// /// public void MapData(dynamic data, dynamic mapData) { List column = new List() { "TrialId", "SiteId", "SubjectId", "SubjectVisitId" }; foreach (var item in column) { try { var value = data.GetType().GetProperty(item).GetValue(data); mapData.GetType().GetProperty(item).SetValue(mapData, value); } catch (Exception) { continue; } } } /// 验证用户签名信息 /// public async Task VerifySignatureAsync(SignDTO signDTO) { var user = await _repository.FirstOrDefaultAsync(u => u.UserName == signDTO.UserName && u.Password == signDTO.PassWord); if (user == null) { return ResponseOutput.NotOk("password error"); } else if (user.Status == UserStateEnum.Disable) { return ResponseOutput.NotOk("The user has been disabled!"); } return ResponseOutput.Ok(); } /// 添加签名记录 /// public async Task AddSignRecordAsync(SignDTO signDTO) { var add = await _repository.AddAsync(_mapper.Map(signDTO)); var success = await _repository.SaveChangesAsync(); return add.Id; } /// 添加稽查记录( 有的会签名,有的不会签名) /// public async Task AddInspectionRecordAsync(DataInspectionAddDTO addDto, Guid? signId) { var add = _mapper.Map(addDto); Guid? parentId = null; parentId = (await _repository.GetQueryable().Where(x => x.TrialId == add.TrialId && x.SubjectVisitId == add.SubjectVisitId && x.SubjectId == add.SubjectId && x.SiteId == x.SiteId).OrderByDescending(x => x.CreateTime).FirstOrDefaultAsync())?.Id; add.ParentId = parentId; add.CreateTime = DateTime.Now; add.CreateUserId = _userInfo.Id; add.SignId = signId; add.IP = _userInfo.IP; await _repository.AddAsync(add); var success = await _repository.SaveChangesAsync(); return ResponseOutput.Ok(success); } } }