迁移管理员逻辑

This commit is contained in:
YunaiV 2020-07-05 18:28:14 +08:00
parent 6a4b6fe67f
commit 51a5e5b750
35 changed files with 676 additions and 323 deletions

View File

@ -0,0 +1,74 @@
package cn.iocoder.mall.managementweb.controller.admin;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.managementweb.controller.admin.dto.AdminCreateDTO;
import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateInfoDTO;
import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateStatusDTO;
import cn.iocoder.mall.managementweb.manager.admin.AdminManager;
import cn.iocoder.mall.security.admin.core.context.AdminSecurityContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@Api("管理员 API")
@RestController
@RequestMapping("/admin")
public class AdminController {
@Autowired
private AdminManager adminManager;
// =========== 管理员管理 API ===========
// @GetMapping("/page")
// @RequiresPermissions("system.admin.page")
// @ApiOperation(value = "管理员分页")
// public CommonResult<PageResult<AdminVO>> page(AdminPageDTO adminPageDTO) {
// PageResult<AdminBO> page = adminService.getAdminPage(adminPageDTO);
// PageResult<AdminVO> resultPage = AdminConvert.INSTANCE.convertAdminVOPage(page);
// // 拼接结果
// if (!resultPage.getList().isEmpty()) {
// // 查询角色数组
// Map<Integer, Collection<RoleBO>> roleMap = adminService.getAdminRolesMap(CollectionUtil.convertList(resultPage.getList(), AdminBO::getId));
// resultPage.getList().forEach(admin -> admin.setRoles(AdminConvert.INSTANCE.convertAdminVORoleList(roleMap.get(admin.getId()))));
//
// // 查询对应部门
// List<DeptmentBO> deptmentBOS = deptmentService.getAllDeptments();
// Map<Integer, String> deptNameMap = deptmentBOS.stream().collect(Collectors.toMap(d->d.getId(), d->d.getName()));
// //管理员所在部门被删后变成未分配状态
// deptNameMap.put(0, "未分配");
// resultPage.getList().forEach(admin->{
// admin.setDeptment(new AdminVO.Deptment(admin.getDeptmentId(), deptNameMap.get(admin.getDeptmentId())));
// });
// }
// return success(resultPage);
// }
@ApiOperation(value = "创建管理员")
@PostMapping("/create")
public CommonResult<Integer> createAdmin(AdminCreateDTO createDTO, HttpServletRequest request) {
return success(adminManager.createAdmin(createDTO, AdminSecurityContextHolder.getAdminId(), HttpUtil.getIp(request)));
}
@PostMapping("/update")
@ApiOperation(value = "更新管理员")
public CommonResult<Boolean> updateAdmin(AdminUpdateInfoDTO updateInfoDTO) {
adminManager.updateAdmin(updateInfoDTO);
return success(true);
}
@PostMapping("/update_status")
@ApiOperation(value = "更新管理员状态")
public CommonResult<Boolean> updateUserStatus(AdminUpdateStatusDTO updateStatusDTO) {
adminManager.updateAdminStatus(updateStatusDTO);
return success(true);
}
}

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.api.dto.admin; package cn.iocoder.mall.managementweb.controller.admin.dto;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -9,12 +9,20 @@ import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern; import javax.validation.constraints.Pattern;
import java.io.Serializable;
@ApiModel("管理员添加 DTO") @ApiModel("管理员创建 DTO")
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class AdminAddDTO implements Serializable { public class AdminCreateDTO {
@ApiModelProperty(value = "昵称", required = true, example = "小王")
@NotEmpty(message = "昵称不能为空")
@Length(max = 10, message = "昵称长度最大为 10 位")
private String nickname;
@ApiModelProperty(value = "部门编号", required = true, example = "1")
@NotNull(message = "部门不能为空")
private Integer departmentId;
@ApiModelProperty(value = "登陆账号", required = true, example = "15601691300") @ApiModelProperty(value = "登陆账号", required = true, example = "15601691300")
@NotEmpty(message = "登陆账号不能为空") @NotEmpty(message = "登陆账号不能为空")
@ -22,18 +30,9 @@ public class AdminAddDTO implements Serializable {
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
private String username; private String username;
@ApiModelProperty(value = "昵称", required = true, example = "小王")
@NotEmpty(message = "昵称不能为空")
@Length(max = 10, message = "昵称长度最大为 10 位")
private String nickname;
@ApiModelProperty(value = "密码", required = true, example = "buzhidao") @ApiModelProperty(value = "密码", required = true, example = "buzhidao")
@NotEmpty(message = "密码不能为空") @NotEmpty(message = "密码不能为空")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位") @Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password; private String password;
@ApiModelProperty(value = "部门ID", required = true, example = "1")
@NotNull(message = "部门不能为空")
private Integer deptmentId;
} }

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.rest.request.admin; package cn.iocoder.mall.managementweb.controller.admin.dto;
import cn.iocoder.common.framework.vo.PageParam; import cn.iocoder.common.framework.vo.PageParam;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
@ -7,11 +7,11 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
@ApiModel("管理员 - 管理员模块 - 管理员分页信息 Request") @ApiModel("管理员分页查询 DTO")
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Accessors(chain = true) @Accessors(chain = true)
public class AdminsAdminPageRequest extends PageParam { public class AdminPageDTO extends PageParam {
@ApiModelProperty(value = "真实名字,模糊匹配", example = "小王") @ApiModelProperty(value = "真实名字,模糊匹配", example = "小王")
private String name; private String name;

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.api.dto.admin; package cn.iocoder.mall.managementweb.controller.admin.dto;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -9,12 +9,11 @@ import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern; import javax.validation.constraints.Pattern;
import java.io.Serializable;
@ApiModel("管理员更新 DTO") @ApiModel("管理员更新信息 DTO")
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class AdminUpdateDTO implements Serializable { public class AdminUpdateInfoDTO {
@ApiModelProperty(value = "管理员编号", required = true, example = "1") @ApiModelProperty(value = "管理员编号", required = true, example = "1")
@NotNull(message = "管理员编号不能为空") @NotNull(message = "管理员编号不能为空")
@ -26,17 +25,18 @@ public class AdminUpdateDTO implements Serializable {
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
private String username; private String username;
@ApiModelProperty(value = "密码", required = true, example = "buzhidao")
@NotEmpty(message = "密码不能为空")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password;
@ApiModelProperty(value = "昵称", required = true, example = "小王") @ApiModelProperty(value = "昵称", required = true, example = "小王")
@NotEmpty(message = "昵称不能为空") @NotEmpty(message = "昵称不能为空")
@Length(max = 10, message = "昵称长度最大为 10 位") @Length(max = 10, message = "昵称长度最大为 10 位")
private String nickname; private String nickname;
@ApiModelProperty(value = "密码", example = "buzhidao") @ApiModelProperty(value = "部门编号", required = true, example = "1")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password;
@ApiModelProperty(value = "部门ID", required = true, example = "1")
@NotNull(message = "部门不能为空") @NotNull(message = "部门不能为空")
private Integer deptmentId; private Integer departmentId;
} }

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.api.dto.admin; package cn.iocoder.mall.managementweb.controller.admin.dto;
import cn.iocoder.common.framework.enums.CommonStatusEnum; import cn.iocoder.common.framework.enums.CommonStatusEnum;
import cn.iocoder.common.framework.validator.InEnum; import cn.iocoder.common.framework.validator.InEnum;

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.rest.response.admin; package cn.iocoder.mall.managementweb.controller.admin.vo;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.rest.response.admin; package cn.iocoder.mall.managementweb.controller.admin.vo;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

View File

@ -4,7 +4,7 @@ import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.managementweb.controller.passport.dto.AdminPassportLoginDTO; import cn.iocoder.mall.managementweb.controller.passport.dto.AdminPassportLoginDTO;
import cn.iocoder.mall.managementweb.controller.passport.vo.AdminPassportVO; import cn.iocoder.mall.managementweb.controller.passport.vo.AdminPassportVO;
import cn.iocoder.mall.managementweb.manager.admin.AdminPassportManager; import cn.iocoder.mall.managementweb.manager.passport.AdminPassportManager;
import cn.iocoder.security.annotations.RequiresNone; import cn.iocoder.security.annotations.RequiresNone;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.rest.request.admin; package cn.iocoder.mall.managementweb.controller.user.dto;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.rest.request.admin; package cn.iocoder.mall.managementweb.controller.user.dto;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -24,4 +24,5 @@ public class AdminsUserUpdateStatusRequest {
@ApiModelProperty(name = "status", value = "用户状态。1 - 开启2 - 禁用", required = true, example = "1") @ApiModelProperty(name = "status", value = "用户状态。1 - 开启2 - 禁用", required = true, example = "1")
@NotNull(message = "用户状态不能为空") @NotNull(message = "用户状态不能为空")
private Integer status; private Integer status;
} }

View File

@ -0,0 +1,25 @@
package cn.iocoder.mall.managementweb.manager.admin;
import cn.iocoder.mall.managementweb.controller.admin.dto.AdminCreateDTO;
import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateInfoDTO;
import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateStatusDTO;
import org.springframework.stereotype.Service;
@Service
public class AdminManager {
//TODO 目前需要增加搜索所有子部门的用户
public Integer createAdmin(AdminCreateDTO createDTO, Integer createAdminId, String createIp) {
return null;
}
public void updateAdmin(AdminUpdateInfoDTO updateInfoDTO) {
}
public void updateAdminStatus(AdminUpdateStatusDTO updateStatusDTO) {
}
}

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.managementweb.manager.admin; package cn.iocoder.mall.managementweb.manager.passport;
import cn.iocoder.common.framework.enums.UserTypeEnum; import cn.iocoder.common.framework.enums.UserTypeEnum;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;

View File

@ -26,12 +26,12 @@ public enum SystemErrorCodeEnum implements ServiceExceptionUtil.Enumerable<Syste
ADMIN_NOT_FOUND(1002002000, "管理员不存在"), ADMIN_NOT_FOUND(1002002000, "管理员不存在"),
ADMIN_PASSWORD_ERROR(1002002001, "密码不正确"), ADMIN_PASSWORD_ERROR(1002002001, "密码不正确"),
ADMIN_IS_DISABLE(1002002002, "账号被禁用"), ADMIN_IS_DISABLE(1002002002, "账号被禁用"),
// ADMIN_USERNAME_EXISTS(1002002002, "账号已经存在"), ADMIN_USERNAME_EXISTS(1002002002, "账号已经存在"),
// ADMIN_STATUS_EQUALS(1002002003, "账号已经是该状态"), ADMIN_STATUS_EQUALS(1002002003, "账号已经是该状态"),
// ADMIN_DELETE_ONLY_DISABLE(1002002004, "只有关闭的账号才可以删除"), // ADMIN_DELETE_ONLY_DISABLE(1002002004, "只有关闭的账号才可以删除"),
// ADMIN_ADMIN_STATUS_CAN_NOT_UPDATE(1002002005, "管理员的账号状态不允许变更"), ADMIN_ADMIN_STATUS_CAN_NOT_UPDATE(1002002005, "管理员的账号状态不允许变更"),
// ADMIN_ASSIGN_ROLE_NOT_EXISTS(1002002006, "分配员工角色时,有角色不存在"), // ADMIN_ASSIGN_ROLE_NOT_EXISTS(1002002006, "分配员工角色时,有角色不存在"),
// ADMIN_ADMIN_CAN_NOT_UPDATE(1002002008, "管理员的账号不允许变更"), ADMIN_ADMIN_CAN_NOT_UPDATE(1002002008, "管理员的账号不允许变更"),
// ========== 资源模块 1002003000 ========== // ========== 资源模块 1002003000 ==========
RESOURCE_NAME_DUPLICATE(1002003000, "已经存在该名字的资源"), RESOURCE_NAME_DUPLICATE(1002003000, "已经存在该名字的资源"),

View File

@ -0,0 +1,24 @@
package cn.iocoder.mall.systemservice.enums.admin;
/**
* 管理员的账号枚举一般枚举特殊的账号
*
* 例如说特殊管理员 admin 禁止编辑
*/
public enum AdminUsernameEnum {
ADMIN("admin"),
DEMO("yudaoyuanma"),
;
private final String username;
AdminUsernameEnum(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}

View File

@ -1,10 +1,12 @@
package cn.iocoder.mall.system.biz.enums.errorcode; package cn.iocoder.mall.systemservice.enums.errorcode;
/** /**
* 错误码枚举内置错误码是在 枚举中 * 错误码的类型枚举
*
* @author ding * @author ding
*/ */
public enum ErrorCodeTypeEnum { public enum ErrorCodeTypeEnum {
/** /**
* 内置错误码 * 内置错误码
*/ */
@ -23,4 +25,5 @@ public enum ErrorCodeTypeEnum {
public Integer getType() { public Integer getType() {
return type; return type;
} }
} }

View File

@ -1,6 +1,10 @@
package cn.iocoder.mall.systemservice.rpc.admin; package cn.iocoder.mall.systemservice.rpc.admin;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminCreateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminPageDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminUpdateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO; import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO;
import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO; import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO;
@ -11,4 +15,10 @@ public interface AdminRpc {
CommonResult<AdminVO> verifyPassword(AdminVerifyPasswordDTO verifyPasswordDTO); CommonResult<AdminVO> verifyPassword(AdminVerifyPasswordDTO verifyPasswordDTO);
CommonResult<Integer> createAdmin(AdminCreateDTO createDTO);
CommonResult<Boolean> updateAdmin(AdminUpdateDTO updateDTO);
CommonResult<PageResult<AdminVO>> pageAdmin(AdminPageDTO pageDTO);
} }

View File

@ -0,0 +1,57 @@
package cn.iocoder.mall.systemservice.rpc.admin.dto;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.Date;
/**
* 管理员添加 BO
*/
@Data
@Accessors(chain = true)
public class AdminCreateDTO implements Serializable {
/**
* 昵称
*/
@NotEmpty(message = "昵称不能为空")
@Length(max = 10, message = "昵称长度最大为 10 位")
private String nickname;
/**
* 部门编号
*/
@NotNull(message = "部门不能为空")
private Integer departmentId;
/**
* 登陆账号
*/
@NotEmpty(message = "登陆账号不能为空")
@Length(min = 5, max = 16, message = "账号长度为 5-16 位")
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
private String username;
/**
* 密码
*/
@NotEmpty(message = "密码不能为空")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password;
/**
* 创建管理员编号
*/
@NotNull(message = "创建管理员编号不能为空")
private String createAdminId;
/**
* 创建 IP
*/
@NotNull(message = "创建 IP 不能为空")
private Date createIp;
}

View File

@ -0,0 +1,22 @@
package cn.iocoder.mall.systemservice.rpc.admin.dto;
import cn.iocoder.common.framework.vo.PageParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@ApiModel("管理员分页查询 DTO")
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class AdminPageDTO extends PageParam {
@ApiModelProperty(value = "真实名字,模糊匹配", example = "小王")
private String name;
@ApiModelProperty(value = "部门编号")
private Integer departmentId;
}

View File

@ -0,0 +1,55 @@
package cn.iocoder.mall.systemservice.rpc.admin.dto;
import cn.iocoder.common.framework.enums.CommonStatusEnum;
import cn.iocoder.common.framework.validator.InEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
/**
* 管理员修改 DTO
*/
@Data
@Accessors(chain = true)
public class AdminUpdateDTO implements Serializable {
/**
* 管理员编号
*/
@NotNull(message = "管理员编号不能为空")
private Integer id;
/**
* 昵称
*/
@ApiModelProperty(value = "昵称", required = true, example = "小王")
@Length(max = 10, message = "昵称长度最大为 10 位")
private String nickname;
/**
* 部门编号
*/
@ApiModelProperty(value = "部门编号", required = true, example = "1")
private Integer departmentId;
/**
* 状态
*/
@InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}")
private Integer status;
/**
* 登录账号
*/
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
private String username;
/**
* 密码
*/
@ApiModelProperty(value = "密码", required = true, example = "buzhidao")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password;
}

View File

@ -2,8 +2,14 @@ package cn.iocoder.mall.systemservice.convert.admin;
import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO; import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminCreateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminPageDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminUpdateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO; import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO; import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminCreateBO;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminPageBO;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminUpdateBO;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
@ -16,9 +22,21 @@ public interface AdminConvert {
AdminBO convert(AdminDO bean); AdminBO convert(AdminDO bean);
@Mapping(source = "records", target = "list") AdminVO convert(AdminBO bean);
PageResult<AdminBO> convertPage(IPage<AdminDO> bean);
AdminVO convert(AdminBO adminBO); AdminDO convert(AdminCreateBO bean);
AdminDO convert(AdminUpdateBO bean);
AdminCreateBO convert(AdminCreateDTO bean);
AdminUpdateBO convert(AdminUpdateDTO bean);
@Mapping(source = "records", target = "list")
PageResult<AdminBO> convertPage(IPage<AdminDO> page);
AdminPageBO convert(AdminPageDTO page);
PageResult<AdminVO> convert(PageResult<AdminBO> adminPage);
} }

View File

@ -7,8 +7,12 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.util.Date;
/** /**
* 管理员实体 * 管理员实体
*
* uk_username 索引基于 {@link #username} 字段
*/ */
@TableName(value = "admin") @TableName(value = "admin")
@Data @Data
@ -50,4 +54,13 @@ public class AdminDO extends BaseDO {
*/ */
private String passwordSalt; private String passwordSalt;
/**
* 创建管理员编号
*/
private String createAdminId;
/**
* 创建 IP
*/
private Date createIp;
} }

View File

@ -1,8 +1,12 @@
package cn.iocoder.mall.systemservice.dal.mysql.mapper.admin; package cn.iocoder.mall.systemservice.dal.mysql.mapper.admin;
import cn.iocoder.mall.mybatis.query.QueryWrapperX;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO; import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminPageBO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
@ -14,10 +18,10 @@ public interface AdminMapper extends BaseMapper<AdminDO> {
); );
} }
// default IPage<AdminDO> selectPage(AdminPageDTO adminPageDTO) { default IPage<AdminDO> selectPage(AdminPageBO adminPageBO) {
// return selectPage(new Page<>(adminPageDTO.getPageNo(), adminPageDTO.getPageSize()), return selectPage(new Page<>(adminPageBO.getPageNo(), adminPageBO.getPageSize()),
// new QueryWrapperX<AdminDO>().likeIfPresent("name", adminPageDTO.getName()) new QueryWrapperX<AdminDO>().likeIfPresent("name", adminPageBO.getName())
// .eqIfPresent("department_id", adminPageDTO.getDepartmentId())); .eqIfPresent("department_id", adminPageBO.getDepartmentId()));
// } }
} }

