| | |
| | | |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.gkhy.exam.common.annotation.DataScope; |
| | | 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; |
| | |
| | | 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; |
| | | import java.util.stream.Collectors; |
| | | |
| | | |
| | | /** |
| | |
| | | @Autowired |
| | | private SysUserRoleMapper userRoleMapper; |
| | | |
| | | @Autowired |
| | | private ExStudentService exStudentService; |
| | | |
| | | @Override |
| | | public CommonPage<SysUser> selectUserList(SysUser user) { |
| | | SysUser currentUser=SecurityUtils.getLoginUser().getUser(); |
| | | List<SysUser> users=new ArrayList<>(); |
| | | if(!currentUser.getUserType().equals(UserTypeEnum.WORKSHOP_USER.getCode())){ |
| | | if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){ |
| | | SysUser currentUser = SecurityUtils.getLoginUser().getUser(); |
| | | List<SysUser> users = new ArrayList<>(); |
| | | if (!currentUser.getUserType().equals(UserTypeEnum.WORKSHOP_USER.getCode())) { |
| | | if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) { |
| | | user.setCompanyId(currentUser.getCompanyId()); |
| | | Map<String,Object> paramsMap=new HashMap<>(); |
| | | Map<String, Object> paramsMap = new HashMap<>(); |
| | | // paramsMap.put("userType",currentUser.getUserType()); |
| | | user.setParams(paramsMap); |
| | | } |
| | | PageUtils.startPage(); |
| | | users=baseMapper.userList(user); |
| | | users = baseMapper.userList(user); |
| | | } |
| | | return CommonPage.restPage(users); |
| | | } |
| | | |
| | | /** |
| | | * 根据条件分页查询已分配用户角色列表 |
| | | * |
| | |
| | | * @return 用户信息集合信息 |
| | | */ |
| | | @Override |
| | | @DataScope( userAlias = "u") |
| | | public List<SysUser> selectAllocatedList(SysUser user) |
| | | { |
| | | @DataScope(userAlias = "u") |
| | | public List<SysUser> selectAllocatedList(SysUser user) { |
| | | return baseMapper.selectAllocatedList(user); |
| | | } |
| | | |
| | |
| | | * @return 用户信息集合信息 |
| | | */ |
| | | @Override |
| | | @DataScope( userAlias = "u") |
| | | public List<SysUser> selectUnallocatedList(SysUser user) |
| | | { |
| | | @DataScope(userAlias = "u") |
| | | public List<SysUser> selectUnallocatedList(SysUser user) { |
| | | return baseMapper.selectUnallocatedList(user); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public SysUser selectUserByUsername(String username) { |
| | | String key=redisUtils.generateKey(CacheConstant.SYS_ADMIN_USER_NAME+username); |
| | | SysUser sysUser =null; |
| | | if(redisUtils.hasKey(key)){ |
| | | sysUser= (SysUser) redisUtils.get(key); |
| | | }else { |
| | | String key = redisUtils.generateKey(CacheConstant.SYS_ADMIN_USER_NAME + username); |
| | | SysUser sysUser = null; |
| | | if (redisUtils.hasKey(key)) { |
| | | sysUser = (SysUser) redisUtils.get(key); |
| | | } else { |
| | | sysUser = baseMapper.getUserByUsername(username); |
| | | redisUtils.set(key,sysUser,10, TimeUnit.MINUTES); |
| | | redisUtils.set(key, sysUser, 10, TimeUnit.MINUTES); |
| | | } |
| | | return sysUser; |
| | | } |
| | | |
| | | public void delCacheByUsername(String username){ |
| | | String key=redisUtils.generateKey(CacheConstant.SYS_ADMIN_USER_NAME+username); |
| | | public void delCacheByUsername(String username) { |
| | | String key = redisUtils.generateKey(CacheConstant.SYS_ADMIN_USER_NAME + username); |
| | | redisUtils.del(key); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public int deleteUserById(Long userId) { |
| | | SysUser user=checkUserDataScope(userId); |
| | | if (user.getId().toString().equals("1")){ |
| | | SysUser user = checkUserDataScope(userId); |
| | | if (user.getId().toString().equals("1")) { |
| | | throw new ApiException("管理员用户不能被删除"); |
| | | } |
| | | delCacheByUsername(user.getUsername()); |
| | |
| | | } |
| | | |
| | | @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); |
| | | int row = baseMapper.insert(user); |
| | | |
| | | if(row<1){ |
| | | 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 |
| | |
| | | checkUserAllowed(user); |
| | | user.setUpdateBy(SecurityUtils.getUsername()); |
| | | user.setPassword(null); |
| | | int row=baseMapper.updateById(user); |
| | | if(row<1){ |
| | | int row = baseMapper.updateById(user); |
| | | batchSaveRole(user.getRoles(), user.getId(), true); |
| | | |
| | | if (row < 1) { |
| | | throw new ApiException("更新用户信息失败"); |
| | | } |
| | | delCacheByUsername(user.getUsername()); |
| | | return row; |
| | | } |
| | | |
| | | public void checkRequestData(SysUser user){ |
| | | if(!user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())&&user.getCompanyId()==null){ |
| | | public void checkRequestData(SysUser user) { |
| | | if (!user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode()) && user.getCompanyId() == null) { |
| | | throw new ApiException("所属公司不能为空"); |
| | | } |
| | | if(!checkUsernameUnique(user)){ |
| | | if (!checkUsernameUnique(user)) { |
| | | throw new ApiException("登录账号已存在"); |
| | | } |
| | | if(StringUtils.isNotBlank(user.getPhone())&&!checkPhoneUnique(user)){ |
| | | if (StringUtils.isNotBlank(user.getPhone()) && !checkPhoneUnique(user)) { |
| | | throw new ApiException("手机号已存在"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public int updateUserStatus(SysUser user) { |
| | | SysUser existUser=checkUserDataScope(user.getId()); |
| | | SysUser su=new SysUser().setId(user.getId()).setStatus(user.getStatus()); |
| | | SysUser existUser = checkUserDataScope(user.getId()); |
| | | SysUser su = new SysUser().setId(user.getId()).setStatus(user.getStatus()); |
| | | su.setUpdateBy(SecurityUtils.getUsername()); |
| | | |
| | | int row= baseMapper.updateById(su); |
| | | if(row<1){ |
| | | int row = baseMapper.updateById(su); |
| | | if (row < 1) { |
| | | throw new ApiException("更新用户状态失败"); |
| | | } |
| | | delCacheByUsername(existUser.getUsername()); |
| | | return row; |
| | | } |
| | | |
| | | @Override |
| | | public int updateSpecialCertificate(SysUser user) { |
| | | SysUser existUser = checkUserDataScope(user.getId()); |
| | | SysUser su = new SysUser().setId(user.getId()).setOperationCertificate(user.getOperationCertificate()); |
| | | su.setUpdateBy(SecurityUtils.getUsername()); |
| | | |
| | | int row = baseMapper.updateById(su); |
| | | if (row < 1) { |
| | | throw new ApiException("更新用户状态失败"); |
| | | } |
| | | return row; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean resetUserPwd(SysUser user) { |
| | | SysUser existUser=checkUserDataScope(user.getId()); |
| | | SysUser su=new SysUser().setId(user.getId()).setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(user.getPassword()))); |
| | | SysUser existUser = checkUserDataScope(user.getId()); |
| | | SysUser su = new SysUser().setId(user.getId()).setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(user.getPassword()))); |
| | | su.setUpdateBy(SecurityUtils.getUsername()); |
| | | delCacheByUsername(existUser.getUsername()); |
| | | return updateById(su); |
| | |
| | | |
| | | @Override |
| | | public boolean checkUsernameUnique(SysUser user) { |
| | | Long userId = user.getId()==null? -1L : user.getId(); |
| | | Long userId = user.getId() == null ? -1L : user.getId(); |
| | | SysUser info = baseMapper.checkLoginNameUnique(user.getUsername()); |
| | | if (info!=null && info.getId().longValue() != userId.longValue()) |
| | | { |
| | | if (info != null && info.getId().longValue() != userId.longValue()) { |
| | | return UserConstant.NOT_UNIQUE; |
| | | } |
| | | return UserConstant.UNIQUE; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> getUserData(Long companyId) { |
| | | List<SysUser> sysUsers = baseMapper.selectList(new LambdaQueryWrapper<>( |
| | | SysUser.class |
| | | ).eq(SysUser::getCompanyId, companyId).eq(SysUser::getDelFlag, UserConstant.ENABLE)); |
| | | int totalUser = sysUsers.size(); |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | //总人数 |
| | | map.put("totalUser", totalUser); |
| | | //专业 |
| | | map.put("post", sysUsers.stream() |
| | | .map(SysUser::getPost) |
| | | .filter(Objects::nonNull) |
| | | .distinct() |
| | | .collect(Collectors.joining(","))); |
| | | //学历硕士以上 |
| | | // 统计学历为硕士(4)和博士及以上(5)的人数 |
| | | long count = sysUsers.stream() |
| | | .filter(user -> user.getQualification() != null) |
| | | .filter(user -> user.getQualification() >= 4) // 4: 硕士, 5: 博士及以上 |
| | | .count(); |
| | | map.put("qualification", count); |
| | | |
| | | long qualification1 = sysUsers.stream() |
| | | .filter(user -> user.getQualification() != null) |
| | | .filter(user -> user.getQualification().equals("1")) |
| | | .count(); |
| | | map.put("qualification1", qualification1);//学历1、高中及以下 |
| | | long qualification2 = sysUsers.stream() |
| | | .filter(user -> user.getQualification() != null) |
| | | .filter(user -> user.getQualification().equals("2")) |
| | | .count(); |
| | | map.put("qualification2", qualification2);//学历2、专科 |
| | | long qualification3 = sysUsers.stream() |
| | | .filter(user -> user.getQualification() != null) |
| | | .filter(user -> user.getQualification().equals("3")) |
| | | .count(); |
| | | map.put("qualification3", qualification3);//学历3本科 |
| | | long qualification4 = sysUsers.stream() |
| | | .filter(user -> user.getQualification() != null) |
| | | .filter(user -> user.getQualification().equals("4")) |
| | | .count(); |
| | | map.put("qualification4", qualification4);//学历4硕士 |
| | | |
| | | long qualification5 = sysUsers.stream() |
| | | .filter(user -> user.getQualification() != null) |
| | | .filter(user -> user.getQualification().equals("5")) |
| | | .count(); |
| | | map.put("qualification5", qualification5);//学历5博士及以上 |
| | | |
| | | //高级职称人数 |
| | | long positionalCount = sysUsers.stream() |
| | | .filter(user -> user.getPositional() != null) |
| | | .filter(user -> user.getPositional().equals("3")) // 4: 硕士, 5: 博士及以上 |
| | | .count(); |
| | | map.put("positional3", positionalCount); |
| | | long positionalCount2 = sysUsers.stream() |
| | | .filter(user -> user.getPositional() != null) |
| | | .filter(user -> user.getPositional().equals("2")) // 4: 硕士, 5: 博士及以上 |
| | | .count(); |
| | | map.put("positional2", positionalCount2);//中级 |
| | | long positionalCount1 = sysUsers.stream() |
| | | .filter(user -> user.getPositional() != null) |
| | | .filter(user -> user.getPositional().equals("1")) // 4: 硕士, 5: 博士及以上 |
| | | .count(); |
| | | map.put("positional1", positionalCount1);//初级 |
| | | |
| | | map.put("positional12", positionalCount2 + positionalCount1);//中级+初级 |
| | | |
| | | long countBk = sysUsers.stream() |
| | | .filter(user -> user.getQualification() != null) |
| | | .filter(user -> user.getQualification() >= 3) // 本科以上 |
| | | .count(); |
| | | //本科人数 |
| | | map.put("qualificationBK", countBk); |
| | | double bachelorRatio = totalUser > 0 ? (double) countBk / totalUser * 100 : 0; |
| | | //本科占比 |
| | | map.put("qualificationBKRatio", String.format("%.2f%%", bachelorRatio)); |
| | | |
| | | long countJS = sysUsers.stream() |
| | | .filter(user -> user.getPersonType() != null) |
| | | .filter(user -> user.getPersonType() == 1) // 技术类 |
| | | .count(); |
| | | map.put("personTypeJS", countJS); |
| | | double jsRatio = totalUser > 0 ? (double) countJS / totalUser * 100 : 0; |
| | | map.put("jsRatio", String.format("%.2f%%", jsRatio)); |
| | | double averageAge = sysUsers.stream() |
| | | .filter(user -> user.getAge() != null) // 过滤年龄为null的用户 |
| | | .mapToInt(SysUser::getAge) // 提取年龄值 |
| | | .average() // 计算平均值 |
| | | .orElse(0.0); |
| | | map.put("avgAge", String.format("%.1f", averageAge));//平均年龄 |
| | | |
| | | long countAge40 = sysUsers.stream() |
| | | .filter(user -> user.getAge() != null) |
| | | .filter(user -> user.getAge() >= 40) // 40以上人数 |
| | | .count(); |
| | | map.put("age40", countAge40); |
| | | double age40Ratio = totalUser > 0 ? (double) countAge40 / totalUser * 100 : 0; |
| | | map.put("age40Ratio", String.format("%.2f%%", age40Ratio)); |
| | | long countAge30 = totalUser - countAge40; |
| | | map.put("age1840", countAge30);//18-40 |
| | | double age30Ratio = totalUser > 0 ? (double) countAge30 / totalUser * 100 : 0; |
| | | map.put("age1840Ratio", String.format("%.2f%%", age30Ratio)); |
| | | |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean checkPhoneUnique(SysUser user) { |
| | | Long userId = user.getId()==null ? -1L : user.getId(); |
| | | Long userId = user.getId() == null ? -1L : user.getId(); |
| | | SysUser info = baseMapper.checkPhoneUnique(user.getPhone()); |
| | | if (info!=null && info.getId().longValue() != userId.longValue()) |
| | | { |
| | | if (info != null && info.getId().longValue() != userId.longValue()) { |
| | | return UserConstant.NOT_UNIQUE; |
| | | } |
| | | return UserConstant.UNIQUE; |
| | |
| | | |
| | | @Override |
| | | public void checkUserAllowed(SysUser user) { |
| | | SysUser currentUser=SecurityUtils.getLoginUser().getUser(); |
| | | Integer currentUserType=currentUser.getUserType(); |
| | | Integer userType=user.getUserType(); |
| | | SysUser currentUser = SecurityUtils.getLoginUser().getUser(); |
| | | Integer currentUserType = currentUser.getUserType(); |
| | | Integer userType = user.getUserType(); |
| | | //校验权限,规则:上一级用户可以增加下一级用户类型的用户 |
| | | // if(currentUserType.equals(UserTypeEnum.SYSTEM_USER.getCode())){ |
| | | // if( !userType.equals(UserTypeEnum.SYSTEM_USER.getCode())&&!userType.equals(UserTypeEnum.OTHER_USER.getCode()) &&!userType.equals(UserTypeEnum.COMPANY_USER.getCode())){ |
| | |
| | | // throw new ApiException("管理员用户不能被删除"); |
| | | // } |
| | | // }else{ |
| | | if(!currentUserType.equals(UserTypeEnum.SYSTEM_USER.getCode())){ |
| | | if(userType.equals(UserTypeEnum.OTHER_USER.getCode())){ |
| | | if (!currentUserType.equals(UserTypeEnum.SYSTEM_USER.getCode())) { |
| | | 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){ |
| | | } else { |
| | | if (userType < currentUserType) { |
| | | throw new ApiException("没有权限操作或者更新上级用户类型的用户"); |
| | | } |
| | | } |
| | |
| | | |
| | | @Override |
| | | public SysUser checkUserDataScope(Long userId) { |
| | | if(userId==null){ |
| | | if (userId == null) { |
| | | throw new ApiException("用户id为空!"); |
| | | } |
| | | SysUser user = getById(userId); |
| | | if (ObjectUtil.isNull(user)) |
| | | { |
| | | if (ObjectUtil.isNull(user)) { |
| | | throw new ApiException("用户数据不存在!"); |
| | | } |
| | | checkUserAllowed(user); |