This commit is contained in:
zhangzhiqiang
2022-12-19 16:08:28 +08:00
parent 7daa0dfe94
commit 4a2736219c
7 changed files with 178 additions and 38 deletions

View File

@@ -24,7 +24,10 @@ import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.nl.common.TableDataInfo; import org.nl.common.TableDataInfo;
import org.nl.common.domain.query.PageQuery; import org.nl.common.domain.query.PageQuery;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.PageUtil; import org.nl.modules.common.utils.PageUtil;
import org.nl.modules.logging.annotation.Log;
import org.nl.modules.system.domain.Dept;
import org.nl.modules.system.domain.vo.DeptVo; import org.nl.modules.system.domain.vo.DeptVo;
import org.nl.modules.system.util.CopyUtil; import org.nl.modules.system.util.CopyUtil;
import org.nl.system.service.dept.ISysDeptService; import org.nl.system.service.dept.ISysDeptService;
@@ -33,9 +36,11 @@ import org.nl.system.service.dept.dto.DeptQuery;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* @author Zheng Jie * @author Zheng Jie
@@ -48,7 +53,6 @@ import java.util.List;
public class DeptController { public class DeptController {
private final ISysDeptService deptService; private final ISysDeptService deptService;
private static final String ENTITY_NAME = "dept";
@ApiOperation("查询部门") @ApiOperation("查询部门")
@GetMapping @GetMapping
@@ -81,30 +85,36 @@ public class DeptController {
return new ResponseEntity<>(deptService.getSuperior(ids),HttpStatus.OK); return new ResponseEntity<>(deptService.getSuperior(ids),HttpStatus.OK);
} }
// //
// @Log("新增部门") @Log("新增部门")
// @ApiOperation("新增部门") @ApiOperation("新增部门")
// @PostMapping @PostMapping
//// @SaCheckPermission("dept:add") // @SaCheckPermission("dept:add")
// public ResponseEntity<Object> create(@Validated @RequestBody Dept resources){ public ResponseEntity<Object> create(@Validated @RequestBody SysDept resources){
//// deptService.create(resources); deptService.createDept(resources);
// return new ResponseEntity<>(HttpStatus.CREATED); return new ResponseEntity<>(HttpStatus.CREATED);
// } }
// //
// @Log("修改部门") @Log("修改部门")
// @ApiOperation("修改部门") @ApiOperation("修改部门")
// @PutMapping @PutMapping
//// @SaCheckPermission("dept:edit") // @SaCheckPermission("dept:edit")
// public ResponseEntity<Object> update(@RequestBody Dept resources){ public ResponseEntity<Object> update(@Validated @RequestBody SysDept dept){
// deptService.update(resources); if (dept.getPid() != null && dept.getDeptId().equals(dept.getPid())) {
// return new ResponseEntity<>(HttpStatus.NO_CONTENT); throw new BadRequestException("上级不能为自己");
// } }
deptService.updateDept(dept);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
// //
// @Log("删除部门") @Log("删除部门")
// @ApiOperation("删除部门") @ApiOperation("删除部门")
// @DeleteMapping @DeleteMapping
//// @SaCheckPermission("dept:del") // @SaCheckPermission("dept:del")
// public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){ public ResponseEntity<Object> delete(@RequestBody Set<String> deptIds){
// deptService.delete(ids); if (CollectionUtils.isEmpty(deptIds)){
// return new ResponseEntity<>(HttpStatus.OK); return ResponseEntity.noContent().build();
// } }
deptService.delateDept(deptIds);
return new ResponseEntity<>(HttpStatus.OK);
}
} }

View File

@@ -10,6 +10,7 @@ import org.nl.system.service.dept.dto.DeptQuery;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* <p> * <p>
@@ -49,4 +50,18 @@ public interface ISysDeptService extends IService<SysDept> {
*/ */
void saveUserDeptRelation(String UserId, Collection<String> deptIds); void saveUserDeptRelation(String UserId, Collection<String> deptIds);
/**
* 更新部门:同时更新节点
* @param dept
*/
void updateDept(SysDept dept);
/**
* 删除部门及子部门
* @param deptIds
*/
void delateDept(Set<String> deptIds);
void createDept(SysDept dept);
} }

View File

@@ -6,6 +6,7 @@ import org.nl.system.service.dept.dao.SysDept;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* <p> * <p>
@@ -17,6 +18,27 @@ import java.util.List;
*/ */
public interface SysDeptMapper extends BaseMapper<SysDept> { public interface SysDeptMapper extends BaseMapper<SysDept> {
/**
* 保存依赖关系
* @param UserId
* @param deptId
*/
void saveDeptRelation(@Param("user") String UserId,@Param("depts") Collection<String> deptId); void saveDeptRelation(@Param("user") String UserId,@Param("depts") Collection<String> deptId);
List<Map> getDeptRelation(@Param("deptIds") Collection<String> deptIds);
/**
* 跟新sub_count字段
* @param deptId
* @return
*/
int updateSubCount(String deptId);
/**
* 返回字符串列表 split = ,
* @param pid
* @return
*/
String findAllChild(String pid);
} }

