feat: 框架代码全面升级webflux,兼容原
This commit is contained in:
parent
b83b7edaa3
commit
de1350abb3
@ -10,6 +10,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/departments")
|
||||
public class DepartmentController extends TreeController<Department, DepartmentQo> {
|
||||
public class DepartmentController extends ReactiveTreeController<Department, DepartmentQo> {
|
||||
|
||||
}
|
||||
|
@ -12,5 +12,5 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/permissions")
|
||||
public class PermissionController extends TreeController<Permission, PermissionQo> {
|
||||
public class PermissionController extends ReactiveTreeController<Permission, PermissionQo> {
|
||||
}
|
||||
|
@ -63,7 +63,6 @@ public class UserController extends ReactiveBaseController<User, UserQo> {
|
||||
// 检查原密码
|
||||
Assert.isTrue(passwordEncoder.matches(passwordDto.getOldPassword(), user.getPassword()), "原密码不正确!");
|
||||
Assert.isTrue(!passwordEncoder.matches(passwordDto.getPassword(), user.getPassword()), "新密码和旧密码一致,输入个新的吧!");
|
||||
userContext.setUser(user);
|
||||
// 更新密码
|
||||
User updating = new User();
|
||||
updating.setId(user.getId());
|
||||
|
@ -5,7 +5,6 @@ import com.flyfish.framework.bean.Result;
|
||||
import com.flyfish.framework.bean.SyncVo;
|
||||
import com.flyfish.framework.configuration.annotations.PagedQuery;
|
||||
import com.flyfish.framework.configuration.annotations.ValidRequestBody;
|
||||
import com.flyfish.framework.context.UserContext;
|
||||
import com.flyfish.framework.domain.base.Domain;
|
||||
import com.flyfish.framework.domain.base.Qo;
|
||||
import com.flyfish.framework.service.BaseReactiveService;
|
||||
@ -27,8 +26,6 @@ public abstract class ReactiveBaseController<T extends Domain, Q extends Qo<T>>
|
||||
|
||||
@Autowired
|
||||
protected BaseReactiveService<T> reactiveService;
|
||||
@Autowired
|
||||
protected UserContext userContext;
|
||||
|
||||
public <S extends BaseReactiveService<T>> S getService() {
|
||||
return CastUtils.cast(reactiveService);
|
||||
|
@ -0,0 +1,66 @@
|
||||
package com.flyfish.framework.controller;
|
||||
|
||||
import com.flyfish.framework.bean.Result;
|
||||
import com.flyfish.framework.domain.base.Qo;
|
||||
import com.flyfish.framework.domain.base.TreeDomain;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 支持树形解析的controller
|
||||
*
|
||||
* @param <T>
|
||||
* @param <Q>
|
||||
*/
|
||||
public abstract class ReactiveTreeController<T extends TreeDomain<T>, Q extends Qo<T>> extends ReactiveBaseController<T, Q> {
|
||||
|
||||
/**
|
||||
* 获取权限树
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@GetMapping("tree")
|
||||
public Mono<Result<List<T>>> getTree(Q qo) {
|
||||
// 第一步,查询全部,并根据条件筛选
|
||||
return reactiveService.getList(qo)
|
||||
.collectList()
|
||||
.flatMap(filtered -> {
|
||||
// 第二步,根据父id组成map
|
||||
Map<String, List<T>> group = filtered.stream()
|
||||
.collect(Collectors.groupingBy(p -> StringUtils.defaultIfBlank(p.getParentId(), "")));
|
||||
// 第三步,筛选一级树深度
|
||||
return Flux.fromIterable(filtered)
|
||||
.filter(item -> null != item && TreeDomain.ROOT.equals(item.getParentId()))
|
||||
.collectList()
|
||||
// 第四步,根据父id的map填充根tree
|
||||
.map(topList -> applyChildren(topList, group));
|
||||
})
|
||||
.map(Result::accept);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归赋值儿子们
|
||||
*
|
||||
* @param children 儿子们
|
||||
* @param group 分组
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
private List<T> applyChildren(List<T> children, Map<String, List<T>> group) {
|
||||
if (CollectionUtils.isNotEmpty(children)) {
|
||||
children.forEach(child -> {
|
||||
List<T> treeNodes = group.getOrDefault(child.getId(), Collections.emptyList());
|
||||
child.setChildren(treeNodes);
|
||||
applyChildren(treeNodes, group);
|
||||
});
|
||||
}
|
||||
return children;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user