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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meishu.common.exception.HttpException;
import com.meishu.dto.course.GetExercisesByTreeDTO;
import com.meishu.dto.student.*;
import com.meishu.dto.subject.BatchStatusOprDTO;
import com.meishu.mapper.*;
import com.meishu.model.*;
import com.meishu.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.util.MathUtil;
import com.meishu.util.RandomUtil;
import com.meishu.util.SMSUtils;
import com.meishu.vo.course.*;
import com.meishu.vo.grade.DimensionStarVO;
import com.meishu.vo.grade.QueryOneGradeVO;
import com.meishu.vo.rule.GetOneExaminationDetailVO;
import com.meishu.vo.rule.GetStudentRulesVO;
import com.meishu.vo.rule.StudentStarVO;
import com.meishu.vo.student.*;
import com.meishu.vo.voddict.GetCourseTreesVodsVO;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 白名单 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2021-04-25
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentDO> implements StudentService {

    @Autowired
    private SmsCodeMapper smsCodeMapper;


    public String sendMsg(StudentDO studentDO) {

        //验证是否是白名单用户
        Integer count = this.baseMapper.selectCount(new QueryWrapper<StudentDO>()
                .lambda()
                .eq(StudentDO::getTelephone, studentDO.getTelephone()));

        StudentDO studentDO1 = this.baseMapper.getStudentInfo(studentDO.getTelephone());

        if (count == 0 && null == studentDO1) {
            throw new HttpException(10010);
        }

        String verifyCode = RandomUtil.getRandomCode(6);
        String param = "{ \"code\":\"" + verifyCode + "\"}";

        //插入短信验证码
        smsCodeMapper.delete(new QueryWrapper<SmsCodeDO>()
                .lambda()
                .eq(SmsCodeDO::getTelephone, studentDO.getTelephone()));

        SmsCodeDO smsCodeDO = new SmsCodeDO();
        smsCodeDO.setTelephone(studentDO.getTelephone());
        smsCodeDO.setCode(verifyCode);
        smsCodeMapper.insert(smsCodeDO);
        return SMSUtils.sendVerifySMS(ConstantUtils.TEMPLATE_CODE, smsCodeDO.getTelephone(), param);
    }

    public LoginVO login(SmsCodeDO smsCodeDO) {

        LoginVO loginVO = new LoginVO();

        Integer count = smsCodeMapper.selectCount(new QueryWrapper<SmsCodeDO>()
                .lambda()
                .eq(SmsCodeDO::getTelephone, smsCodeDO.getTelephone())
                .eq(SmsCodeDO::getCode, smsCodeDO.getCode())
                .gt(SmsCodeDO::getCreateDate, LocalDateTime.now().minusMinutes(10L))
        );

        //提交小程序,验证专用
        if ("15666666666".equals(smsCodeDO.getTelephone())) {
            count = 1;
        }

        if (count > 0) {
            StudentDO studentDO = this.baseMapper.selectOne(new QueryWrapper<StudentDO>()
                    .lambda()
                    .eq(StudentDO::getTelephone, smsCodeDO.getTelephone()));
            if (smsCodeDO == null) {
                throw new HttpException(10013);
            }
            //该手机号是学生账号
            if (null != studentDO) {
                BeanUtils.copyProperties(studentDO, loginVO);

                loginVO.setIsStudent("1");
            } else {
                StudentDO studentDO2 = this.baseMapper.getStudentInfo(smsCodeDO.getTelephone());
                loginVO.setUsername(studentDO2.getUsername());
                List<SubjectReportVO> subjectReportVOS = this.baseMapper.getSubjectReport(studentDO2.getId());
                loginVO.setSubjectReportVOS(subjectReportVOS);
                loginVO.setId(studentDO2.getId());
                loginVO.setIsStudent("0");
            }
            return loginVO;
        } else {
            throw new HttpException(10003);
        }
    }

    public String addStudent(StudentDO studentDO) {

        Integer count = this.baseMapper.selectCount(new QueryWrapper<StudentDO>()
        .lambda()
        .eq(StudentDO::getTelephone,studentDO.getTelephone()));

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

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

    public String updateStudent(StudentDO studentDO) {
        this.baseMapper.updateById(studentDO);
        return ConstantUtils.SUCCESS_UPDATE;
    }

    @Transactional(rollbackFor = Exception.class)
    public String batchUpdateStudent(BatchStatusOprDTO batchStatusOprDTO) {
        List<Long> longs = batchStatusOprDTO.getIds();
        for (Long lg : longs) {
            StudentDO studentDO = new StudentDO();
            studentDO.setStatus(batchStatusOprDTO.getStatus());
            studentDO.setId(lg);
            this.baseMapper.updateById(studentDO);
        }
        return ConstantUtils.SUCCESS_UPDATE;
    }

    public String deleteStudent(DeleteStudentDTO deleteStudentDTO) {

        List<Long> ids = deleteStudentDTO.getIds();
        for (Long id : ids) {
            this.baseMapper.deleteById(id);
        }
        return ConstantUtils.DELETE_SUCCESS;
    }

    public QueryStudentVO queryStudent(StudentDO studentDO) {
        QueryStudentVO queryStudentVO = new QueryStudentVO();
        StudentDO studentDO1 = this.baseMapper.selectById(studentDO.getId());
        BeanUtils.copyProperties(studentDO1, queryStudentVO);
        return queryStudentVO;
    }


}