View File

@ -1,18 +1,29 @@
package cn.iocoder.mall.systemservice.manager.admin; package cn.iocoder.mall.systemservice.manager.admin;
import cn.iocoder.common.framework.enums.UserTypeEnum;
import cn.iocoder.common.framework.util.StringUtils;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.convert.admin.AdminConvert; import cn.iocoder.mall.systemservice.convert.admin.AdminConvert;
import cn.iocoder.mall.systemservice.enums.admin.AdminStatusEnum;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminCreateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminPageDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminUpdateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO; import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO;
import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO; import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO;
import cn.iocoder.mall.systemservice.service.admin.AdminService; import cn.iocoder.mall.systemservice.service.admin.AdminService;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO; import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO;
import cn.iocoder.mall.systemservice.service.oauth.OAuth2Service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service @Service
public class AdminManager { public class AdminManager {
@Autowired @Autowired
private AdminService adminService; private AdminService adminService;
@Autowired
private OAuth2Service oauth2Service;
public AdminVO verifyPassword(AdminVerifyPasswordDTO verifyPasswordDTO) { public AdminVO verifyPassword(AdminVerifyPasswordDTO verifyPasswordDTO) {
AdminBO adminBO = adminService.verifyPassword(verifyPasswordDTO.getUsername(), AdminBO adminBO = adminService.verifyPassword(verifyPasswordDTO.getUsername(),
@ -20,4 +31,25 @@ public class AdminManager {
return AdminConvert.INSTANCE.convert(adminBO); return AdminConvert.INSTANCE.convert(adminBO);
} }
public AdminVO createAdmin(AdminCreateDTO createDTO) {
AdminBO adminBO = adminService.createAdmin(AdminConvert.INSTANCE.convert(createDTO));
return AdminConvert.INSTANCE.convert(adminBO);
}
@Transactional
public void updateAdmin(AdminUpdateDTO updateDTO) {
// 更新管理员信息
adminService.updateAdmin(AdminConvert.INSTANCE.convert(updateDTO));
// 如果修改密码或者禁用管理员
if (StringUtils.hasText(updateDTO.getPassword())
|| AdminStatusEnum.INACTIVE.getStatus().equals(updateDTO.getStatus())) {
oauth2Service.removeToken(updateDTO.getId(), UserTypeEnum.ADMIN.getValue());
}
}
public PageResult<AdminVO> pageAdmin(AdminPageDTO pageDTO) {
PageResult<AdminBO> adminPage = adminService.pageAdmin(AdminConvert.INSTANCE.convert(pageDTO));
return AdminConvert.INSTANCE.convert(adminPage);
}
} }

View File

@ -1,7 +1,11 @@
package cn.iocoder.mall.systemservice.rpc.admin; package cn.iocoder.mall.systemservice.rpc.admin;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.manager.admin.AdminManager; import cn.iocoder.mall.systemservice.manager.admin.AdminManager;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminCreateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminPageDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminUpdateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO; import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO;
import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO; import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO;
import org.apache.dubbo.config.annotation.Service; import org.apache.dubbo.config.annotation.Service;
@ -20,4 +24,21 @@ public class AdminRpcImpl implements AdminRpc {
return success(adminManager.verifyPassword(verifyPasswordDTO)); return success(adminManager.verifyPassword(verifyPasswordDTO));
} }
@Override
public CommonResult<Integer> createAdmin(AdminCreateDTO createDTO) {
AdminVO adminVO = adminManager.createAdmin(createDTO);
return success(adminVO.getId());
}
@Override
public CommonResult<Boolean> updateAdmin(AdminUpdateDTO updateDTO) {
adminManager.updateAdmin(updateDTO);
return success(true);
}
@Override
public CommonResult<PageResult<AdminVO>> pageAdmin(AdminPageDTO pageDTO) {
return success(adminManager.pageAdmin(pageDTO));
}
} }

