package com.qmth.teachcloud.mark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.qmth.teachcloud.mark.entity.MarkHeaderTrack;
import com.qmth.teachcloud.mark.mapper.MarkHeaderTrackMapper;
import com.qmth.teachcloud.mark.service.MarkHeaderTrackService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 组长轨迹给分表 服务实现类
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@Service
public class MarkHeaderTrackServiceImpl extends MppServiceImpl<MarkHeaderTrackMapper, MarkHeaderTrack> implements MarkHeaderTrackService {

    @Override
    public List<MarkHeaderTrack> listByExamAndPaperNumberAndGroupNumberAndStudentId(Long examId, String paperNumber, Integer groupNumber, Long studentId) {
        QueryWrapper<MarkHeaderTrack> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MarkHeaderTrack::getExamId, examId)
                .eq(MarkHeaderTrack::getPaperNumber, paperNumber)
                .eq(MarkHeaderTrack::getGroupNumber, groupNumber)
                .eq(MarkHeaderTrack::getStudentId, studentId)
                .orderByAsc(MarkHeaderTrack::getNumber);
        return this.list(queryWrapper);
    }

    @Override
    public List<MarkHeaderTrack> listByStudentIdAndQuestionNumber(Long studentId, String questionNumber) {
        QueryWrapper<MarkHeaderTrack> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MarkHeaderTrack::getStudentId, studentId)
                .eq(MarkHeaderTrack::getQuestionNumber, questionNumber)
                .orderByAsc(MarkHeaderTrack::getNumber);
        return this.list(queryWrapper);
    }

    @Override
    public void deleteByExamIdAndPaperNumberAndGroupNumberAndStudentId(Long examId, String paperNumber, Integer groupNumber, Long studentId) {
        UpdateWrapper<MarkHeaderTrack> updateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<MarkHeaderTrack> lambdaUpdateWrapper = updateWrapper.lambda();
        lambdaUpdateWrapper.eq(MarkHeaderTrack::getExamId, examId)
                .eq(MarkHeaderTrack::getPaperNumber, paperNumber)
                .eq(MarkHeaderTrack::getGroupNumber, groupNumber);
        if (studentId != null) {
            lambdaUpdateWrapper.eq(MarkHeaderTrack::getStudentId, studentId);
        }
        this.remove(updateWrapper);
    }

    @Override
    public void deleteByStudentId(Long studentId) {
        UpdateWrapper<MarkHeaderTrack> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MarkHeaderTrack::getStudentId, studentId);
        this.remove(updateWrapper);
    }

    @Override
    public void deleteByExamIdAndPaperNumberAndGroupNumberAndStudentIdAndQuestionNumber(Long examId, String paperNumber, Integer groupNumber, Long studentId, String questionNumber) {
        UpdateWrapper<MarkHeaderTrack> updateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<MarkHeaderTrack> lambdaUpdateWrapper = updateWrapper.lambda();
        lambdaUpdateWrapper.eq(MarkHeaderTrack::getExamId, examId)
                .eq(MarkHeaderTrack::getPaperNumber, paperNumber)
                .eq(MarkHeaderTrack::getGroupNumber, groupNumber)
                .eq(MarkHeaderTrack::getStudentId, studentId)
                .eq(MarkHeaderTrack::getQuestionNumber, questionNumber);
        this.remove(updateWrapper);
    }

}
