CostCalculationItem/IRaCIS.Core.Application/Common/AttachmentService.cs

127 lines
4.5 KiB
C#

using AutoMapper;
using AutoMapper.QueryableExtensions;
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 System;
using System.Collections.Generic;
using System.Linq;
namespace IRaCIS.Application.Services
{
public class AttachmentService : IAttachmentService
{
private readonly IAttachmentRepository _doctorAttachmentRepository;
private readonly IMapper _mapper;
public AttachmentService(IAttachmentRepository doctorAttachmentRepository, IMapper mapper)
{
_doctorAttachmentRepository = doctorAttachmentRepository;
_mapper = mapper;
}
/// <summary>
/// 删除附件
/// </summary>
/// <param name="attachmentId"></param>
/// <returns></returns>
public IResponseOutput DeleteAttachment(Guid attachmentId)
{
var success = _doctorAttachmentRepository.Delete(a => a.Id == attachmentId);
return ResponseOutput.Result(success);
}
/// <summary>
/// 根据附件类型获取附件信息
/// </summary>
/// <param name="doctorId"></param>
/// <param name="type"></param>
/// <returns></returns>
public IEnumerable<AttachmentDTO> GetAttachmentByType(Guid doctorId, string type)
{
return _doctorAttachmentRepository.Find(a => a.DoctorId == doctorId && a.Type.Equals(type)).ProjectTo<AttachmentDTO>(_mapper.ConfigurationProvider);
}
public IEnumerable<AttachmentDTO> GetAttachmentByTypes(Guid doctorId, string[] types)
{
return _doctorAttachmentRepository.Find(a => a.DoctorId == doctorId && types.Contains(a.Type)).OrderBy(s => s.Type).ThenBy(m => m.CreateTime).ProjectTo<AttachmentDTO>(_mapper.ConfigurationProvider);
}
public IEnumerable<AttachmentDTO> GetAttachments(Guid doctorId)
{
return _doctorAttachmentRepository.Find(a => a.DoctorId == doctorId).OrderBy(s => s.Type).ThenBy(m => m.CreateTime).ProjectTo<AttachmentDTO>(_mapper.ConfigurationProvider);
}
public AttachmentDTO GetDetailById(Guid attachmentId)
{
var attachment = _doctorAttachmentRepository.FindSingleOrDefault(a => a.Id == attachmentId);
return _mapper.Map<AttachmentDTO>(attachment);
}
/// <summary>
/// 保存多个附件
/// </summary>
/// <param name="attachmentList"></param>
/// <returns></returns>
public IEnumerable<AttachmentDTO> SaveAttachmentRange(IEnumerable<AttachmentDTO> attachmentList)
{
var attachments = _mapper.Map<IEnumerable<Attachment>>(attachmentList);
foreach (var item in attachments)
{
if (item.Id != Guid.Empty)
{
_doctorAttachmentRepository.Delete(a => a.Id == item.Id);
}
}
var result = _doctorAttachmentRepository.AddRange(attachments);
_doctorAttachmentRepository.SaveChanges();
return _mapper.Map<IEnumerable<AttachmentDTO>>(result);
}
public string GetDoctorOfficialCV(Guid doctorId)
{
var result = _doctorAttachmentRepository.FindSingleOrDefault(a => a.DoctorId == doctorId &&
a.IsOfficial && a.Type.Equals("Resume"));
if (result != null)
{
return result.Path;
}
return string.Empty;
}
public IResponseOutput SetOfficial(Guid doctorId, Guid attachmentId)
{
var resumeList = _doctorAttachmentRepository.Find(t => t.DoctorId == doctorId && t.Type == "Resume");
foreach (var item in resumeList)
{
if (item.Id == attachmentId) item.IsOfficial = true;
else item.IsOfficial = false;
_doctorAttachmentRepository.Update(item);
}
//_doctorAttachmentRepository.Update(t => t.DoctorId == doctorId && t.Type == "Resume", entity => new Attachment()
//{
// IsOfficial = false
//});
//var success1 = _doctorAttachmentRepository.Update(t => t.Id == attachmentId, attach => new Attachment()
//{
// IsOfficial = true
//});
return ResponseOutput.Result(_doctorAttachmentRepository.SaveChanges());
}
}
}