提交 daf441a0 authored 作者: taojinlong's avatar taojinlong

Merge branch 'main' of github.com:dataease/dataease into main

......@@ -14,8 +14,6 @@ public class SysDept implements Serializable {
private String name;
private Integer level;
private Integer deptSort;
private Boolean enabled;
......
......@@ -355,66 +355,6 @@ public class SysDeptExample {
return (Criteria) this;
}
public Criteria andLevelIsNull() {
addCriterion("`level` is null");
return (Criteria) this;
}
public Criteria andLevelIsNotNull() {
addCriterion("`level` is not null");
return (Criteria) this;
}
public Criteria andLevelEqualTo(Integer value) {
addCriterion("`level` =", value, "level");
return (Criteria) this;
}
public Criteria andLevelNotEqualTo(Integer value) {
addCriterion("`level` <>", value, "level");
return (Criteria) this;
}
public Criteria andLevelGreaterThan(Integer value) {
addCriterion("`level` >", value, "level");
return (Criteria) this;
}
public Criteria andLevelGreaterThanOrEqualTo(Integer value) {
addCriterion("`level` >=", value, "level");
return (Criteria) this;
}
public Criteria andLevelLessThan(Integer value) {
addCriterion("`level` <", value, "level");
return (Criteria) this;
}
public Criteria andLevelLessThanOrEqualTo(Integer value) {
addCriterion("`level` <=", value, "level");
return (Criteria) this;
}
public Criteria andLevelIn(List<Integer> values) {
addCriterion("`level` in", values, "level");
return (Criteria) this;
}
public Criteria andLevelNotIn(List<Integer> values) {
addCriterion("`level` not in", values, "level");
return (Criteria) this;
}
public Criteria andLevelBetween(Integer value1, Integer value2) {
addCriterion("`level` between", value1, value2, "level");
return (Criteria) this;
}
public Criteria andLevelNotBetween(Integer value1, Integer value2) {
addCriterion("`level` not between", value1, value2, "level");
return (Criteria) this;
}
public Criteria andDeptSortIsNull() {
addCriterion("dept_sort is null");
return (Criteria) this;
......
......@@ -6,7 +6,6 @@
<result column="pid" jdbcType="BIGINT" property="pid" />
<result column="sub_count" jdbcType="INTEGER" property="subCount" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="level" jdbcType="INTEGER" property="level" />
<result column="dept_sort" jdbcType="INTEGER" property="deptSort" />
<result column="enabled" jdbcType="BIT" property="enabled" />
<result column="create_by" jdbcType="VARCHAR" property="createBy" />
......@@ -73,8 +72,8 @@
</where>
</sql>
<sql id="Base_Column_List">
dept_id, pid, sub_count, `name`, `level`, dept_sort, enabled, create_by, update_by,
create_time, update_time
dept_id, pid, sub_count, `name`, dept_sort, enabled, create_by, update_by, create_time,
update_time
</sql>
<select id="selectByExample" parameterType="io.dataease.base.domain.SysDeptExample" resultMap="BaseResultMap">
select
......@@ -108,13 +107,13 @@
</delete>
<insert id="insert" parameterType="io.dataease.base.domain.SysDept">
insert into sys_dept (dept_id, pid, sub_count,
`name`, `level`, dept_sort,
enabled, create_by, update_by,
create_time, update_time)
`name`, dept_sort, enabled,
create_by, update_by, create_time,
update_time)
values (#{deptId,jdbcType=BIGINT}, #{pid,jdbcType=BIGINT}, #{subCount,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR}, #{level,jdbcType=INTEGER}, #{deptSort,jdbcType=INTEGER},
#{enabled,jdbcType=BIT}, #{createBy,jdbcType=VARCHAR}, #{updateBy,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
#{name,jdbcType=VARCHAR}, #{deptSort,jdbcType=INTEGER}, #{enabled,jdbcType=BIT},
#{createBy,jdbcType=VARCHAR}, #{updateBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
#{updateTime,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="io.dataease.base.domain.SysDept">
insert into sys_dept
......@@ -131,9 +130,6 @@
<if test="name != null">
`name`,
</if>
<if test="level != null">
`level`,
</if>
<if test="deptSort != null">
dept_sort,
</if>
......@@ -166,9 +162,6 @@
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="level != null">
#{level,jdbcType=INTEGER},
</if>
<if test="deptSort != null">
#{deptSort,jdbcType=INTEGER},
</if>
......@@ -210,9 +203,6 @@
<if test="record.name != null">
`name` = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.level != null">
`level` = #{record.level,jdbcType=INTEGER},
</if>
<if test="record.deptSort != null">
dept_sort = #{record.deptSort,jdbcType=INTEGER},
</if>
......@@ -242,7 +232,6 @@
pid = #{record.pid,jdbcType=BIGINT},
sub_count = #{record.subCount,jdbcType=INTEGER},
`name` = #{record.name,jdbcType=VARCHAR},
`level` = #{record.level,jdbcType=INTEGER},
dept_sort = #{record.deptSort,jdbcType=INTEGER},
enabled = #{record.enabled,jdbcType=BIT},
create_by = #{record.createBy,jdbcType=VARCHAR},
......@@ -265,9 +254,6 @@
<if test="name != null">
`name` = #{name,jdbcType=VARCHAR},
</if>
<if test="level != null">
`level` = #{level,jdbcType=INTEGER},
</if>
<if test="deptSort != null">
dept_sort = #{deptSort,jdbcType=INTEGER},
</if>
......@@ -294,7 +280,6 @@
set pid = #{pid,jdbcType=BIGINT},
sub_count = #{subCount,jdbcType=INTEGER},
`name` = #{name,jdbcType=VARCHAR},
`level` = #{level,jdbcType=INTEGER},
dept_sort = #{deptSort,jdbcType=INTEGER},
enabled = #{enabled,jdbcType=BIT},
create_by = #{createBy,jdbcType=VARCHAR},
......
package io.dataease.base.mapper.ext;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import java.util.List;
@Mapper
public interface ExtDeptMapper {
@Update(" update sys_dept set sub_count = sub_count+1 where dept_id = #{deptId} ")
int incrementalSubcount(@Param("deptId") Long deptId);
@Update(" update sys_dept set sub_count = sub_count-1 where dept_id = #{deptId} and sub_count > 0")
int decreasingSubcount(@Param("deptId") Long deptId);
@Delete("<script> " +
" delete from sys_dept where dept_id in " +
" <foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach> " +
"</script>")
int batchDelete(@Param("ids") List<Long> ids);
}
package io.dataease.controller.sys;
import io.dataease.base.domain.SysDept;
import io.dataease.commons.utils.BeanUtils;
import io.dataease.controller.ResultHolder;
import io.dataease.controller.sys.request.DeptCreateRequest;
import io.dataease.controller.sys.request.DeptDeleteRequest;
import io.dataease.controller.sys.request.DeptStatusRequest;
import io.dataease.controller.sys.response.DeptNodeResponse;
import io.dataease.service.sys.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
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.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequiredArgsConstructor
......@@ -23,17 +27,45 @@ public class SysDeptController extends ResultHolder {
@Resource
private DeptService deptService;
@PostMapping("/childNodes/{pid}")
public List<DeptNodeResponse> childNodes(@PathVariable("pid") Long pid){
List<SysDept> nodes = deptService.nodesByPid(pid);
List<DeptNodeResponse> nodeResponses = nodes.stream().map(node -> {
DeptNodeResponse deptNodeResponse = BeanUtils.copyBean(new DeptNodeResponse(), node);
deptNodeResponse.setHasChildren(node.getSubCount() > 0);
deptNodeResponse.setTop(node.getPid() == deptService.DEPT_ROOT_PID);
return deptNodeResponse;
}).collect(Collectors.toList());
return nodeResponses;
}
@ApiOperation("查询部门")
@PostMapping("/root")
public ResultHolder rootData(){
List<SysDept> root = deptService.root();
List<SysDept> root = deptService.nodesByPid(null);
return success(root);
}
@ApiOperation("新增部门")
@PostMapping("/create")
public void create(@RequestBody SysDept dept){
public void create(@RequestBody DeptCreateRequest dept){
deptService.add(dept);
}
@ApiOperation("删除部门")
@PostMapping("/delete")
public void delete(@RequestBody List<DeptDeleteRequest> requests){
deptService.batchDelete(requests);
}
@ApiOperation("更新部门")
@PostMapping("/update")
public void update(@RequestBody DeptCreateRequest dept){
deptService.update(dept);
}
@ApiOperation("更新状态")
@PostMapping("/updateStatus")
public void updateStatus(@RequestBody DeptStatusRequest request){
deptService.updateStatus(request);
}
}
package io.dataease.controller.sys.request;
public class DeptCreateRequest {
import io.dataease.base.domain.SysDept;
import lombok.Data;
@Data
public class DeptCreateRequest extends SysDept {
private boolean top;
}
package io.dataease.controller.sys.request;
import lombok.Data;
@Data
public class DeptDeleteRequest {
private Long deptId;
private Long pid;
}
package io.dataease.controller.sys.request;
import lombok.Data;
@Data
public class DeptStatusRequest {
private Long deptId;
private boolean status;
}
package io.dataease.controller.sys.response;
import io.dataease.base.domain.SysDept;
import lombok.Data;
@Data
public class DeptNodeResponse extends SysDept {
private boolean hasChildren;
private boolean top;
}
......@@ -3,46 +3,121 @@ package io.dataease.service.sys;
import io.dataease.base.domain.SysDept;
import io.dataease.base.domain.SysDeptExample;
import io.dataease.base.mapper.SysDeptMapper;
import io.dataease.base.mapper.ext.ExtDeptMapper;
import io.dataease.commons.utils.BeanUtils;
import io.dataease.controller.sys.request.DeptCreateRequest;
import io.dataease.controller.sys.request.DeptDeleteRequest;
import io.dataease.controller.sys.request.DeptStatusRequest;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class DeptService {
private final static Integer DEPT_ROOT_LEVEL = 0;
private final static Integer DEFAULT_SUBCOUNT = 0;
public final static Long DEPT_ROOT_PID = 0L;
@Resource
private SysDeptMapper sysDeptMapper;
public List<SysDept> root(){
@Resource
private ExtDeptMapper extDeptMapper;
public List<SysDept> nodesByPid(Long pid){
SysDeptExample example = new SysDeptExample();
example.createCriteria().andLevelEqualTo(DEPT_ROOT_LEVEL);
SysDeptExample.Criteria criteria = example.createCriteria();
if (ObjectUtils.isEmpty(pid)){
criteria.andPidEqualTo(0L);
}else {
criteria.andPidEqualTo(pid);
}
example.setOrderByClause("dept_sort");
List<SysDept> sysDepts = sysDeptMapper.selectByExample(example);
return sysDepts;
}
public boolean add(SysDept sysDept){
if (ObjectUtils.isEmpty(sysDept.getLevel())){
sysDept.setLevel(DEPT_ROOT_LEVEL);
@Transactional
public boolean add(DeptCreateRequest deptCreateRequest){
SysDept sysDept = BeanUtils.copyBean(new SysDept(), deptCreateRequest);
if (deptCreateRequest.isTop()){
sysDept.setPid(DEPT_ROOT_PID);
}
Date now = new Date();
sysDept.setCreateTime(now);
sysDept.setUpdateTime(now);
sysDept.setCreateBy(null);
sysDept.setUpdateBy(null);
sysDept.setSubCount(DEFAULT_SUBCOUNT);
try {
int insert = sysDeptMapper.insert(sysDept);
Long pid = null;
if ((pid = sysDept.getPid()) != DEPT_ROOT_PID ){
//这里需要更新上级节点SubCount
extDeptMapper.incrementalSubcount(pid);
}
if (insert == 1){
return true;
}
}catch (Exception e){
return false;
e.printStackTrace();
}
return false;
}
@Transactional
public int batchDelete(List<DeptDeleteRequest> requests){
/* Integer index = ids.stream().map(sysDeptMapper::deleteByPrimaryKey).reduce(Integer::sum).orElse(-1);
return index;*/
List<Long> ids = requests.stream().map(request -> {
Long pid = request.getPid();
if (pid != DEPT_ROOT_PID){
extDeptMapper.decreasingSubcount(pid);
}
return request.getDeptId();
}).collect(Collectors.toList());
return extDeptMapper.batchDelete(ids);
}
@Transactional
public int update(DeptCreateRequest deptCreateRequest){
SysDept sysDept = BeanUtils.copyBean(new SysDept(), deptCreateRequest);
if (deptCreateRequest.isTop()){
sysDept.setPid(DEPT_ROOT_PID);
}
sysDept.setUpdateTime(new Date());
sysDept.setUpdateBy(null);
Long deptId = sysDept.getDeptId();
SysDept dept_old = sysDeptMapper.selectByPrimaryKey(deptId);
//如果PID发生了改变
//判断oldPid是否是跟节点PID ? nothing : parent.subcount-1
//判断newPid是否是跟节点PID ? nothing : parent.subcount+1
if (sysDept.getPid() != dept_old.getPid()){
Long oldPid = dept_old.getPid();
if (oldPid != DEPT_ROOT_PID){
extDeptMapper.decreasingSubcount(oldPid);
}
if (sysDept.getPid() != DEPT_ROOT_PID){
extDeptMapper.incrementalSubcount(sysDept.getPid());
}
}
return sysDeptMapper.updateByPrimaryKeySelective(sysDept);
}
public int updateStatus(DeptStatusRequest request){
Long deptId = request.getDeptId();
boolean status = request.isStatus();
SysDept sysDept = new SysDept();
sysDept.setDeptId(deptId);
sysDept.setEnabled(status);
return sysDeptMapper.updateByPrimaryKeySelective(sysDept);
}
}
CREATE TABLE IF NOT EXISTS `sys_dept` (
`dept_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`pid` bigint(20) DEFAULT NULL COMMENT '上级部门',
`sub_count` int(5) DEFAULT 0 COMMENT '子部门数目',
`name` varchar(255) NOT NULL COMMENT '名称',
`dept_sort` int(5) DEFAULT 999 COMMENT '排序',
`enabled` bit(1) NOT NULL COMMENT '状态',
`create_by` varchar(255) DEFAULT NULL COMMENT '创建者',
`update_by` varchar(255) DEFAULT NULL COMMENT '更新者',
`create_time` datetime DEFAULT NULL COMMENT '创建日期',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`dept_id`) USING BTREE,
KEY `inx_pid` (`pid`),
KEY `inx_enabled` (`enabled`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='部门';
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论