完善代码
diff --git a/samples/client/src/main/java/com/supwisdom/leaveschool/client/controller/web/admin/WebAdminUserController.java b/samples/client/src/main/java/com/supwisdom/leaveschool/client/controller/web/admin/WebAdminUserController.java
index d1d68a0..88246c9 100644
--- a/samples/client/src/main/java/com/supwisdom/leaveschool/client/controller/web/admin/WebAdminUserController.java
+++ b/samples/client/src/main/java/com/supwisdom/leaveschool/client/controller/web/admin/WebAdminUserController.java
@@ -2,6 +2,7 @@
import java.util.HashMap;
+import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,12 +30,12 @@
logger.debug(AuthenticationUtil.currentUsername());
- PagerRequestModel pagerRequestModel = new PagerRequestModel(); // 分页请求,默认 pageIndex=0, pageSize=20
- // 查询条件
- pagerRequestModel.setMapBean(new HashMap<String, Object>());
- pagerRequestModel.getMapBean().put("status", "1");
+ int pageIndex = 0;
+ int pageSize = 20;
+ Map<String, Object> mapBean = new HashMap<String, Object>();
+ mapBean.put("mapBean[status]", "1");
- PagerResponseModel<User> users = sampleUser1AdminUserRemoteService.list(pagerRequestModel);
+ PagerResponseModel<User> users = sampleUser1AdminUserRemoteService.list(pageIndex, pageSize, mapBean);
logger.debug("list is {}, {}", users==null?"":users.getClass().getName(), users);
diff --git a/samples/client/src/main/java/com/supwisdom/leaveschool/client/controller/web/api/HelloController.java b/samples/client/src/main/java/com/supwisdom/leaveschool/client/controller/web/api/HelloController.java
index 5719a86..35d19bc 100644
--- a/samples/client/src/main/java/com/supwisdom/leaveschool/client/controller/web/api/HelloController.java
+++ b/samples/client/src/main/java/com/supwisdom/leaveschool/client/controller/web/api/HelloController.java
@@ -3,10 +3,14 @@
import java.util.HashMap;
import java.util.Map;
+import org.springframework.http.HttpStatus;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@@ -19,4 +23,13 @@
result.put("message", "Good " + name);
return result;
}
+
+ @PostMapping(consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
+ @ResponseStatus(value = HttpStatus.OK)
+ @ResponseBody
+ public String post() {
+
+
+ return "abc";
+ }
}
diff --git a/samples/client/src/main/java/com/supwisdom/leaveschool/client/service/SampleUser1AdminUserRemoteService.java b/samples/client/src/main/java/com/supwisdom/leaveschool/client/service/SampleUser1AdminUserRemoteService.java
index 6d361df..1cf0886 100644
--- a/samples/client/src/main/java/com/supwisdom/leaveschool/client/service/SampleUser1AdminUserRemoteService.java
+++ b/samples/client/src/main/java/com/supwisdom/leaveschool/client/service/SampleUser1AdminUserRemoteService.java
@@ -1,14 +1,16 @@
package com.supwisdom.leaveschool.client.service;
+import java.util.Map;
+
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
import com.supwisdom.leaveschool.client.service.fallback.SampleUser1AdminUserRemoteHystrix;
-import com.supwisdom.leaveschool.proxy.common.model.PagerRequestModel;
import com.supwisdom.leaveschool.proxy.common.model.PagerResponseModel;
import com.supwisdom.leaveschool.proxy.common.model.SuccessResponseModel;
import com.supwisdom.leaveschool.proxy.user.domain.User;
@@ -21,7 +23,7 @@
public interface SampleUser1AdminUserRemoteService {
@RequestMapping(method = RequestMethod.GET, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
- public PagerResponseModel<User> list(PagerRequestModel pagerRequestModel);
+ public PagerResponseModel<User> list(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize, @RequestParam("mapBean") Map<String, Object> mapBean);
@RequestMapping(path = "/{id}", method = RequestMethod.GET, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public User get(@PathVariable("id") String id);
diff --git a/samples/client/src/main/java/com/supwisdom/leaveschool/client/service/fallback/SampleUser1AdminUserRemoteHystrix.java b/samples/client/src/main/java/com/supwisdom/leaveschool/client/service/fallback/SampleUser1AdminUserRemoteHystrix.java
index e60a53b..3cc046d 100644
--- a/samples/client/src/main/java/com/supwisdom/leaveschool/client/service/fallback/SampleUser1AdminUserRemoteHystrix.java
+++ b/samples/client/src/main/java/com/supwisdom/leaveschool/client/service/fallback/SampleUser1AdminUserRemoteHystrix.java
@@ -1,11 +1,12 @@
package com.supwisdom.leaveschool.client.service.fallback;
+import java.util.Map;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.supwisdom.leaveschool.client.service.SampleUser1AdminUserRemoteService;
-import com.supwisdom.leaveschool.proxy.common.model.PagerRequestModel;
import com.supwisdom.leaveschool.proxy.common.model.PagerResponseModel;
import com.supwisdom.leaveschool.proxy.common.model.SuccessResponseModel;
import com.supwisdom.leaveschool.proxy.user.domain.User;
@@ -16,7 +17,7 @@
private static final Logger logger = LoggerFactory.getLogger(SampleUser1AdminUserRemoteHystrix.class);
@Override
- public PagerResponseModel<User> list(PagerRequestModel pagerRequestModel) {
+ public PagerResponseModel<User> list(int pageIndex, int pageSize, Map<String, Object> mapBean) {
return null;
}
diff --git a/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/PermissionRepository.java b/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/PermissionRepository.java
index db67b3c..9d7e50d 100644
--- a/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/PermissionRepository.java
+++ b/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/PermissionRepository.java
@@ -52,21 +52,24 @@
@Query(value = "select max(p.rgt) from Permission p")
public int selectMaxRgt();
+ @Query(value = "select p from Permission p where p.lft>:lft and p.rgt<:rgt order by p.lft")
+ public List<Permission> selectBetweenLftRgt(@Param("lft") int lft, @Param("rgt") int rgt);
+
@Modifying
@Query(value = "update TB_U_PERMISSION "
+ "set "
- + " LFT = (case when LFT >= :rgt then LFT + 2 else LFT end), "
- + " RGT = RGT + 2 "
+ + " LFT = (case when LFT >= :rgt then LFT + :offset else LFT end), "
+ + " RGT = RGT + :offset "
+ "where RGT >= :rgt", nativeQuery = true)
- public int updateLftRgtWhenInsert(@Param("rgt") int rgt);
+ public int updateLftRgtWhenInsert(@Param("rgt") int rgt, @Param("offset") int offset);
@Modifying
@Query(value = "update TB_U_PERMISSION "
+ "set "
- + " LFT = (case when LFT >= :rgt then LFT - 2 else LFT end), "
- + " RGT = RGT - 2 "
+ + " LFT = (case when LFT >= :rgt then LFT - :offset else LFT end), "
+ + " RGT = RGT - :offset "
+ "where RGT >= :rgt", nativeQuery = true)
- public int updateLftRgtWhenDelete(@Param("rgt") int rgt);
+ public int updateLftRgtWhenDelete(@Param("rgt") int rgt, @Param("offset") int offset);
@Override
@@ -78,18 +81,19 @@
if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
int maxRgt = selectMaxRgt();
- entity.setLft(maxRgt + 1);
- entity.setRgt(maxRgt + 2);
+ entity.setLft((maxRgt+1));
+ entity.setRgt((maxRgt+1) + 1);
- entity.setLevel(0);
+ entity.setLevel(1);
} else {
Permission parentEntity = this.selectById(entity.getParentId());
if (parentEntity == null) {
throw new RuntimeException(String.format("父级对象不存在!"));
} else {
- // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +2
+ // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
int rgt = parentEntity.getRgt();
- updateLftRgtWhenInsert(rgt);
+ int offset = 2;
+ updateLftRgtWhenInsert(rgt, offset);
entity.setLft(rgt);
entity.setRgt(rgt + 1);
@@ -122,41 +126,68 @@
}
if (!originEntity.getParentId().equals(entity.getParentId()) ) {
+
+ int lft = originEntity.getLft();
+ int rgt = originEntity.getRgt();
+ int level = originEntity.getLevel();
+ int offset = rgt - lft +1;
+ List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
+
if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
- // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -2
- int rgt = originEntity.getRgt();
- updateLftRgtWhenDelete(rgt);
+ // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
+ updateLftRgtWhenDelete(rgt, offset);
int maxRgt = selectMaxRgt();
- entity.setLft(maxRgt + 1);
- entity.setRgt(maxRgt + 2);
+ entity.setLft((maxRgt+1));
+ entity.setRgt((maxRgt+1) + 1 +offset-2);
- entity.setLevel(0);
+ entity.setLevel(1);
} else {
+ // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
+ updateLftRgtWhenDelete(rgt, offset);
+
Permission parentEntity = this.selectById(entity.getParentId());
if (parentEntity == null) {
throw new RuntimeException(String.format("父级对象不存在!"));
}
-
+ //System.out.println(String.format("pLft %s, pRgt %s", parentEntity.getLft(), parentEntity.getRgt()));
if (parentEntity.getLft() >= originEntity.getLft() && parentEntity.getRgt() <= originEntity.getRgt()) {
throw new RuntimeException(String.format("不能设置自身或自身的子节点作为父级!"));
}
- // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -2
- int rgt = originEntity.getRgt();
- updateLftRgtWhenDelete(rgt);
-
- parentEntity = this.selectById(entity.getParentId());
- // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +2
+ //parentEntity = this.selectById(entity.getParentId()); System.out.println(String.format("pLft %s, pRgt %s", parentEntity.getLft(), parentEntity.getRgt()));
+ // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
+ //int pLft = parentEntity.getLft();
int pRgt = parentEntity.getRgt();
- updateLftRgtWhenInsert(pRgt);
+ updateLftRgtWhenInsert(pRgt, offset);
entity.setLft(pRgt);
- entity.setRgt(pRgt + 1);
-
+ entity.setRgt(pRgt + 1 + offset-2);
+
entity.setLevel(parentEntity.getLevel() + 1);
}
+
+ int newLft = entity.getLft();
+ int newRgt = entity.getRgt();
+ int newLevel = entity.getLevel();
+ //System.out.println(String.format("newLft %s, newRgt %s, newLevel %s", newLft, newRgt, newLevel));
+ //System.out.println(String.format("lft %s, rgt %s, level %s", lft, rgt, level));
+ for (Permission childEntity : childEntities) {
+ //Permission pEntity = this.selectById(childEntity.getParentId());
+
+ int cLft = childEntity.getLft();
+ int cRgt = childEntity.getRgt();
+ int cLevel = childEntity.getLevel();
+
+ childEntity.setLft(cLft + (newLft - lft));
+ childEntity.setRgt(cRgt + (newRgt - rgt));
+
+ childEntity.setLevel(cLevel + (newLevel - level));
+
+ BaseJpaRepository.super.update(childEntity);
+ }
+
}
return BaseJpaRepository.super.update(entity);
@@ -169,12 +200,23 @@
if (originEntity == null) {
return;
}
-
- // FIXME: 判断是否有子节点
-
- // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -2
+
+ int lft = originEntity.getLft();
int rgt = originEntity.getRgt();
- updateLftRgtWhenDelete(rgt);
+ int offset = rgt - lft +1;
+
+ // FIXME: 判断是否有子节点
+ //if (lft + 1 != rgt) {
+ // return;
+ //}
+
+ List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
+ for (Permission childEntity : childEntities) {
+ BaseJpaRepository.super.delete(childEntity.getId());
+ }
+
+ // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
+ updateLftRgtWhenDelete(rgt, offset);
BaseJpaRepository.super.delete(id);
}
diff --git a/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/UserRepository.java b/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/UserRepository.java
index dbeb279..625cf29 100644
--- a/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/UserRepository.java
+++ b/samples/user/src/main/java/com/supwisdom/leaveschool/user/repository/UserRepository.java
@@ -16,6 +16,21 @@
@Repository
public interface UserRepository extends BaseJpaRepository<User> {
+ public default Page<User> selectPageList(int pageIndex, int pageSize, User probe) {
+
+ ExampleMatcher matcher = ExampleMatcher.matching()
+ .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
+ .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
+ .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact());
+
+ PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
+ Example<User> example = Example.of(probe, matcher);
+
+ Page<User> page = this.findAll(example, pageRequest);
+
+ return page;
+ }
+
public default Page<User> selectPageList(int pageIndex, int pageSize, Map<String, Object> mapBean) {
User probe = new User();
if (mapBean != null) {