部门代码优化

This commit is contained in:
none 2023-02-25 11:18:09 +08:00
parent bfa1040a54
commit 5672c70ccb
7 changed files with 69 additions and 83 deletions

View File

@ -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;
}
}

View File

@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
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.domain.Department;
import xyz.playedu.api.event.DepartmentDestroyEvent;
@ -30,9 +30,6 @@ public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@Autowired
private DepartmentBus departmentBus;
@Autowired
private ApplicationContext ctx;
@ -56,22 +53,8 @@ public class DepartmentController {
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_STORE)
@PostMapping("/create")
public JsonResponse store(@RequestBody @Validated DepartmentRequest request) throws NotFoundException {
String parentChain = "";
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);
public JsonResponse store(@RequestBody @Validated DepartmentRequest req) throws NotFoundException {
departmentService.create(req.getName(), req.getParentId(), req.getSort());
return JsonResponse.success();
}
@ -84,9 +67,9 @@ public class DepartmentController {
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_UPDATE)
@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);
departmentService.update(department, request.getName(), request.getParentId(), request.getSort());
departmentService.update(department, req.getName(), req.getParentId(), req.getSort());
return JsonResponse.success();
}
@ -95,9 +78,7 @@ public class DepartmentController {
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
Department department = departmentService.findOrFail(id);
departmentService.deleteById(department.getId());
ctx.publishEvent(new DepartmentDestroyEvent(this, id, new Date()));
ctx.publishEvent(new DepartmentDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), department.getId(), new Date()));
return JsonResponse.success();
}

View File

@ -1,6 +1,5 @@
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.validation.annotation.Validated;
@ -25,7 +24,6 @@ import xyz.playedu.api.util.RequestUtil;
import java.util.Date;
import java.util.HashMap;
@Slf4j
@RestController
@RequestMapping("/backend/v1/auth")
public class LoginController {
@ -44,14 +42,14 @@ public class LoginController {
public JsonResponse login(@RequestBody @Validated LoginRequest loginRequest) {
AdminUser adminUser = adminUserService.findByEmail(loginRequest.email);
if (adminUser == null) {
return JsonResponse.error("邮箱不存在");
return JsonResponse.error("邮箱或密码错误");
}
String password = HelperUtil.MD5(loginRequest.getPassword() + adminUser.getSalt()).toLowerCase();
if (!adminUser.getPassword().equals(password)) {
return JsonResponse.error("密码错误");
return JsonResponse.error("邮箱或密码错误");
}
if (adminUser.getIsBanLogin() == 1) {
return JsonResponse.error("当前用户禁止登录");
return JsonResponse.error("当前用户禁止登录");
}
String url = RequestUtil.url();

View File

@ -15,10 +15,12 @@ import java.util.Date;
public class DepartmentDestroyEvent extends ApplicationEvent {
private Integer depId;
private Integer adminId;
private Date at;
public DepartmentDestroyEvent(Object source, Integer depId, Date date) {
public DepartmentDestroyEvent(Object source, Integer adminId, Integer depId, Date date) {
super(source);
this.adminId = adminId;
this.depId = depId;
this.at = date;
}

View File

@ -1,6 +1,7 @@
package xyz.playedu.api.request.backend;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
@ -17,15 +18,16 @@ public class DepartmentRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@NotNull(message = "请输入部门名称")
@NotBlank(message = "请输入部门名称")
@NotNull(message = "name参数不存在")
@Length(min = 1, max = 20, message = "部门名称长度在1-20个字符之间")
private String name;
@JsonProperty("parent_id")
@NotNull(message = "请选择上级部门")
@NotNull(message = "parent_id参数不存在")
private Integer parentId;
@NotNull(message = "请输入排序值")
@NotNull(message = "sort参数不存在")
private Integer sort;
}

View File

@ -25,4 +25,10 @@ public interface DepartmentService extends IService<Department> {
List<Integer> allIds();
String compParentChain(Integer parentId) throws NotFoundException;
String childrenParentChain(Department department);
void create(String name, Integer parentId, Integer sort) throws NotFoundException;
}

View File

@ -1,11 +1,8 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.playedu.api.bus.DepartmentBus;
import xyz.playedu.api.domain.Department;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.DepartmentService;
@ -13,7 +10,7 @@ import xyz.playedu.api.mapper.DepartmentMapper;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
@ -48,7 +45,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
@Transactional
public void deleteById(Integer id) throws NotFoundException {
Department department = findOrFail(id);
updateParentChain(department.getParentChain(), DepartmentBus.childrenParentChain(department));
updateParentChain(department.getParentChain(), childrenParentChain(department));
removeById(department.getId());
}
@ -56,7 +53,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
@Transactional
public void update(Department department, String name, Integer parentId, Integer sort) throws NotFoundException {
//计算该部门作为其它子部门的parentChain值
String childrenChainPrefix = DepartmentBus.childrenParentChain(department);
String childrenChainPrefix = childrenParentChain(department);
Department data = new Department();
data.setId(department.getId());
@ -71,7 +68,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
data.setParentChain("");
} else {
Department parentDepartment = findOrFail(parentId);
data.setParentChain(DepartmentBus.childrenParentChain(parentDepartment));
data.setParentChain(childrenParentChain(parentDepartment));
}
}
if (!department.getSort().equals(sort)) {//更换部门排序值
@ -82,7 +79,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
updateById(data);
department = getById(department.getId());
updateParentChain(DepartmentBus.childrenParentChain(department), childrenChainPrefix);
updateParentChain(childrenParentChain(department), childrenChainPrefix);
}
private void updateParentChain(String newChildrenPC, String oldChildrenPC) {
@ -125,6 +122,47 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
}
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);
}
}