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

using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infra.EFCore;
using Microsoft.AspNetCore.Mvc;

namespace IRaCIS.Core.Application.Contracts
{
    /// <summary>
    /// 系统邮件配置表
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Common")]
    public class EmailNoticeConfigService : BaseService, IEmailNoticeConfigService
    {
        private readonly IRepository<EmailNoticeConfig> _emailNoticeConfigrepository;

        public EmailNoticeConfigService(IRepository<EmailNoticeConfig> repository)
        {
            _emailNoticeConfigrepository = repository;
        }

        [HttpPost]
        public async Task<PageOutput<EmailNoticeConfigView>> GetEmailNoticeConfigList(EmailNoticeConfigQuery queryEmailNoticeConfig)
        {
            var emailNoticeConfigQueryable = _emailNoticeConfigrepository
            .WhereIf(queryEmailNoticeConfig.BusinessScenarioEnum != null, t => t.BusinessScenarioEnum == queryEmailNoticeConfig.BusinessScenarioEnum)
            .WhereIf(queryEmailNoticeConfig.IsReturnRequired != null, t => t.IsReturnRequired == queryEmailNoticeConfig.IsReturnRequired)
            .WhereIf(queryEmailNoticeConfig.IsUrgent != null, t => t.IsUrgent == queryEmailNoticeConfig.IsUrgent)
            .WhereIf(queryEmailNoticeConfig.IsEnable != null, t => t.IsEnable == queryEmailNoticeConfig.IsEnable)
            .ProjectTo<EmailNoticeConfigView>(_mapper.ConfigurationProvider);

            return await emailNoticeConfigQueryable.ToPagedListAsync(queryEmailNoticeConfig.PageIndex, queryEmailNoticeConfig.PageSize, queryEmailNoticeConfig.SortField, queryEmailNoticeConfig.Asc);
        }


        public async Task<IResponseOutput> AddOrUpdateEmailNoticeConfig(EmailNoticeConfigAddOrEdit addOrEditEmailNoticeConfig)
        {
   
            var entity = await _emailNoticeConfigrepository.InsertOrUpdateAsync(addOrEditEmailNoticeConfig, true);

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

        }


        [HttpDelete("{emailNoticeConfigId:guid}")]
        public async Task<IResponseOutput> DeleteEmailNoticeConfig(Guid emailNoticeConfigId)
        {
            var success = await _emailNoticeConfigrepository.BatchDeleteNoTrackingAsync(t => t.Id == emailNoticeConfigId);

            return ResponseOutput.Result(success);
        }



        public async Task<Dictionary<object, string>> GetEmailScenarioEnumSelect()
        {
            return await Task.FromResult(EnumToSelectExtension.ToSelect<EmailScenarioEnum>());
        }
    }
}
