//using IRaCIS.Application.Interfaces;
//using IRaCIS.Application.Contracts;
//using IRaCIS.Core.Infra.EFCore;
//using IRaCIS.Core.Infrastructure;

//using Microsoft.AspNetCore.Http;
//using Microsoft.AspNetCore.Mvc;
//using Panda.DynamicWebApi.Attributes;

//namespace IRaCIS.Application.Services
//{
//    /// <summary>
//    /// 日志、项目审计日志
//    /// </summary>
//    [ApiExplorerSettings(GroupName = "Common")]
//    public class LogService : BaseService, ILogService
//    {
//        private readonly IRepository<SystemLog> _systemLogRepository;
//        private readonly IHttpContextAccessor _context;
//        private readonly IRepository<TrialAudit> _trialAuditRepository;
//        private readonly IRepository<Subject> _subjectRepository;
//        private readonly IRepository<Trial> _trialRepository;

//        public LogService(IRepository<SystemLog> systemLogRepository, IHttpContextAccessor context, IRepository<TrialAudit> trialAuditRepository,
//            IRepository<Subject> subjectRepository, IRepository<Trial> trialRepository)
//        {
//            _systemLogRepository = systemLogRepository;
//            _context = context;
//            _trialAuditRepository = trialAuditRepository;
//            _subjectRepository = subjectRepository;
//            _trialRepository = trialRepository;
//        }

//        [HttpPost]
//        public PageOutput<AuditDTO> GetAuditList(AuditQueryDTO param)
//        {                            
//            var subjectInfo = param.SubjectInfo == null ? string.Empty : param.SubjectInfo.Trim();

//            var query = _trialAuditRepository.Where(x => x.TrialId == param.TrialId)
//                 .WhereIf(param.AuditType != null, t => t.AuditType == param.AuditType)
//                 .WhereIf(param.OptUserId != null, t => t.OptUserId == param.OptUserId)
//                 .WhereIf(param.SubjectId != null, t => t.SubjectId == param.SubjectId)
//                 .WhereIf(!string.IsNullOrEmpty(subjectInfo), t => t.Subject.Code.Contains(subjectInfo) || (t.Subject.LastName + " / " + t.Subject.FirstName).Contains(subjectInfo))
//                 .WhereIf(param.StudyId != null, t => t.StudyId == param.StudyId)
//                 .WhereIf(param.StartDate != null, t => t.OptTime >= param.StartDate)
//                 .WhereIf(param.EndDate != null, t => t.OptTime <= param.EndDate)
//                 .ProjectTo<AuditDTO>(_mapper.ConfigurationProvider);


//            return query.ToPagedList(param.PageIndex, param.PageSize, string.IsNullOrWhiteSpace(param.SortField) ? "OptTime" : param.SortField, param.Asc);

//        }

//        /// <summary> 查询系统日志信息   </summary>
//        [HttpPost]
//        public PageOutput<SystemLogDTO> GetLogList(QueryLogQueryDTO param)
//        {
         
//            var LogCategory = param.LogCategory == null ? string.Empty : param.LogCategory.Trim();
//            var keyword = param.Keyword == null ? string.Empty : param.Keyword.Trim();
//            var logQueryable = _systemLogRepository
//               .WhereIf(param.BeginTime!=null,t=>t.RequestTime>= param.BeginTime)
//               .WhereIf(param.EndTime != null, t => t.RequestTime <= param.EndTime)
//               .WhereIf(!string.IsNullOrEmpty(LogCategory), t => t.LogCategory == param.LogCategory)
//               .WhereIf(!string.IsNullOrEmpty(keyword), t => t.Params.Contains(keyword) || t.Result.Contains(keyword))
//               .ProjectTo<SystemLogDTO>(_mapper.ConfigurationProvider);
                       
//            return logQueryable.ToPagedList(param.PageIndex, param.PageSize, string.IsNullOrWhiteSpace(param.SortField) ? "RequestTime" : param.SortField, param.Asc);

//        }

//        [HttpGet("{trialId:guid}")]
//        public List<OptUserDto> GetOptUserList(Guid trialId)
//        {
//            var list = _trialAuditRepository.Where(t => t.TrialId == trialId).Select(u => new OptUserDto()
//            {
//                OptUserId = u.OptUserId,
//                OptUser = u.OptUser
//            }).Distinct().ToList();

//            return list;
//        }

//        /// <summary>
//        ///  审计列表   受试者下拉框   从受试者那里进去看的时候，这里需要固定，如果不采用下拉框，请传递指定格式的受试者信息查询才行
//        /// </summary>
//        /// <param name="trialId"></param>
//        /// <returns></returns>
//        [HttpGet("{trialId:guid}")]
//        public List<AuditSubjectSelectDto> GetSubjectList(Guid trialId)
//        {
//            var query = from trialAudit in _trialAuditRepository.Where(t => t.TrialId == trialId)
//                        join subject in _subjectRepository.AsQueryable() on trialAudit.SubjectId equals subject.Id
//                        select new AuditSubjectSelectDto()
//                        {
//                            SubjectCode = subject.Code,
//                            SubjectId = trialAudit.SubjectId,
//                            SubjectName = subject.LastName + " / " + subject.FirstName
//                        };

//            return query.Distinct().ToList();
//        }

//        [NonDynamicMethod]
//        public IResponseOutput SaveLog2Db(SystemLogDTO input)
//        {
//            input.ClientIP = IPHelper.GetIP(_context?.HttpContext?.Request);

//            _systemLogRepository.Add(_mapper.Map<SystemLog>(input));
//            var success = _systemLogRepository.SaveChanges();
//            return ResponseOutput.Result(success);
//        }
//    }
//}
