mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-08 02:04:04 +08:00
代码优化
This commit is contained in:
parent
b1022fde0a
commit
e7d335eb88
5
pom.xml
5
pom.xml
@ -92,6 +92,11 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.4</version>
|
||||
</dependency>
|
||||
|
||||
<!--JWT -->
|
||||
<dependency>
|
||||
|
@ -106,9 +106,10 @@ public class AdminRoleController {
|
||||
|
||||
roleService.updateById(newRole);
|
||||
|
||||
if (request.getPermissionIds().length > 0) {
|
||||
// 先清空已有的权限
|
||||
rolePermissionService.removeRolePermissionsByRoleId(role.getId());
|
||||
|
||||
if (request.getPermissionIds().length > 0) {
|
||||
// 重新关联权限
|
||||
List<AdminRolePermission> rolePermissions = new ArrayList<>();
|
||||
for (int i = 0; i < request.getPermissionIds().length; i++) {
|
||||
|
@ -149,10 +149,10 @@ public class AdminUserController {
|
||||
return JsonResponse.error("更新管理员资料失败");
|
||||
}
|
||||
|
||||
if (request.getRoleIds().length > 0) {
|
||||
// 先删除管理员与权限的已有关联关系
|
||||
userRoleService.removeUserRolesByUserId(adminUser.getId());
|
||||
|
||||
if (request.getRoleIds().length > 0) {
|
||||
// 重新绑定关联关系
|
||||
List<AdminUserRole> userRoles = new ArrayList<>();
|
||||
for (int i = 0; i < request.getRoleIds().length; i++) {
|
||||
|
@ -37,7 +37,7 @@ public class LoginController {
|
||||
private JWTService jwtService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
private ApplicationContext context;
|
||||
|
||||
@PostMapping("/login")
|
||||
@ImageCaptchaCheckMiddleware
|
||||
@ -61,7 +61,7 @@ public class LoginController {
|
||||
data.put("token", token.getToken());
|
||||
data.put("expire", token.getExpire());
|
||||
|
||||
applicationContext.publishEvent(new AdminUserLoginEvent(this, adminUser.getId(), new Date(), token.getToken(), IpUtil.getIpAddress(), adminUser.getLoginTimes()));
|
||||
context.publishEvent(new AdminUserLoginEvent(this, adminUser.getId(), new Date(), token.getToken(), IpUtil.getIpAddress(), adminUser.getLoginTimes()));
|
||||
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
@ -3,21 +3,26 @@ package xyz.playedu.api.controller.backend;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.constant.BPermissionConstant;
|
||||
import xyz.playedu.api.domain.User;
|
||||
import xyz.playedu.api.domain.UserDepartment;
|
||||
import xyz.playedu.api.event.UserDestroyEvent;
|
||||
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
|
||||
import xyz.playedu.api.request.backend.UserRequest;
|
||||
import xyz.playedu.api.service.UserDepartmentService;
|
||||
import xyz.playedu.api.service.UserService;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
import xyz.playedu.api.types.paginate.UserPaginateFilter;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
@ -32,23 +37,14 @@ public class UserController {
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
private UserDepartmentService userDepartmentService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@BackendPermissionMiddleware(slug = BPermissionConstant.USER_INDEX)
|
||||
@GetMapping("/index")
|
||||
public JsonResponse index(
|
||||
@RequestParam(name = "page", defaultValue = "1") Integer page,
|
||||
@RequestParam(name = "size", defaultValue = "10") Integer size,
|
||||
@RequestParam(name = "name", required = false) String name,
|
||||
@RequestParam(name = "email", required = false) String email,
|
||||
@RequestParam(name = "nickname", required = false) String nickname,
|
||||
@RequestParam(name = "id_card", required = false) String idCard,
|
||||
@RequestParam(name = "is_active", required = false) Integer isActive,
|
||||
@RequestParam(name = "is_lock", required = false) Integer isLock,
|
||||
@RequestParam(name = "is_verify", required = false) Integer isVerify,
|
||||
@RequestParam(name = "is_set_password", required = false) Integer isSetPassword,
|
||||
@RequestParam(name = "created_at", required = false) Date[] createdAt
|
||||
) {
|
||||
public JsonResponse index(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "10") Integer size, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "email", required = false) String email, @RequestParam(name = "nickname", required = false) String nickname, @RequestParam(name = "id_card", required = false) String idCard, @RequestParam(name = "is_active", required = false) Integer isActive, @RequestParam(name = "is_lock", required = false) Integer isLock, @RequestParam(name = "is_verify", required = false) Integer isVerify, @RequestParam(name = "is_set_password", required = false) Integer isSetPassword, @RequestParam(name = "created_at", required = false) Date[] createdAt) {
|
||||
UserPaginateFilter filter = new UserPaginateFilter();
|
||||
if (name != null && name.length() > 0) {
|
||||
filter.setName(name);
|
||||
@ -90,6 +86,7 @@ public class UserController {
|
||||
|
||||
@BackendPermissionMiddleware(slug = BPermissionConstant.USER_STORE)
|
||||
@PostMapping("/create")
|
||||
@Transactional
|
||||
public JsonResponse store(@RequestBody @Validated UserRequest request) {
|
||||
if (userService.emailIsExists(request.getEmail())) {
|
||||
return JsonResponse.error("邮箱已存在");
|
||||
@ -120,6 +117,17 @@ public class UserController {
|
||||
|
||||
userService.save(user);
|
||||
|
||||
if (request.getDepIds() != null && request.getDepIds().length > 0) {
|
||||
List<UserDepartment> userDepartments = new ArrayList<>();
|
||||
for (int i = 0; i < request.getDepIds().length; i++) {
|
||||
UserDepartment userDepartment = new UserDepartment();
|
||||
userDepartment.setUserId(user.getId());
|
||||
userDepartment.setDepId(request.getDepIds()[i]);
|
||||
userDepartments.add(userDepartment);
|
||||
}
|
||||
userDepartmentService.saveBatch(userDepartments);
|
||||
}
|
||||
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@ -139,6 +147,7 @@ public class UserController {
|
||||
|
||||
@BackendPermissionMiddleware(slug = BPermissionConstant.USER_UPDATE)
|
||||
@PutMapping("/{id}")
|
||||
@Transactional
|
||||
public JsonResponse update(@PathVariable(name = "id") Integer id, @RequestBody @Validated UserRequest request) {
|
||||
User user = userService.getById(id);
|
||||
if (user == null) {
|
||||
@ -173,6 +182,20 @@ public class UserController {
|
||||
|
||||
userService.updateById(newUser);
|
||||
|
||||
//先删除关联关系
|
||||
userDepartmentService.removeByUserId(user.getId());
|
||||
|
||||
if (request.getDepIds() != null && request.getDepIds().length > 0) { //重新建立关系
|
||||
List<UserDepartment> userDepartments = new ArrayList<>();
|
||||
for (int i = 0; i < request.getDepIds().length; i++) {
|
||||
UserDepartment userDepartment = new UserDepartment();
|
||||
userDepartment.setUserId(user.getId());
|
||||
userDepartment.setDepId(request.getDepIds()[i]);
|
||||
userDepartments.add(userDepartment);
|
||||
}
|
||||
userDepartmentService.saveBatch(userDepartments);
|
||||
}
|
||||
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@ -185,7 +208,7 @@ public class UserController {
|
||||
}
|
||||
userService.removeById(user.getId());
|
||||
|
||||
applicationContext.publishEvent(new UserDestroyEvent(this, user.getId(), new Date()));
|
||||
context.publishEvent(new UserDestroyEvent(this, user.getId(), new Date()));
|
||||
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
63
src/main/java/xyz/playedu/api/domain/UserDepartment.java
Normal file
63
src/main/java/xyz/playedu/api/domain/UserDepartment.java
Normal file
@ -0,0 +1,63 @@
|
||||
package xyz.playedu.api.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @TableName user_department
|
||||
*/
|
||||
@TableName(value = "user_department")
|
||||
@Data
|
||||
public class UserDepartment implements Serializable {
|
||||
@JsonProperty("user_id")
|
||||
private Integer userId;
|
||||
|
||||
@JsonProperty("dep_id")
|
||||
private Integer depId;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object that) {
|
||||
if (this == that) {
|
||||
return true;
|
||||
}
|
||||
if (that == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != that.getClass()) {
|
||||
return false;
|
||||
}
|
||||
UserDepartment other = (UserDepartment) that;
|
||||
return (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
|
||||
&& (this.getDepId() == null ? other.getDepId() == null : this.getDepId().equals(other.getDepId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
|
||||
result = prime * result + ((getDepId() == null) ? 0 : getDepId().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getClass().getSimpleName());
|
||||
sb.append(" [");
|
||||
sb.append("Hash = ").append(hashCode());
|
||||
sb.append(", userId=").append(userId);
|
||||
sb.append(", depId=").append(depId);
|
||||
sb.append(", serialVersionUID=").append(serialVersionUID);
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package xyz.playedu.api.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xyz.playedu.api.event.UserDestroyEvent;
|
||||
import xyz.playedu.api.service.UserDepartmentService;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/23 15:18
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class UserDestroyListener {
|
||||
|
||||
@Autowired
|
||||
private UserDepartmentService userDepartmentService;
|
||||
|
||||
@Order(1)
|
||||
@EventListener
|
||||
public void updateLoginInfo(UserDestroyEvent event) {
|
||||
userDepartmentService.removeByUserId(event.getUserId());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package xyz.playedu.api.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import xyz.playedu.api.domain.UserDepartment;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【user_department】的数据库操作Mapper
|
||||
* @createDate 2023-02-23 15:08:38
|
||||
* @Entity xyz.playedu.api.domain.UserDepartment
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserDepartmentMapper extends BaseMapper<UserDepartment> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -51,4 +51,7 @@ public class UserRequest {
|
||||
@JsonProperty("is_set_password")
|
||||
private Integer isSetPassword;
|
||||
|
||||
@JsonProperty("dep_ids")
|
||||
private Integer[] depIds;
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
package xyz.playedu.api.service;
|
||||
|
||||
import xyz.playedu.api.domain.UserDepartment;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【user_department】的数据库操作Service
|
||||
* @createDate 2023-02-23 15:08:38
|
||||
*/
|
||||
public interface UserDepartmentService extends IService<UserDepartment> {
|
||||
|
||||
void removeByUserId(Integer userId);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import xyz.playedu.api.domain.UserDepartment;
|
||||
import xyz.playedu.api.service.UserDepartmentService;
|
||||
import xyz.playedu.api.mapper.UserDepartmentMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【user_department】的数据库操作Service实现
|
||||
* @createDate 2023-02-23 15:08:38
|
||||
*/
|
||||
@Service
|
||||
public class UserDepartmentServiceImpl extends ServiceImpl<UserDepartmentMapper, UserDepartment>
|
||||
implements UserDepartmentService {
|
||||
@Override
|
||||
public void removeByUserId(Integer userId) {
|
||||
remove(query().getWrapper().eq("user_id", userId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
15
src/main/resources/mapper/UserDepartmentMapper.xml
Normal file
15
src/main/resources/mapper/UserDepartmentMapper.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="xyz.playedu.api.mapper.UserDepartmentMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.UserDepartment">
|
||||
<result property="userId" column="user_id" jdbcType="INTEGER"/>
|
||||
<result property="depId" column="dep_id" jdbcType="INTEGER"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
user_id,dep_id
|
||||
</sql>
|
||||
</mapper>
|
Loading…
x
Reference in New Issue
Block a user