using New_College.IRepository;
using New_College.IRepository.UnitOfWork;
using New_College.Model.Models;
using New_College.Model.ViewModels.Query;
using New_College.Model.ViewModels.Result;
using New_College.Repository.Base;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace New_College.Repository
{
    /// <summary>
    /// T_EnrollmentPlaneRepository
    /// </summary>
    public class T_EnrollmentPlaneRepository : BaseRepository<T_EnrollmentPlane>, IT_EnrollmentPlaneRepository
    {
        public T_EnrollmentPlaneRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }

      
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<EnrollmentPlansResult>> GetEnrollmentPlans(EnrollmentPlanRequest request)
        {
            var planinfo = await this.Db.Queryable<T_EnrollmentPlanedesc, D_University, T_EnrollmentBatch>((plans, universitys, batchs) => new object[] { JoinType.Left, plans.UniversityId == universitys.Id, JoinType.Left, plans.BatchtypeId == batchs.Id })
                .Where((plans, universitys) => plans.PlanId == request.PlanId)
                .WhereIF(!string.IsNullOrEmpty(request.UniversityName), (plans, universitys, batchs) => universitys.Name.Contains(request.UniversityName))
                 .WhereIF(!string.IsNullOrEmpty(request.MajorName), (plans, universitys, batchs) => plans.MajorName.Contains(request.MajorName))
                 .OrderBy((plans) => plans.OrderSort, OrderByType.Desc).Select((plans, universitys, batchs) => new EnrollmentPlansResult
                 {
                     Id = plans.Id,
                     BatchName = batchs.Batch_name,
                     MajorName = plans.MajorName,
                     PlanCount = plans.Plancount,
                     UniversityName = universitys.Name
                 }).ToListAsync();
            return planinfo;
            ;
        }
    }
}