View File

@ -1,16 +1,24 @@
package cn.iocoder.mall.systemservice.service.admin; package cn.iocoder.mall.systemservice.service.admin;
import cn.iocoder.common.framework.enums.CommonStatusEnum;
import cn.iocoder.common.framework.util.DigestUtils; import cn.iocoder.common.framework.util.DigestUtils;
import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.util.StringUtils;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.systemservice.convert.admin.AdminConvert; import cn.iocoder.mall.systemservice.convert.admin.AdminConvert;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO; import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.admin.AdminMapper; import cn.iocoder.mall.systemservice.dal.mysql.mapper.admin.AdminMapper;
import cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum;
import cn.iocoder.mall.systemservice.enums.admin.AdminStatusEnum; import cn.iocoder.mall.systemservice.enums.admin.AdminStatusEnum;
import cn.iocoder.mall.systemservice.enums.admin.AdminUsernameEnum;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO; import cn.iocoder.mall.systemservice.service.admin.bo.AdminBO;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminCreateBO;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminPageBO;
import cn.iocoder.mall.systemservice.service.admin.bo.AdminUpdateBO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*;
@Service @Service
public class AdminService { public class AdminService {
@ -20,24 +28,143 @@ public class AdminService {
public AdminBO verifyPassword(String username, String password, String ip) { public AdminBO verifyPassword(String username, String password, String ip) {
AdminDO adminDO = adminMapper.selectByUsername(username); AdminDO adminDO = adminMapper.selectByUsername(username);
if (adminDO == null) { if (adminDO == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ADMIN_NOT_FOUND); throw ServiceExceptionUtil.exception(ADMIN_NOT_FOUND);
} }
// 校验密码是否正确 // 校验密码是否正确
String encodedPassword = DigestUtils.bcrypt(password, adminDO.getPasswordSalt()); String encodedPassword = DigestUtils.bcrypt(password, adminDO.getPasswordSalt());
if (!encodedPassword.equals(adminDO.getPassword())) { if (!encodedPassword.equals(adminDO.getPassword())) {
// TODO 需要补充密码错误上限 // TODO 需要补充密码错误上限
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ADMIN_PASSWORD_ERROR); throw ServiceExceptionUtil.exception(ADMIN_PASSWORD_ERROR);
} }
// 账号被禁用 // 账号被禁用
if (!AdminStatusEnum.ACTIVE.getStatus().equals(adminDO.getStatus())) { if (!AdminStatusEnum.ACTIVE.getStatus().equals(adminDO.getStatus())) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ADMIN_IS_DISABLE); throw ServiceExceptionUtil.exception(ADMIN_IS_DISABLE);
} }
// 返回 // 返回
return AdminConvert.INSTANCE.convert(adminDO); return AdminConvert.INSTANCE.convert(adminDO);
} }
// public PageResult<AdminBO> getAdminPage(AdminPageDTO pageDTO) { public PageResult<AdminBO> pageAdmin(AdminPageBO adminPageBO) {
// return AdminConvert.INSTANCE.convertPage(adminMapper.selectPage(pageDTO)); return AdminConvert.INSTANCE.convertPage(adminMapper.selectPage(adminPageBO));
}
public AdminBO createAdmin(AdminCreateBO createBO) {
// 校验账号唯一
if (adminMapper.selectByUsername(createBO.getUsername()) != null) {
throw ServiceExceptionUtil.exception(ADMIN_USERNAME_EXISTS);
}
// 加密密码
String passwordSalt = genPasswordSalt();
String password = encodePassword(createBO.getPassword(), passwordSalt);
// 保存到数据库
AdminDO admin = AdminConvert.INSTANCE.convert(createBO)
.setPassword(password).setPasswordSalt(passwordSalt)
.setStatus(CommonStatusEnum.ENABLE.getValue());
adminMapper.insert(admin);
// 返回成功
return AdminConvert.INSTANCE.convert(admin);
}
private String genPasswordSalt() {
return DigestUtils.genBcryptSalt();
}
private String encodePassword(String password, String salt) {
return DigestUtils.bcrypt(password, salt);
}
public void updateAdmin(AdminUpdateBO updateDTO) {
// 校验账号存在
AdminDO admin = adminMapper.selectById(updateDTO.getId());
if (admin == null) {
throw ServiceExceptionUtil.exception(ADMIN_NOT_FOUND);
}
// 校验是否为特殊账号不允许编辑
if (AdminUsernameEnum.ADMIN.getUsername().equals(admin.getUsername())
|| AdminUsernameEnum.DEMO.getUsername().equals(admin.getUsername())) {
throw ServiceExceptionUtil.exception(ADMIN_ADMIN_CAN_NOT_UPDATE);
}
// 校验账号唯一
if (StringUtils.hasText(updateDTO.getUsername())) {
AdminDO usernameAdmin = adminMapper.selectByUsername(updateDTO.getUsername());
if (usernameAdmin != null && !usernameAdmin.getId().equals(updateDTO.getId())) {
throw ServiceExceptionUtil.exception(ADMIN_USERNAME_EXISTS);
}
}
// 如果有更新状态则校验是否已经是该状态
if (updateDTO.getStatus() != null && updateDTO.getStatus().equals(admin.getStatus())) {
throw ServiceExceptionUtil.exception(ADMIN_STATUS_EQUALS);
}
// 更新到数据库
AdminDO updateAdmin = AdminConvert.INSTANCE.convert(updateDTO);
// 如果更新密码需要特殊加密
if (StringUtils.hasText(updateDTO.getPassword())) {
String passwordSalt = genPasswordSalt();
String password = encodePassword(updateDTO.getPassword(), passwordSalt);
updateAdmin.setPassword(password).setPasswordSalt(passwordSalt);
}
adminMapper.updateById(updateAdmin);
}
//
// @Override
// public Map<Integer, Collection<RoleBO>> getAdminRolesMap(Collection<Integer> adminIds) {
// // 查询管理员拥有的角色关联数据
// List<AdminRoleDO> adminRoleList = adminRoleMapper.selectListByAdminIds(adminIds);
// if (adminRoleList.isEmpty()) {
// return Collections.emptyMap();
// }
// // 查询角色数据
// List<RoleBO> roleList = roleService.getRoleList(CollectionUtil.convertSet(adminRoleList, AdminRoleDO::getRoleId));
// Map<Integer, RoleBO> roleMap = CollectionUtil.convertMap(roleList, RoleBO::getId);
// // 拼接数据
// Multimap<Integer, RoleBO> result = ArrayListMultimap.create();
// adminRoleList.forEach(adminRole -> result.put(adminRole.getAdminId(), roleMap.get(adminRole.getRoleId())));
// return result.asMap();
// }
//
// @Override
// public List<RoleBO> getRoleList(Integer adminId) {
// // 查询管理员拥有的角色关联数据
// List<AdminRoleDO> adminRoleList = adminRoleMapper.selectByAdminId(adminId);
// if (adminRoleList.isEmpty()) {
// return Collections.emptyList();
// }
// // 查询角色数据
// return roleService.getRoleList(CollectionUtil.convertSet(adminRoleList, AdminRoleDO::getRoleId));
// }
//
// @Override
// @Transactional
// public Boolean assignAdminRole(Integer adminId, AdminAssignRoleDTO adminAssignRoleDTO) {
// // 校验账号存在
// AdminDO admin = adminMapper.selectById(adminAssignRoleDTO.getId());
// if (admin == null) {
// throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
// }
// // 校验是否有不存在的角色
// if (!CollectionUtil.isEmpty(adminAssignRoleDTO.getRoleIds())) {
// List<RoleDO> roles = roleService.getRoles(adminAssignRoleDTO.getRoleIds());
// if (roles.size() != adminAssignRoleDTO.getRoleIds().size()) {
// throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_ASSIGN_ROLE_NOT_EXISTS.getCode());
// }
// }
// // TODO 芋艿这里先简单实现即方式是删除老的分配的角色关系然后添加新的分配的角色关系
// // 标记管理员角色源关系都为删除
// adminRoleMapper.deleteByAdminId(adminAssignRoleDTO.getId());
// // 创建 RoleResourceDO 数组并插入到数据库
// if (!CollectionUtil.isEmpty(adminAssignRoleDTO.getRoleIds())) {
// List<AdminRoleDO> adminRoleDOs = adminAssignRoleDTO.getRoleIds().stream().map(roleId -> {
// AdminRoleDO roleResource = new AdminRoleDO().setAdminId(adminAssignRoleDTO.getId()).setRoleId(roleId);
// roleResource.setCreateTime(new Date());
// roleResource.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
// return roleResource;
// }).collect(Collectors.toList());
// adminRoleMapper.insertList(adminRoleDOs);
// }
// // TODO 插入操作日志
// // 返回成功
// return true;
// } // }
} }

