package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.dto.TaskPrintHouseDto;
import com.qmth.distributed.print.business.entity.ExamTaskPrint;
import com.qmth.distributed.print.business.entity.TeachClazz;
import com.qmth.distributed.print.business.entity.TeachCourse;
import com.qmth.distributed.print.business.entity.TeachStudent;
import com.qmth.distributed.print.business.mapper.ExamTaskPrintMapper;
import com.qmth.distributed.print.business.service.*;
import com.qmth.teachcloud.common.bean.dto.TaskPrintClassDto;
import com.qmth.teachcloud.common.entity.BasicCourse;
import com.qmth.teachcloud.common.entity.BasicUserCourse;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.OrgTypeEnum;
import com.qmth.teachcloud.common.enums.TeachBasicEnum;
import com.qmth.teachcloud.common.service.*;
import com.qmth.teachcloud.common.util.RedisUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Date: 2021/8/2.
 */
@Service
public class ExamTaskPrintServiceImpl extends ServiceImpl<ExamTaskPrintMapper, ExamTaskPrint> implements ExamTaskPrintService {

    @Resource
    private BasicStudentService basicStudentService;

    @Resource
    private ExamDetailService examDetailService;

    @Resource
    private ExamDetailCourseService examDetailCourseService;

    @Resource
    private ExamStudentService examStudentService;

    @Resource
    private ExamPrintPlanService examPrintPlanService;

    @Resource
    private BasicExamRuleService basicExamRuleService;

    @Resource
    private BasicUserCourseService basicUserCourseService;

    @Resource
    TBTaskService tbTaskService;

    @Resource
    @Lazy
    PrintCommonService printCommonService;

    @Resource
    BasicClazzService basicClazzService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    private TeachCourseService teachCourseService;

    @Resource
    private TeachClazzService teachClazzService;

    @Resource
    private TeachStudentService teachStudentService;

    @Resource
    private BasicCourseService basicCourseService;

    @Override
    public List<TaskPrintHouseDto> listHouse() {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        List<TaskPrintHouseDto> houseDtos = this.baseMapper.listTaskPrintHouse(schoolId, OrgTypeEnum.PRINTING_HOUSE.name());
        return houseDtos;
    }

    @Override
    public List<TaskPrintClassDto> listExamTaskClass(String courseCode) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        List<TaskPrintClassDto> taskPrintClassDtos = new ArrayList<>();
//        BasicCourse basicCourse = basicCourseService.findByCourseCode(courseCode, schoolId);
//        // 1.判断教学表中是否有数据
//        List<TeachCourse> teachCourseDatasource = teachCourseService.list(new QueryWrapper<TeachCourse>().lambda()
//                .eq(TeachCourse::getSchoolId, schoolId)
//                .eq(TeachCourse::getUserId, requestUser.getId()));
//        if (teachCourseDatasource != null && teachCourseDatasource.size() > 0) {
//            // 如果该用户创建了任意教学课程，则走教学课程查询逻辑
//            List<TeachCourse> teachCourseList = teachCourseDatasource.stream().filter(e -> basicCourseService.getById(e.getBasicCourseId()).getCode().equals(courseCode)).collect(Collectors.toList());
//            if (teachCourseList.size() > 1) {
//                throw ExceptionResultEnum.ERROR.exception("基础信息管理中[" + basicCourse.getName() + "]教学课程信息存在多条数据异常");
//            }
//            if (teachCourseList.size() == 0) {
//                // 没创建该教学课程
//                throw ExceptionResultEnum.ERROR.exception("请优先在基础信息管理中创建[" + basicCourse.getName() + "]的教学课程信息");
//            }
//            TeachCourse teachCourse = teachCourseList.get(0);
//            List<TeachClazz> teachClazzList = teachClazzService.list(new QueryWrapper<TeachClazz>().lambda().in(TeachClazz::getBasicCourseId, teachCourse.getBasicCourseId()));
//            for (TeachClazz teachClazz : teachClazzList) {
//                TaskPrintClassDto dto = new TaskPrintClassDto();
//                dto.setClassId(String.valueOf(teachClazz.getId()));
//                dto.setTeachBasicEnum(TeachBasicEnum.TEACH_CLAZZ);
//                dto.setClassName(teachClazz.getTeachClazzName());
//                dto.setStudentCount(teachStudentService.count(new QueryWrapper<TeachStudent>().lambda().eq(TeachStudent::getTeachClazzId, teachClazz.getId())));
//                taskPrintClassDtos.add(dto);
//            }
//        } else {
//            List<BasicUserCourse> courseList = basicUserCourseService.listBySchoolIdAndCourseCode(schoolId, courseCode);
//            Set<String> stringList = courseList.stream().map(m -> String.valueOf(m.getClazzId())).collect(Collectors.toSet());
//
//            if (!CollectionUtils.isEmpty(stringList)) {
//                taskPrintClassDtos = basicStudentService.listByClass(schoolId, stringList);
//            }
//        }
        return taskPrintClassDtos;
    }

}
