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.ExamStudentPdfInfoDto;
import com.qmth.distributed.print.business.bean.dto.SyncExamStudentDto;
import com.qmth.distributed.print.business.entity.ExamDetail;
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 com.qmth.teachcloud.common.entity.SysOrg;
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 void updatePaperType(List<Long> examDetailCourseIds, String relatePaperType) {
        UpdateWrapper<ExamStudent> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(ExamStudent::getPaperType, relatePaperType).in(ExamStudent::getExamDetailCourseId, examDetailCourseIds);
        this.update(updateWrapper);
    }

    @Override
    public List<String> listByExamDetailCourseId(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 List<SyncExamStudentDto> listStudentByPrintPlanIdAndSyncStatus(Long printPlanId) {
        return this.baseMapper.listStudentByPrintPlanIdAndSyncStatus(printPlanId);
    }

    @Override
    public void insertBatch(List<ExamStudent> examStudents) {
        this.baseMapper.insertBatch(examStudents);
    }

    @Override
    public List<ExamStudent> listExamStudentBySchoolIdAndClazzId(Long schoolId, String classId) {
        return this.baseMapper.listExamStudentBySchoolIdAndClazzId(schoolId, classId);
    }

    @Override
    public SysOrg getSysOrgByBelongOrgId(String studentCode, Long schoolId) {
        return this.baseMapper.getSysOrgByBelongOrgId(studentCode, schoolId);
    }

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

    @Override
    public List<Map> listStudentScoreSync(Long schoolId, String studentCode, String examNumber, String subjectCode, Long printPlanId) {
        return this.baseMapper.listStudentScoreSync(schoolId, studentCode, examNumber, subjectCode, printPlanId);
    }
}
