using IRaCIS.Application.Interfaces;
using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using System.Text;
namespace IRaCIS.Core.Application.Service
{
    [ApiExplorerSettings(GroupName = "Common")]
    public class FileService : BaseService
    {
        /// 
        /// New  医生首页 多选 获取多个医生信息+文件路径列表  医生压缩包名称  doctorCode + "_" + doctorName _(时间戳或者随机的Guid)
        /// 
        /// 
        [HttpPost]
        public async Task>> GetOfficialResume(GetDoctorPathCommand command,
            [FromServices] IRepository _attachmentrepository)
        {
            var list = await _attachmentrepository.Where(t => command.DoctorIdList.Contains(t.DoctorId) && command.Language == t.Language).GroupBy(t => new { Name = t.Doctor.FirstName + "_" + t.Doctor.LastName, ReviewerCode = t.Doctor.ReviewerCode, t.DoctorId })
                .Select(g => new DoctorDownloadInfo()
                {
                    Id = g.Key.DoctorId,
                    Name = g.Key.Name,
                    ReviewerCode = g.Key.ReviewerCode,
                    FileList = g.Select(t => new DownloadFileInfo() { FileName = t.FileName, Path = t.Path }).ToList()
                }).ToListAsync();
            return ResponseOutput.Ok(list);
        }
        /// 
        /// New  项目入组  勾选获取简历路径
        /// 
        /// 
        /// 
        /// 
        /// 
        [HttpPost]
        public async Task>> GetTrialDoctorOfficialResume(GetDoctorPathCommand command,
           [FromServices] IDoctorService _doctorService,
           [FromServices] IRepository _attachmentrepository)
        {
            var list = await _attachmentrepository.Where(t => command.DoctorIdList.Contains(t.DoctorId) && command.Language == t.Language && t.IsOfficial && t.Type.Equals("Resume")).GroupBy(t => new { Name = t.Doctor.FirstName + "_" + t.Doctor.LastName, ReviewerCode = t.Doctor.ReviewerCode, t.DoctorId })
                .Select(g => new DoctorDownloadInfo()
                {
                    Id = g.Key.DoctorId,
                    Name = g.Key.Name,
                    ReviewerCode = g.Key.ReviewerCode,
                    FileList = g.Select(t => new DownloadFileInfo() { FileName = t.FileName, Path = t.Path }).ToList()
                }).ToListAsync();
            return ResponseOutput.Ok(list);
        }
        /// 
        /// new  医生详情  勾选或者下载文件路径
        /// 
        /// 
        /// 
        /// 
        /// 
        [HttpPost]
        public async Task> GetDoctorAttachment(GetDoctoreAttachPathCommand command,
         [FromServices] IDoctorService _doctorService,
         [FromServices] IRepository _attachmentrepository)
        {
            var find = await _attachmentrepository.Where(t => command.DoctorId == t.DoctorId && command.AttachmentIdList.Contains(t.Id)).GroupBy(t => new { Name = t.Doctor.FirstName + "_" + t.Doctor.LastName, ReviewerCode = t.Doctor.ReviewerCode, t.DoctorId })
                .Select(g => new DoctorDownloadInfo()
                {
                    Id = g.Key.DoctorId,
                    Name = g.Key.Name,
                    ReviewerCode = g.Key.ReviewerCode,
                    FileList = g.Select(t => new DownloadFileInfo() { FileName = t.FileName, Path = t.Path }).ToList()
                }).FirstOrDefaultAsync();
            return ResponseOutput.Ok(find);
        }
    }
    #region DTO
    public class DoctorDownloadInfo
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string ReviewerCode { get; set; }
        public List FileList { get; set; }
    }
    public class DownloadFileInfo
    {
        public string FileName { get; set; }
        public string Path { get; set; }
    }
    public class GetDoctorPathCommand
    {
        public int Language { get; set; }
        public List DoctorIdList { get; set; }
    }
    public class GetDoctoreAttachPathCommand
    {
        public Guid DoctorId { get; set; }
        public List AttachmentIdList { get; set; }
    }
    #endregion
}