mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-09-24 22:36:39 +08:00
优化枚举类校验;优化Swagger配置,添加默认参数;fix删除部门;优化ResponseDTO;
This commit is contained in:
parent
f7155ac4c2
commit
ea0159ddb4
@ -85,6 +85,10 @@ public class ResponseDTO<T> {
|
||||
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() {
|
||||
return msg;
|
||||
}
|
||||
|
@ -1,15 +1,10 @@
|
||||
package net.lab1024.smartadmin.common.validator.en;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import net.lab1024.smartadmin.common.domain.BaseEnum;
|
||||
import net.lab1024.smartadmin.module.support.file.constant.FileServiceTypeEnum;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ -22,9 +17,9 @@ import java.util.stream.Stream;
|
||||
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
|
||||
public void initialize(CheckEnum constraintAnnotation) {
|
||||
// 获取注解传入的枚举类对象
|
||||
enumClass = constraintAnnotation.enumClazz();
|
||||
required = constraintAnnotation.required();
|
||||
Class<? extends BaseEnum> enumClass = constraintAnnotation.enumClazz();
|
||||
enumValList = Stream.of(enumClass.getEnumConstants()).map(BaseEnum::getValue).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@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()) {
|
||||
return false;
|
||||
}
|
||||
List<Object> enumValList = Stream.of(enumClass.getEnumConstants()).map(BaseEnum::getValue).collect(Collectors.toList());
|
||||
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;
|
||||
return enumValList.containsAll(list);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package net.lab1024.smartadmin.config;
|
||||
|
||||
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.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
@ -9,6 +8,8 @@ import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.swagger.annotations.Api;
|
||||
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.factory.config.BeanDefinition;
|
||||
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 springfox.documentation.RequestHandler;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.ParameterBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.schema.ModelRef;
|
||||
import springfox.documentation.service.*;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||
@ -89,9 +92,9 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
||||
|
||||
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
|
||||
public void setEnvironment(Environment environment) {
|
||||
@ -108,7 +111,7 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
||||
this.groupBuild();
|
||||
for (Map.Entry<String, List<String>> entry : groupMap.entrySet()) {
|
||||
String group = entry.getKey();
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Docket.class, this :: baseDocket);
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Docket.class, this::baseDocket);
|
||||
BeanDefinition beanDefinition = builder.getRawBeanDefinition();
|
||||
registry.registerBeanDefinition(group + "Api", beanDefinition);
|
||||
}
|
||||
@ -137,6 +140,16 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
||||
}
|
||||
|
||||
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();
|
||||
// controller 包路径
|
||||
@ -151,17 +164,18 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
||||
.build()
|
||||
.apiInfo(this.serviceApiInfo())
|
||||
.securitySchemes(securitySchemes())
|
||||
.securityContexts(securityContexts());
|
||||
.securityContexts(securityContexts())
|
||||
.globalOperationParameters(Lists.newArrayList(parameter));
|
||||
}
|
||||
|
||||
private List<ApiKey> securitySchemes() {
|
||||
List<ApiKey> apiKeyList= new ArrayList<>();
|
||||
List<ApiKey> apiKeyList = new ArrayList<>();
|
||||
apiKeyList.add(new ApiKey("x-access-token", "x-access-token", "header"));
|
||||
return apiKeyList;
|
||||
}
|
||||
|
||||
private List<SecurityContext> securityContexts() {
|
||||
List<SecurityContext> securityContexts=new ArrayList<>();
|
||||
List<SecurityContext> securityContexts = new ArrayList<>();
|
||||
securityContexts.add(
|
||||
SecurityContext.builder()
|
||||
.securityReferences(defaultAuth())
|
||||
@ -174,7 +188,7 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
||||
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
|
||||
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
||||
authorizationScopes[0] = authorizationScope;
|
||||
List<SecurityReference> securityReferences=new ArrayList<>();
|
||||
List<SecurityReference> securityReferences = new ArrayList<>();
|
||||
securityReferences.add(new SecurityReference("x-access-token", authorizationScopes));
|
||||
return securityReferences;
|
||||
}
|
||||
@ -196,9 +210,9 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
||||
};
|
||||
groupIndex++;
|
||||
return Predicates.or(
|
||||
Predicates.and(RequestHandlerSelectors.withClassAnnotation(RestController.class),methodPredicate),
|
||||
Predicates.and(RequestHandlerSelectors.withClassAnnotation(RestController.class), methodPredicate),
|
||||
Predicates.and(
|
||||
RequestHandlerSelectors.withMethodAnnotation(ResponseBody.class),methodPredicate)
|
||||
RequestHandlerSelectors.withMethodAnnotation(ResponseBody.class), methodPredicate)
|
||||
);
|
||||
}
|
||||
|
||||
@ -219,6 +233,4 @@ public class SmartSwaggerDynamicGroupConfig implements EnvironmentAware, BeanDef
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ import java.util.List;
|
||||
@Component
|
||||
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}")
|
||||
private String accessControlAllowOrigin;
|
||||
|
@ -59,15 +59,15 @@ public class DepartmentController {
|
||||
}
|
||||
|
||||
@ApiOperation(value = "删除部门", notes = "删除部门")
|
||||
@PostMapping("/department/delete/{departmentId}")
|
||||
public ResponseDTO<String> delDepartment(@PathVariable("departmentId") Long departmentId) {
|
||||
return departmentService.delDepartment(departmentId);
|
||||
@PostMapping("/department/delete/{deptId}")
|
||||
public ResponseDTO<String> delDepartment(@PathVariable Long deptId) {
|
||||
return departmentService.delDepartment(deptId);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取部门信息", notes = "获取部门")
|
||||
@GetMapping("/department/query/{departmentId}")
|
||||
public ResponseDTO<DepartmentVO> getDepartment(@PathVariable("departmentId") Long departmentId) {
|
||||
return departmentService.getDepartmentById(departmentId);
|
||||
@GetMapping("/department/query/{deptId}")
|
||||
public ResponseDTO<DepartmentVO> getDepartment(@PathVariable Long deptId) {
|
||||
return departmentService.getDepartmentById(deptId);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "查询部门列表", notes = "查询部门列表")
|
||||
@ -78,21 +78,21 @@ public class DepartmentController {
|
||||
|
||||
|
||||
@ApiOperation(value = "上下移动")
|
||||
@GetMapping("/department/upOrDown/{departmentId}/{swapId}")
|
||||
public ResponseDTO<String> upOrDown(@PathVariable("departmentId") Long departmentId,@PathVariable("swapId") Long swapId) {
|
||||
return departmentService.upOrDown(departmentId,swapId);
|
||||
@GetMapping("/department/upOrDown/{deptId}/{swapId}")
|
||||
public ResponseDTO<String> upOrDown(@PathVariable Long deptId, @PathVariable Long swapId) {
|
||||
return departmentService.upOrDown(deptId, swapId);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "升级")
|
||||
@GetMapping("/department/upgrade/{departmentId}")
|
||||
public ResponseDTO<String> upgrade(@PathVariable("departmentId") Long departmentId) {
|
||||
return departmentService.upgrade(departmentId);
|
||||
@GetMapping("/department/upgrade/{deptId}")
|
||||
public ResponseDTO<String> upgrade(@PathVariable Long deptId) {
|
||||
return departmentService.upgrade(deptId);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "降级")
|
||||
@GetMapping("/department/downgrade/{departmentId}/{preId}")
|
||||
public ResponseDTO<String> downgrade(@PathVariable("departmentId") Long departmentId,@PathVariable("preId") Long preId) {
|
||||
return departmentService.downgrade(departmentId,preId);
|
||||
@GetMapping("/department/downgrade/{deptId}/{preId}")
|
||||
public ResponseDTO<String> downgrade(@PathVariable Long deptId, @PathVariable Long preId) {
|
||||
return departmentService.downgrade(deptId, preId);
|
||||
}
|
||||
|
||||
|
||||
|
@ -19,14 +19,13 @@ import java.util.List;
|
||||
@Mapper
|
||||
public interface DepartmentDao extends BaseMapper<DepartmentEntity> {
|
||||
|
||||
|
||||
/**
|
||||
* 根据部门id,查询此部门子部门的数量
|
||||
* 根据部门id,查询此部门直接子部门的数量
|
||||
*
|
||||
* @param departmentId
|
||||
* @param deptId
|
||||
* @return int 子部门的数量
|
||||
*/
|
||||
Integer countSubDepartment(@Param("departmentId") Long departmentId);
|
||||
Integer countSubDepartment(@Param("deptId") Long deptId);
|
||||
|
||||
/**
|
||||
* 获取全部部门列表
|
||||
|
@ -14,7 +14,7 @@ public class DepartmentResponseCodeConst extends ResponseCodeConst {
|
||||
/**
|
||||
* 部门不存在 1001
|
||||
*/
|
||||
public static final DepartmentResponseCodeConst DEPT_NOT_EXISTS = new DepartmentResponseCodeConst(2001, "部门不存在!");
|
||||
public static final DepartmentResponseCodeConst DEPT_NOT_EXISTS = new DepartmentResponseCodeConst(2001, "部门不存在");
|
||||
|
||||
/**
|
||||
* 当前部门有子级部门 不能删除 10003
|
||||
|
@ -64,7 +64,7 @@ public class DepartmentService {
|
||||
departmentVOList = filterDepartment(departmentVOList, departmentName);
|
||||
}
|
||||
|
||||
Map<Long, DepartmentVO> departmentMap = departmentVOList.stream().collect(Collectors.toMap(DepartmentVO :: getId, Function.identity()));
|
||||
Map<Long, DepartmentVO> departmentMap = departmentVOList.stream().collect(Collectors.toMap(DepartmentVO::getId, Function.identity()));
|
||||
// 获取全部员工列表
|
||||
List<EmployeeDTO> employeeList = employeeDao.listAll();
|
||||
employeeList.forEach(employeeDTO -> {
|
||||
@ -103,7 +103,7 @@ public class DepartmentService {
|
||||
List<DepartmentVO> filterResult = new ArrayList<>();
|
||||
getParentDepartment(departmentVOList, parentId, filterResult);
|
||||
for (DepartmentVO dto : filterResult) {
|
||||
if (! departmentMap.containsKey(dto.getId())) {
|
||||
if (!departmentMap.containsKey(dto.getId())) {
|
||||
departmentMap.put(dto.getId(), dto);
|
||||
}
|
||||
}
|
||||
@ -165,23 +165,27 @@ public class DepartmentService {
|
||||
* 1、需要判断当前部门是否有子部门,有子部门则不允许删除
|
||||
* 2、需要判断当前部门是否有员工,有员工则不能删除
|
||||
*
|
||||
* @param departmentId
|
||||
* @return AjaxResult<String>
|
||||
* @param deptId
|
||||
* @return
|
||||
*/
|
||||
@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) {
|
||||
return ResponseDTO.wrap(DepartmentResponseCodeConst.CANNOT_DEL_DEPARTMENT_WITH_CHILD);
|
||||
}
|
||||
|
||||
// 是否有员工
|
||||
int employeeNum = employeeDao.countByDepartmentId(departmentId);
|
||||
// 是否有未删除员工
|
||||
int employeeNum = employeeDao.countByDepartmentId(deptId, false);
|
||||
if (employeeNum > 0) {
|
||||
return ResponseDTO.wrap(DepartmentResponseCodeConst.CANNOT_DEL_DEPARTMENT_WITH_EMPLOYEE);
|
||||
}
|
||||
departmentDao.deleteById(departmentId);
|
||||
departmentDao.deleteById(deptId);
|
||||
return ResponseDTO.succ();
|
||||
}
|
||||
|
||||
@ -278,5 +282,4 @@ public class DepartmentService {
|
||||
departmentDao.updateById(departmentEntity);
|
||||
return ResponseDTO.succ();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ public interface EmployeeDao extends BaseMapper<EmployeeEntity> {
|
||||
|
||||
/**
|
||||
* 不带分页查询员工列表
|
||||
*
|
||||
* @param queryDTO
|
||||
* @return
|
||||
*/
|
||||
@ -84,10 +85,11 @@ public interface EmployeeDao extends BaseMapper<EmployeeEntity> {
|
||||
/**
|
||||
* 获取某个部门员工数
|
||||
*
|
||||
* @param departmentId
|
||||
* @param depId
|
||||
* @param deleteFlag 可以null
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* 获取某个部门的员工
|
||||
*
|
||||
@ -128,6 +131,7 @@ public interface EmployeeDao extends BaseMapper<EmployeeEntity> {
|
||||
|
||||
/**
|
||||
* 查询所有员工
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<EmployeeVO> selectAll();
|
||||
|
@ -42,7 +42,6 @@ public class LoginController {
|
||||
return loginService.login(loginForm, request);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/session/get")
|
||||
@ApiOperation(value = "获取session", notes = "获取session")
|
||||
@NoValidPrivilege
|
||||
|
@ -18,15 +18,6 @@
|
||||
d.create_time
|
||||
</sql>
|
||||
|
||||
<select id="countSubDepartment" resultType="integer">
|
||||
SELECT
|
||||
count(1)
|
||||
FROM
|
||||
t_department
|
||||
WHERE
|
||||
parent_id = #{departmentId}
|
||||
</select>
|
||||
|
||||
<select id="listAll" resultMap="DepartmentVO">
|
||||
SELECT
|
||||
<include refid="baseSql"></include>,
|
||||
@ -50,5 +41,14 @@
|
||||
</select>
|
||||
|
||||
|
||||
<select id="countSubDepartment" resultType="java.lang.Integer">
|
||||
SELECT
|
||||
count(1)
|
||||
FROM
|
||||
t_department
|
||||
WHERE
|
||||
parent_id = #{deptId}
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
@ -124,7 +124,10 @@
|
||||
FROM
|
||||
t_employee e
|
||||
WHERE
|
||||
e.department_id = #{departmentId}
|
||||
e.department_id = #{depId}
|
||||
<if test="deleteFlag != null">
|
||||
AND e.is_delete = #{deleteFlag}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user