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

import java.util.List;

import javax.validation.constraints.NotNull;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.teachcloud.mark.entity.ScanStudentPaper;
import com.qmth.teachcloud.mark.mapper.ScanStudentPaperMapper;
import com.qmth.teachcloud.mark.service.ScanStudentPaperService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@Service
public class ScanStudentPaperServiceImpl extends ServiceImpl<ScanStudentPaperMapper, ScanStudentPaper>
        implements ScanStudentPaperService {

    @Override
    public List<ScanStudentPaper> findByStudentId(Long studentId) {
        QueryWrapper<ScanStudentPaper> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ScanStudentPaper> lw = wrapper.lambda();
        lw.eq(ScanStudentPaper::getStudentId, studentId);
        lw.orderByAsc(ScanStudentPaper::getPaperIndex);
        return this.list(wrapper);
    }

    @Override
    public ScanStudentPaper findByPaperId(Long paperId) {
        QueryWrapper<ScanStudentPaper> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ScanStudentPaper> lw = wrapper.lambda();
        lw.eq(ScanStudentPaper::getPaperId, paperId);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<ScanStudentPaper> findByPaperIds(List<Long> paperIds) {
        QueryWrapper<ScanStudentPaper> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ScanStudentPaper> lw = wrapper.lambda();
        lw.in(ScanStudentPaper::getPaperId, paperIds);
        return this.list(wrapper);
    }

    @Transactional
    @Override
    public int removeByStudentId(Long studentId) {
        QueryWrapper<ScanStudentPaper> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ScanStudentPaper> lw = wrapper.lambda();
        lw.eq(ScanStudentPaper::getStudentId, studentId);
        return this.baseMapper.delete(wrapper);
    }

    @Override
    public ScanStudentPaper findByStudentIdAndPaperNumber(Long studentId, Integer paperNumber) {
        QueryWrapper<ScanStudentPaper> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ScanStudentPaper> lw = wrapper.lambda();
        lw.eq(ScanStudentPaper::getStudentId, studentId);
        lw.eq(ScanStudentPaper::getPaperIndex, paperNumber);
        return baseMapper.selectOne(wrapper);
    }

    @Transactional
    @Override
    public void removeByStudentIdAndPaperId(@NotNull Long studentId, @NotNull Long paperId) {
        QueryWrapper<ScanStudentPaper> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ScanStudentPaper> lw = wrapper.lambda();
        lw.eq(ScanStudentPaper::getStudentId, studentId);
        lw.eq(ScanStudentPaper::getPaperId, paperId);
        this.baseMapper.delete(wrapper);
    }
}
