BatchDictServiceImpl.java 5.93 KB
package com.meishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meishu.common.exception.HttpException;
import com.meishu.dto.batch.AddBatchDTO;
import com.meishu.mapper.BatchExamMapper;
import com.meishu.mapper.BatchExamsStudentsMapper;
import com.meishu.mapper.BatchStudentMappingMapper;
import com.meishu.model.BatchDictDO;
import com.meishu.mapper.BatchDictMapper;
import com.meishu.model.BatchExamDO;
import com.meishu.model.BatchExamStudentsDO;
import com.meishu.model.BatchStudentMappingDO;
import com.meishu.service.BatchDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.vo.batch.BatchesVO;
import com.meishu.vo.student.BatchStudentsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 批次字典表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2023-07-12
 */
@Service
public class BatchDictServiceImpl extends ServiceImpl<BatchDictMapper, BatchDictDO> implements BatchDictService {

    @Autowired
    private BatchExamMapper batchExamMapper;

    @Autowired
    private BatchStudentMappingMapper batchStudentMappingMapper;

    @Autowired
    private BatchExamsStudentsMapper batchExamsStudentsMapper;

    public String addBatch(AddBatchDTO addBatchDTO) {

        int count = this.baseMapper.selectCount(new QueryWrapper<BatchDictDO>()
                .lambda()
                .eq(BatchDictDO::getBatchName, addBatchDTO.getBatchName()));

        if (count > 0) {
            throw new HttpException(40001);
        }

        BatchDictDO batchDictDO = new BatchDictDO();
        BeanUtils.copyProperties(addBatchDTO,batchDictDO);
        this.baseMapper.insert(batchDictDO);
        List<BatchStudentMappingDO> batchExamStudentsDOS = addBatchDTO.getStudentIds();
        for (BatchStudentMappingDO besd : batchExamStudentsDOS) {
            besd.setBatchId(batchDictDO.getId());
            batchStudentMappingMapper.insert(besd);

            //批次下的考试
            List<BatchExamDO> batchExamDOS = batchExamMapper.selectList(new QueryWrapper<BatchExamDO>()
            .lambda()
            .eq(BatchExamDO::getBatchId,batchDictDO.getId()));
            for (BatchExamDO batchExamDO : batchExamDOS){
                BatchExamStudentsDO batchExamStudentsDO = new BatchExamStudentsDO();
                batchExamStudentsDO.setBatchExamId(batchExamDO.getId());
                batchExamStudentsDO.setStudentId(besd.getStudentId());
                batchExamStudentsDO.setExamCode(besd.getExamCode());
                batchExamMapper.insert(batchExamDO);
            }

        }
        return ConstantUtils.ADD_SUCCESS;
    }

    public String updateBatch(AddBatchDTO addBatchDTO) {

        int count = this.baseMapper.selectCount(new QueryWrapper<BatchDictDO>()
                .lambda()
                .eq(BatchDictDO::getBatchName, addBatchDTO.getBatchName())
                .ne(BatchDictDO::getId, addBatchDTO.getId()));

        if (count > 0) {
            throw new HttpException(40001);
        }

        BatchDictDO batchDictDO = new BatchDictDO();
        BeanUtils.copyProperties(addBatchDTO,batchDictDO);

        this.baseMapper.updateById(batchDictDO);

        //先删
        batchStudentMappingMapper.delete(new QueryWrapper<BatchStudentMappingDO>()
        .lambda()
        .eq(BatchStudentMappingDO::getBatchId,batchDictDO.getId()));

        //批次下的考试
        List<BatchExamDO> batchExamDOS = batchExamMapper.selectList(new QueryWrapper<BatchExamDO>()
                .lambda()
                .eq(BatchExamDO::getBatchId,batchDictDO.getId()));
        for (BatchExamDO batchExamDO : batchExamDOS){
            batchExamsStudentsMapper.delete(new QueryWrapper<BatchExamStudentsDO>()
            .lambda()
            .eq(BatchExamStudentsDO::getBatchExamId,batchExamDO.getId()));
        }

        //后增
        List<BatchStudentMappingDO> batchStudentMappingDOS = addBatchDTO.getStudentIds();

        for (BatchStudentMappingDO batchStudentMappingDO : batchStudentMappingDOS){
            batchStudentMappingDO.setBatchId(batchDictDO.getId());
            batchStudentMappingMapper.insert(batchStudentMappingDO);
            for (BatchExamDO batchExamDO : batchExamDOS){
                BatchExamStudentsDO batchExamStudentsDO = new BatchExamStudentsDO();
                batchExamStudentsDO.setBatchExamId(batchExamDO.getId());
                batchExamStudentsDO.setStudentId(batchStudentMappingDO.getStudentId());
                batchExamStudentsDO.setExamCode(batchExamStudentsDO.getExamCode());
                batchExamsStudentsMapper.insert(batchExamStudentsDO);
            }
        }
        return ConstantUtils.SET_SUCCESS;
    }

    public String deleteBatch(BatchDictDO batchDictDO){

        //当前批次下有考试 的话删不掉

        Integer count = batchExamMapper.selectCount(new QueryWrapper<BatchExamDO>()
                .lambda()
                .eq(BatchExamDO::getBatchId, batchDictDO.getId()));

        if (count>0){
            throw new HttpException(50001);
        }

        this.baseMapper.deleteById(batchDictDO.getId());
        return ConstantUtils.DELETE_SUCCESS;
    }

    public List<BatchesVO> batches(){

        List<BatchesVO> batchesVOS = new ArrayList<>();

        List<BatchDictDO> batchDictDOS = this.baseMapper.selectList(null);
        for (BatchDictDO batchDictDO : batchDictDOS){
            BatchesVO batchesVO =  new BatchesVO();
            BeanUtils.copyProperties(batchDictDO,batchesVO);
            //查看 该批次下的学生
            List<BatchStudentsVO> batchStudentsVOS = batchStudentMappingMapper.batchStudents(batchDictDO.getId());
            batchesVO.setBatchStudentsVOS(batchStudentsVOS);
            batchesVOS.add(batchesVO);
        }

        return batchesVOS;
    }

}