
using New_College.IServices;
using New_College.Model.Models;
using New_College.Services.BASE;
using New_College.IRepository.Base;
using New_College.Common.Excel;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using New_College.IRepository;
using New_College.Model.ViewModels;
using New_College.Model;

namespace New_College.Services
{
    public class D_MajorSalaryServices : BaseServices<D_MajorSalary>, ID_MajorSalaryServices
    {
        private readonly IBaseRepository<D_MajorSalary> _dal;
        private readonly ID_MajorRepository d_MajorRepository;
        private readonly ID_MajorClassRepository d_MajorClassRepository;
        private readonly ID_MajorCategoryRepository d_MajorCategoryRepository;

        public D_MajorSalaryServices(IBaseRepository<D_MajorSalary> dal
            , ID_MajorRepository ID_MajorRepository
            , ID_MajorClassRepository ID_MajorClassRepository
            , ID_MajorCategoryRepository ID_MajorCategoryRepository)
        {
            this._dal = dal;
            d_MajorRepository = ID_MajorRepository;
            d_MajorClassRepository = ID_MajorClassRepository;
            d_MajorCategoryRepository = ID_MajorCategoryRepository;
            base.BaseDal = dal;
        }

        #region 专业薪酬
        /// <summary>
        /// 专业薪酬
        /// </summary>
        /// <returns></returns>
        public async Task<bool> Import()
        {
            var dataSet = ExcelUtil.ReadExcelToDataSet("D:\\Ashuju\\新版志愿好帮手\\专业薪酬.xlsx");
            if (dataSet.Tables.Count > 0)
            {
                List<D_MajorSalary> list = new List<D_MajorSalary>() { };
                var majorinfo = await d_MajorRepository.Query(x => x.IsDelete == false);
                List<string> nohave = new List<string>() { };
                foreach (DataRow dr in dataSet.Tables[0].Rows)
                {
                    string majorname = dr["专业名称"].ToString();
                    string Salary = dr["薪资水平"].ToString();


                    var majorid = majorinfo.Where(x => x.MajorName == majorname).Select(x => x.Id).FirstOrDefault();
                    if (majorid <= 0)
                    {
                        nohave.Add(majorname);
                        continue;
                    }
                    var info = await _dal.Add(new D_MajorSalary()
                    {
                        Salary = float.Parse(Salary),
                        MajorName = majorname,
                        MajorId = majorid
                    });
                }
                var nohavestring = string.Join(",", nohave);
                await _dal.Add(list);
            }
            return true;
        }
        #endregion

        /// <summary>
        /// 获取高薪推荐  type传1位本科 传2位专科
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<MessageModel<PageModel<MajorSalaryResult>>> GetMajorSalaryResult(MajorSalaryQuery query)
        {
            var majorcate = await d_MajorCategoryRepository.Query(x => x.Type == query.Type);
            if (majorcate.Count <= 0)
                return new MessageModel<PageModel<MajorSalaryResult>>() { success = false, msg = "获取失败，数据为空" };
            var majorcateids = majorcate.Select(x => x.Id).ToList();
            var majorclass = await d_MajorClassRepository.Query(x => majorcateids.Contains(x.TradeId) && x.IsDelete == false);
            if (majorclass.Count <= 0)
                return new MessageModel<PageModel<MajorSalaryResult>>() { success = false, msg = "获取失败，数据为空" };
            var majorclassids = majorclass.Select(x => x.Id).ToList();
            var major = await d_MajorRepository.Query(x => majorclassids.Contains(x.CategoryClass_Id) && x.IsDelete == false);
            if (major.Count <= 0)
                return new MessageModel<PageModel<MajorSalaryResult>>() { success = false, msg = "获取失败，数据为空" };
            var majorids = major.Select(x => x.Id).ToList();
            List<MajorSalaryResult> list = new List<MajorSalaryResult>() { };
            var info = await _dal.QueryPage(x => x.IsDelete == false && majorids.Contains(x.MajorId), query.PageIndex, query.PageSize, "Salary desc");
            if (info.data.Count <= 0)
                return new MessageModel<PageModel<MajorSalaryResult>>() { success = false, msg = "获取失败，数据为空" };
            foreach (var item in info.data)
            {
                var nowinfo = major.Where(x => x.Id == item.MajorId).FirstOrDefault();
                var majorclassinfo = majorclass.Where(x => x.Id == nowinfo.CategoryClass_Id).FirstOrDefault();
                if (majorclassinfo == null)
                    continue;
                list.Add(new MajorSalaryResult()
                {
                    MajorId=item.MajorId,
                    MajorName = item.MajorName,
                    Money = item.Salary,
                    ParentName = majorclassinfo.Name,
                    Type = query.Type == 1 ? "本科" : "专科"
                });
            }
            return new MessageModel<PageModel<MajorSalaryResult>>()
            {
                success = true,
                msg = "获取成功",
                response = new PageModel<MajorSalaryResult>()
                {
                    data = list,
                    dataCount = info.dataCount,
                    page = info.page,
                    pageCount = info.pageCount,
                    PageSize = info.PageSize
                }
            };
        }
    }
}