View File

@ -0,0 +1,56 @@
package cn.iocoder.mall.systemservice.service.admin.bo;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Date;
/**
* 管理员添加 BO
*/
@Data
@Accessors(chain = true)
public class AdminCreateBO {
/**
* 昵称
*/
@NotEmpty(message = "昵称不能为空")
@Length(max = 10, message = "昵称长度最大为 10 位")
private String nickname;
/**
* 部门编号
*/
@NotNull(message = "部门不能为空")
private Integer departmentId;
/**
* 登录账号
*/
@NotEmpty(message = "登陆账号不能为空")
@Length(min = 5, max = 16, message = "账号长度为 5-16 位")
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
private String username;
/**
* 密码
*/
@NotEmpty(message = "密码不能为空")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password;
/**
* 创建管理员编号
*/
@NotNull(message = "创建管理员编号不能为空")
private String createAdminId;
/**
* 创建 IP
*/
@NotNull(message = "创建 IP 不能为空")
private Date createIp;
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.mall.systemservice.service.admin.bo;
import cn.iocoder.common.framework.vo.PageParam;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@ApiModel("管理员分页查询 BO")
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class AdminPageBO extends PageParam {
/**
* 真实名字模糊匹配
*/
private String name;
/**
* 部门编号
*/
private Integer departmentId;
}

View File

@ -0,0 +1,52 @@
package cn.iocoder.mall.systemservice.service.admin.bo;
import cn.iocoder.common.framework.enums.CommonStatusEnum;
import cn.iocoder.common.framework.validator.InEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
/**
* 管理员修改 BO
*/
@Data
@Accessors(chain = true)
public class AdminUpdateBO {
/**
* 管理员编号
*/
@NotNull(message = "管理员编号不能为空")
private Integer id;
/**
* 昵称
*/
@Length(max = 10, message = "昵称长度最大为 10 位")
private String nickname;
/**
* 部门编号
*/
@NotNull(message = "部门不能为空")
private Integer departmentId;
/**
* 状态
*/
@InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}")
private Integer status;
/**
* 登录账号
*/
@Length(min = 5, max = 16, message = "账号长度为 5-16 位")
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
private String username;
/**
* 密码
*/
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password;
}

