using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using New_College.IServices;
using New_College.Model;
using New_College.Model.ViewModels.Query;
using New_College.Model.ViewModels.Result;

namespace New_College.Api.Controllers.Front
{

    /// <summary>
    ///招生计划
    /// </summary>
    [Route("api/front/[controller]/[action]")]
    [ApiController]
    public class EnrollMentPlansController : ControllerBase
    {
        private readonly IT_EnrollmentPlaneServices t_EnrollmentPlane;
        private readonly IT_EnrollmentBatchServices t_EnrollmentBatch;
        public EnrollMentPlansController(IT_EnrollmentPlaneServices enrollmentPlaneServices, IT_EnrollmentBatchServices t_EnrollmentBatchServices)
        {
            this.t_EnrollmentPlane = enrollmentPlaneServices;
            this.t_EnrollmentBatch = t_EnrollmentBatchServices;
        }


        /// <summary>
        /// 根据院校和专业获取招生计划列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<EnrollmentPlansResult>>> GetEnrollmentPlans([FromQuery] EnrollmentPlanRequest request)
        {
            try
            {
                var planinfo = await this.t_EnrollmentPlane.Query(e => e.IsDelete == false && e.Area_Id == request.AreaId && e.Years == request.Years);
                if (planinfo.Any())
                {
                    var BatchtypeId = (await this.t_EnrollmentBatch.Query(e => e.Year == request.Years && e.AreaId == request.AreaId && e.Batch_name == request.BatchName)).FirstOrDefault().Id;
                    request.PlanId = planinfo.FirstOrDefault().Id;
                    request.BatchtypeId = BatchtypeId;
                    var result = await this.t_EnrollmentPlane.GetEnrollmentPlans(request);
                    return new MessageModel<PageModel<EnrollmentPlansResult>>()
                    {
                        msg = "success",
                        response = result,
                        success = true
                    };
                }
                else
                {
                    return new MessageModel<PageModel<EnrollmentPlansResult>>()
                    {
                        msg = "相关信息更新中...",
                    };
                }
            }
            catch (Exception ex)
            {
                return new MessageModel<PageModel<EnrollmentPlansResult>>()
                {
                    msg = ex.Message
                };
            }
        }




    }
}
