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.Result;

namespace New_College.Api.Controllers.Front
{
    /// <summary>
    /// 历史大学
    /// </summary>
    [Route("api/front/[controller]/[action]")]
    [ApiController]
    public class MyHistoryInfoController : ControllerBase
    {
        private readonly ID_MyHistoryInfoServices _MyHistory;
        private readonly ID_UniversityServices _UniversityServices;
        public MyHistoryInfoController(ID_MyHistoryInfoServices d_MyHistoryInfoServices, ID_UniversityServices d_UniversityServices)
        {
            this._MyHistory = d_MyHistoryInfoServices;
            this._UniversityServices = d_UniversityServices;
        }


        /// <summary>
        /// 浏览历史记录列表
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<MyHistoryResponse>>> GetMyHistory([FromQuery] int CustomerId)
        {
            var response = new List<MyHistoryResponse>();
            try
            {
                var query = await _MyHistory.Query(e => e.UId == CustomerId);
                if (query.Any())
                {
                    var uids = query.Select(s => s.UId).ToList();
                    var universitysinfo = await this._UniversityServices.Query(q => uids.Contains(q.Id));
                    response = universitysinfo.Select(s => new MyHistoryResponse()
                    {
                        AreaName = s.Area_Name,
                        Id = s.Id,
                        Logo = s.Logo,
                        Name = s.Name,
                        Nature = s.Nature,
                        Rank = s.Rank,
                        Syl = s.Syl == 1 ? true : false,
                        Nhef = s.Nhef == 1 ? true : false,
                        Sff = s.Sff == 1 ? true : false        
                    }).ToList();
                }
                return new MessageModel<List<MyHistoryResponse>>()
                {
                    msg = "success",
                    response = response,
                    success = true
                };
            }
            catch (Exception ex)
            {
                return new MessageModel<List<MyHistoryResponse>>()
                {
                    msg = ex.Message
                };
            }


        }



    }
}