View File

@ -80,6 +80,12 @@ public class OAuth2Service {
return OAuth2Convert.INSTANCE.convert(oauth2AccessTokenDO); return OAuth2Convert.INSTANCE.convert(oauth2AccessTokenDO);
} }
@Transactional
public void removeToken(Integer userId, Integer userType) {
oauth2AccessTokenMapper.deleteByUserIdAndUserType(userId, userType);
oauth2RefreshTokenMapper.deleteByUserIdAndUserType(userId, userType);
}
private OAuth2AccessTokenDO createOAuth2AccessToken(OAuth2RefreshTokenDO refreshTokenDO, String createIp) { private OAuth2AccessTokenDO createOAuth2AccessToken(OAuth2RefreshTokenDO refreshTokenDO, String createIp) {
OAuth2AccessTokenDO accessToken = new OAuth2AccessTokenDO() OAuth2AccessTokenDO accessToken = new OAuth2AccessTokenDO()
.setId(generateAccessToken()) .setId(generateAccessToken())

View File

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>system</artifactId>
<groupId>cn.iocoder.mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>system-biz-api</artifactId>
<dependencies>
<!-- Mall 相关 -->
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>common-framework</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -1,6 +0,0 @@
/**
* 该项目主要用于暴露一些共享的枚举类等
*
* 例如说RPC 接口提供错误码给调用方
*/
package cn.iocoder.mall.system.biz;

View File

@ -1,15 +0,0 @@
package cn.iocoder.mall.system.api.constant;
public class AdminConstants {
/**
* 账号 - 管理员
*/
public static final String USERNAME_ADMIN = "admin";
/**
* 账号 - 演示账号
*/
public static final String USERNAME_DEMO = "yudaoyuanma";
}

View File

@ -1,21 +0,0 @@
package cn.iocoder.mall.system.api.dto.admin;
import cn.iocoder.common.framework.vo.PageParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
@ApiModel(value = "管理员分页 DTO")
@Data
@Accessors(chain = true)
public class AdminPageDTO extends PageParam {
@ApiModelProperty(value = "昵称,模糊匹配", example = "小王")
private String nickname;
@ApiModelProperty(value = "所在部门ID")
private Integer deptmentId;
}

View File

@ -43,161 +43,6 @@ public class AdminServiceImpl implements AdminService {
@Autowired @Autowired
private RoleServiceImpl roleService; private RoleServiceImpl roleService;
@Override
public PageResult<AdminBO> getAdminPage(AdminPageDTO adminPageDTO) {
IPage<AdminDO> page = adminMapper.selectPage(adminPageDTO);
return AdminConvert.INSTANCE.convert(page);
}
@Override
public AdminBO addAdmin(Integer adminId, AdminAddDTO adminAddDTO) {
// 校验账号唯一
if (adminMapper.selectByUsername(adminAddDTO.getUsername()) != null) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_EXISTS.getCode());
}
// 保存到数据库
AdminDO admin = AdminConvert.INSTANCE.convert(adminAddDTO)
.setPassword(encodePassword(adminAddDTO.getPassword())) // 加密密码
.setStatus(CommonStatusEnum.ENABLE.getValue());
admin.setCreateTime(new Date());
admin.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
adminMapper.insert(admin);
// TODO 插入操作日志
// 返回成功
return AdminConvert.INSTANCE.convert(admin);
}
@Override
public Boolean updateAdmin(Integer adminId, AdminUpdateDTO adminUpdateDTO) {
// 校验账号存在
AdminDO admin = adminMapper.selectById(adminUpdateDTO.getId());
if (admin == null) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
}
if (AdminConstants.USERNAME_ADMIN.equals(admin.getUsername())
|| AdminConstants.USERNAME_DEMO.equals(admin.getUsername())) { // 特殊账号不允许编辑
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_ADMIN_CAN_NOT_UPDATE.getCode());
}
// 校验账号唯一
AdminDO usernameAdmin = adminMapper.selectByUsername(adminUpdateDTO.getUsername());
if (usernameAdmin != null && !usernameAdmin.getId().equals(adminUpdateDTO.getId())) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_EXISTS.getCode());
}
// 更新到数据库
AdminDO updateAdmin = AdminConvert.INSTANCE.convert(adminUpdateDTO);
adminMapper.updateById(updateAdmin);
// TODO 插入操作日志
// 返回成功
return true;
}
@Override
@Transactional
public Boolean updateAdminStatus(Integer adminId, AdminUpdateStatusDTO adminUpdateStatusDTO) {
// 校验账号存在
AdminDO admin = adminMapper.selectById(adminUpdateStatusDTO.getId());
if (admin == null) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
}
if (AdminConstants.USERNAME_ADMIN.equals(admin.getUsername())
|| AdminConstants.USERNAME_DEMO.equals(admin.getUsername())) { // 特殊账号不允许编辑
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_ADMIN_STATUS_CAN_NOT_UPDATE.getCode());
}
// 如果状态相同则返回错误
if (adminUpdateStatusDTO.getStatus().equals(admin.getStatus())) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_STATUS_EQUALS.getCode());
}
// 更新管理员状态
AdminDO updateAdmin = new AdminDO().setId(adminUpdateStatusDTO.getId()).setStatus(adminUpdateStatusDTO.getStatus());
adminMapper.updateById(updateAdmin);
// 如果是关闭管理员则标记 token 失效否则管理员还可以继续蹦跶
if (CommonStatusEnum.DISABLE.getValue().equals(adminUpdateStatusDTO.getStatus())) {
oauth2Service.removeToken(new OAuth2RemoveTokenByUserDTO().setUserId(adminId).setUserType(UserTypeEnum.ADMIN.getValue()));
}
// TODO 插入操作日志
// 返回成功
return true;
}
@Override
@Transactional
public Boolean deleteAdmin(Integer adminId, Integer updateAdminId) {
// 校验账号存在
AdminDO admin = adminMapper.selectById(updateAdminId);
if (admin == null) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
}
// 只有禁用的账号才可以删除
if (CommonStatusEnum.ENABLE.getValue().equals(admin.getStatus())) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_DELETE_ONLY_DISABLE.getCode());
}
// 标记删除 AdminDO
adminMapper.deleteById(updateAdminId); // 标记删除
// 标记删除 AdminRole
adminRoleMapper.deleteByAdminId(updateAdminId);
// TODO 插入操作日志
// 返回成功
return true;
}
@Override
public Map<Integer, Collection<RoleBO>> getAdminRolesMap(Collection<Integer> adminIds) {
// 查询管理员拥有的角色关联数据
List<AdminRoleDO> adminRoleList = adminRoleMapper.selectListByAdminIds(adminIds);
if (adminRoleList.isEmpty()) {
return Collections.emptyMap();
}
// 查询角色数据
List<RoleBO> roleList = roleService.getRoleList(CollectionUtil.convertSet(adminRoleList, AdminRoleDO::getRoleId));
Map<Integer, RoleBO> roleMap = CollectionUtil.convertMap(roleList, RoleBO::getId);
// 拼接数据
Multimap<Integer, RoleBO> result = ArrayListMultimap.create();
adminRoleList.forEach(adminRole -> result.put(adminRole.getAdminId(), roleMap.get(adminRole.getRoleId())));
return result.asMap();
}
@Override
public List<RoleBO> getRoleList(Integer adminId) {
// 查询管理员拥有的角色关联数据
List<AdminRoleDO> adminRoleList = adminRoleMapper.selectByAdminId(adminId);
if (adminRoleList.isEmpty()) {
return Collections.emptyList();
}
// 查询角色数据
return roleService.getRoleList(CollectionUtil.convertSet(adminRoleList, AdminRoleDO::getRoleId));
}
@Override
@Transactional
public Boolean assignAdminRole(Integer adminId, AdminAssignRoleDTO adminAssignRoleDTO) {
// 校验账号存在
AdminDO admin = adminMapper.selectById(adminAssignRoleDTO.getId());
if (admin == null) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
}
// 校验是否有不存在的角色
if (!CollectionUtil.isEmpty(adminAssignRoleDTO.getRoleIds())) {
List<RoleDO> roles = roleService.getRoles(adminAssignRoleDTO.getRoleIds());
if (roles.size() != adminAssignRoleDTO.getRoleIds().size()) {
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.ADMIN_ASSIGN_ROLE_NOT_EXISTS.getCode());
}
}
// TODO 芋艿这里先简单实现即方式是删除老的分配的角色关系然后添加新的分配的角色关系
// 标记管理员角色源关系都为删除
adminRoleMapper.deleteByAdminId(adminAssignRoleDTO.getId());
// 创建 RoleResourceDO 数组并插入到数据库
if (!CollectionUtil.isEmpty(adminAssignRoleDTO.getRoleIds())) {
List<AdminRoleDO> adminRoleDOs = adminAssignRoleDTO.getRoleIds().stream().map(roleId -> {
AdminRoleDO roleResource = new AdminRoleDO().setAdminId(adminAssignRoleDTO.getId()).setRoleId(roleId);
roleResource.setCreateTime(new Date());
roleResource.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
return roleResource;
}).collect(Collectors.toList());
adminRoleMapper.insertList(adminRoleDOs);
}
// TODO 插入操作日志
// 返回成功
return true;
}
} }

