mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-11-14 14:43:48 +08:00
优化枚举类校验;优化Swagger配置,添加默认参数;fix删除部门;优化ResponseDTO;
This commit is contained in:
@@ -85,6 +85,10 @@ public class ResponseDTO<T> {
|
|||||||
return new ResponseDTO<T>(codeConst, msg);
|
return new ResponseDTO<T>(codeConst, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> ResponseDTO<T> wrapMsg(ResponseCodeConst codeConst, String msg) {
|
||||||
|
return new ResponseDTO<T>(codeConst, msg);
|
||||||
|
}
|
||||||
|
|
||||||
public String getMsg() {
|
public String getMsg() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,10 @@
|
|||||||
package net.lab1024.smartadmin.common.validator.en;
|
package net.lab1024.smartadmin.common.validator.en;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import net.lab1024.smartadmin.common.domain.BaseEnum;
|
import net.lab1024.smartadmin.common.domain.BaseEnum;
|
||||||
import net.lab1024.smartadmin.module.support.file.constant.FileServiceTypeEnum;
|
|
||||||
|
|
||||||
import javax.validation.ConstraintValidator;
|
import javax.validation.ConstraintValidator;
|
||||||
import javax.validation.ConstraintValidatorContext;
|
import javax.validation.ConstraintValidatorContext;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -22,9 +17,9 @@ import java.util.stream.Stream;
|
|||||||
public class EnumValidator implements ConstraintValidator<CheckEnum, Object> {
|
public class EnumValidator implements ConstraintValidator<CheckEnum, Object> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 枚举类的类对象
|
* 枚举类实例集合
|
||||||
*/
|
*/
|
||||||
private Class<? extends BaseEnum> enumClass;
|
private List<Object> enumValList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否必须
|
* 是否必须
|
||||||
@@ -34,8 +29,9 @@ public class EnumValidator implements ConstraintValidator<CheckEnum, Object> {
|
|||||||
@Override
|
@Override
|
||||||
public void initialize(CheckEnum constraintAnnotation) {
|
public void initialize(CheckEnum constraintAnnotation) {
|
||||||
// 获取注解传入的枚举类对象
|
// 获取注解传入的枚举类对象
|
||||||
enumClass = constraintAnnotation.enumClazz();
|
|
||||||
required = constraintAnnotation.required();
|
required = constraintAnnotation.required();
|
||||||
|
Class<? extends BaseEnum> enumClass = constraintAnnotation.enumClazz();
|
||||||
|
enumValList = Stream.of(enumClass.getEnumConstants()).map(BaseEnum::getValue).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -51,7 +47,7 @@ public class EnumValidator implements ConstraintValidator<CheckEnum, Object> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 校验是否为合法的枚举值
|
// 校验是否为合法的枚举值
|
||||||
return this.hasEnum(value);
|
return enumValList.contains(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,23 +66,6 @@ public class EnumValidator implements ConstraintValidator<CheckEnum, Object> {
|
|||||||
if (count != list.size()) {
|
if (count != list.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
List<Object> enumValList = Stream.of(enumClass.getEnumConstants()).map(BaseEnum::getValue).collect(Collectors.toList());
|
return enumValList.containsAll(list);
|
||||||
for (Object obj : list) {
|
|
||||||
if (!enumValList.contains(obj)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasEnum(Object value) {
|
|
||||||
// 校验是否为合法的枚举值
|
|
||||||
BaseEnum[] enums = enumClass.getEnumConstants();
|
|
||||||
for (BaseEnum baseEnum : enums) {
|
|
||||||
if (baseEnum.getValue().equals(value)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package net.lab1024.smartadmin.config;
|
package net.lab1024.smartadmin.config;
|
||||||
|
|
||||||
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
||||||
import net.lab1024.smartadmin.constant.SwaggerTagConst;
|
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
import com.google.common.base.Predicates;
|
import com.google.common.base.Predicates;
|
||||||
@@ -9,6 +8,8 @@ import com.google.common.collect.Lists;
|
|||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.lab1024.smartadmin.constant.SwaggerTagConst;
|
||||||
|
import net.lab1024.smartadmin.interceptor.SmartAuthenticationInterceptor;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
@@ -23,8 +24,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import springfox.documentation.RequestHandler;
|
import springfox.documentation.RequestHandler;
|
||||||
import springfox.documentation.builders.ApiInfoBuilder;
|
import springfox.documentation.builders.ApiInfoBuilder;
|
||||||
|
import springfox.documentation.builders.ParameterBuilder;
|
||||||
import springfox.documentation.builders.PathSelectors;
|
import springfox.documentation.builders.PathSelectors;
|
||||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||||
|
import springfox.documentation.schema.ModelRef;
|
||||||
import springfox.documentation.service.*;
|
import springfox.documentation.service.*;
|
||||||
import springfox.documentation.spi.DocumentationType;
|
import springfox.documentation.spi.DocumentationType;
|
||||||
import springfox.documentation.spi.service.contexts.SecurityContext;
|
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||||
@@ -89,9 +92,9 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
|||||||
|
|
||||||
private String groupName = "default";
|
private String groupName = "default";
|
||||||
|
|
||||||
private List<String> groupList = Lists.newArrayList();
|
private final List<String> groupList = Lists.newArrayList();
|
||||||
|
|
||||||
private Map<String, List<String>> groupMap = Maps.newHashMap();
|
private final Map<String, List<String>> groupMap = Maps.newHashMap();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEnvironment(Environment environment) {
|
public void setEnvironment(Environment environment) {
|
||||||
@@ -137,6 +140,16 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Docket baseDocket() {
|
private Docket baseDocket() {
|
||||||
|
// 配置全局参数 token
|
||||||
|
ParameterBuilder tokenPar = new ParameterBuilder();
|
||||||
|
Parameter parameter = tokenPar.name(SmartAuthenticationInterceptor.TOKEN_NAME)
|
||||||
|
.description("token")
|
||||||
|
.modelRef(new ModelRef("string"))
|
||||||
|
.parameterType("header")
|
||||||
|
.defaultValue("")
|
||||||
|
.required(false)
|
||||||
|
.build();
|
||||||
|
|
||||||
// 请求类型过滤规则
|
// 请求类型过滤规则
|
||||||
Predicate<RequestHandler> controllerPredicate = getControllerPredicate();
|
Predicate<RequestHandler> controllerPredicate = getControllerPredicate();
|
||||||
// controller 包路径
|
// controller 包路径
|
||||||
@@ -151,7 +164,8 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
|||||||
.build()
|
.build()
|
||||||
.apiInfo(this.serviceApiInfo())
|
.apiInfo(this.serviceApiInfo())
|
||||||
.securitySchemes(securitySchemes())
|
.securitySchemes(securitySchemes())
|
||||||
.securityContexts(securityContexts());
|
.securityContexts(securityContexts())
|
||||||
|
.globalOperationParameters(Lists.newArrayList(parameter));
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ApiKey> securitySchemes() {
|
private List<ApiKey> securitySchemes() {
|
||||||
@@ -219,6 +233,4 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ import java.util.List;
|
|||||||
@Component
|
@Component
|
||||||
public class SmartAuthenticationInterceptor extends HandlerInterceptorAdapter {
|
public class SmartAuthenticationInterceptor extends HandlerInterceptorAdapter {
|
||||||
|
|
||||||
private static final String TOKEN_NAME = "x-access-token";
|
public static final String TOKEN_NAME = "x-access-token";
|
||||||
|
|
||||||
@Value("${access-control-allow-origin}")
|
@Value("${access-control-allow-origin}")
|
||||||
private String accessControlAllowOrigin;
|
private String accessControlAllowOrigin;
|
||||||
|
|||||||
@@ -59,15 +59,15 @@ public class DepartmentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "删除部门", notes = "删除部门")
|
@ApiOperation(value = "删除部门", notes = "删除部门")
|
||||||
@PostMapping("/department/delete/{departmentId}")
|
@PostMapping("/department/delete/{deptId}")
|
||||||
public ResponseDTO<String> delDepartment(@PathVariable("departmentId") Long departmentId) {
|
public ResponseDTO<String> delDepartment(@PathVariable Long deptId) {
|
||||||
return departmentService.delDepartment(departmentId);
|
return departmentService.delDepartment(deptId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "获取部门信息", notes = "获取部门")
|
@ApiOperation(value = "获取部门信息", notes = "获取部门")
|
||||||
@GetMapping("/department/query/{departmentId}")
|
@GetMapping("/department/query/{deptId}")
|
||||||
public ResponseDTO<DepartmentVO> getDepartment(@PathVariable("departmentId") Long departmentId) {
|
public ResponseDTO<DepartmentVO> getDepartment(@PathVariable Long deptId) {
|
||||||
return departmentService.getDepartmentById(departmentId);
|
return departmentService.getDepartmentById(deptId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "查询部门列表", notes = "查询部门列表")
|
@ApiOperation(value = "查询部门列表", notes = "查询部门列表")
|
||||||
@@ -78,21 +78,21 @@ public class DepartmentController {
|
|||||||
|
|
||||||
|
|
||||||
@ApiOperation(value = "上下移动")
|
@ApiOperation(value = "上下移动")
|
||||||
@GetMapping("/department/upOrDown/{departmentId}/{swapId}")
|
@GetMapping("/department/upOrDown/{deptId}/{swapId}")
|
||||||
public ResponseDTO<String> upOrDown(@PathVariable("departmentId") Long departmentId,@PathVariable("swapId") Long swapId) {
|
public ResponseDTO<String> upOrDown(@PathVariable Long deptId, @PathVariable Long swapId) {
|
||||||
return departmentService.upOrDown(departmentId,swapId);
|
return departmentService.upOrDown(deptId, swapId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "升级")
|
@ApiOperation(value = "升级")
|
||||||
@GetMapping("/department/upgrade/{departmentId}")
|
@GetMapping("/department/upgrade/{deptId}")
|
||||||
public ResponseDTO<String> upgrade(@PathVariable("departmentId") Long departmentId) {
|
public ResponseDTO<String> upgrade(@PathVariable Long deptId) {
|
||||||
return departmentService.upgrade(departmentId);
|
return departmentService.upgrade(deptId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "降级")
|
@ApiOperation(value = "降级")
|
||||||
@GetMapping("/department/downgrade/{departmentId}/{preId}")
|
@GetMapping("/department/downgrade/{deptId}/{preId}")
|
||||||
public ResponseDTO<String> downgrade(@PathVariable("departmentId") Long departmentId,@PathVariable("preId") Long preId) {
|
public ResponseDTO<String> downgrade(@PathVariable Long deptId, @PathVariable Long preId) {
|
||||||
return departmentService.downgrade(departmentId,preId);
|
return departmentService.downgrade(deptId, preId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,14 +19,13 @@ import java.util.List;
|
|||||||
@Mapper
|
@Mapper
|
||||||
public interface DepartmentDao extends BaseMapper<DepartmentEntity> {
|
public interface DepartmentDao extends BaseMapper<DepartmentEntity> {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据部门id,查询此部门子部门的数量
|
* 根据部门id,查询此部门直接子部门的数量
|
||||||
*
|
*
|
||||||
* @param departmentId
|
* @param deptId
|
||||||
* @return int 子部门的数量
|
* @return int 子部门的数量
|
||||||
*/
|
*/
|
||||||
Integer countSubDepartment(@Param("departmentId") Long departmentId);
|
Integer countSubDepartment(@Param("deptId") Long deptId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取全部部门列表
|
* 获取全部部门列表
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public class DepartmentResponseCodeConst extends ResponseCodeConst {
|
|||||||
/**
|
/**
|
||||||
* 部门不存在 1001
|
* 部门不存在 1001
|
||||||
*/
|
*/
|
||||||
public static final DepartmentResponseCodeConst DEPT_NOT_EXISTS = new DepartmentResponseCodeConst(2001, "部门不存在!");
|
public static final DepartmentResponseCodeConst DEPT_NOT_EXISTS = new DepartmentResponseCodeConst(2001, "部门不存在");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前部门有子级部门 不能删除 10003
|
* 当前部门有子级部门 不能删除 10003
|
||||||
|
|||||||
@@ -165,23 +165,27 @@ public class DepartmentService {
|
|||||||
* 1、需要判断当前部门是否有子部门,有子部门则不允许删除
|
* 1、需要判断当前部门是否有子部门,有子部门则不允许删除
|
||||||
* 2、需要判断当前部门是否有员工,有员工则不能删除
|
* 2、需要判断当前部门是否有员工,有员工则不能删除
|
||||||
*
|
*
|
||||||
* @param departmentId
|
* @param deptId
|
||||||
* @return AjaxResult<String>
|
* @return
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public ResponseDTO<String> delDepartment(Long departmentId) {
|
public ResponseDTO<String> delDepartment(Long deptId) {
|
||||||
|
DepartmentEntity departmentEntity = departmentDao.selectById(deptId);
|
||||||
|
if (null == departmentEntity) {
|
||||||
|
return ResponseDTO.wrap(DepartmentResponseCodeConst.DEPT_NOT_EXISTS);
|
||||||
|
}
|
||||||
// 是否有子级部门
|
// 是否有子级部门
|
||||||
int subDepartmentNum = departmentDao.countSubDepartment(departmentId);
|
int subDepartmentNum = departmentDao.countSubDepartment(deptId);
|
||||||
if (subDepartmentNum > 0) {
|
if (subDepartmentNum > 0) {
|
||||||
return ResponseDTO.wrap(DepartmentResponseCodeConst.CANNOT_DEL_DEPARTMENT_WITH_CHILD);
|
return ResponseDTO.wrap(DepartmentResponseCodeConst.CANNOT_DEL_DEPARTMENT_WITH_CHILD);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 是否有员工
|
// 是否有未删除员工
|
||||||
int employeeNum = employeeDao.countByDepartmentId(departmentId);
|
int employeeNum = employeeDao.countByDepartmentId(deptId, false);
|
||||||
if (employeeNum > 0) {
|
if (employeeNum > 0) {
|
||||||
return ResponseDTO.wrap(DepartmentResponseCodeConst.CANNOT_DEL_DEPARTMENT_WITH_EMPLOYEE);
|
return ResponseDTO.wrap(DepartmentResponseCodeConst.CANNOT_DEL_DEPARTMENT_WITH_EMPLOYEE);
|
||||||
}
|
}
|
||||||
departmentDao.deleteById(departmentId);
|
departmentDao.deleteById(deptId);
|
||||||
return ResponseDTO.succ();
|
return ResponseDTO.succ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,5 +282,4 @@ public class DepartmentService {
|
|||||||
departmentDao.updateById(departmentEntity);
|
departmentDao.updateById(departmentEntity);
|
||||||
return ResponseDTO.succ();
|
return ResponseDTO.succ();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ public interface EmployeeDao extends BaseMapper<EmployeeEntity> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 不带分页查询员工列表
|
* 不带分页查询员工列表
|
||||||
|
*
|
||||||
* @param queryDTO
|
* @param queryDTO
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -84,10 +85,11 @@ public interface EmployeeDao extends BaseMapper<EmployeeEntity> {
|
|||||||
/**
|
/**
|
||||||
* 获取某个部门员工数
|
* 获取某个部门员工数
|
||||||
*
|
*
|
||||||
* @param departmentId
|
* @param depId
|
||||||
|
* @param deleteFlag 可以null
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Integer countByDepartmentId(@Param("departmentId") Long departmentId);
|
Integer countByDepartmentId(@Param("depId") Long depId, @Param("deleteFlag") Boolean deleteFlag);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取一批员工
|
* 获取一批员工
|
||||||
@@ -99,6 +101,7 @@ public interface EmployeeDao extends BaseMapper<EmployeeEntity> {
|
|||||||
|
|
||||||
|
|
||||||
EmployeeDTO getEmployeeById(@Param("id") Long employeeId);
|
EmployeeDTO getEmployeeById(@Param("id") Long employeeId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取某个部门的员工
|
* 获取某个部门的员工
|
||||||
*
|
*
|
||||||
@@ -128,6 +131,7 @@ public interface EmployeeDao extends BaseMapper<EmployeeEntity> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询所有员工
|
* 查询所有员工
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<EmployeeVO> selectAll();
|
List<EmployeeVO> selectAll();
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ public class LoginController {
|
|||||||
return loginService.login(loginForm, request);
|
return loginService.login(loginForm, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/session/get")
|
@GetMapping("/session/get")
|
||||||
@ApiOperation(value = "获取session", notes = "获取session")
|
@ApiOperation(value = "获取session", notes = "获取session")
|
||||||
@NoValidPrivilege
|
@NoValidPrivilege
|
||||||
|
|||||||
@@ -18,15 +18,6 @@
|
|||||||
d.create_time
|
d.create_time
|
||||||
</sql>
|
</sql>
|
||||||
|
|
||||||
<select id="countSubDepartment" resultType="integer">
|
|
||||||
SELECT
|
|
||||||
count(1)
|
|
||||||
FROM
|
|
||||||
t_department
|
|
||||||
WHERE
|
|
||||||
parent_id = #{departmentId}
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<select id="listAll" resultMap="DepartmentVO">
|
<select id="listAll" resultMap="DepartmentVO">
|
||||||
SELECT
|
SELECT
|
||||||
<include refid="baseSql"></include>,
|
<include refid="baseSql"></include>,
|
||||||
@@ -50,5 +41,14 @@
|
|||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
<select id="countSubDepartment" resultType="java.lang.Integer">
|
||||||
|
SELECT
|
||||||
|
count(1)
|
||||||
|
FROM
|
||||||
|
t_department
|
||||||
|
WHERE
|
||||||
|
parent_id = #{deptId}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -124,7 +124,10 @@
|
|||||||
FROM
|
FROM
|
||||||
t_employee e
|
t_employee e
|
||||||
WHERE
|
WHERE
|
||||||
e.department_id = #{departmentId}
|
e.department_id = #{depId}
|
||||||
|
<if test="deleteFlag != null">
|
||||||
|
AND e.is_delete = #{deleteFlag}
|
||||||
|
</if>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user