173 lines
7.7 KiB
C#
173 lines
7.7 KiB
C#
using AutoMapper;
|
||
using AutoMapper.QueryableExtensions;
|
||
using IRaCIS.Application.ExpressionExtend;
|
||
using IRaCIS.Application.Interfaces;
|
||
using IRaCIS.Application.ViewModels;
|
||
using IRaCIS.Core.Application.Contracts.RequestAndResponse;
|
||
using IRaCIS.Core.Domain.Interfaces;
|
||
using IRaCIS.Core.Domain.Models;
|
||
using IRaCIS.Core.Domain.Share;
|
||
using IRaCIS.Infra.Data.ExpressionExtend;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
|
||
namespace IRaCIS.Application.Services
|
||
{
|
||
public class SiteService : ISiteService
|
||
{
|
||
private readonly ISiteRepository _siteRepository;
|
||
private readonly IHospitalRepository _hospitalRepository;
|
||
private readonly IMapper _mapper;
|
||
private readonly IUserTrialRepository _userTrialRepository;
|
||
private readonly IUserRepository _userRepository;
|
||
|
||
public SiteService(ISiteRepository researchCenterRepository,
|
||
IHospitalRepository hospitalRepository, IMapper mapper,
|
||
IUserTrialRepository userTrialRepository,
|
||
IUserRepository userRepository)
|
||
{
|
||
_siteRepository = researchCenterRepository;
|
||
_hospitalRepository = hospitalRepository;
|
||
_mapper = mapper;
|
||
_userTrialRepository = userTrialRepository;
|
||
_userRepository = userRepository;
|
||
}
|
||
|
||
/// <summary> 分页获取研究中心列表 </summary>
|
||
public PageOutput<SiteSelectDTO> GetSiteList(SiteQueryParam searchModel)
|
||
{
|
||
Expression<Func<Site, bool>> researchCenterLambda = x => true;
|
||
|
||
if (!string.IsNullOrWhiteSpace(searchModel.SiteName.Trim()))
|
||
{
|
||
researchCenterLambda = researchCenterLambda.And(t => t.SiteName.Contains(searchModel.SiteName.Trim()));
|
||
}
|
||
|
||
IQueryable<SiteSelectDTO> siteQueryable = default;
|
||
if (searchModel.UserId == Guid.Empty) //查询所有Site
|
||
{
|
||
siteQueryable = from site in _siteRepository.GetAll().Where(researchCenterLambda)
|
||
join hospital in _hospitalRepository.GetAll() on site.HospitalId equals hospital.Id into h
|
||
from centerHospital in h.DefaultIfEmpty()
|
||
select new SiteSelectDTO()
|
||
{
|
||
Id = site.Id,
|
||
SiteName = site.SiteName,
|
||
City = site.City,
|
||
Country = site.Country,
|
||
SiteCode = site.SiteCode,
|
||
HospitalId = site.HospitalId,
|
||
DirectorName = site.DirectorName,
|
||
DirectorPhone = site.DirectorPhone,
|
||
ContactName = site.ContactName,
|
||
ContactPhone = site.ContactPhone,
|
||
HospitalName = centerHospital.HospitalName
|
||
};
|
||
|
||
//researchCenterQueryable = _researchCenterRepository.GetAll().Where(researchCenterLambda).ProjectTo<ResearchCenterSelectViewModel>();
|
||
}
|
||
else //查询该用户没有是否选中改研究中心 )
|
||
{
|
||
siteQueryable =
|
||
from site in _siteRepository.GetAll().Where(researchCenterLambda)
|
||
join userResearchCenter in _userTrialRepository
|
||
.Find(t => t.UserId == searchModel.UserId) on site.Id equals userResearchCenter
|
||
.SiteId into t
|
||
from userResearch in t.DefaultIfEmpty()
|
||
join hospital in _hospitalRepository.GetAll() on site.HospitalId equals hospital.Id into h
|
||
from centerHospital in h.DefaultIfEmpty()
|
||
select new SiteSelectDTO()
|
||
{
|
||
Id = site.Id,
|
||
SiteName = site.SiteName,
|
||
SiteCode = site.SiteCode,
|
||
IsSelect = userResearch == null ? false : true,
|
||
HospitalId = site.HospitalId,
|
||
HospitalName = centerHospital.HospitalName
|
||
};
|
||
}
|
||
var count = siteQueryable.Count();
|
||
|
||
//处理排序字段
|
||
var propName = string.IsNullOrWhiteSpace(searchModel.SortField) ? "SiteName" : searchModel.SortField;
|
||
//处理升序和降序
|
||
siteQueryable = searchModel.Asc
|
||
? siteQueryable.OrderBy(propName)
|
||
: siteQueryable.OrderByDescending(propName);
|
||
|
||
//分页
|
||
siteQueryable = siteQueryable
|
||
.Skip((searchModel.PageIndex - 1) * searchModel.PageSize)
|
||
.Take(searchModel.PageSize);
|
||
var researchCenterList = siteQueryable.ToList();
|
||
return new PageOutput<SiteSelectDTO>(searchModel.PageIndex, searchModel.PageSize, count, researchCenterList);
|
||
}
|
||
|
||
public IEnumerable<SiteSelectionDTO> GetSiteList()
|
||
{
|
||
return _siteRepository.GetAll().ProjectTo<SiteSelectionDTO>(_mapper.ConfigurationProvider);
|
||
}
|
||
|
||
|
||
/// <summary> 添加研究中心 </summary>
|
||
public IResponseOutput AddOrUpdateSite(SiteCommand site, Guid userId)
|
||
{
|
||
if (site.Id == Guid.Empty || site.Id == null)
|
||
{
|
||
if (_siteRepository.GetAll().FirstOrDefault(t => t.SiteName == site.SiteName) != null)
|
||
{
|
||
return ResponseOutput.NotOk("Please specify another center name");
|
||
}
|
||
var temp = _mapper.Map<Site>(site);
|
||
|
||
var saveMode = _siteRepository.Add(temp);
|
||
var success = _siteRepository.SaveChanges();
|
||
|
||
return ResponseOutput.Result(success, saveMode.Id.ToString());
|
||
}
|
||
else
|
||
{
|
||
var updateModel = site;
|
||
bool exist = _siteRepository.IsExist(u => u.SiteName == updateModel.SiteName && u.Id != updateModel.Id);
|
||
if (exist)
|
||
{
|
||
return ResponseOutput.NotOk("已存在同名的中心名称");
|
||
}
|
||
|
||
var success = _siteRepository.Update(u => u.Id == updateModel.Id, s => new Site
|
||
{
|
||
SiteCode = updateModel.SiteCode,
|
||
SiteName = updateModel.SiteName,
|
||
City = updateModel.City,
|
||
Country = updateModel.Country,
|
||
HospitalId = updateModel.HospitalId,
|
||
DirectorName = updateModel.DirectorName,
|
||
DirectorPhone = updateModel.DirectorPhone,
|
||
ContactName = updateModel.ContactName,
|
||
ContactPhone = updateModel.ContactPhone,
|
||
UpdateUserId = userId,
|
||
UpdateTime = DateTime.Now
|
||
});
|
||
|
||
return ResponseOutput.Result(success, success ? string.Empty : StaticData.UpdateFailed);
|
||
}
|
||
}
|
||
|
||
/// <summary> 删除研究中心 </summary>
|
||
public IResponseOutput DeleteSite(Guid siteId)
|
||
{
|
||
if (_userRepository.Find().Any(t => t.OrganizationId == siteId))
|
||
{
|
||
return ResponseOutput.NotOk("该Site下存在用户,暂时无法删除。");
|
||
}
|
||
if (_userTrialRepository.Find().Any(t => t.SiteId == siteId))
|
||
{
|
||
return ResponseOutput.NotOk("有项目在该Site下,暂时无法删除。");
|
||
}
|
||
var success = _siteRepository.Delete(t => t.Id == siteId);
|
||
return ResponseOutput.Result(success);
|
||
}
|
||
}
|
||
} |