//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2022-06-29 13:38:40 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------

using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
namespace IRaCIS.Core.Application.Service
{
    /// <summary>
    /// 医学审核生成规则
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Trial")]
    public class TaskMedicalReviewRuleService : BaseService, ITaskMedicalReviewRuleService
    {

        private readonly IRepository<TaskMedicalReviewRule> _taskMedicalReviewRuleRepository;

        public TaskMedicalReviewRuleService(IRepository<TaskMedicalReviewRule> taskMedicalReviewRuleRepository)
        {
            _taskMedicalReviewRuleRepository = taskMedicalReviewRuleRepository;
        }


        [HttpPost]
        public async Task<(List<TaskMedicalReviewRuleView>,object)> GetTaskMedicalReviewRuleList(TaskMedicalReviewRuleQuery inQuery)
        {


            var taskTaskMedicalReviewRuleQueryable = _taskMedicalReviewRuleRepository.Where(t => t.TrialId == inQuery.TrialId)
            .ProjectTo<TaskMedicalReviewRuleView>(_mapper.ConfigurationProvider);

            var isHaveMIM = await _repository.AnyAsync<TrialUser>(t => t.User.UserTypeEnum == Domain.Share.UserTypeEnum.MIM && t.TrialId==inQuery.TrialId);

            return (await taskTaskMedicalReviewRuleQueryable.ToListAsync(),new {IsHaveMIM=isHaveMIM});
        }

        [HttpPost]
        public async Task<IResponseOutput> AddOrUpdateTaskMedicalReviewRule(TaskMedicalReviewRuleAddOrEdit addOrEditTaskTaskMedicalReviewRule)
        {
            var verifyExp1 = new EntityVerifyExp<TaskMedicalReviewRule>()
            {
                VerifyExp = t => t.DoctorUserId == addOrEditTaskTaskMedicalReviewRule.DoctorUserId && t.TrialId == addOrEditTaskTaskMedicalReviewRule.TrialId,
                VerifyMsg = "已有该医生配置，不允许继续增加"
            };

            var entity = await _taskMedicalReviewRuleRepository.InsertOrUpdateAsync(addOrEditTaskTaskMedicalReviewRule, true, verifyExp1);

            return ResponseOutput.Ok(entity.Id.ToString());

        }


        [HttpDelete("{taskMedicalReviewRuleId:guid}")]
        public async Task<IResponseOutput> DeleteTaskMedicalReviewRule(Guid taskMedicalReviewRuleId)
        {
            var success = await _taskMedicalReviewRuleRepository.DeleteFromQueryAsync(t => t.Id == taskMedicalReviewRuleId,true);

            return ResponseOutput.Ok();
        }


    }
}
