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

using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace IRaCIS.Core.Application.Service
{
    /// <summary>
    /// ExploreRecommendService
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Common")]
    public class ExploreRecommendService(IRepository<ExploreRecommend> _exploreRecommendRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, IExploreRecommendService
    {


        [HttpPost]
        public async Task<PageOutput<ExploreRecommendView>> GetExploreRecommendList(ExploreRecommendQuery inQuery)
        {

            var exploreRecommendQueryable =

               _exploreRecommendRepository.Where().IgnoreQueryFilters()
               .WhereIf(!string.IsNullOrEmpty(inQuery.Title), t => t.Title.Contains(inQuery.Title))
               .WhereIf(!string.IsNullOrEmpty(inQuery.FileName), t => t.Title.Contains(inQuery.FileName))
               .WhereIf(!string.IsNullOrEmpty(inQuery.DownloadUrl), t => t.Title.Contains(inQuery.DownloadUrl))
               .WhereIf(!string.IsNullOrEmpty(inQuery.Version), t => t.Title.Contains(inQuery.Version))
                .WhereIf(inQuery.IsDeleted != null, t => t.IsDeleted == t.IsDeleted)
             .ProjectTo<ExploreRecommendView>(_mapper.ConfigurationProvider);

            var pageList = await exploreRecommendQueryable
            .ToPagedListAsync(inQuery);

            return pageList;
        }


        public async Task<IResponseOutput> AddOrUpdateExploreRecommend(ExploreRecommendAddOrEdit addOrEditExploreRecommend)
        {
            var verifyExp2 = new EntityVerifyExp<ExploreRecommend>()
            {
                VerifyExp = u => u.IsDeleted == addOrEditExploreRecommend.IsDeleted && u.ExploreType == addOrEditExploreRecommend.ExploreType,

                // "当前类型浏览器启用版本只允许有一个"
                VerifyMsg = _localizer["ExploreRecommend_OnlyOneTypePerType"],

                IsVerify = addOrEditExploreRecommend.IsDeleted == false
            };

            var entity = await _exploreRecommendRepository.InsertOrUpdateAsync(addOrEditExploreRecommend, true, verifyExp2);

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

        }


        [HttpDelete("{exploreRecommendId:guid}")]
        public async Task<IResponseOutput> DeleteExploreRecommend(Guid exploreRecommendId)
        {
            var success = await _exploreRecommendRepository.DeleteFromQueryAsync(t => t.Id == exploreRecommendId, true, true);
            return ResponseOutput.Ok();
        }

        [AllowAnonymous]
        public async Task<List<ExploreRecommendView>> GetExploreRecommentInfo()
        {


            var result = await _exploreRecommendRepository.Where(t => t.IsDeleted == false).ProjectTo<ExploreRecommendView>(_mapper.ConfigurationProvider).ToListAsync();

            if (result.Count == 0)
            {             //"系统浏览器版本推荐未维护，请联系维护人员"
                throw new QueryBusinessObjectNotExistException(_localizer["ExploreRecommend_NoExporeRecord"]);
            }

            return result;
        }


    }
}
