package com.ruoyi.system.service.impl;
|
|
import com.ruoyi.common.basic.PageInfo;
|
import com.ruoyi.common.constant.UserConstants;
|
import com.ruoyi.common.core.domain.entity.SysRole;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.exception.ServiceException;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.system.dto.SysPlatformAdminAddRequest;
|
import com.ruoyi.system.dto.SysPlatformAdminDeleteRequest;
|
import com.ruoyi.system.dto.SysPlatformAdminDetailRequest;
|
import com.ruoyi.system.dto.SysPlatformAdminEditRequest;
|
import com.ruoyi.system.dto.SysPlatformAdminPageRequest;
|
import com.ruoyi.system.dto.SysPlatformAdminResetPasswordRequest;
|
import com.ruoyi.system.dto.SysPlatformAdminStatusRequest;
|
import com.ruoyi.system.mapper.SysRoleMapper;
|
import com.ruoyi.system.mapper.SysUserMapper;
|
import com.ruoyi.system.service.ISysPlatformAdminService;
|
import com.ruoyi.system.service.ISysUserService;
|
import com.ruoyi.system.vo.SysPlatformAdminVO;
|
import com.ruoyi.system.vo.SysRoleOptionVO;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.regex.Pattern;
|
|
/**
|
* 平台管理员服务实现
|
*
|
* @author ruoyi
|
*/
|
@Service
|
public class SysPlatformAdminServiceImpl implements ISysPlatformAdminService
|
{
|
private static final Pattern MOBILE_PATTERN = Pattern.compile("^1\\d{10}$");
|
|
private static final Pattern PASSWORD_UPPER_PATTERN = Pattern.compile("[A-Z]");
|
|
private static final Pattern PASSWORD_LOWER_PATTERN = Pattern.compile("[a-z]");
|
|
private static final Pattern PASSWORD_SPECIAL_PATTERN = Pattern.compile("[^A-Za-z0-9]");
|
|
@Autowired
|
private SysUserMapper userMapper;
|
|
@Autowired
|
private SysRoleMapper roleMapper;
|
|
@Autowired
|
private ISysUserService userService;
|
|
@Override
|
public PageInfo<SysPlatformAdminVO> pageList(SysPlatformAdminPageRequest request)
|
{
|
SysPlatformAdminPageRequest query = request == null ? new SysPlatformAdminPageRequest() : request;
|
query.setNickName(StringUtils.trim(query.getNickName()));
|
query.setPhonenumber(StringUtils.trim(query.getPhonenumber()));
|
query.setStatus(StringUtils.trim(query.getStatus()));
|
PageInfo<SysPlatformAdminVO> pageInfo = new PageInfo<SysPlatformAdminVO>(query.getPageNum(), query.getPageSize());
|
List<SysPlatformAdminVO> list = userMapper.selectPlatformAdminPage(query, pageInfo);
|
pageInfo.setRecords(list);
|
return pageInfo;
|
}
|
|
@Override
|
public SysPlatformAdminVO detail(SysPlatformAdminDetailRequest request)
|
{
|
Long userId = getRequiredUserId(request == null ? null : request.getUserId());
|
SysPlatformAdminVO detail = userMapper.selectPlatformAdminById(userId);
|
if (detail == null)
|
{
|
throw new ServiceException("平台管理员不存在");
|
}
|
return detail;
|
}
|
|
@Override
|
@Transactional
|
public int add(SysPlatformAdminAddRequest request, String username)
|
{
|
if (request == null)
|
{
|
throw new ServiceException("平台管理员内容不能为空");
|
}
|
validatePassword(request.getPassword());
|
SysUser user = buildUser(request.getNickName(), request.getUserName(), request.getPhonenumber(),
|
request.getRoleId(), request.getStatus(), null);
|
user.setPassword(SecurityUtils.encryptPassword(StringUtils.trim(request.getPassword())));
|
user.setCreateBy(username);
|
return userService.insertUser(user);
|
}
|
|
@Override
|
@Transactional
|
public int edit(SysPlatformAdminEditRequest request, String username)
|
{
|
if (request == null)
|
{
|
throw new ServiceException("平台管理员内容不能为空");
|
}
|
Long userId = getRequiredUserId(request.getUserId());
|
SysUser exists = getExistingUser(userId);
|
userService.checkUserAllowed(exists);
|
checkPlatformAdminAllowed(userId);
|
SysUser user = buildUser(request.getNickName(), request.getUserName(), request.getPhonenumber(),
|
request.getRoleId(), request.getStatus(), userId);
|
user.setUserId(userId);
|
user.setUpdateBy(username);
|
return userService.updateUser(user);
|
}
|
|
@Override
|
public int changeStatus(SysPlatformAdminStatusRequest request, String username)
|
{
|
Long userId = getRequiredUserId(request == null ? null : request.getUserId());
|
SysUser exists = getExistingUser(userId);
|
userService.checkUserAllowed(exists);
|
checkPlatformAdminAllowed(userId);
|
SysUser user = new SysUser();
|
user.setUserId(userId);
|
user.setStatus(normalizeRequiredStatus(request.getStatus()));
|
user.setUpdateBy(username);
|
return userService.updateUserStatus(user);
|
}
|
|
@Override
|
public int resetPassword(SysPlatformAdminResetPasswordRequest request, String username)
|
{
|
Long userId = getRequiredUserId(request == null ? null : request.getUserId());
|
SysUser exists = getExistingUser(userId);
|
userService.checkUserAllowed(exists);
|
checkPlatformAdminAllowed(userId);
|
validatePassword(request.getPassword());
|
SysUser user = new SysUser();
|
user.setUserId(userId);
|
user.setPassword(SecurityUtils.encryptPassword(StringUtils.trim(request.getPassword())));
|
user.setUpdateBy(username);
|
return userService.resetPwd(user);
|
}
|
|
@Override
|
@Transactional
|
public int delete(SysPlatformAdminDeleteRequest request)
|
{
|
List<Long> userIds = request == null ? null : request.getUserIds();
|
if (CollectionUtils.isEmpty(userIds))
|
{
|
throw new ServiceException("请选择平台管理员");
|
}
|
for (Long userId : userIds)
|
{
|
Long requiredUserId = getRequiredUserId(userId);
|
checkPlatformAdminAllowed(requiredUserId);
|
}
|
return userService.deleteUserByIds(userIds);
|
}
|
|
@Override
|
public List<SysRoleOptionVO> roleOptions()
|
{
|
SysRole query = new SysRole();
|
query.setStatus(0);
|
List<SysRole> roles = roleMapper.selectRoleList(query);
|
List<SysRoleOptionVO> options = new ArrayList<SysRoleOptionVO>();
|
for (SysRole role : roles)
|
{
|
SysRoleOptionVO option = new SysRoleOptionVO();
|
option.setRoleId(role.getRoleId());
|
option.setRoleName(role.getRoleName());
|
options.add(option);
|
}
|
return options;
|
}
|
|
private SysUser buildUser(String nickName, String userName, String phonenumber, Long roleId, String status, Long excludeUserId)
|
{
|
String requiredNickName = getRequiredText(nickName, "姓名不能为空");
|
String requiredUserName = getRequiredText(userName, "登录账号不能为空");
|
String requiredPhone = getRequiredPhone(phonenumber);
|
validateRole(roleId);
|
|
SysUser uniqueUser = new SysUser();
|
uniqueUser.setUserId(excludeUserId);
|
uniqueUser.setUserName(requiredUserName);
|
if (!userService.checkUserNameUnique(uniqueUser))
|
{
|
throw new ServiceException("登录账号已存在");
|
}
|
|
SysUser uniquePhone = new SysUser();
|
uniquePhone.setUserId(excludeUserId);
|
uniquePhone.setPhonenumber(requiredPhone);
|
if (!userService.checkPhoneUnique(uniquePhone))
|
{
|
throw new ServiceException("联系电话已存在");
|
}
|
|
SysUser user = new SysUser();
|
user.setNickName(requiredNickName);
|
user.setUserName(requiredUserName);
|
user.setPhonenumber(requiredPhone);
|
user.setRoleId(roleId);
|
user.setStatus(normalizeStatus(status));
|
return user;
|
}
|
|
private void validateRole(Long roleId)
|
{
|
if (roleId == null || roleId <= 0)
|
{
|
throw new ServiceException("角色不能为空");
|
}
|
SysRole role = roleMapper.selectRoleById(roleId);
|
if (role == null || Integer.valueOf(2).equals(role.getDelFlag()))
|
{
|
throw new ServiceException("角色不存在");
|
}
|
if (Integer.valueOf(1).equals(role.getStatus()))
|
{
|
throw new ServiceException("角色已禁用");
|
}
|
}
|
|
private SysUser getExistingUser(Long userId)
|
{
|
SysUser user = userMapper.selectUserById(userId);
|
if (user == null)
|
{
|
throw new ServiceException("平台管理员不存在");
|
}
|
return user;
|
}
|
|
private void checkPlatformAdminAllowed(Long userId)
|
{
|
SysPlatformAdminVO admin = userMapper.selectPlatformAdminById(userId);
|
if (admin == null)
|
{
|
throw new ServiceException("平台管理员不存在");
|
}
|
if (SysUser.isAdmin(admin.getUserId()) || SysRole.isAdmin(admin.getRoleId()))
|
{
|
throw new ServiceException("不允许操作超级管理员");
|
}
|
}
|
|
private Long getRequiredUserId(Long userId)
|
{
|
if (userId == null || userId <= 0)
|
{
|
throw new ServiceException("用户ID不能为空");
|
}
|
return userId;
|
}
|
|
private String getRequiredText(String value, String message)
|
{
|
String result = StringUtils.trim(value);
|
if (StringUtils.isEmpty(result))
|
{
|
throw new ServiceException(message);
|
}
|
return result;
|
}
|
|
private String getRequiredPhone(String value)
|
{
|
String phone = StringUtils.trim(value);
|
if (!MOBILE_PATTERN.matcher(phone).matches())
|
{
|
throw new ServiceException("联系电话必须为11位手机号");
|
}
|
return phone;
|
}
|
|
private String normalizeStatus(String status)
|
{
|
String value = StringUtils.trim(status);
|
if (StringUtils.isEmpty(value))
|
{
|
return UserConstants.NORMAL;
|
}
|
if (!UserConstants.NORMAL.equals(value) && !UserConstants.USER_DISABLE.equals(value))
|
{
|
throw new ServiceException("账号状态不正确");
|
}
|
return value;
|
}
|
|
private String normalizeRequiredStatus(String status)
|
{
|
String value = StringUtils.trim(status);
|
if (StringUtils.isEmpty(value))
|
{
|
throw new ServiceException("账号状态不能为空");
|
}
|
return normalizeStatus(value);
|
}
|
|
private void validatePassword(String password)
|
{
|
String value = StringUtils.trim(password);
|
if (StringUtils.isEmpty(value))
|
{
|
throw new ServiceException("密码不能为空");
|
}
|
if (value.length() < 12
|
|| !PASSWORD_UPPER_PATTERN.matcher(value).find()
|
|| !PASSWORD_LOWER_PATTERN.matcher(value).find()
|
|| !PASSWORD_SPECIAL_PATTERN.matcher(value).find())
|
{
|
throw new ServiceException("密码至少12位,且包含大小写字母和特殊字符");
|
}
|
}
|
}
|