mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-17 16:37:00 +08:00
部门代码优化
This commit is contained in:
parent
bfa1040a54
commit
5672c70ccb
@ -1,41 +0,0 @@
|
|||||||
package xyz.playedu.api.bus;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import xyz.playedu.api.domain.Department;
|
|
||||||
import xyz.playedu.api.exception.NotFoundException;
|
|
||||||
import xyz.playedu.api.service.DepartmentService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author 杭州白书科技有限公司
|
|
||||||
* @create 2023/2/19 11:02
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class DepartmentBus {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private DepartmentService departmentService;
|
|
||||||
|
|
||||||
public String compParentChain(Integer parentId) throws NotFoundException {
|
|
||||||
String parentChain = "";
|
|
||||||
if (parentId != 0) {
|
|
||||||
Department parentDepartment = departmentService.getById(parentId);
|
|
||||||
if (parentDepartment == null) {
|
|
||||||
throw new NotFoundException("父级部门不存在");
|
|
||||||
}
|
|
||||||
String pc = parentDepartment.getParentChain();
|
|
||||||
parentChain = pc == null || pc.length() == 0 ? parentId + "" : pc + "," + parentId;
|
|
||||||
}
|
|
||||||
return parentChain;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String childrenParentChain(Department department) {
|
|
||||||
String prefix = department.getId() + "";
|
|
||||||
if (department.getParentChain() != null && department.getParentChain().length() > 0) {
|
|
||||||
prefix = department.getParentChain() + "," + prefix;
|
|
||||||
}
|
|
||||||
return prefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import xyz.playedu.api.bus.DepartmentBus;
|
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||||
import xyz.playedu.api.constant.BPermissionConstant;
|
import xyz.playedu.api.constant.BPermissionConstant;
|
||||||
import xyz.playedu.api.domain.Department;
|
import xyz.playedu.api.domain.Department;
|
||||||
import xyz.playedu.api.event.DepartmentDestroyEvent;
|
import xyz.playedu.api.event.DepartmentDestroyEvent;
|
||||||
@ -30,9 +30,6 @@ public class DepartmentController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DepartmentService departmentService;
|
private DepartmentService departmentService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private DepartmentBus departmentBus;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext ctx;
|
private ApplicationContext ctx;
|
||||||
|
|
||||||
@ -56,22 +53,8 @@ public class DepartmentController {
|
|||||||
|
|
||||||
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_STORE)
|
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_STORE)
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
public JsonResponse store(@RequestBody @Validated DepartmentRequest request) throws NotFoundException {
|
public JsonResponse store(@RequestBody @Validated DepartmentRequest req) throws NotFoundException {
|
||||||
String parentChain = "";
|
departmentService.create(req.getName(), req.getParentId(), req.getSort());
|
||||||
if (request.getParentId() != 0) {
|
|
||||||
parentChain = departmentBus.compParentChain(request.getParentId());
|
|
||||||
}
|
|
||||||
|
|
||||||
Department department = new Department();
|
|
||||||
department.setName(request.getName());
|
|
||||||
department.setParentId(request.getParentId());
|
|
||||||
department.setParentChain(parentChain);
|
|
||||||
department.setSort(request.getSort());
|
|
||||||
department.setCreatedAt(new Date());
|
|
||||||
department.setUpdatedAt(new Date());
|
|
||||||
|
|
||||||
departmentService.save(department);
|
|
||||||
|
|
||||||
return JsonResponse.success();
|
return JsonResponse.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,9 +67,9 @@ public class DepartmentController {
|
|||||||
|
|
||||||
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_UPDATE)
|
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_UPDATE)
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public JsonResponse update(@PathVariable Integer id, @RequestBody DepartmentRequest request) throws NotFoundException {
|
public JsonResponse update(@PathVariable Integer id, @RequestBody DepartmentRequest req) throws NotFoundException {
|
||||||
Department department = departmentService.findOrFail(id);
|
Department department = departmentService.findOrFail(id);
|
||||||
departmentService.update(department, request.getName(), request.getParentId(), request.getSort());
|
departmentService.update(department, req.getName(), req.getParentId(), req.getSort());
|
||||||
return JsonResponse.success();
|
return JsonResponse.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,9 +78,7 @@ public class DepartmentController {
|
|||||||
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
|
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
|
||||||
Department department = departmentService.findOrFail(id);
|
Department department = departmentService.findOrFail(id);
|
||||||
departmentService.deleteById(department.getId());
|
departmentService.deleteById(department.getId());
|
||||||
|
ctx.publishEvent(new DepartmentDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), department.getId(), new Date()));
|
||||||
ctx.publishEvent(new DepartmentDestroyEvent(this, id, new Date()));
|
|
||||||
|
|
||||||
return JsonResponse.success();
|
return JsonResponse.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package xyz.playedu.api.controller.backend;
|
package xyz.playedu.api.controller.backend;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@ -25,7 +24,6 @@ import xyz.playedu.api.util.RequestUtil;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/backend/v1/auth")
|
@RequestMapping("/backend/v1/auth")
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
@ -44,14 +42,14 @@ public class LoginController {
|
|||||||
public JsonResponse login(@RequestBody @Validated LoginRequest loginRequest) {
|
public JsonResponse login(@RequestBody @Validated LoginRequest loginRequest) {
|
||||||
AdminUser adminUser = adminUserService.findByEmail(loginRequest.email);
|
AdminUser adminUser = adminUserService.findByEmail(loginRequest.email);
|
||||||
if (adminUser == null) {
|
if (adminUser == null) {
|
||||||
return JsonResponse.error("邮箱不存在");
|
return JsonResponse.error("邮箱或密码错误");
|
||||||
}
|
}
|
||||||
String password = HelperUtil.MD5(loginRequest.getPassword() + adminUser.getSalt()).toLowerCase();
|
String password = HelperUtil.MD5(loginRequest.getPassword() + adminUser.getSalt()).toLowerCase();
|
||||||
if (!adminUser.getPassword().equals(password)) {
|
if (!adminUser.getPassword().equals(password)) {
|
||||||
return JsonResponse.error("密码错误");
|
return JsonResponse.error("邮箱或密码错误");
|
||||||
}
|
}
|
||||||
if (adminUser.getIsBanLogin() == 1) {
|
if (adminUser.getIsBanLogin() == 1) {
|
||||||
return JsonResponse.error("当前用户禁止登录");
|
return JsonResponse.error("当前用户已禁止登录");
|
||||||
}
|
}
|
||||||
|
|
||||||
String url = RequestUtil.url();
|
String url = RequestUtil.url();
|
||||||
|
@ -15,10 +15,12 @@ import java.util.Date;
|
|||||||
public class DepartmentDestroyEvent extends ApplicationEvent {
|
public class DepartmentDestroyEvent extends ApplicationEvent {
|
||||||
|
|
||||||
private Integer depId;
|
private Integer depId;
|
||||||
|
private Integer adminId;
|
||||||
private Date at;
|
private Date at;
|
||||||
|
|
||||||
public DepartmentDestroyEvent(Object source, Integer depId, Date date) {
|
public DepartmentDestroyEvent(Object source, Integer adminId, Integer depId, Date date) {
|
||||||
super(source);
|
super(source);
|
||||||
|
this.adminId = adminId;
|
||||||
this.depId = depId;
|
this.depId = depId;
|
||||||
this.at = date;
|
this.at = date;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package xyz.playedu.api.request.backend;
|
package xyz.playedu.api.request.backend;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.hibernate.validator.constraints.Length;
|
import org.hibernate.validator.constraints.Length;
|
||||||
@ -17,15 +18,16 @@ public class DepartmentRequest implements Serializable {
|
|||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@NotNull(message = "请输入部门名称")
|
@NotBlank(message = "请输入部门名称")
|
||||||
|
@NotNull(message = "name参数不存在")
|
||||||
@Length(min = 1, max = 20, message = "部门名称长度在1-20个字符之间")
|
@Length(min = 1, max = 20, message = "部门名称长度在1-20个字符之间")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@JsonProperty("parent_id")
|
@JsonProperty("parent_id")
|
||||||
@NotNull(message = "请选择上级部门")
|
@NotNull(message = "parent_id参数不存在")
|
||||||
private Integer parentId;
|
private Integer parentId;
|
||||||
|
|
||||||
@NotNull(message = "请输入排序值")
|
@NotNull(message = "sort参数不存在")
|
||||||
private Integer sort;
|
private Integer sort;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,4 +25,10 @@ public interface DepartmentService extends IService<Department> {
|
|||||||
|
|
||||||
List<Integer> allIds();
|
List<Integer> allIds();
|
||||||
|
|
||||||
|
String compParentChain(Integer parentId) throws NotFoundException;
|
||||||
|
|
||||||
|
String childrenParentChain(Department department);
|
||||||
|
|
||||||
|
void create(String name, Integer parentId, Integer sort) throws NotFoundException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
package xyz.playedu.api.service.impl;
|
package xyz.playedu.api.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import xyz.playedu.api.bus.DepartmentBus;
|
|
||||||
import xyz.playedu.api.domain.Department;
|
import xyz.playedu.api.domain.Department;
|
||||||
import xyz.playedu.api.exception.NotFoundException;
|
import xyz.playedu.api.exception.NotFoundException;
|
||||||
import xyz.playedu.api.service.DepartmentService;
|
import xyz.playedu.api.service.DepartmentService;
|
||||||
@ -13,7 +10,7 @@ import xyz.playedu.api.mapper.DepartmentMapper;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,7 +45,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void deleteById(Integer id) throws NotFoundException {
|
public void deleteById(Integer id) throws NotFoundException {
|
||||||
Department department = findOrFail(id);
|
Department department = findOrFail(id);
|
||||||
updateParentChain(department.getParentChain(), DepartmentBus.childrenParentChain(department));
|
updateParentChain(department.getParentChain(), childrenParentChain(department));
|
||||||
removeById(department.getId());
|
removeById(department.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +53,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void update(Department department, String name, Integer parentId, Integer sort) throws NotFoundException {
|
public void update(Department department, String name, Integer parentId, Integer sort) throws NotFoundException {
|
||||||
//计算该部门作为其它子部门的parentChain值
|
//计算该部门作为其它子部门的parentChain值
|
||||||
String childrenChainPrefix = DepartmentBus.childrenParentChain(department);
|
String childrenChainPrefix = childrenParentChain(department);
|
||||||
|
|
||||||
Department data = new Department();
|
Department data = new Department();
|
||||||
data.setId(department.getId());
|
data.setId(department.getId());
|
||||||
@ -71,7 +68,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|||||||
data.setParentChain("");
|
data.setParentChain("");
|
||||||
} else {
|
} else {
|
||||||
Department parentDepartment = findOrFail(parentId);
|
Department parentDepartment = findOrFail(parentId);
|
||||||
data.setParentChain(DepartmentBus.childrenParentChain(parentDepartment));
|
data.setParentChain(childrenParentChain(parentDepartment));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!department.getSort().equals(sort)) {//更换部门排序值
|
if (!department.getSort().equals(sort)) {//更换部门排序值
|
||||||
@ -82,7 +79,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|||||||
updateById(data);
|
updateById(data);
|
||||||
|
|
||||||
department = getById(department.getId());
|
department = getById(department.getId());
|
||||||
updateParentChain(DepartmentBus.childrenParentChain(department), childrenChainPrefix);
|
updateParentChain(childrenParentChain(department), childrenChainPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateParentChain(String newChildrenPC, String oldChildrenPC) {
|
private void updateParentChain(String newChildrenPC, String oldChildrenPC) {
|
||||||
@ -125,6 +122,47 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
|
|||||||
}
|
}
|
||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String compParentChain(Integer parentId) throws NotFoundException {
|
||||||
|
String parentChain = "";
|
||||||
|
if (parentId != 0) {
|
||||||
|
Department parentDepartment = getById(parentId);
|
||||||
|
if (parentDepartment == null) {
|
||||||
|
throw new NotFoundException("父级部门不存在");
|
||||||
|
}
|
||||||
|
String pc = parentDepartment.getParentChain();
|
||||||
|
parentChain = pc == null || pc.length() == 0 ? parentId + "" : pc + "," + parentId;
|
||||||
|
}
|
||||||
|
return parentChain;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String childrenParentChain(Department department) {
|
||||||
|
String prefix = department.getId() + "";
|
||||||
|
if (department.getParentChain() != null && department.getParentChain().length() > 0) {
|
||||||
|
prefix = department.getParentChain() + "," + prefix;
|
||||||
|
}
|
||||||
|
return prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void create(String name, Integer parentId, Integer sort) throws NotFoundException {
|
||||||
|
String parentChain = "";
|
||||||
|
if (parentId != 0) {
|
||||||
|
parentChain = compParentChain(parentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
Department department = new Department();
|
||||||
|
department.setName(name);
|
||||||
|
department.setParentId(parentId);
|
||||||
|
department.setParentChain(parentChain);
|
||||||
|
department.setSort(sort);
|
||||||
|
department.setCreatedAt(new Date());
|
||||||
|
department.setUpdatedAt(new Date());
|
||||||
|
|
||||||
|
save(department);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user