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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.marking.Marker;
import com.qmth.distributed.print.business.entity.ExamPaperGroupMarker;
import com.qmth.distributed.print.business.mapper.ExamPaperGroupMarkerMapper;
import com.qmth.distributed.print.business.service.ExamPaperGroupMarkerService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description: 评卷员-分组关系服务实现类
 * @Author: CaoZixuan
 * @Date: 2022-04-13
 */
@Service
public class ExamPaperGroupMarkerServiceImpl extends ServiceImpl<ExamPaperGroupMarkerMapper,ExamPaperGroupMarker> implements ExamPaperGroupMarkerService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveExamPaperGroupMarkerByGroupId(Long examPaperGroupId, List<Marker> markerList) {
        Set<ExamPaperGroupMarker> examPaperGroupMarkerSet = markerList.stream().flatMap(e -> {
            ExamPaperGroupMarker marker = new ExamPaperGroupMarker();
            marker.setGroupId(examPaperGroupId);
            marker.setMarkerId(e.getId());
            String loginName = e.getLoginName();
            if (!SystemConstant.strNotNull(loginName)){
                throw ExceptionResultEnum.ERROR.exception("缺少评卷员用户登录名信息");
            }
            marker.setLoginName(loginName);
            return Stream.of(marker);
        }).collect(Collectors.toSet());
        this.saveBatch(examPaperGroupMarkerSet);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteExamPaperGroupMarkerService(Set<Long> groupIdSet) {
        if (groupIdSet.size() > 0) {
            this.remove(new QueryWrapper<ExamPaperGroupMarker>().lambda().in(ExamPaperGroupMarker::getGroupId, groupIdSet));
        }
    }

    @Override
    public List<ExamPaperGroupMarker> listByGroupId(Long id) {
        QueryWrapper<ExamPaperGroupMarker> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExamPaperGroupMarker::getGroupId, id);
        return this.baseMapper.selectList(queryWrapper);
    }
}
