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 IRaCIS.Core.Infrastructure; using Panda.DynamicWebApi.Attributes; namespace IRaCIS.Core.Application.Service.Inspection { [NonDynamicWebApi] public class InspectionService : BaseService, IInspectionService { private readonly IRepository _dataInspectionRepository; private readonly IRepository _frontAuditConfigRepository; public InspectionService(IRepository dataInspectionRepository, IRepository frontAuditConfigRepository ) { this._dataInspectionRepository = dataInspectionRepository; this._frontAuditConfigRepository = frontAuditConfigRepository; } public async Task> GetInspectionList(GetDataInspectionDto dto) { //_repository.GetQueryable.GetQueryable < DataInspection > var trialData= await _repository.GetQueryable().Where(x=>x.Id== dto.TrialId).AsNoTracking().FirstOrDefaultAsync(); trialData.TrialFinishTime = trialData.TrialFinishTime == null ? DateTime.Now : trialData.TrialFinishTime; #region 逻辑代码 var query = from data in _repository.GetQueryable().Where(x => (x.TrialId == dto.TrialId)||(x.TrialId==null&&x.CreateTime> trialData.CreateTime&&x.CreateTime < trialData.TrialFinishTime)) join trial in _repository.GetQueryable().IgnoreQueryFilters() on data.TrialId equals trial.Id into trialtemp from leftrial in trialtemp.DefaultIfEmpty() join site in _repository.GetQueryable().IgnoreQueryFilters() on data.SiteId equals site.Id into sitetemp from leftsite in sitetemp.DefaultIfEmpty() join subject in _repository.GetQueryable().IgnoreQueryFilters() on data.SubjectId equals subject.Id into subtemp from leftsubject in subtemp.DefaultIfEmpty() join subjectVisit in _repository.GetQueryable().IgnoreQueryFilters() 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().IgnoreQueryFilters() on data.CreateUserId equals user.Id into usertemp from leftuser in usertemp.DefaultIfEmpty() join usertype in _repository.GetQueryable().IgnoreQueryFilters() on leftuser.UserTypeId equals usertype.Id into usertypetemp from leftusertype in usertypetemp.DefaultIfEmpty() //join moduleTyped in _repository.GetQueryable().Where(x => x.Code == "ModuleType") on 1 equals 1 //join moduleTypec in _repository.GetQueryable() on new { ParentId = moduleTyped.Id, ModuleType = data.ModuleType } equals new { ParentId = moduleTypec.ParentId.Value, ModuleType = moduleTypec.Value } into moduleTypectemp //join childrenTyped in _repository.GetQueryable().Where(x => x.Code == "ChildrenType") on 1 equals 1 //join childrenTypec in _repository.GetQueryable() on new { ParentId = childrenTyped.Id, ModuleType = data.ChildrenType } equals new { ParentId = childrenTypec.ParentId.Value, ModuleType = childrenTypec.Value } into childrenTypectemp //from leftchildrenTypec in childrenTypectemp.DefaultIfEmpty() //join ObjectTyped in _repository.GetQueryable().Where(x => x.Code == "ObjectType") on 1 equals 1 //join ObjectTypec in _repository.GetQueryable() on new { ParentId = ObjectTyped.Id, ModuleType = data.ObjectType } equals new { ParentId = ObjectTypec.ParentId.Value, ModuleType = ObjectTypec.Value } into objectTypetemp //from leftObjectType in objectTypetemp.DefaultIfEmpty() //join OptTyped in _repository.GetQueryable().Where(x => x.Code == "OptType") on 1 equals 1 //join OptTypec in _repository.GetQueryable() on new { ParentId = OptTyped.Id, ModuleType = data.OptType } equals new { ParentId = OptTypec.ParentId.Value, ModuleType = OptTypec.Value } into optTypetemp //from leftOptType in optTypetemp.DefaultIfEmpty() join trialSign in _repository.GetQueryable().IgnoreQueryFilters() on data.SignId equals trialSign.Id into trialSigntemp from lefttrialSign in trialSigntemp.DefaultIfEmpty() join leftfrontAuditConfig in _repository.GetQueryable().Where(x=>x.ConfigType=="M"&&x.Identification!=null&&x.IsEnable==true) on data.Identification.ToLower() equals leftfrontAuditConfig.Identification.ToLower() join moduleTypec in _repository.GetQueryable() on new { ModuleType = leftfrontAuditConfig.ModuleTypeId.Value } equals new { ModuleType = moduleTypec.Id } into moduleTypectemp from leftmoduleTypec in moduleTypectemp.DefaultIfEmpty() join OptTypec in _repository.GetQueryable() on new { ModuleType = leftfrontAuditConfig.OptTypeId.Value } equals new { ModuleType = OptTypec.Id } into optTypetemp from leftOptType in optTypetemp.DefaultIfEmpty() select new GetDataInspectionOutDto() { CreateTime = data.CreateTime, CreateUserId = data.CreateUserId, ModuleType = leftmoduleTypec.Id, BlindName = leftsubjectVisit.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 = data.SiteName, //ExperimentName = data.TrialName, FirstName = leftsubject.FirstName, LastName = leftsubject.LastName, Id = data.Id, ParentJson = leftparent.JsonDetail, VisitName = leftsubjectVisit.VisitName, CreateUser = leftuser.UserName, UserFirstName = leftuser.FirstName, UserLastName = leftuser.LastName, //SubjectCode=leftsubject.Code, SubjectCode =leftsubject.Code, SubjectVisitName=leftsubjectVisit.VisitName, CreateUserName = leftuser.FirstName + leftuser.LastName, RoleName= leftusertype.UserTypeShortName, TrialName= leftrial.ExperimentName, SiteCode=leftsite.SiteCode, ResearchProgramNo= leftrial.ResearchProgramNo, ObjectType=data.ObjectType, Description=leftfrontAuditConfig.Description, ModuleTypeName= leftmoduleTypec.ValueCN, SignText= lefttrialSign.SignText, Identification= leftfrontAuditConfig.Identification, FrontAuditConfigId= leftfrontAuditConfig.Id, ParentIdentification = leftparent.Identification, OptType = leftOptType.Id, VisitNum=leftsubjectVisit.VisitNum, //IsFrontAdd=data.IsFrontAdd, BatchId=data.BatchId, OptTypeName = leftOptType.Value, }; query = query.WhereIf(dto.SiteId != null, x => x.SiteId == dto.SiteId) .WhereIf(dto.BatchId != null, x => x.BatchId == dto.BatchId) .WhereIf(!dto.SubjectInfo.IsNullOrEmpty(), x => x.SubjectCode.Contains(dto.SubjectInfo)) .WhereIf(dto.VisitPlanInfo != null, x => x.VisitNum == dto.VisitPlanInfo) .WhereIf(dto.StartTime != null, x => x.CreateTime >= dto.StartTime) .WhereIf(dto.EndTime != null, x => x.CreateTime <= dto.EndTime) .WhereIf(dto.ModuleType!=null, x => x.ModuleType == dto.ModuleType) .WhereIf(!dto.Description.IsNullOrEmpty(), x => x.Description == dto.Description) .WhereIf(!dto.OpByUserName.IsNullOrEmpty(), x => x.CreateUserName.Contains(dto.OpByUserName)) //.WhereIf(!dto.SubjectInfo.IsNullOrEmpty(), x => x.SubjectCode.Contains(dto.SubjectInfo)) .WhereIf(dto.IsSign != null, x => x.IsSign == dto.IsSign); #endregion dto.Asc = false; return await query.ToPagedListAsync(dto.PageIndex, dto.PageSize,"CreateTime", dto.Asc); } /// /// 传入参数记录ID /// /// /// public async Task RecordSing(SignDTO SignInfo) { if (SignInfo != null) { var verifyResult = await VerifySignatureAsync(SignInfo); var signId = await AddSignRecordAsync(SignInfo); _userInfo.SignId = signId; return signId; } else { return default(Guid); } } /// /// 完成签名 /// /// /// /// public async Task CompletedSign(Guid signId, IResponseOutput response) { if (response.IsSuccess) { await _repository.BatchUpdateAsync(t => t.Id == signId, u => new TrialSign() { IsCompleted = true }); } } /// 验证用户签名信息 /// public async Task VerifySignatureAsync(SignDTO signDTO) { var user = await _repository.FirstOrDefaultAsync(u => u.UserName == signDTO.UserName && u.Password == signDTO.PassWord); if (user == null) { throw new BusinessValidationFailedException("用户名或密码错误"); } else if (user.Status == UserStateEnum.Disable) { throw new BusinessValidationFailedException("当前用户已被禁用。"); } 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; } } }