提交 0566adad 编写于 作者: X xiongchun

重构资源模块和诸多改进

上级 f2f41520
package com.gitee.myclouds.base.helper.treebuiler;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.Setter;
/**
* 树结构数据模型构造器
*
* @author xiongchun
*
*/
public class TreeBuilder {
@Getter
@Setter
List<TreeNodeVO> nodes = Lists.newArrayList();
public TreeBuilder() {
}
public TreeBuilder(List<TreeNodeVO> nodes) {
super();
this.nodes = nodes;
}
public String buildJSONTree() {
List<TreeNodeVO> nodeTree = buildTree();
return JSON.toJSONString(nodeTree);
}
public List<TreeNodeVO> buildTree() {
List<TreeNodeVO> treeTreeNodeVOs = Lists.newArrayList();
List<TreeNodeVO> rootTreeNodeVOs = getParentTreeNodes();
for (TreeNodeVO rootTreeNodeVO : rootTreeNodeVOs) {
buildChildTreeNodes(rootTreeNodeVO);
treeTreeNodeVOs.add(rootTreeNodeVO);
}
return treeTreeNodeVOs;
}
private void buildChildTreeNodes(TreeNodeVO node) {
List<TreeNodeVO> children = getChildTreeNodes(node);
if (!children.isEmpty()) {
for (TreeNodeVO child : children) {
buildChildTreeNodes(child);
}
node.setChildren(children);
}
}
public List<TreeNodeVO> getChildTreeNodes(TreeNodeVO pnode) {
List<TreeNodeVO> childTreeNodeVOs = Lists.newArrayList();
for (TreeNodeVO n : nodes) {
if (pnode.getId().equals(n.getPId())) {
childTreeNodeVOs.add(n);
}
}
return childTreeNodeVOs;
}
private boolean isParentNode(TreeNodeVO node) {
boolean isParentNode = true;
for (TreeNodeVO n : nodes) {
if (node.getPId().equals(n.getId())) {
isParentNode = false;
break;
}
}
return isParentNode;
}
public List<TreeNodeVO> getParentTreeNodes() {
List<TreeNodeVO> rootTreeNodeVOs = Lists.newArrayList();
for (TreeNodeVO n : nodes) {
if (isParentNode(n)) {
rootTreeNodeVOs.add(n);
}
}
return rootTreeNodeVOs;
}
}
package com.gitee.myclouds.base.helper.treebuiler;
import java.io.Serializable;
import java.util.List;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 树节点VO
*
* @author xiongchun
*
*/
@Data
@Accessors(chain = true)
public class TreeNodeVO implements Serializable {
private static final long serialVersionUID = 1L;
// 节点ID
private String id;
// 父节点ID
private String pId;
// 节点名称
private String label;
// 子孙节点集合
private List<TreeNodeVO> children;
}
......@@ -3,6 +3,8 @@ package com.gitee.myclouds.base.vo;
import java.io.Serializable;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
/**
* 数据字典通用值对象
......@@ -11,6 +13,8 @@ import lombok.Data;
*
*/
@Data
@ToString
@Accessors(chain = true)
public class DictVO implements Serializable{
private static final long serialVersionUID = 1L;
......
......@@ -2,8 +2,9 @@ package com.gitee.myclouds.base.vo;
import java.io.Serializable;
import lombok.Getter;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
/**
* 接口返回通用值对象
......@@ -11,8 +12,9 @@ import lombok.ToString;
* @author xiongchun
*
*/
@Getter
@Data
@ToString
@Accessors(chain = true)
public class OutVO implements Serializable{
private static final long serialVersionUID = 1L;
......@@ -41,35 +43,4 @@ public class OutVO implements Serializable{
public OutVO(int code) {
setCode(code);
}
public OutVO setCode(int code) {
this.code = code;
return this;
}
public OutVO setMsg(String msg) {
this.msg = msg;
return this;
}
public OutVO setCount(int count) {
this.count = count;
return this;
}
public OutVO setData(Object data) {
this.data = data;
return this;
}
public OutVO setTrace(String trace) {
this.trace = trace;
return this;
}
public OutVO setAdvice(String advice) {
this.advice = advice;
return this;
}
}
......@@ -4,6 +4,8 @@ import java.io.Serializable;
import java.util.List;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
/**
* 当前会话用户数据对象
......@@ -12,6 +14,8 @@ import lombok.Data;
*
*/
@Data
@ToString
@Accessors(chain = true)
public class UserVO implements Serializable {
private static final long serialVersionUID = 1L;
......
......@@ -21,6 +21,8 @@ import java.sql.Timestamp;
import java.io.Serializable;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
##java.lang.*下的类型不需要导入
/**
......@@ -33,6 +35,8 @@ import lombok.Data;
* @date $!sysdate
*/
@Data
@ToString
@Accessors(chain = true)
public class ${tableDto.upname}Entity implements Serializable {
private static final long serialVersionUID = 1L;
......
......@@ -4,6 +4,8 @@ import java.io.Serializable;
import java.util.List;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
/**
* 功能菜单简单值对象(配合前端使用的数据结构)
......@@ -12,6 +14,8 @@ import lombok.Data;
*
*/
@Data
@ToString
@Accessors(chain = true)
public class MenuVO implements Serializable{
private static final long serialVersionUID = 1L;
......
......@@ -3,14 +3,12 @@ package com.gitee.myclouds.system.module.module;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
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 org.springframework.web.bind.annotation.RestController;
import com.gitee.myclouds.base.vo.OutVO;
import com.gitee.myclouds.common.wrapper.Dto;
import com.gitee.myclouds.common.wrapper.Dtos;
/**
......@@ -34,7 +32,7 @@ public class ModuleController {
*/
@RequestMapping(value = "list", produces = "application/json")
public OutVO list(@RequestBody Map<String, Object> inMap){
return moduleService.list(Dtos.newPageDto(inMap));
return moduleService.list(Dtos.newDto(inMap));
}
/**
......@@ -43,9 +41,9 @@ public class ModuleController {
* @param inMap
* @return
*/
@RequestMapping(value = "get",method = { RequestMethod.POST }, produces = "application/json")
public String get(@RequestParam Integer id){
return moduleService.get(id);
@RequestMapping(value = "get", produces = "application/json")
public OutVO get(@RequestBody Map<String,Object> inMap){
return moduleService.get(Dtos.newDto(inMap).getInteger("id"));
}
/**
......@@ -54,9 +52,9 @@ public class ModuleController {
* @param inMap
* @return
*/
@RequestMapping(value = "save",method = { RequestMethod.POST}, produces = "application/json")
public Dto save(@RequestParam Map<String,Object> inMap){
return moduleService.save(Dtos.newDto(inMap));
@PostMapping(value = "add", produces = "application/json")
public OutVO add(@RequestBody Map<String,Object> inMap){
return moduleService.add(Dtos.newDto(inMap));
}
/**
......@@ -65,8 +63,8 @@ public class ModuleController {
* @param inMap
* @return
*/
@RequestMapping(value = "update",method = { RequestMethod.POST}, produces = "application/json")
public Dto update(@RequestParam Map<String,Object> inMap){
@PostMapping(value = "update", produces = "application/json")
public OutVO update(@RequestBody Map<String,Object> inMap){
return moduleService.update(Dtos.newDto(inMap));
}
......@@ -76,19 +74,19 @@ public class ModuleController {
* @param inMap
* @return
*/
@RequestMapping(value = "delete",method = { RequestMethod.POST}, produces = "application/json")
public Dto delete(@RequestParam Map<String,Object> inMap){
@PostMapping(value = "delete", produces = "application/json")
public OutVO delete(@RequestBody Map<String,Object> inMap){
return moduleService.delete(Dtos.newDto(inMap));
}
/**
*查询资源树
*查询资源树(返回树桩数据模型)
*
* @param inMap
* @return
*/
@RequestMapping(value = "listModuleTree",method = { RequestMethod.POST}, produces = "application/json")
public String listOrgTree(@RequestParam Map<String,Object> inMap){
@RequestMapping(value = "listTree", produces = "application/json")
public OutVO listTree(@RequestBody Map<String, Object> inMap){
return moduleService.listModuleTree(Dtos.newDto(inMap));
}
......
......@@ -33,21 +33,15 @@
FROM my_role_module WHERE module_id = #{module_id}
</select>
<!-- 查询资源树 -->
<select id="listModuleTree" resultType="Dto" parameterType="Dto">
<!-- 查询资源树集合 -->
<select id="listModuleTree" resultType="TreeNodeVO" parameterType="Dto">
SELECT
id,
parent_id AS pId,
name
name AS label
FROM
my_module
<where>
type IN
<foreach item="type" index="index" collection="types" open="(" separator="," close=")">
#{type}
</foreach>
</where>
ORDER BY parent_id, sort_no
ORDER BY sort_no
</select>
<!-- 统计直系子节点个数 -->
......
......@@ -5,8 +5,11 @@ import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.gitee.myclouds.base.exception.BizException;
import com.gitee.myclouds.base.helper.treebuiler.TreeBuilder;
import com.gitee.myclouds.base.helper.treebuiler.TreeNodeVO;
import com.gitee.myclouds.base.vo.OutVO;
import com.gitee.myclouds.common.util.MyUtil;
import com.gitee.myclouds.common.wrapper.Dto;
......@@ -47,9 +50,19 @@ public class ModuleService {
* @param id
* @return
*/
public String get(Integer id) {
public OutVO get(Integer id) {
OutVO outVO = new OutVO(0);
MyModuleEntity myModuleEntity = myModuleMapper.selectByKey(id);
return JSON.toJSONString(myModuleEntity);
MyModuleEntity parentEntity = myModuleMapper.selectByKey(myModuleEntity.getParent_id());
Dto dto = Dtos.newDto();
MyUtil.copyProperties(myModuleEntity, dto);
if (MyUtil.isNotEmpty(parentEntity)) {
dto.put("parent_name", parentEntity.getName());
}else {
dto.put("parent_name", "无");
}
outVO.setData(dto);
return outVO;
}
/**
......@@ -58,14 +71,16 @@ public class ModuleService {
* @param inDto
* @return
*/
public Dto save(Dto inDto) {
Dto outDto = null;
//拷贝参数对象中的属性到实体对象中
public OutVO add(Dto inDto) {
OutVO outVO = new OutVO(0);
MyModuleEntity myModuleEntity = new MyModuleEntity();
MyUtil.copyProperties(inDto, myModuleEntity);
if (MyUtil.isEmpty(myModuleEntity.getParent_id())) {
myModuleEntity.setParent_id(0);
}
myModuleMapper.insert(myModuleEntity);
outDto = Dtos.newDto().put2("code", "1").put2("msg", "资源模块信息保存成功");
return outDto;
outVO.setMsg("资源模块新增成功");
return outVO;
}
/**
......@@ -74,13 +89,13 @@ public class ModuleService {
* @param inDto
* @return
*/
public Dto update(Dto inDto) {
Dto outDto = null;
public OutVO update(Dto inDto) {
OutVO outVO = new OutVO(0);
MyModuleEntity myModuleEntity = new MyModuleEntity();
MyUtil.copyProperties(inDto, myModuleEntity);
myModuleMapper.updateByKey(myModuleEntity);
outDto = Dtos.newDto().put2("code", "1").put2("msg", "资源模块信息修改成功");
return outDto;
outVO.setMsg("资源模块修改成功");
return outVO;
}
/**
......@@ -89,26 +104,31 @@ public class ModuleService {
* @param inDto
* @return
*/
public Dto delete(Dto inDto) {
@Transactional
public OutVO delete(Dto inDto) {
OutVO outVO = new OutVO(0);
Integer moduleId = inDto.getInteger("id");
Integer cnt = sqlSession.selectOne("sql.module.countSubModules", moduleId);
if (cnt > 0) {
return Dtos.newDto().put2("code", "-1").put2("msg", "该节点包含子节点,不能删除。请先删除子节点。");
throw new BizException(1, "操作取消。请先删除子节点。");
}
myModuleMapper.deleteByKey(moduleId);
sqlSession.delete("sql.module.deleteMyRoleModule", moduleId);
Dto outDto = Dtos.newDto().put2("code", "1").put2("msg", "资源模块删除成功");
return outDto;
outVO.setMsg("资源模块修改成功");
return outVO;
}
/**
* 查询资源树
* 查询资源树(返回树状数据模型)
*
* @param inDto
* @return
*/
public String listModuleTree(Dto inDto) {
return null;
public OutVO listModuleTree(Dto inDto) {
OutVO outVO = new OutVO(0);
List<TreeNodeVO> treeNodeVOs = sqlSession.selectList("sql.module.listModuleTree", inDto);
outVO.setData(new TreeBuilder(treeNodeVOs).buildTree());
return outVO;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册