View File

@ -50,56 +50,6 @@ public class AdminController {
@Autowired @Autowired
private DeptmentService deptmentService; private DeptmentService deptmentService;
// =========== 管理员管理 API ===========
//TODO 目前需要增加搜索所有子部门的用户
@GetMapping("/page")
@RequiresPermissions("system.admin.page")
@ApiOperation(value = "管理员分页")
public CommonResult<PageResult<AdminVO>> page(AdminPageDTO adminPageDTO) {
PageResult<AdminBO> page = adminService.getAdminPage(adminPageDTO);
PageResult<AdminVO> resultPage = AdminConvert.INSTANCE.convertAdminVOPage(page);
// 拼接结果
if (!resultPage.getList().isEmpty()) {
// 查询角色数组
Map<Integer, Collection<RoleBO>> roleMap = adminService.getAdminRolesMap(CollectionUtil.convertList(resultPage.getList(), AdminBO::getId));
resultPage.getList().forEach(admin -> admin.setRoles(AdminConvert.INSTANCE.convertAdminVORoleList(roleMap.get(admin.getId()))));
// 查询对应部门
List<DeptmentBO> deptmentBOS = deptmentService.getAllDeptments();
Map<Integer, String> deptNameMap = deptmentBOS.stream().collect(Collectors.toMap(d->d.getId(), d->d.getName()));
//管理员所在部门被删后变成未分配状态
deptNameMap.put(0, "未分配");
resultPage.getList().forEach(admin->{
admin.setDeptment(new AdminVO.Deptment(admin.getDeptmentId(), deptNameMap.get(admin.getDeptmentId())));
});
}
return success(resultPage);
}
@PostMapping("/add")
@ApiOperation(value = "创建管理员")
public CommonResult<AdminBO> add(AdminAddDTO adminAddDTO) {
return success(adminService.addAdmin(AdminSecurityContextHolder.getContext().getAdminId(), adminAddDTO));
}
@PostMapping("/update")
@ApiOperation(value = "更新管理员")
public CommonResult<Boolean> update(AdminUpdateDTO adminUpdateDTO) {
return success(adminService.updateAdmin(AdminSecurityContextHolder.getContext().getAdminId(), adminUpdateDTO));
}
@PostMapping("/update_status")
@ApiOperation(value = "更新管理员状态")
public CommonResult<Boolean> updateStatus(AdminUpdateStatusDTO adminUpdateStatusDTO) {
return success(adminService.updateAdminStatus(AdminSecurityContextHolder.getContext().getAdminId(), adminUpdateStatusDTO));
}
@PostMapping("/delete")
@ApiOperation(value = "删除管理员")
@ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1")
public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
return success(adminService.deleteAdmin(AdminSecurityContextHolder.getContext().getAdminId(), id));
}
} }