package com.yida.data.school.exam.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.exam.CoreExamination;
import com.yida.data.common.core.entity.exam.CoreExaminationCourse;
import com.yida.data.common.core.entity.exam.CoreExaminationStudent;
import com.yida.data.common.core.entity.exam.CoreExaminationStudentCourse;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.school.exam.mapper.CoreExaminationCourseMapper;
import com.yida.data.school.exam.mapper.CoreExaminationMapper;
import com.yida.data.school.exam.mapper.CoreExaminationStudentCourseMapper;
import com.yida.data.school.exam.mapper.CoreExaminationStudentMapper;
import com.yida.data.school.exam.service.CoreExaminationService;
import com.yida.data.school.vo.exam.ExamVO;
import com.yida.data.system.feign.RemoteAreaService;
import com.yida.data.system.feign.RemoteDeptService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
@Transactional
@AllArgsConstructor
public class CoreExaminationServiceImpl extends ServiceImpl<CoreExaminationMapper, CoreExamination> implements CoreExaminationService {
    private final CoreExaminationCourseMapper coreExaminationCourseMapper;
    private final RemoteAreaService remoteAreaService;
    private final RemoteDeptService remoteDeptService;
    private final CoreExaminationStudentMapper coreExaminationStudentMapper;
    private final CoreExaminationStudentCourseMapper coreExaminationStudentCourseMapper;

    @Override
    public IPage<CoreExamination> findExaminationPageList(CoreExamination coreExamination,
                                                          CurrentUser currentUser) {
        if (coreExamination.getSchoolId() == null) {
            if (currentUser.getDeptType() != 5) {
                coreExamination.setSchoolId(currentUser.getDeptId());
            }
        }
        return baseMapper.findExaminationPageList(new Page(coreExamination.getPageNum(),
                coreExamination.getPageSize()), coreExamination);
    }

    @Override
    @Transactional
    public void saveExamination(CoreExamination coreExamination, CurrentUser currentUser) {

        Long areaId = remoteDeptService.findSchoolByDept(coreExamination.getSchoolId().longValue()).getData().getAreaId();

        List<Long> data = remoteAreaService.findAreaParentId(areaId).getData();
        coreExamination.setVillageId(areaId);
        coreExamination.setProvinceId(data.get(2));
        coreExamination.setCityId(data.get(1));
        coreExamination.setCountyId(data.get(0));
        //登录用户id
        Long userId = 1L;
        if (currentUser != null) {
            userId = currentUser.getUserId();
        }

        //判断是新增还是修改
        if (coreExamination.getId() == null) {
            //新增
            //学校名称
            String deptName = currentUser.getDeptName();
            coreExamination.setSchoolName(deptName);
            //发布状态
            coreExamination.setPublish("0");
        } else {
            //删除考试课程信息
            coreExaminationCourseMapper.delete(Wrappers.lambdaQuery(new CoreExaminationCourse()).eq(CoreExaminationCourse::getCoreExaminationId,
                    coreExamination.getId()));
        }
        List<CoreExaminationCourse> saveCourseList = coreExamination.getCourseList();
        //总分数
        double score = 0;
        for (CoreExaminationCourse examinationCourse : saveCourseList) {
            score += examinationCourse.getCoreCourseScore();
        }
        coreExamination.setScore(score);
        //保存考试信息
        saveOrUpdate(coreExamination);
        //保存对应的考试课程信息
        for (CoreExaminationCourse coreExaminationCourse : saveCourseList) {
            //考试主键id
            coreExaminationCourse.setCoreExaminationId(coreExamination.getId());
        }
        for (CoreExaminationCourse course : saveCourseList) {
            coreExaminationCourseMapper.insert(course);
        }
    }

    @Override
    public CoreExamination findExamination(Integer id) {
        CoreExamination coreExamination = baseMapper.selectById(id);
        if (coreExamination != null) {
            coreExamination.setCourseList(coreExaminationCourseMapper.selectList(Wrappers.lambdaQuery(new CoreExaminationCourse()).eq(CoreExaminationCourse::getCoreExaminationId, id)));
        }
        return coreExamination;
    }

    @Override
    public Integer countExaminationCourseByExaminationId(Integer examinationId) {
        return baseMapper.countExaminationCourseByExaminationId(examinationId);
    }

    @Override
    public Integer countExaminationClassByExaminationId(Integer examinationId) {
        return baseMapper.countExaminationClassByExaminationId(examinationId);
    }

    @Override
    public Integer countExaminationStudentByExaminationId(Integer examinationId) {
        return baseMapper.countExaminationStudentByExaminationId(examinationId);
    }

    @Override
    public List findHistoryExamination(Long schoolId, Long studentId) {

        return baseMapper.findHistoryExamination(schoolId, studentId);
    }

    @Override
    public List<ExamVO> findHistoryExaminationList(String examName, Long studentId) {
        return baseMapper.findHistoryExaminationList(examName, studentId);
    }

    @Override
    public void delExamBatch(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        Asserts.isNull(baseMapper.existPublishExam(ids), "存在已发布的考试");
        // 删除考试
        removeByIds(idList);
        // 删除考试的科目
        coreExaminationCourseMapper.delete(Wrappers.<CoreExaminationCourse>lambdaQuery()
                .in(CoreExaminationCourse::getCoreExaminationId, idList));
        // 删除考生
        coreExaminationStudentMapper.delete(Wrappers.<CoreExaminationStudent>lambdaQuery()
                .in(CoreExaminationStudent::getCoreExaminationId, idList));

        // 删除考生的科目成绩
        coreExaminationStudentCourseMapper.delete(Wrappers.<CoreExaminationStudentCourse>lambdaQuery()
                .in(CoreExaminationStudentCourse::getCoreExaminationId, idList));
    }
}



