Commit e22b7f1c authored by 刘弈臻's avatar 刘弈臻

菜单增删改查

parent b50749bd
package com.govmade.modules.system.controller; package com.govmade.modules.system.controller;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.govmade.common.utils.PageTreeUtils;
import com.govmade.common.utils.R; import com.govmade.common.utils.R;
import com.govmade.common.validator.Assert;
import com.govmade.modules.basic.controller.AbstractController; import com.govmade.modules.basic.controller.AbstractController;
import com.govmade.modules.system.entity.MenuEntity; import com.govmade.modules.system.entity.MenuEntity;
import com.govmade.modules.system.service.MenuService; import com.govmade.modules.system.service.MenuService;
...@@ -28,15 +35,15 @@ public class MenuController extends AbstractController { ...@@ -28,15 +35,15 @@ public class MenuController extends AbstractController {
private MenuService menuService; private MenuService menuService;
/** /**
* 菜单列表 * 所有菜单数据树结构
*/ */
@GetMapping("/list") @GetMapping("/listTree")
public R list(Map<String, Object> params) { public R listTree(@RequestParam Map<String, Object> params) {
PageTreeUtils page = menuService.queryPage(params);
menuService.queryPage(params); return R.ok().put("page", page);
return null;
} }
/** /**
* 保存或更新用户 * 保存或更新用户
*/ */
...@@ -48,6 +55,7 @@ public class MenuController extends AbstractController { ...@@ -48,6 +55,7 @@ public class MenuController extends AbstractController {
return R.error(menu.getName() + " 已存在!"); return R.error(menu.getName() + " 已存在!");
} }
} }
menuService.save(menu);
return R.ok(); return R.ok();
} }
...@@ -61,4 +69,20 @@ public class MenuController extends AbstractController { ...@@ -61,4 +69,20 @@ public class MenuController extends AbstractController {
public R info(@PathVariable("id") Long id) { public R info(@PathVariable("id") Long id) {
return R.ok().put("menu", menuService.queryById(id)); return R.ok().put("menu", menuService.queryById(id));
} }
/**
* 批量删除菜单
*
* @param ids
* @return
*/
@DeleteMapping("/delete")
public R deleteArea(@RequestBody Set<Long> ids) {
Assert.isNull(ids, "删除项不能为空");
menuService.deleteBatch(ids);
return R.ok();
}
} }
package com.govmade.modules.system.dao; package com.govmade.modules.system.dao;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.mapper.BaseMapper; import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.govmade.modules.system.entity.MenuEntity; import com.govmade.modules.system.entity.MenuEntity;
...@@ -15,4 +21,14 @@ import com.govmade.modules.system.entity.MenuEntity; ...@@ -15,4 +21,14 @@ import com.govmade.modules.system.entity.MenuEntity;
@Mapper @Mapper
public interface MenuDao extends BaseMapper<MenuEntity> { public interface MenuDao extends BaseMapper<MenuEntity> {
/**
* 批量删除或删除
*/
void deleteBatch(@Param("ids") Set<Long> ids);
/**
* 查询树形数据
*/
List<MenuEntity> listTree(@Param("params") Map<String, Object> params);
} }
package com.govmade.modules.system.entity; package com.govmade.modules.system.entity;
import java.util.List;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.annotations.TableName;
import com.govmade.modules.basic.entity.BaseEntity; import com.govmade.modules.basic.entity.BaseEntity;
...@@ -33,6 +36,9 @@ public class MenuEntity extends BaseEntity<Long> { ...@@ -33,6 +36,9 @@ public class MenuEntity extends BaseEntity<Long> {
private Long weight; private Long weight;
@TableField(exist=false)
private List<MenuEntity> children;
public Long getPid() { public Long getPid() {
return pid; return pid;
} }
...@@ -104,4 +110,13 @@ public class MenuEntity extends BaseEntity<Long> { ...@@ -104,4 +110,13 @@ public class MenuEntity extends BaseEntity<Long> {
public void setWeight(Long weight) { public void setWeight(Long weight) {
this.weight = weight; this.weight = weight;
} }
public List<MenuEntity> getChildren() {
return children;
}
public void setChildren(List<MenuEntity> children) {
this.children = children;
}
} }
...@@ -2,8 +2,10 @@ package com.govmade.modules.system.service; ...@@ -2,8 +2,10 @@ package com.govmade.modules.system.service;
import java.util.Map; import java.util.Map;
import java.util.Set;
import com.baomidou.mybatisplus.service.IService; import com.baomidou.mybatisplus.service.IService;
import com.govmade.common.utils.PageTreeUtils;
import com.govmade.common.utils.PageUtils; import com.govmade.common.utils.PageUtils;
import com.govmade.modules.system.entity.MenuEntity; import com.govmade.modules.system.entity.MenuEntity;
...@@ -17,7 +19,7 @@ import com.govmade.modules.system.entity.MenuEntity; ...@@ -17,7 +19,7 @@ import com.govmade.modules.system.entity.MenuEntity;
public interface MenuService extends IService<MenuEntity> { public interface MenuService extends IService<MenuEntity> {
PageUtils queryPage(Map<String, Object> params); PageTreeUtils queryPage(Map<String, Object> params);
/** /**
* 保存或修改菜单 * 保存或修改菜单
...@@ -30,4 +32,9 @@ public interface MenuService extends IService<MenuEntity> { ...@@ -30,4 +32,9 @@ public interface MenuService extends IService<MenuEntity> {
Integer checkMenu(String name); Integer checkMenu(String name);
MenuEntity queryById(Long id); MenuEntity queryById(Long id);
/**
* 删除菜单
*/
void deleteBatch(Set<Long> ids);
} }
...@@ -2,16 +2,21 @@ package com.govmade.modules.system.service.impl; ...@@ -2,16 +2,21 @@ package com.govmade.modules.system.service.impl;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.govmade.common.utils.PageTreeUtils;
import com.govmade.common.utils.PageUtils; import com.govmade.common.utils.PageUtils;
import com.govmade.modules.system.dao.MenuDao; import com.govmade.modules.system.dao.MenuDao;
import com.govmade.modules.system.entity.MenuEntity; import com.govmade.modules.system.entity.MenuEntity;
import com.govmade.modules.system.service.MenuService; import com.govmade.modules.system.service.MenuService;
import cn.hutool.core.util.StrUtil;
/** /**
* 系统管理 - 菜单设置 * 系统管理 - 菜单设置
* *
...@@ -24,10 +29,35 @@ public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements ...@@ -24,10 +29,35 @@ public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements
@Override @Override
public PageUtils queryPage(Map<String, Object> params) { public PageTreeUtils queryPage(Map<String, Object> params) {
// TODO Auto-generated method stub String name = (String) params.get("name");
//this.baseMapper.selectPage(); int currPage = Integer.parseInt((String) params.get("currPage"));
return null; int pageSize = Integer.parseInt((String) params.get("pageSize"));
int start = (currPage-1)*pageSize;
params.put("name2", name);
params.put("pageSize2", pageSize);
params.put("start", start);
List<MenuEntity> allList=this.baseMapper.listTree(params);
List<MenuEntity> list=buildAreaTree(allList,0L);
int totalCount = super.selectCount(new EntityWrapper<MenuEntity>().like(StrUtil.isNotBlank(name), "name", name)
.eq("pid", 0).eq("state", 1));
return new PageTreeUtils(list,totalCount,pageSize,currPage);
}
/**
*递归方法
*/
private List<MenuEntity> buildAreaTree(List<MenuEntity> menuList, Long id) {
List<MenuEntity> mList = Lists.newArrayList();
for (MenuEntity menu : menuList) {
if (id == menu.getPid()) {
menu.setChildren(buildAreaTree(menuList, menu.getId()));
mList.add(menu);
}
}
return mList;
} }
/** /**
...@@ -57,4 +87,9 @@ public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements ...@@ -57,4 +87,9 @@ public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements
return super.selectById(id); return super.selectById(id);
} }
@Override
public void deleteBatch(Set<Long> ids) {
this.baseMapper.deleteBatch(ids);
}
} }
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.govmade.modules.system.dao.MenuDao">
<!-- 批量删除 -->
<update id="deleteBatch">
UPDATE system_menus SET state =${@com.govmade.common.utils.Constant@STATE_DELETE} WHERE id IN
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</update>
<!-- 查询树形数据-->
<select id="listTree" resultType="MenuEntity">
SELECT * FROM system_menus AS t
INNER JOIN (
SELECT id AS id2 FROM system_menus
WHERE
pid = 0
AND state =1
<if test="params.name2 != null and params.name2 !=''">
AND NAME LIKE CONCAT('%',#{params.name2},'%')
</if>
LIMIT #{params.start},#{params.pageSize2}
) AS t2 ON t.root_id = t2.id2 AND t.state=1
UNION ALL(SELECT *, 0 AS id2 FROM system_menus
WHERE
pid = 0
AND state =1
<if test="params.name2 != null and params.name2 !=''">
AND NAME LIKE CONCAT('%',#{params.name2},'%')
</if>
LIMIT #{params.start},#{params.pageSize2}
)
ORDER BY id
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment