|
@@ -0,0 +1,194 @@
|
|
|
+package com.hfln.portal.infrastructure.gateway.impl;
|
|
|
+
|
|
|
+
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import cn.hfln.framework.extension.BizException;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.hfln.portal.common.constant.UserConstants;
|
|
|
+import com.hfln.portal.common.dto.data.user.AdminUserDto;
|
|
|
+import com.hfln.portal.common.request.web.*;
|
|
|
+import com.hfln.portal.common.vo.PageRecord;
|
|
|
+import com.hfln.portal.domain.customer.AdminUserType;
|
|
|
+import com.hfln.portal.domain.customer.util.CopyUtils;
|
|
|
+import com.hfln.portal.domain.customer.util.PasswordUtil;
|
|
|
+import com.hfln.portal.domain.exception.ErrorEnum;
|
|
|
+import com.hfln.portal.domain.gateway.WebUserGateway;
|
|
|
+import com.hfln.portal.infrastructure.po.AdminUserInfo;
|
|
|
+import com.hfln.portal.infrastructure.po.TblRole;
|
|
|
+import com.hfln.portal.infrastructure.po.TblUserRole;
|
|
|
+import com.hfln.portal.infrastructure.service.AdminUserService;
|
|
|
+import com.hfln.portal.infrastructure.service.TblRoleService;
|
|
|
+import com.hfln.portal.infrastructure.service.TblUserRoleService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class WebUserGatewayImpl implements WebUserGateway {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AdminUserService adminUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TblUserRoleService tblUserRoleService;
|
|
|
+ @Autowired
|
|
|
+ private TblRoleService tblRoleService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageRecord<AdminUserDto> queryManager(QueryManagerParam param) {
|
|
|
+
|
|
|
+ // 对 后台超级管理员 和用户超级管理员 进行权限控制
|
|
|
+ String userType = (String)StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
|
|
|
+ log.info("当前登录用户类型:userType:{}", userType);
|
|
|
+ if (!AdminUserType.getBgManagerTypes().contains(userType)) {
|
|
|
+ throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行分页查询
|
|
|
+ Page<AdminUserInfo> userInfoPage = adminUserService.queryList(param);
|
|
|
+ // 换为目标VO
|
|
|
+ List<AdminUserDto> targets = CopyUtils.copyList(userInfoPage.getRecords(), AdminUserDto.class);
|
|
|
+ return CopyUtils.copyPage(userInfoPage, targets);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void addAccount(AddAccountParam param) {
|
|
|
+ // 1. 检查账号是否已存在
|
|
|
+ AdminUserInfo exist = adminUserService.queryByAccount(param.getAccount());
|
|
|
+ if (Objects.nonNull(exist)) {
|
|
|
+ throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 对 后台超级管理员 和用户超级管理员 进行权限控制
|
|
|
+ String userType = (String)StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
|
|
|
+ log.info("当前登录用户类型:userType:{}", userType);
|
|
|
+ if (!AdminUserType.getBgManagerTypes().contains(userType)) {
|
|
|
+ throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
|
|
|
+ }
|
|
|
+ // 如果创建的用户类型为普通用户,则需要指定租户id
|
|
|
+ if (AdminUserType.getUserTypes().contains(param.getUserType()) && param.getTenantId() == null) {
|
|
|
+ throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
|
|
|
+ }
|
|
|
+ check(userType, param.getUserType());
|
|
|
+
|
|
|
+ //2.保存新用户到 admin_user_info 表
|
|
|
+ AdminUserInfo adminUserInfo = CopyUtils.copy(param, AdminUserInfo.class);
|
|
|
+ adminUserInfo.setPassword(PasswordUtil.encrypt(param.getPassword()));
|
|
|
+ adminUserInfo.setUserType(param.getUserType());
|
|
|
+ adminUserService.save(adminUserInfo);
|
|
|
+
|
|
|
+ //3.保存用户角色到 tbl_user_role 表
|
|
|
+ TblUserRole userRole = new TblUserRole();
|
|
|
+ userRole.setUserId(adminUserInfo.getUserId());
|
|
|
+ userRole.setRoleId(param.getRoleId());
|
|
|
+ tblUserRoleService.save(userRole);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageRecord<AdminUserDto> queryClient(ClientQueryParam param) {
|
|
|
+ // 对 后台超级管理员 和用户超级管理员 进行权限控制
|
|
|
+ String userType = (String)StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
|
|
|
+ Long tenantId = (Long)StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
|
|
|
+ log.info("当前登录用户类型:userType:{}, tenantId:{}", userType, tenantId);
|
|
|
+ if (!StringUtils.hasText(userType) || !AdminUserType.USER_ADMIN.getCode().equals(userType) || tenantId == null) {
|
|
|
+ throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行分页查询
|
|
|
+ Page<AdminUserInfo> userInfoPage = adminUserService.queryClientList(param, tenantId);
|
|
|
+ // 换为目标VO
|
|
|
+ List<AdminUserDto> targets = CopyUtils.copyList(userInfoPage.getRecords(), AdminUserDto.class);
|
|
|
+ return CopyUtils.copyPage(userInfoPage, targets);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addClient(ClientAddParam param) {
|
|
|
+ // 1. 检查账号是否已存在
|
|
|
+ AdminUserInfo exist = adminUserService.queryByAccount(param.getAccount());
|
|
|
+ if (Objects.nonNull(exist)) {
|
|
|
+ throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 对 后台超级管理员 和用户超级管理员 进行权限控制
|
|
|
+ String userType = (String)StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
|
|
|
+ Long tenantId = (Long)StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
|
|
|
+ log.info("当前登录用户类型:userType:{}, tenantId:{}", userType, tenantId);
|
|
|
+ if (!AdminUserType.USER_ADMIN.getCode().equals(userType) || tenantId == null) {
|
|
|
+ throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ //2.保存新用户到 admin_user_info 表
|
|
|
+ AdminUserInfo adminUserInfo = CopyUtils.copy(param, AdminUserInfo.class);
|
|
|
+ adminUserInfo.setPassword(PasswordUtil.encrypt(param.getPassword()));
|
|
|
+ adminUserInfo.setUserType(AdminUserType.USER_MANAGER.getCode());
|
|
|
+ adminUserService.save(adminUserInfo);
|
|
|
+
|
|
|
+ //3.保存用户角色到 tbl_user_role 表
|
|
|
+ TblRole tblRole = tblRoleService.queryByRoleCode(UserConstants.ROLE_USER_MANAGER_DEFAULT);
|
|
|
+ TblUserRole userRole = new TblUserRole();
|
|
|
+ userRole.setUserId(adminUserInfo.getUserId());
|
|
|
+ userRole.setRoleId(tblRole.getRoleId());
|
|
|
+ tblUserRoleService.save(userRole);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void modManager(AdminModParam param) {
|
|
|
+
|
|
|
+ // 1. 根据用户ID查询现有用户信息
|
|
|
+ AdminUserInfo exist = adminUserService.getById(param.getUserId());
|
|
|
+ if (Objects.isNull(exist)) {
|
|
|
+ throw new BizException(ErrorEnum.USER_IS_NOT_EXIST.getErrorCode(), ErrorEnum.USER_IS_NOT_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 对 后台超级管理员 和用户超级管理员 进行权限控制
|
|
|
+ String userType = (String)StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
|
|
|
+ log.info("当前登录用户类型:userType:{}", userType);
|
|
|
+ if (!AdminUserType.getBgManagerTypes().contains(userType)) {
|
|
|
+ throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果修改的用户类型为普通用户,则需要指定租户id
|
|
|
+ if (AdminUserType.getUserTypes().contains(param.getUserType()) && param.getTenantId() == null) {
|
|
|
+ throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
|
|
|
+ }
|
|
|
+ check(userType, param.getUserType());
|
|
|
+
|
|
|
+ //2.更新用户信息到 admin_user_info 表
|
|
|
+ AdminUserInfo adminUserInfo = CopyUtils.copy(param, AdminUserInfo.class);
|
|
|
+ // 如果密码不为空则更新密码
|
|
|
+ if(StringUtils.hasText(param.getPassword())) {
|
|
|
+ adminUserInfo.setPassword(PasswordUtil.encrypt(param.getPassword()));
|
|
|
+ } else {
|
|
|
+ adminUserInfo.setPassword(exist.getPassword());
|
|
|
+ }
|
|
|
+ adminUserInfo.setUserType(param.getUserType());
|
|
|
+ adminUserService.updateById(adminUserInfo);
|
|
|
+
|
|
|
+ //3.更新用户角色到 tbl_user_role 表
|
|
|
+ TblUserRole userRole = tblUserRoleService.getByUserId(param.getUserId());
|
|
|
+ if (Objects.isNull(userRole)) {
|
|
|
+ userRole = new TblUserRole();
|
|
|
+ userRole.setUserId(adminUserInfo.getUserId());
|
|
|
+ }
|
|
|
+ userRole.setRoleId(param.getRoleId());
|
|
|
+ tblUserRoleService.saveOrUpdate(userRole);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void check(String userType, String createdUserType) {
|
|
|
+ AdminUserType currentUserTypeEnum = AdminUserType.getByCode(userType);
|
|
|
+ AdminUserType createdUserTypeEnum = AdminUserType.getByCode(createdUserType);
|
|
|
+ if (createdUserTypeEnum != null) {
|
|
|
+ if (currentUserTypeEnum.getLevel() < createdUserTypeEnum.getLevel()) {
|
|
|
+ throw new BizException(ErrorEnum.CREATE_NOT_ALLOWED.getErrorCode(), ErrorEnum.CREATE_NOT_ALLOWED.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|