heheng
2 天以前 826976ff37a83fc8fec64aeb19727d69afc6fcfa
multi-system/src/main/java/com/gkhy/exam/system/service/impl/SysUserServiceImpl.java
@@ -7,6 +7,7 @@
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.constant.CacheConstant;
import com.gkhy.exam.common.constant.UserConstant;
import com.gkhy.exam.common.domain.entity.SysRole;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.enums.UserTypeEnum;
import com.gkhy.exam.common.exception.ApiException;
@@ -14,19 +15,19 @@
import com.gkhy.exam.common.utils.RedisUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.common.utils.StringUtils;
import com.gkhy.exam.system.domain.ExStudent;
import com.gkhy.exam.system.domain.SysUserRole;
import com.gkhy.exam.system.mapper.SysUserMapper;
import com.gkhy.exam.system.mapper.SysUserRoleMapper;
import com.gkhy.exam.system.service.ExStudentService;
import com.gkhy.exam.system.service.SysConfigService;
import com.gkhy.exam.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -49,6 +50,9 @@
    private Validator validator;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private ExStudentService exStudentService;
    @Override
    public CommonPage<SysUser> selectUserList(SysUser user) {
@@ -132,23 +136,54 @@
    }
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int addUser(SysUser user) {
        ExStudent exStudent = new ExStudent();
        exStudent.setCompanyId(user.getCompanyId());
        exStudent.setName(user.getName());
        exStudent.setPassword(user.getPassword());
        exStudent.setDeptId(user.getDeptId());
        exStudent.setDuty(user.getDuty());
        exStudent.setSex(user.getSex());
        exStudent.setPhone(user.getPhone());
        checkRequestData(user);
        checkUserAllowed(user);
        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(user.getPassword())));
        int row=baseMapper.insert(user);
        List<SysUserRole> userRoleList = new ArrayList<>();
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(user.getId());
        sysUserRole.setRoleId(2L);
        userRoleList.add(sysUserRole);
        userRoleMapper.batchUserRole(userRoleList);
        batchSaveRole(user.getRoles(),user.getId(),false);
        exStudentService.insertStudent(exStudent);
        if(row<1){
            throw new ApiException("新增用户失败");
        }
        return row;
    }
    public void batchSaveRole(List<SysRole> roleIds, Long userId, boolean isUpdate){
        if (isUpdate){
            userRoleMapper.deleteUserRoleByUserId(userId);
        }
        List<SysUserRole> userRoleList = new ArrayList<>();
        if(roleIds != null && !roleIds.isEmpty()){
            for (SysRole roleId : roleIds) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(userId);
                sysUserRole.setRoleId(roleId.getRoleId());
                userRoleList.add(sysUserRole);
            }
        }else {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(2L);
            userRoleList.add(sysUserRole);
        }
        userRoleMapper.batchUserRole(userRoleList);
    }
    @Override
@@ -158,6 +193,8 @@
        user.setUpdateBy(SecurityUtils.getUsername());
        user.setPassword(null);
        int row=baseMapper.updateById(user);
        batchSaveRole(user.getRoles(),user.getId(),true);
        if(row<1){
            throw new ApiException("更新用户信息失败");
        }
@@ -242,12 +279,12 @@
            if(userType.equals(UserTypeEnum.OTHER_USER.getCode())){
                throw new ApiException("没有权限操作或者更新上级用户类型的用户");
            }
            if(currentUserType.equals(UserTypeEnum.OTHER_USER.getCode())){
                if(userType<=UserTypeEnum.COMPANY_USER.getCode()){
            if(currentUserType.equals(UserTypeEnum.OTHER_USER.getCode()) || currentUserType.equals(UserTypeEnum.COMPANY_ADMIN.getCode())){
                if(userType<=UserTypeEnum.COMPANY_USER.getCode() && !currentUserType.equals(UserTypeEnum.COMPANY_ADMIN.getCode())){
                    throw new ApiException("没有权限操作或者更新上级用户类型的用户");
                }
            }else{
                if(userType<=currentUserType){
                if(userType<currentUserType){
                    throw new ApiException("没有权限操作或者更新上级用户类型的用户");
                }
            }