irc-netcore-api/IRaCIS.Core.Application/Service/Inspection/InspectionService.cs

245 lines
11 KiB
C#

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<PageOutput<GetDataInspectionOutDto>> GetInspectionData(GetDataInspectionDto dto)
{
//_repository.GetQueryable.GetQueryable < DataInspection >
#region 逻辑代码
var query = from data in _repository.GetQueryable<DataInspection>()
join trial in _repository.GetQueryable<Trial>() on data.TrialId equals trial.Id into trialtemp
from leftrial in trialtemp.DefaultIfEmpty()
join site in _repository.GetQueryable<Site>() on data.SiteId equals site.Id into sitetemp
from leftsite in sitetemp.DefaultIfEmpty()
join subject in _repository.GetQueryable<Subject>() on data.SubjectId equals subject.Id into subtemp
from leftsubject in subtemp.DefaultIfEmpty()
join subjectVisit in _repository.GetQueryable<SubjectVisit>() on data.SubjectVisitId equals subjectVisit.Id into subjectVisittemp
from leftsubjectVisit in subjectVisittemp.DefaultIfEmpty()
join parent in _repository.GetQueryable<DataInspection>() on data.ParentId equals parent.Id into parenttemp
from leftparent in parenttemp.DefaultIfEmpty()
join user in _repository.GetQueryable<User>() 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.IsNullOrEmpty(), 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<DataInspection>()
// join parent in _repository.GetQueryable<DataInspection>() on data.ParentId equals parent.Id into parenttemp
// from leftparent in parenttemp.DefaultIfEmpty()
// join subjectVisit in _repository.GetQueryable<SubjectVisit>() 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);
}
/// <summary>
/// 通用逻辑封装
/// </summary>
/// <param name="OptCommand">方法参数</param>
/// <param name="AuditInfo">添加稽查</param>
/// <param name="SignInfo">用户签名</param>
/// <param name="fun">委托</param>
/// <returns></returns>
public async Task<dynamic> 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;
}
/// <summary>
/// 映射 SiteId SubjectId SubjectVisitId TrialId 最开始没有 需要特殊处理
/// </summary>
/// <param name="data"></param>
/// <param name="mapData"></param>
public void MapData(dynamic data, dynamic mapData)
{
List<string> column = new List<string>() { "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;
}
}
}
/// <summary> 验证用户签名信息 </summary> ///
public async Task<IResponseOutput> VerifySignatureAsync(SignDTO signDTO)
{
var user = await _repository.FirstOrDefaultAsync<User>(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();
}
/// <summary> 添加签名记录 </summary> ///
public async Task<Guid> AddSignRecordAsync(SignDTO signDTO)
{
var add = await _repository.AddAsync(_mapper.Map<TrialSign>(signDTO));
var success = await _repository.SaveChangesAsync();
return add.Id;
}
/// <summary> 添加稽查记录( 有的会签名,有的不会签名) </summary> ///
public async Task<IResponseOutput> AddInspectionRecordAsync(DataInspectionAddDTO addDto, Guid? signId)
{
var add = _mapper.Map<DataInspection>(addDto);
Guid? parentId = null;
parentId = (await _repository.GetQueryable<DataInspection>().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);
}
}
}