View File

@@ -8,4 +8,29 @@
(#{user},#{dept}) (#{user},#{dept})
</foreach> </foreach>
</insert> </insert>
<update id="updateSubCount">
update sys_dept set sub_count =
(select m.count from (select count(*) count from sys_dept where pid = #{pid}) as m)
where dept_id = #{pid}
</update>
<select id="findAllChild" resultType="java.lang.String">
SELECT
max(t3.childId) as deptIds
from
(
select *,
if( find_in_set(t1.pid, @p) > 0,@p := concat(@p,',',id),0 ) as childId
from
(select dept_id as id, pid from sys_dept t order by id) t1,
(select @p := #{pid}) t2
) t3
where childId != '0'
</select>
<select id="getDeptRelation" resultType="java.util.Map">
select * from sys_user_dept where dept_id in (
<foreach collection="deptIds" separator="," item="dept">
#{dept}
</foreach>
)
</select>
</mapper> </mapper>

View File

@@ -1,6 +1,8 @@
package org.nl.system.service.dept.impl; package org.nl.system.service.dept.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -9,15 +11,22 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.nl.common.domain.query.PageQuery; import org.nl.common.domain.query.PageQuery;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.SecurityUtils;
import org.nl.modules.common.utils.dto.CurrentUser;
import org.nl.modules.system.domain.vo.DeptVo; import org.nl.modules.system.domain.vo.DeptVo;
import org.nl.modules.system.service.dto.DeptTree; import org.nl.modules.system.service.dto.DeptTree;
import org.nl.modules.system.util.CopyUtil; import org.nl.modules.system.util.CopyUtil;
import org.nl.modules.tools.IdUtil;
import org.nl.modules.wql.core.bean.ResultBean;
import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.system.service.dept.ISysDeptService; import org.nl.system.service.dept.ISysDeptService;
import org.nl.system.service.dept.dao.SysDept; import org.nl.system.service.dept.dao.SysDept;
import org.nl.system.service.dept.dao.mapper.SysDeptMapper; import org.nl.system.service.dept.dao.mapper.SysDeptMapper;
import org.nl.system.service.dept.dto.DeptQuery; import org.nl.system.service.dept.dto.DeptQuery;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -103,4 +112,63 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
} }
sysDeptMapper.saveDeptRelation(userId,deptIds); sysDeptMapper.saveDeptRelation(userId,deptIds);
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void updateDept(SysDept dept) {
if (dept == null ||StringUtils.isEmpty(dept.getDeptId())){
return;
}
this.updateById(dept);
//删除节点信息
sysDeptMapper.updateSubCount(dept.getDeptId());
if (StringUtils.isNotEmpty(dept.getPid())){
sysDeptMapper.updateSubCount(dept.getPid());
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delateDept(Set<String> deptIds) {
if (CollectionUtils.isEmpty(deptIds)){
return;
}
verification(deptIds);
Set<String> depts = new HashSet<>();
for (String deptId : deptIds) {
String allChild = sysDeptMapper.findAllChild(deptId);
if (StringUtils.isNotEmpty(allChild)){
String[] split = allChild.split(",");
depts.addAll(Arrays.asList(split));
}
}
this.remove(new QueryWrapper<SysDept>().in("dept_id", depts));
for (String deptId : deptIds) {
sysDeptMapper.updateSubCount(deptId);
}
}
private void verification(Set<String> depeIds) {
if (!CollectionUtils.isEmpty(depeIds)){
List<Map> deptRelation = sysDeptMapper.getDeptRelation(depeIds);
if (!CollectionUtils.isEmpty(deptRelation)){
throw new BadRequestException("部门存在绑定的人员,请先解绑人员对应部门");
}
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void createDept(SysDept dept) {
dept.setDeptId(IdUtil.getStringId());
CurrentUser user = SecurityUtils.getCurrentUser();
dept.setCreateId(user.getId());
dept.setCreateName(user.getPresonName());
dept.setCreateTime(new Date());
this.save(dept);
// 清理缓存
if (StringUtils.isNotEmpty(dept.getPid())){
sysDeptMapper.updateSubCount(dept.getPid());
}
}
} }

View File

@@ -14,10 +14,10 @@ import lombok.RequiredArgsConstructor;
import org.nl.common.domain.query.PageQuery; import org.nl.common.domain.query.PageQuery;
import org.nl.modules.common.exception.BadRequestException; import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.SecurityUtils; import org.nl.modules.common.utils.SecurityUtils;
import org.nl.system.service.menu.dao.mapper.SysMenuMapper;
import org.nl.system.service.role.ISysRoleService; import org.nl.system.service.role.ISysRoleService;
import org.nl.system.service.role.dao.SysRole; import org.nl.system.service.role.dao.SysRole;
import org.nl.system.service.role.dao.mapper.SysRoleMapper; import org.nl.system.service.role.dao.mapper.SysRoleMapper;
import org.nl.system.service.menu.dao.mapper.SysMenuMapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;

View File

@@ -43,9 +43,9 @@
<el-form-item label="部门名称" prop="name"> <el-form-item label="部门名称" prop="name">
<el-input v-model="form.name" style="width: 370px;" /> <el-input v-model="form.name" style="width: 370px;" />
</el-form-item> </el-form-item>
<el-form-item label="部门排序" prop="dept_sort"> <el-form-item label="部门排序" prop="deptSort">
<el-input-number <el-input-number
v-model.number="form.dept_sort" v-model.number="form.deptSort"
:min="0" :min="0"
:max="999" :max="999"
controls-position="right" controls-position="right"
@@ -94,7 +94,7 @@
:load="getDeptDatas" :load="getDeptDatas"
:tree-props="{children: 'children', hasChildren: 'hasChildren'}" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
:data="crud.data" :data="crud.data"
row-key="dept_id" row-key="deptId"
@select="crud.selectChange" @select="crud.selectChange"
@select-all="crud.selectAllChange" @select-all="crud.selectAllChange"
@selection-change="crud.selectionChangeHandler" @selection-change="crud.selectionChangeHandler"
@@ -102,7 +102,7 @@
<el-table-column :selectable="checkboxT" type="selection" width="55" /> <el-table-column :selectable="checkboxT" type="selection" width="55" />
<!-- <el-table-column label="编码" prop="code" />--> <!-- <el-table-column label="编码" prop="code" />-->
<el-table-column label="名称" prop="name" /> <el-table-column label="名称" prop="name" />
<el-table-column label="排序" prop="dept_sort" /> <el-table-column label="排序" prop="deptSort" />
<el-table-column label="状态" align="center" prop="isUsed"> <el-table-column label="状态" align="center" prop="isUsed">
<template slot-scope="scope"> <template slot-scope="scope">
<el-switch <el-switch
@@ -112,11 +112,11 @@
inactive-color="#F56C6C" inactive-color="#F56C6C"
active-value="1" active-value="1"
inactive-value="0" inactive-value="0"
@change="changeEnabled(scope.row, scope.row.enabled,)" @change="changeEnabled(scope.row, scope.row.isUsed,)"
/> />
</template> </template>
</el-table-column> </el-table-column>
<el-table-column prop="create_time" label="创建日期" /> <el-table-column prop="createTime" label="创建日期" />
<el-table-column <el-table-column
v-permission="['admin','dept:edit','dept:del']" v-permission="['admin','dept:edit','dept:del']"
label="操作" label="操作"
@@ -152,7 +152,7 @@ const defaultForm = {
isTop: '1', isTop: '1',
sub_count: 0, sub_count: 0,
pid: null, pid: null,
dept_sort: 999, deptSort: 999,
isUsed: '1', isUsed: '1',
ext_id: null ext_id: null
} }
@@ -160,7 +160,7 @@ export default {
name: 'Dept', name: 'Dept',
components: { Treeselect, crudOperation, rrOperation, udOperation }, components: { Treeselect, crudOperation, rrOperation, udOperation },
cruds() { cruds() {
return CRUD({ title: '部门', idField: 'dept_id', url: 'api/dept/vo', crudMethod: { ...crudDept }}) return CRUD({ title: '部门', idField: 'deptId', url: 'api/dept/vo', crudMethod: { ...crudDept }})
}, },
mixins: [presenter(), header(), form(defaultForm), crud()], mixins: [presenter(), header(), form(defaultForm), crud()],
// 设置数据字典 // 设置数据字典
@@ -172,7 +172,7 @@ export default {
name: [ name: [
{ required: true, message: '请输入名称', trigger: 'blur' } { required: true, message: '请输入名称', trigger: 'blur' }
], ],
dept_sort: [ deptSort: [
{ required: true, message: '请输入序号', trigger: 'blur', type: 'number' } { required: true, message: '请输入序号', trigger: 'blur', type: 'number' }
] ]
}, },
@@ -189,7 +189,7 @@ export default {
}, },
methods: { methods: {
getDeptDatas(tree, treeNode, resolve) { getDeptDatas(tree, treeNode, resolve) {
const params = { pid: tree.dept_id } const params = { pid: tree.deptId }
setTimeout(() => { setTimeout(() => {
crudDept.getDeptvo(params).then(res => { crudDept.getDeptvo(params).then(res => {
resolve(res.content) resolve(res.content)
@@ -240,7 +240,7 @@ export default {
// 获取弹窗内部门数据 // 获取弹窗内部门数据
loadDepts({ action, parentNode, callback }) { loadDepts({ action, parentNode, callback }) {
if (action === LOAD_CHILDREN_OPTIONS) { if (action === LOAD_CHILDREN_OPTIONS) {
crudDept.getDeptvo({ isUsed: '1', pid: parentNode.dept_id }).then(res => { crudDept.getDeptvo({ isUsed: '1', pid: parentNode.deptId }).then(res => {
parentNode.children = res.content.map(function(obj) { parentNode.children = res.content.map(function(obj) {
obj.children = null obj.children = null
return obj return obj
@@ -287,7 +287,7 @@ export default {
}, },
normalizer(node) { normalizer(node) {
return { return {
id: node.dept_id, id: node.deptId,
label: node.name, label: node.name,
children: node.children children: node.children
} }