using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using System.Collections.Generic;

namespace IRaCIS.Core.Infrastructure.Extention
{
    public static class IQueryableExtensions
    {
       /// <summary>
       /// 获取第一条 为null提示
       /// </summary>
       /// <typeparam name="TSource"></typeparam>
       /// <param name="source"></param>
       /// <returns></returns>
       /// <exception cref="QueryBusinessObjectNotExistException"></exception>
        public  static async Task<TSource> FirstNotNullAsync<TSource>(this IQueryable<TSource> source)
        {
            var result =await source.FirstOrDefaultAsync();

            if (result == null)
            {
                throw new QueryBusinessObjectNotExistException($"The query object does not exist in database, Please check the query parameters");
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 查询字符串 为null 返回string.Empty
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static  string FirstIsNullReturnEmpty(this IEnumerable<string> source)
        {
            var result =  source.FirstOrDefault();

            if (result == null || result == string.Empty)
            {
                return string.Empty;
            }
            else
            {
                return result;
            }
        }

    }
}
