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 Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; 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 { private readonly IRepository _dataInspectionRepository; private readonly IRepository _frontAuditConfigRepository; public InspectionService(IRepository dataInspectionRepository, IRepository frontAuditConfigRepository ) { this._dataInspectionRepository = dataInspectionRepository; this._frontAuditConfigRepository = frontAuditConfigRepository; } 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() //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() on data.SignId equals trialSign.Id into trialSigntemp from lefttrialSign in trialSigntemp.DefaultIfEmpty() join frontAuditConfig in _repository.GetQueryable().Where(x=>x.ConfigType=="M"&&x.Identification!=null) on new { data.Identification } equals new { frontAuditConfig.Identification, } into frontAuditConfigtemp from leftfrontAuditConfig in frontAuditConfigtemp.DefaultIfEmpty() join moduleTypec in _repository.GetQueryable() on new { ModuleType = leftfrontAuditConfig.ModuleTypeId } equals new { ModuleType = moduleTypec.Id.ToString() } into moduleTypectemp from leftmoduleTypec in moduleTypectemp.DefaultIfEmpty() join OptTypec in _repository.GetQueryable() on new { ModuleType = leftfrontAuditConfig.OptTypeId } equals new { ModuleType = OptTypec.Id.ToString() } into optTypetemp from leftOptType in optTypetemp.DefaultIfEmpty() select new GetDataInspectionOutDto() { CreateTime = data.CreateTime, CreateUserId = data.CreateUserId, ModuleType = leftmoduleTypec.Id.ToString(), 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 = data.SiteName, ExperimentName = data.TrialName, FirstName = leftsubject.FirstName, LastName = leftsubject.LastName, Id = data.Id, ParentJson = leftparent.JsonDetail, VisitName = data.SubjectVisitName, CreateUser = leftuser.UserName, UserFirstName = leftuser.FirstName, UserLastName = leftuser.LastName, //SubjectCode=leftsubject.Code, SubjectCode =data.SubjectCode, SubjectVisitName=data.SubjectVisitName, CreateUserName = data.CreateUserName, RoleName=data.RoleName, TrialName=data.TrialName, SiteCode=data.SiteCode, ResearchProgramNo=data.ResearchProgramNo, ObjectType=data.ObjectType, Description=leftfrontAuditConfig.Description, ModuleTypeName= leftmoduleTypec.ValueCN, SignText= lefttrialSign.SignText, Identification= leftfrontAuditConfig.Identification, OptType= leftOptType.Value, VisitNum=leftsubjectVisit.VisitNum, }; query = query.WhereIf(dto.TrialId != null, x => x.TrialId == dto.TrialId) .WhereIf(dto.SiteId != null, x => x.SiteId == dto.SiteId) .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.IsNullOrEmpty(), 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); } /// /// 通用逻辑封装 /// /// 方法参数 /// 添加稽查 /// 用户签名 /// 委托 /// 方法返回的结果 /// public async Task Enforcement(dynamic OptCommand, DataInspectionAddDTO AuditInfo, SignDTO SignInfo, dynamic fun, IResponseOutput? response=null) { if (response!=null&&response.IsSuccess == false) { return response; } 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; } if (AuditInfo.IsSign) { var signSuccess = await _repository.BatchUpdateAsync(t => t.Id == signId, u => new TrialSign() { IsCompleted = true }); } // 判断是否需要前面 await AddInspectionRecordAsync(AuditInfo, signId); 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 i = mapData.GetType().GetProperty(item).GetValue(mapData); if (i == null) { 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) { throw new BusinessValidationFailedException("password error"); } else if (user.Status == UserStateEnum.Disable) { throw new BusinessValidationFailedException("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; } /// /// 添加稽查记录 /// /// 添加对象 /// 签名Id /// public async Task AddInspectionRecordAsync(DataInspectionAddDTO addDto, Guid? signId=null) { if (addDto == new DataInspectionAddDTO()) { return ResponseOutput.Ok(); } await SetEnum(addDto); var add = _mapper.Map(addDto); await SetInspectionNameValue(add); Guid? parentId = null; parentId = (await _repository.GetQueryable().Where( x => x.TrialId == add.TrialId && x.SubjectVisitId == add.SubjectVisitId && x.SubjectId == add.SubjectId && x.SiteId == add.SiteId&&x.ChildrenType==add.ChildrenType && x.ObjectType==add.ObjectType&&x.VisitStageId==add.VisitStageId&&x.GeneralId==add.GeneralId).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); } /// /// 批量添加稽查记录 /// /// /// public async Task AddListInspectionRecordAsync(List datas) { //var trialIds= datas.Select(x=>x.TrialId).Distinct().ToList(); //var subjectVisitIds= datas.Select(x=>x.SubjectVisitId).Distinct().ToList(); //var subjectIds = datas.Select(x => x.SubjectId).Distinct().ToList(); //var siteIds = datas.Select(x => x.SiteId).Distinct().ToList(); //var childrenTypes= datas.Select(x => x.ChildrenType).Distinct().ToList(); //var objectTypes = datas.Select(x => x.ObjectType).Distinct().ToList(); foreach (var add in datas) { await SetInspectionNameValue(add); add.ParentId = (await _repository.GetQueryable().Where(x => x.TrialId == add.TrialId && x.SubjectVisitId == add.SubjectVisitId && x.SubjectId == add.SubjectId && x.SiteId == add.SiteId && x.ChildrenType == add.ChildrenType && x.ObjectType == add.ObjectType&&x.VisitStageId==add.VisitStageId&& x.GeneralId == add.GeneralId).OrderByDescending(x => x.CreateTime).FirstOrDefaultAsync())?.Id; add.CreateUserId = _userInfo.Id; add.IP = _userInfo.IP; var JsonData = JsonConvert.DeserializeObject>(add.JsonDetail); foreach (var item in JsonData.Keys) { if (JsonData[item].ToString().ToLower() == "true".ToLower()) { JsonData[item] = "是"; } else if (JsonData[item].ToString().ToLower() == "false".ToLower()) { JsonData[item] = "否"; } } add.JsonDetail= JsonConvert.SerializeObject(JsonData); } await _dataInspectionRepository.AddRangeAsync(datas); } /// /// 往json里面添加属性 /// /// json /// 字典 /// public string AddJsonItem(string json, Dictionary keyValues) { var JsonData = JsonConvert.DeserializeObject>(json); foreach (var item in keyValues) { if (JsonData.ContainsKey(item.Key)) { JsonData[item.Key] = item.Value; } else { JsonData.Add(item.Key, item.Value); } } return JsonConvert.SerializeObject(JsonData); } /// /// 处理枚举 /// /// public async Task SetEnum(DataInspectionAddDTO Data) { #region 枚举 try { var JsonData = JsonConvert.DeserializeObject>(Data.JsonDetail); foreach (var item in Data.EnumList) { if (!JsonData.ContainsKey(item.Key)) { continue; } var value = JsonData[item.Key]; if (value.GetType() == typeof(JArray)) { JArray arrays = (JArray)value; if (item.Type.ToLower() == "id".ToLower()) { List guids = new List(); arrays.ForEach(x => { guids.Add(Guid.Parse(x.ToString())); }); JsonData[item.Key] = string.Join(',', await _repository.GetQueryable().Where(x => guids.Contains(x.Id)).Select(x => x.ValueCN).ToListAsync()); } else if (item.Type.ToLower() == "ChildGroup".ToLower()) { List guids = new List(); arrays.ForEach(x => { guids.Add(x.ToString()); }); JsonData[item.Key] = string.Join(',', await _repository.GetQueryable().Where(x => x.Code == item.Code).GroupJoin( _repository.GetQueryable().Where(x => guids.Contains(x.ChildGroup)), a => a.Id, b => b.ParentId, (a, b) => new { parent = b }).SelectMany(a => a.parent, (m, n) => new { value = n.ValueCN }).Select(x => x.value).ToListAsync() ); } else { List guids =new List(); arrays.ForEach(x => { guids.Add(x.ToString()); }); JsonData[item.Key] = string.Join(',', await _repository.GetQueryable().Where(x => x.Code == item.Code).GroupJoin( _repository.GetQueryable().Where(x=>guids.Contains(x.Code)), a => a.Id, b => b.ParentId, (a, b) => new { parent = b }).SelectMany(a => a.parent, (m, n) => new { value = n.ValueCN }).Select(x => x.value).ToListAsync() ); } } else { if (item.Type.ToLower() == "id".ToLower()) { Guid guid = Guid.Parse(value.ToString()); JsonData[item.Key] = await _repository.GetQueryable().Where(x => guid == x.Id).Select(x => x.ValueCN).FirstOrDefaultAsync(); } else if (item.Type.ToLower() == "ChildGroup".ToLower()) { JsonData[item.Key] = await _repository.GetQueryable().Where(x => x.Code == item.Code).Join(_repository.GetQueryable().Where(x => x.ChildGroup == value.ToString()), a => a.Id, b => b.ParentId, (a, b) => new { value = b.ValueCN }).Select(x => x.value).FirstOrDefaultAsync(); } else { JsonData[item.Key] = await _repository.GetQueryable().Where(x=>x.Code==item.Code).Join(_repository.GetQueryable().Where(x=>x.Code== value.ToString()), a => a.Id, b => b.ParentId, (a, b) => new { value=b.ValueCN }).Select(x=>x.value).FirstOrDefaultAsync(); } } } IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); timeFormat.DateTimeFormat = "yyyy-MM-dd"; Data.JsonDetail = JsonConvert.SerializeObject(JsonData, Newtonsoft.Json.Formatting.Indented, timeFormat); } catch (Exception) { throw new BusinessValidationFailedException("Json 对象枚举异常"); } #endregion } /// /// 设置项目以及名称 /// /// /// public async Task SetInspectionNameValue(DataInspection Data) { #region 项目名称 var trialdata = await _repository.GetQueryable().FirstOrDefaultAsync(x => x.Id == Data.TrialId); Data.ResearchProgramNo = trialdata?.ResearchProgramNo; if (Data.TrialName.IsNullOrEmpty()) { Data.TrialName = trialdata?.ExperimentName; } #endregion #region 测试中心名称 Data.SiteCode = (await _repository.GetQueryable().FirstOrDefaultAsync(x => x.TrialId == Data.TrialId && x.SiteId == Data.SiteId))?.TrialSiteCode; var sitedata = await _repository.GetQueryable().FirstOrDefaultAsync(x => x.Id == Data.SiteId); if (Data.SiteName.IsNullOrEmpty()) { Data.SiteName = sitedata?.SiteName; } #endregion #region 受试者 if (Data.SubjectCode.IsNullOrEmpty()) { Data.SubjectCode = (await _repository.GetQueryable().FirstOrDefaultAsync(x => x.Id == Data.SubjectId))?.Code; } #endregion #region 访视 if (Data.SubjectVisitName.IsNullOrEmpty()) { Data.SubjectVisitName = (await _repository.GetQueryable().FirstOrDefaultAsync(x => x.Id == Data.SubjectVisitId))?.VisitName; } #endregion #region 创建者 if (Data.CreateUserName.IsNullOrEmpty() || Data.RoleName.IsNullOrEmpty()) { var userdata = await _repository.GetQueryable().Where(x => x.Id == Data.CreateUserId).GroupJoin(_repository.GetQueryable(), a => a.UserTypeId, b => b.Id, (a, b) => new { UserName = a.FirstName + a.LastName, Role = b }).SelectMany(a => a.Role, (m, n) => new { UserName = m.UserName, RoleName = n.UserTypeShortName }).FirstOrDefaultAsync(); if (userdata != null) { if (Data.CreateUserName.IsNullOrEmpty()) { Data.CreateUserName = userdata?.UserName; } if (Data.RoleName.IsNullOrEmpty()) { Data.RoleName = userdata?.RoleName; } } } #endregion #region 取操作类型 try { var from = await _frontAuditConfigRepository.FirstOrDefaultAsync(x => x.Identification == Data.Identification); Data.ObjectType = from.ObjectTypeId; Data.OptType = from.OptTypeId; Data.ChildrenType = from.ChildrenTypeId; Data.ModuleType = from.ModuleTypeId; } catch (Exception) { throw new BusinessValidationFailedException("操作标识异常"); } #endregion } } }