UserRoleServiceImpl.java 5.57 KB
package com.meishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meishu.common.exception.HttpException;
import com.meishu.dto.sms.VerifyCodeDTO;
import com.meishu.dto.userrole.AccountLoginDTO;
import com.meishu.dto.userrole.AddStudentsDTO;
import com.meishu.dto.userrole.QueryStudentsDTO;
import com.meishu.mapper.UserRoleCopy1Mapper;
import com.meishu.model.*;
import com.meishu.mapper.UserRoleMapper;
import com.meishu.service.ClassesDictService;
import com.meishu.service.ClassesUserMappingService;
import com.meishu.service.SmsCodeService;
import com.meishu.service.UserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.util.excel.ExcelUtil;
import com.meishu.vo.userrole.QueryStudentsVO;
import com.meishu.vo.userrole.UserInfoVO;
import com.meishu.vo.userrole.UserRoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2021-08-16
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleDO> implements UserRoleService {

    @Autowired
    private SmsCodeService smsCodeService;

    @Autowired
    private ClassesDictService classesDictService;

    @Autowired
    private UserRoleCopy1Mapper userRoleCopy1Mapper;

    public IPage<QueryStudentsVO> queryStudents(QueryStudentsDTO queryStudentsDTO){
        Page pager = new Page(queryStudentsDTO.getPageNum(), queryStudentsDTO.getPageSize());
        return this.baseMapper.queryStudents(pager,queryStudentsDTO.getUserName(),queryStudentsDTO.getGrade(),queryStudentsDTO.getSession(),queryStudentsDTO.getUserStatus());
    }

    public String addStudents(UserRoleDO userRoleDO){
        userRoleDO.setUserStatus("在读");
        userRoleDO.setRoleType("学生");
        this.baseMapper.insert(userRoleDO);
        return ConstantUtils.ADD_SUCCESS;
    }

    public String updateStudent(UserRoleDO userRoleDO){
        this.baseMapper.updateById(userRoleDO);
        return ConstantUtils.SUCCESS_UPDATE;
    }

    public List<QueryStudentsVO> exportStudents(QueryStudentsDTO queryStudentsDTO){
        return this.baseMapper.exportStudents(queryStudentsDTO.getUserName(),queryStudentsDTO.getGrade(),queryStudentsDTO.getSession(),queryStudentsDTO.getUserStatus());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserRoleVO login(VerifyCodeDTO verifyCodeDTO) {
        UserRoleVO userRoleVO = new UserRoleVO();
        UserRoleDO userRoleDO = this.baseMapper.selectOne(new QueryWrapper<UserRoleDO>()
                .lambda()
                .eq(UserRoleDO::getPhone, verifyCodeDTO.getPhone()));
        if(userRoleDO == null) {
            throw new HttpException(10010);
        }
        BeanUtils.copyProperties(userRoleDO, userRoleVO);

        SmsCodeDO smsCodeDO = smsCodeService.getOne(new QueryWrapper<SmsCodeDO>()
                .lambda()
                .eq(SmsCodeDO::getTelephone, verifyCodeDTO.getPhone())
                .eq(SmsCodeDO::getCode, verifyCodeDTO.getVerifyCode()));
        if(smsCodeDO == null) {
            throw new HttpException(70010);
        }

        smsCodeService.removeById(smsCodeDO.getId());
        return userRoleVO;
    }

    @Override
    public UserInfoVO userInfo(Integer id) {
        UserInfoVO userInfoVO = new UserInfoVO();
        UserRoleDO userRoleDO = this.baseMapper.selectById(id);
        if(userRoleDO != null) {
            BeanUtils.copyProperties(userRoleDO, userInfoVO);
            ClassesDictDO classesDictDO = classesDictService.getClassesById((long)id);
            if(classesDictDO != null) {
                userInfoVO.setClasses(classesDictDO.getClassName());
            }
        }
        return userInfoVO;
    }

    @Override
    public UserRoleVO accountLogin(AccountLoginDTO accountLoginDTO) {
        UserRoleVO userRoleVO = new UserRoleVO();
        UserRoleDO userRoleDO = this.baseMapper.selectOne(new QueryWrapper<UserRoleDO>()
                .lambda()
                .eq(UserRoleDO::getPassword, accountLoginDTO.getPassword())
                .eq(UserRoleDO::getIdCard, accountLoginDTO.getIdCard()));
        if(userRoleDO == null) {
            throw new HttpException(10004);
        }

        BeanUtils.copyProperties(userRoleDO, userRoleVO);
        return userRoleVO;
    }

    public void studycode(){

        List<UserRoleDO> userRoleDOS = this.baseMapper.selectList(null);

        for (UserRoleDO userRoleDO : userRoleDOS){

            try {

                List<UserRoleCopy1DO> userRoleCopy1DO = userRoleCopy1Mapper.selectList(new QueryWrapper<UserRoleCopy1DO>()
                .lambda()
                .eq(UserRoleCopy1DO::getUserName,userRoleDO.getUserName())
                .eq(UserRoleCopy1DO::getGender,userRoleDO.getGender())
                .eq(UserRoleCopy1DO::getSession,userRoleDO.getSession()));

                if (userRoleCopy1DO.size()>0){
                    userRoleDO.setStudyCode(userRoleCopy1DO.get(0).getStudyCode());
                    this.baseMapper.updateById(userRoleDO);
                }
            }catch (Exception e){
                System.out.println("==========="+userRoleDO.getId()+"==="+userRoleDO.getUserName());
            }


        }


    }

}