
using New_College.IServices;
using New_College.Model.Models;
using New_College.Services.BASE;
using New_College.IRepository.Base;
using System.Collections.Generic;
using New_College.Model.ViewModels;
using System.Threading.Tasks;
using System.Linq;
using New_College.IRepository;
using New_College.Common.WebApiClients.HttpApis;
using YIJIYI.Core.Common.Helper;
using System;

namespace New_College.Services
{
    public class Test_CategoryInfoServices : BaseServices<Test_CategoryInfo>, ITest_CategoryInfoServices
    {
        private readonly IBaseRepository<Test_CategoryInfo> _dal;
        private readonly ITest_PsychMeasurementInfoRepository test_PsychMeasurementInfoRepository;
        private readonly ITest_CycleTimeInfoRepository test_CycleTimeInfoRepository;
        private readonly IV_OrderInfoRepository v_OrderInfoRepository;

        public Test_CategoryInfoServices(IBaseRepository<Test_CategoryInfo> dal
            , ITest_PsychMeasurementInfoRepository ITest_PsychMeasurementInfoRepository
            , ITest_CycleTimeInfoRepository ITest_CycleTimeInfoRepository
            , IV_OrderInfoRepository IV_OrderInfoRepository)
        {
            this._dal = dal;
            test_PsychMeasurementInfoRepository = ITest_PsychMeasurementInfoRepository;
            test_CycleTimeInfoRepository = ITest_CycleTimeInfoRepository;
            v_OrderInfoRepository = IV_OrderInfoRepository;
            base.BaseDal = dal;
        }

        /// <summary>
        /// 获取分类  根绝type
        /// </summary>
        /// <returns></returns>
        public async Task<List<CategoryInfoResult>> GetCateSelectByType(CategoryInfoQuery query)
        {
            var info = await _dal.Query(x => x.IsDelete == false && x.OrderSort == query.Type);
            if (info.Count <= 0)
                return new List<CategoryInfoResult>() { };
            //var orderinfo = await v_OrderInfoRepository.Query(x => x.IsDelete == false);
            var ids = info.Select(x => x.Id).ToList();
            var testinfo = await test_PsychMeasurementInfoRepository.Query(x => x.IsDelete == false && ids.Contains(x.CategoryId));
            //var cycleinfo = await test_CycleTimeInfoRepository.Query(x => x.IsDelete == false);
            List<CategoryInfoResult> list = new List<CategoryInfoResult>() { };
            foreach (var item in info)
            {
                list.Add(new CategoryInfoResult()
                {
                    Id = item.Id,
                    Name = item.Name,
                    Desc = item.Desc,
                    Detail = item.Detail,
                    JoinCount = testinfo.Count(x => x.CategoryId == item.Id)
                });
            }
            return list;
        }

        /// <summary>
        /// 获取测评状态
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<CategoryStatusResult> GetTestStatus(CategoryStatusQuery query)
        {
            var info = await v_OrderInfoRepository.Query(x => x.CustomerId == query.CustomerId && x.IsDelete == false);
            if (info.Count <= 0)
                return new CategoryStatusResult() { };
            var viptype = info.OrderByDescending(x => x.CreateTime).FirstOrDefault().CardTypeId;
            var cycleinfo = await test_CycleTimeInfoRepository.Query(x => x.VipCardTypeId == viptype && x.IsDelete == false && x.CategoryId == query.CategoryId);
            if (cycleinfo.Count <= 0)
                return new CategoryStatusResult() { };
            var psych = await test_PsychMeasurementInfoRepository.Query(x => x.CategoryId == query.CategoryId && x.StudentId == query.CustomerId);
            if (psych.Count <= 0)
                return new CategoryStatusResult()
                {
                    CycleTimeId = cycleinfo.FirstOrDefault().Id,
                    TestNum = 0
                };
            var CycleTimeId = 0;
            var TestNum = 0;
            DateTime? TopTime = TimeUtil.GetCstDateTime();
            foreach (var item in cycleinfo)
            {
                if (CycleTimeId > 0)
                    continue;
                var nowinfo = psych.Where(x => x.CycleTimeId == item.Id).FirstOrDefault();
                if (nowinfo != null)
                {
                    TopTime = nowinfo.CreateTime;
                    TestNum++;
                }
                else
                {
                    CycleTimeId = item.Id;
                }
                
            }
            return new CategoryStatusResult()
            {
                CycleTimeId = CycleTimeId,
                TestNum = TestNum,
                TopTime = TopTime
            };
        }
    }
}