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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.dto.ExamStudentCourseDto;
import com.qmth.distributed.print.business.bean.dto.ExamStudentDto;
import com.qmth.distributed.print.business.bean.dto.ExamStudentPdfInfoDto;
import com.qmth.distributed.print.business.entity.ExamDetailCourse;
import com.qmth.distributed.print.business.entity.ExamStudent;
import com.qmth.distributed.print.business.mapper.ExamDetailCourseMapper;
import com.qmth.distributed.print.business.mapper.ExamStudentMapper;
import com.qmth.distributed.print.business.service.ExamStudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 考生 服务实现类
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Service
public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentMapper, ExamStudent> implements ExamStudentService {

    @Resource
    ExamStudentMapper examStudentMapper;

    @Resource
    ExamDetailCourseMapper examDetailCourseMapper;

    @Override
    public List<String> listPaperTypeByExamDetailCourseId(String examDetailCourseId) {
        QueryWrapper<ExamStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExamStudent::getExamDetailCourseId, examDetailCourseId);
        List<ExamStudent> examStudents = this.list(queryWrapper);
        if (!examStudents.isEmpty()) {
            return examStudents.stream().filter(m -> StringUtils.isNotBlank(m.getPaperType())).map(m -> m.getPaperType()).distinct().collect(Collectors.toList());
        } else {
            ExamDetailCourse examDetailCourse = examDetailCourseMapper.selectById(examDetailCourseId);
            if (examDetailCourse != null && StringUtils.isNotBlank(examDetailCourse.getPaperType())) {
                return Arrays.stream(examDetailCourse.getPaperType().split(",")).collect(Collectors.toList());
            }
        }
        return null;
    }

    @Override
    public Map<String, Object> getStudentDetail(Long id) {
        Map<String, Object> map = this.baseMapper.getStudentDetail(id);
        Object extendFields = map.get("extendFields");
        if (Objects.nonNull(extendFields)) {
            List<Map> list = JSONObject.parseArray(extendFields.toString(), Map.class);
            map.put("extendFields", list);
        }
        return map;
    }

    /**
     * 根据科目查询考生
     *
     * @param schoolId
     * @param examDetailCourseIds
     * @return
     */
    @Override
    public List<ExamStudentCourseDto> queryBySchoolIdAndExamDetailCourseIds(Long schoolId, List<Long> examDetailCourseIds) {
        return examStudentMapper.queryBySchoolIdAndExamDetailCourseIds(schoolId, examDetailCourseIds);
    }

    @Override
    public ExamStudentPdfInfoDto getByStudentId(Long id) {
        return this.baseMapper.getByStudentId(id);
    }

    @Override
    public void removeByExamDetailCourseId(Long id) {
        UpdateWrapper<ExamStudent> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ExamStudent::getExamDetailCourseId, id);
        this.remove(updateWrapper);
    }

    @Override
    public List<String> listUserClass(Long schoolId, Long examId, String paperNumber) {
        return this.baseMapper.listUserClass(schoolId, examId, paperNumber);
    }

    @Override
    public ExamStudent listByExamDetailCourseIdLimit1(Long schoolId, Long examDetailCourseId) {
        return this.baseMapper.listByExamDetailCourseIdLimit1(schoolId, examDetailCourseId);
    }

    @Override
    public List<ExamStudent> listByExamDetailCourseIdNotIntMarkStudent(Long examDetailCourseId, String paperType) {
        return this.baseMapper.listByExamDetailCourseIdNotIntMarkStudent(examDetailCourseId, paperType);
    }

    @Override
    public int countByExamDetailCourseIdAndStudentCode(Long examDetailCourseId, String studentCode) {
        QueryWrapper<ExamStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExamStudent::getExamDetailCourseId, examDetailCourseId)
                .eq(ExamStudent::getStudentCode, studentCode);
        return this.count(queryWrapper);
    }

    @Override
    public List<ExamStudent> listByExamDetailCourseId(Long examDetailCourseId) {
        QueryWrapper<ExamStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExamStudent::getExamDetailCourseId, examDetailCourseId)
                .orderByAsc(ExamStudent::getId);
        return this.list(queryWrapper);
    }

    /**
     * 查询班级和老师信息
     *
     * @param examId
     * @param courseCode
     * @param paperNumber
     * @return
     */
    @Override
    public ExamStudentDto queryExamStudent(Long examId, String courseCode, String paperNumber) {
        return this.baseMapper.queryExamStudent(examId, courseCode, paperNumber);
    }
}
