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

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.entity.ExamDetailCourse;
import com.qmth.distributed.print.business.enums.ExamDetailStatusEnum;
import com.qmth.distributed.print.business.enums.ExamStatusEnum;
import com.qmth.distributed.print.business.mapper.ExamDetailCourseMapper;
import com.qmth.distributed.print.business.service.ExamDetailCourseService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicCourse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 考务数据（考场关联科目） 服务实现类
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Service
public class ExamDetailCourseServiceImpl extends ServiceImpl<ExamDetailCourseMapper, ExamDetailCourse> implements ExamDetailCourseService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public double calculatePackagesByDetailId(Long examDetailId) {
        System.out.println(this.count(new QueryWrapper<ExamDetailCourse>().lambda().eq(ExamDetailCourse::getExamDetailId, examDetailId)));
        return this.count(new QueryWrapper<ExamDetailCourse>().lambda().eq(ExamDetailCourse::getExamDetailId, examDetailId));
    }

    @Override
    public List<Map<String, Object>> listByExamDetailId(Long examDetailId) {
        return this.baseMapper.listByExamDetailId(examDetailId, ExamDetailStatusEnum.FINISH.name());
    }

    @Override
    public void updatePaperNumber(List<Long> examDetailCourseIds, String paperNumber, String relatePaperType) {
        UpdateWrapper<ExamDetailCourse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(ExamDetailCourse::getPaperNumber, paperNumber)
                .set(ExamDetailCourse::getPaperType, relatePaperType)
                .in(ExamDetailCourse::getId, examDetailCourseIds);
        this.update(updateWrapper);
    }

    @Override
    public List<ExamDetailCourse> listByExamDetailIdAndStatus(Long examDetailId) {
        return this.baseMapper.listByExamDetailIdAndStatus(examDetailId, ExamStatusEnum.SUBMIT.name());
    }

    @Override
    public List<BasicCourse> listCoursesByPrintPlanId(String param, List<Long> printPlanIdList) {
        return this.baseMapper.listCoursesByPrintPlanId(SystemConstant.translateSpecificSign(param), printPlanIdList);
    }

    @Override
    public List<String> listPaperNumberByPrintPlanId(String param, List<Long> printPlanIdList, Set<Long> orgIds) {
        return this.baseMapper.listPaperNumberByPrintPlanId(SystemConstant.translateSpecificSign(param), printPlanIdList, orgIds);
    }

    @Override
    public List<ExamDetailCourse> listByPrintPlanIdAndCourseCodeAndPaperNumber(Long schoolId, Long printPlanId, String courseCode, String paperNumber) {
        return this.baseMapper.listByPrintPlanIdAndCourseCodeAndPaperNumber(schoolId, printPlanId, courseCode, paperNumber);
    }

    @Override
    public List<ExamDetailCourse> listByCourseCodeAndPaperNumber(Long schoolId, String courseCode, String paperNumber) {
        return this.baseMapper.listByAndCourseCodeAndPaperNumber(schoolId, courseCode, paperNumber);
    }
}
