using IRaCIS.Application.Interfaces;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Infra.EFCore;
using Microsoft.AspNetCore.Mvc;

namespace IRaCIS.Application.Services
{
    [ ApiExplorerSettings(GroupName = "Institution")]
    public class CroService : BaseService, ICroService
    {
        private readonly IRepository<CRO> _croRepository;
        private readonly IRepository<Trial> _trialRepository;

        public CroService(IRepository<CRO> croRepository, IRepository<Trial> trialRepository)
        {
            _croRepository = croRepository;
            this._trialRepository = trialRepository;
        }

        /// <summary> 分页获取CRO列表 </summary>
        [HttpPost]
        public async Task<PageOutput<CROCompanyDTO>> GetCroList(CROCompanyQueryDTO croCompanySearchModel)
        {        
            var croQueryable = _croRepository
                .WhereIf(!string.IsNullOrEmpty(croCompanySearchModel.CROName), t => t.CROName.Contains(croCompanySearchModel.CROName) || t.CRONameCN.Contains(croCompanySearchModel.CROName))
                 .WhereIf(!string.IsNullOrEmpty(croCompanySearchModel.CROCode), t => t.CROCode.Contains(croCompanySearchModel.CROCode) )
                .ProjectTo<CROCompanyDTO>(_mapper.ConfigurationProvider);

            return await croQueryable.ToPagedListAsync(croCompanySearchModel.PageIndex, croCompanySearchModel.PageSize, 
                string.IsNullOrWhiteSpace(croCompanySearchModel.SortField) ? "CROName": croCompanySearchModel.SortField, croCompanySearchModel.Asc);


        }

        /// <summary> 根据CRO 名称查询所有CRO 列表 </summary>
        public async Task<IEnumerable<CroSelectDTO>> GetAllCROList()
        {
            return await _croRepository.ProjectTo<CroSelectDTO>(_mapper.ConfigurationProvider, new { isEn_Us = _userInfo.IsEn_Us }).ToListAsync();
        }



        /// <summary> 添加CRO信息 </summary>
       
        public async Task<IResponseOutput> AddOrUpdateCro(CROCompanyDTO addCroCompanyCommand)
        {

            var exp = new EntityVerifyExp<CRO>()
            {
                VerifyExp = cro => cro.CROName.Equals(addCroCompanyCommand.CROName) ,
                //---已经存在同名的CRO，请确认。
                VerifyMsg = _localizer["CRO_DupName"]
            };

            var cro = await _croRepository.InsertOrUpdateAsync(addCroCompanyCommand, true, exp);

         

            return ResponseOutput.Ok( cro.Id.ToString());
          
        }

        /// <summary> 删除CRO信息 </summary>
       
        [HttpDelete("{croCompanyId:guid}")]
        public  async Task<IResponseOutput> DeleteCro(Guid cROCompanyId)
        {
            if (await _trialRepository.AnyAsync(t => t.CROId == cROCompanyId))
            {
                //---该CRO已经参与项目，不能被删除。
                return ResponseOutput.NotOk(_localizer["CRO_InProject"]);
            }
            //if (_userRepository.Find().Any(t => t.OrganizationId == cROCompanyId))
            //{
            //    return ResponseOutput.NotOk("该CRO下存在用户，暂时无法删除。");
            //}
            var success = await _croRepository.BatchDeleteNoTrackingAsync(x=>x.Id== cROCompanyId);

            return ResponseOutput.Result(success);
        }
    }
}
