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;
}
///
/// 删除附件
///
///
///
public IResponseOutput DeleteAttachment(Guid attachmentId)
{
var success = _doctorAttachmentRepository.Delete(a => a.Id == attachmentId);
return ResponseOutput.Result(success);
}
///
/// 根据附件类型获取附件信息
///
///
///
///
public IEnumerable GetAttachmentByType(Guid doctorId, string type)
{
return _doctorAttachmentRepository.Find(a => a.DoctorId == doctorId && a.Type.Equals(type)).ProjectTo(_mapper.ConfigurationProvider);
}
public IEnumerable 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(_mapper.ConfigurationProvider);
}
public IEnumerable GetAttachments(Guid doctorId)
{
return _doctorAttachmentRepository.Find(a => a.DoctorId == doctorId).OrderBy(s => s.Type).ThenBy(m => m.CreateTime).ProjectTo(_mapper.ConfigurationProvider);
}
public AttachmentDTO GetDetailById(Guid attachmentId)
{
var attachment = _doctorAttachmentRepository.FindSingleOrDefault(a => a.Id == attachmentId);
return _mapper.Map(attachment);
}
///
/// 保存多个附件
///
///
///
public IEnumerable SaveAttachmentRange(IEnumerable attachmentList)
{
var attachments = _mapper.Map>(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>(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());
}
}
}