StudentServiceImpl.java 4.64 KB
package com.meishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meishu.common.exception.HttpException;
import com.meishu.dto.student.LoginDTO;
import com.meishu.mapper.BatchDictMapper;
import com.meishu.mapper.BatchExamsStudentsMapper;
import com.meishu.mapper.BatchStudentMappingMapper;
import com.meishu.model.BatchDictDO;
import com.meishu.model.BatchStudentMappingDO;
import com.meishu.model.StudentDO;
import com.meishu.mapper.StudentMapper;
import com.meishu.model.BatchExamStudentsDO;
import com.meishu.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.vo.student.LoginVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2023-07-11
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentDO> implements StudentService {

    @Autowired
    private BatchExamsStudentsMapper batchExamsStudentsMapper;

    @Autowired
    private BatchStudentMappingMapper batchStudentMappingMapper;

    @Autowired
    private BatchDictMapper batchDictMapper;

    public LoginVO login(LoginDTO loginDTO) throws Exception{

        StudentDO studentDO1 = this.baseMapper.selectOne(new QueryWrapper<StudentDO>()
                .lambda()
                .eq(StudentDO::getIdCard, loginDTO.getIdCard()));

        if (null == studentDO1){
            throw new HttpException(10001);
        }

        BatchStudentMappingDO batchStudentMappingDO = batchStudentMappingMapper.selectOne(new QueryWrapper<BatchStudentMappingDO>()
                .lambda()
                .eq(BatchStudentMappingDO::getStudentId, studentDO1.getId())
                .eq(BatchStudentMappingDO::getExamCode, loginDTO.getExamCode()));

        if (null == batchStudentMappingDO) {
            throw new HttpException(10001);
        }

        BatchDictDO batchDictDO = batchDictMapper.selectById(batchStudentMappingDO.getBatchId());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (simpleDateFormat.parse(batchDictDO.getStartDate()).after(new Date())||simpleDateFormat.parse(batchDictDO.getEndDate()).before(new Date())){
            throw new HttpException(10004);
        }

        //签到 取第一次
        if (null == batchStudentMappingDO.getSignDate()){
            batchStudentMappingDO.setSignDate(new Date());
            batchStudentMappingMapper.updateById(batchStudentMappingDO);
        }

        LoginVO loginVO = new LoginVO();
        loginVO.setId(studentDO1.getId());
        loginVO.setUserName(studentDO1.getUserName());
        loginVO.setExamCode(batchStudentMappingDO.getExamCode());
        loginVO.setCompanyName(studentDO1.getCompanyName());
        loginVO.setBatchId(batchStudentMappingDO.getBatchId());
        return loginVO;
    }

    public List<StudentDO> students(StudentDO studentDO) {
        return this.baseMapper.students(studentDO.getUserName());
    }

    public String deleteStudent(StudentDO studentDO) {

        Integer count = batchExamsStudentsMapper.selectCount(new QueryWrapper<BatchExamStudentsDO>()
                .lambda()
                .eq(BatchExamStudentsDO::getStudentId, studentDO.getId()));
        if (count > 0) {
            throw new HttpException(20001);
        }
        this.baseMapper.deleteById(studentDO.getId());
        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateStudent(StudentDO studentDO) {

        StudentDO studentDO1 = this.baseMapper.selectById(studentDO.getId());

        if (StringUtils.isNotEmpty(studentDO.getIdCard())) {
            studentDO1.setIdCard(studentDO.getIdCard());
        }

        if (StringUtils.isNotEmpty(studentDO.getCompanyName())) {
            studentDO1.setCompanyName(studentDO.getCompanyName());
        }

        if (StringUtils.isNotEmpty(studentDO.getUserName())) {
            studentDO1.setUserName(studentDO.getUserName());
        }
        this.baseMapper.updateById(studentDO1);
        return ConstantUtils.SET_SUCCESS;
    }

    public String addStudent(StudentDO studentDO) {

        int coount = this.baseMapper.selectCount(new QueryWrapper<StudentDO>()
                .lambda()
                .eq(StudentDO::getIdCard, studentDO.getIdCard()));

        if (coount > 0) {
            throw new HttpException(10002);
        }

        this.baseMapper.insert(studentDO);
        return ConstantUtils.ADD_SUCCESS;
    }

}