namespace Tiobon.Core.Services;

/// <summary>
/// 开班反馈 (服务)
/// </summary>
public class Ghre_OpenClassFeedbackServices : BaseServices<Ghre_OpenClassFeedback, Ghre_OpenClassFeedbackDto, InsertGhre_OpenClassFeedbackInput, EditGhre_OpenClassFeedbackInput>, IGhre_OpenClassFeedbackServices
{
    private readonly IBaseRepository<Ghre_OpenClassFeedback> _dal;
    public Ghre_OpenClassFeedbackServices(ICaching caching, IBaseRepository<Ghre_OpenClassFeedback> dal)
    {
        this._dal = dal;
        base.BaseDal = dal;
        base._caching = caching;
    }

    public override async Task<ServicePageResult<Ghre_OpenClassFeedbackDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
    {
        var result = await base.QueryFilterPage(filter, condition, IsEnable);

        //var ids = result.result.DT_TableDataT1.Select(x => x.OpenClassId).ToList();
        var staffIds = result.result.DT_TableDataT1.Where(x => x.StaffId != null).Select(x => x.StaffId.Value).Distinct().ToList();
        var teacherIds = result.result.DT_TableDataT1.Where(x => x.TeacherId != null).Select(x => x.TeacherId.Value).Distinct().ToList();
        var openClassIds = result.result.DT_TableDataT1.Where(x => x.OpenClassId != null).Select(x => x.OpenClassId.Value).Distinct().ToList();
        //var surveyIds = result.result.DT_TableDataT1.Where(x => x.SurveyId != null).Select(x => x.SurveyId.Value).Distinct().ToList();
        var courseIds = result.result.DT_TableDataT1.Where(x => x.CourseId != null).Select(x => x.CourseId.Value).Distinct().ToList();


        var staffs = await Db.Queryable<Ghra_Staff>()
            .Where(x => staffIds.Contains(x.StaffID)).Select(x => new { x.StaffID, x.StaffNo, x.StaffName }).ToListAsync();

        var openClasss = await Db.Queryable<Ghre_OpenClass>()
            .Where(x => openClassIds.Contains(x.Id)).Select(x => new { x.Id, x.OpenClassName, x.StartTime, x.EndTime, x.ExamEndTime, x.ExamStartTime }).ToListAsync();

        var courses = await Db.Queryable<Ghre_Course>()
            .Where(x => courseIds.Contains(x.Id)).Select(x => new { x.Id, x.CourseName, x.CourseNo, x.StandardHour }).ToListAsync();

        var courseScenes = await Db.Queryable<Ghre_CourseScene>()
           .Where(x => courseIds.Contains(x.Id)).Select(x => new { x.Id, x.SceneName, x.SceneNo, x.StandardHour }).ToListAsync();

        var teachers = await Db.Queryable<Ghre_Teacher>()
            .Where(x => teacherIds.Contains(x.Id)).Select(x => new { x.Id, x.TeacherName, x.DeptID }).ToListAsync();

        var studyRecords = await Db.Queryable<Ghre_StudyRecord>()
           .Where(x => x.OpenClassId != null && openClassIds.Contains(x.OpenClassId.Value))
           .Select(x => new { x.Id, x.StandardDuration, x.OpenClassId }).ToListAsync();

        for (int i = 0; i < result.result.DT_TableDataT1.Count; i++)
        {

            var DT_TableDataT1 = result.result.DT_TableDataT1[i];
            var openClass = openClasss.Where(x => x.Id == DT_TableDataT1.OpenClassId).FirstOrDefault();

            if (DT_TableDataT1.StaffId != null)
                DT_TableDataT1.StaffName = staffs.Where(x => x.StaffID == DT_TableDataT1.StaffId).FirstOrDefault()?.StaffName;

            if (DT_TableDataT1.OpenClassId != null)
                DT_TableDataT1.OpenClassName = openClass?.OpenClassName;

            if (DT_TableDataT1.CourseId != null)
            {
                var course = courses.Where(x => x.Id == DT_TableDataT1.CourseId).FirstOrDefault();
                if (course != null)
                {
                    DT_TableDataT1.CourseName = $"{course?.CourseName}（{course?.CourseNo}）";
                    DT_TableDataT1.StandardHour = course?.StandardHour;

                }

                var courseScene = courseScenes.Where(x => x.Id == DT_TableDataT1.CourseId).FirstOrDefault();
                if (courseScene != null)
                {
                    DT_TableDataT1.CourseSceneName = $"{courseScene?.SceneName}（{courseScene?.SceneNo}）";
                    DT_TableDataT1.StandardHour = courseScene?.StandardHour;

                }
            }

            if (DT_TableDataT1.TeacherId != null)
            {
                var teacher = teachers.Where(x => x.Id == DT_TableDataT1.TeacherId).FirstOrDefault();
                DT_TableDataT1.TeacherName = teacher?.TeacherName;
                if (teacher != null && teacher.DeptID != null)
                    DT_TableDataT1.TeacherDeptName = await Db.Queryable<Ghro_Dept>()
                        .Where(x => x.DeptID == teacher.DeptID).Select(x => x.DeptName).FirstAsync();
            }

            if (DT_TableDataT1.ExpiryTime != null)
            {
                if (DateTimeHelper.ConvertToSecondString(DT_TableDataT1.ExpiryTime) == DateTimeHelper.ConvertToSecondString(DateTime.MaxValue))
                    DT_TableDataT1.ExpiryTime1 = "长期";
                else DT_TableDataT1.ExpiryTime1 = DateTimeHelper.ConvertToSecondString(DT_TableDataT1.ExpiryTime);
            }
            else
                DT_TableDataT1.ExpiryTime1 = "长期";

            var studyRecord = studyRecords.Where(x => x.OpenClassId == DT_TableDataT1.Id).FirstOrDefault();
            if (studyRecord != null)
                DT_TableDataT1.StandardDuration = studyRecord.StandardDuration;
            if (openClass != null)
            {
                DT_TableDataT1.OpenClassStartEndTime = $"{DateTimeHelper.ConvertToMiniuteString(openClass.StartTime)}~{DateTimeHelper.ConvertToMiniuteString(openClass.EndTime)}";
                DT_TableDataT1.OpenClassExamStartEndTime = $"{DateTimeHelper.ConvertToMiniuteString(openClass.ExamStartTime)}~{DateTimeHelper.ConvertToMiniuteString(openClass.ExamEndTime)}";

            }

            DT_TableDataT1.CourseBeginEndTime = $"{DateTimeHelper.ConvertToMiniuteString(DT_TableDataT1.CourseBeginTime)}~{DateTimeHelper.ConvertToMiniuteString(DT_TableDataT1.CourseEndTime)}";
            result.result.DT_TableDataT1[i] = DT_TableDataT1;
        }
        return result;
    }

}