| | |
| | | |
| | | @RequestMapping(value = "/get/{id}", method = RequestMethod.POST) |
| | | public RestResponse update(@PathVariable Integer id) { |
| | | return RestResponse.ok(departmentService.getById(id)); |
| | | return RestResponse.ok(departmentService.getByIdVO(id)); |
| | | } |
| | | |
| | | @RequestMapping(value = "/getUserByDepartment/{id}", method = RequestMethod.POST) |
| | |
| | | package com.mindskip.xzs.domain; |
| | | |
| | | import com.mindskip.xzs.domain.enums.YesOrNoEnum; |
| | | import com.mindskip.xzs.vo.TreeNode; |
| | | import lombok.Data; |
| | | |
| | |
| | | */ |
| | | private List<Integer> adminId; |
| | | |
| | | /** |
| | | * 是否能管理所有level为3的派出所 |
| | | */ |
| | | private YesOrNoEnum special; |
| | | |
| | | /** |
| | | * 部门几级 |
| | | */ |
| | | private Integer level; |
| | | |
| | | private String deleted; |
| | | |
| | | } |
New file |
| | |
| | | package com.mindskip.xzs.domain.enums; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.EnumValue; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * @author:xp |
| | | * @date:2024/7/12 15:32 |
| | | */ |
| | | @Getter |
| | | public enum YesOrNoEnum { |
| | | |
| | | YES("yes", "是"), |
| | | NO("yes", "否"), |
| | | ; |
| | | |
| | | @EnumValue |
| | | private final String value; |
| | | |
| | | private final String desc; |
| | | |
| | | YesOrNoEnum(String value, String desc) { |
| | | this.value = value; |
| | | this.desc = desc; |
| | | } |
| | | } |
| | |
| | | package com.mindskip.xzs.domain.vo; |
| | | |
| | | import com.mindskip.xzs.domain.enums.YesOrNoEnum; |
| | | import lombok.Data; |
| | | |
| | | import java.util.List; |
| | |
| | | */ |
| | | private Integer parentId; |
| | | |
| | | private String special; |
| | | |
| | | private Integer level; |
| | | |
| | | /** |
| | | * 子级数据 |
| | | */ |
New file |
| | |
| | | package com.mindskip.xzs.domain.vo; |
| | | |
| | | import com.mindskip.xzs.domain.enums.YesOrNoEnum; |
| | | import lombok.Data; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author:xp |
| | | * @date:2024/7/12 17:40 |
| | | */ |
| | | @Data |
| | | public class DepartmentEditVO { |
| | | |
| | | private Integer id; |
| | | |
| | | private String name; |
| | | |
| | | /** |
| | | * 上级部门ID |
| | | */ |
| | | private Integer parentId; |
| | | private List<Integer> parentIds; |
| | | |
| | | /** |
| | | * 部门管理员 |
| | | */ |
| | | private List<Integer> adminId; |
| | | |
| | | /** |
| | | * 是否能管理所有level为3的派出所 |
| | | */ |
| | | private YesOrNoEnum special; |
| | | |
| | | /** |
| | | * 部门几级 |
| | | */ |
| | | private Integer level; |
| | | |
| | | } |
| | |
| | | * @return |
| | | */ |
| | | List<Integer> getChilds(@Param("deptIds") List<Integer> deptIds); |
| | | |
| | | /** |
| | | * 获取该部门的上级(不包含自己) |
| | | * |
| | | * @param deptId |
| | | * @return |
| | | */ |
| | | List<Integer> getFather(@Param("deptId") Integer deptId); |
| | | } |
| | |
| | | package com.mindskip.xzs.service; |
| | | |
| | | import com.mindskip.xzs.domain.Department; |
| | | import com.mindskip.xzs.domain.vo.BaseSelect; |
| | | import com.mindskip.xzs.domain.vo.CascaderDataVO; |
| | | import com.mindskip.xzs.domain.vo.UpdateDeptAdminVO; |
| | | import com.mindskip.xzs.domain.vo.*; |
| | | import com.mindskip.xzs.viewmodel.admin.department.DepartmentResponseVM; |
| | | |
| | | import java.util.List; |
| | |
| | | |
| | | Department getById(Integer id); |
| | | |
| | | DepartmentEditVO getByIdVO(Integer id); |
| | | |
| | | List<Department> gets(List<Integer> deptId); |
| | | |
| | | Department getName(String name); |
| | |
| | | import com.mindskip.xzs.domain.enums.DeptAdminEnum; |
| | | import com.mindskip.xzs.domain.vo.BaseSelect; |
| | | import com.mindskip.xzs.domain.vo.CascaderDataVO; |
| | | import com.mindskip.xzs.domain.vo.DepartmentEditVO; |
| | | import com.mindskip.xzs.domain.vo.UpdateDeptAdminVO; |
| | | import com.mindskip.xzs.repository.BaseMapper; |
| | | import com.mindskip.xzs.repository.DepartmentMapper; |
| | |
| | | import com.mindskip.xzs.utility.TreeUtils; |
| | | import com.mindskip.xzs.viewmodel.admin.department.DepartmentResponseVM; |
| | | import org.modelmapper.ModelMapper; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | |
| | | @Override |
| | | public Integer add(Department model) { |
| | | model.setDeleted("0"); |
| | | if (Objects.nonNull(model.getParentId())) { |
| | | // 查出父级的层级 |
| | | List<Integer> fatherList = departmentMapper.getFather(model.getParentId()); |
| | | Collections.reverse(fatherList); |
| | | model.setLevel(fatherList.size() + 1); |
| | | } |
| | | return departmentMapper.add(model); |
| | | } |
| | | |
| | | @Override |
| | | public Integer remove(Integer id) { |
| | | Department department = new Department(); |
| | | department.setId(id); |
| | | department.setDeleted("1"); |
| | | return departmentMapper.update(department); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public Integer update(Department department) { |
| | | // userDepartmentMapper.removeByDepartmentId(department.getId()); |
| | | Department entity = departmentMapper.getById(department.getId()); |
| | | if (Objects.isNull(entity)) { |
| | | throw new RuntimeException("该部门不存在"); |
| | |
| | | // 标识部门管理员 |
| | | userMapper.updateDeptAdmin(department.getAdminId(), DeptAdminEnum.YES.getValue(), entity.getId()); |
| | | } |
| | | if (Objects.nonNull(department.getParentId())) { |
| | | // 查出父级的层级 |
| | | List<Integer> fatherList = departmentMapper.getFather(department.getParentId()); |
| | | department.setLevel(fatherList.size() + 1); |
| | | } else { |
| | | department.setLevel(1); |
| | | } |
| | | Integer update = departmentMapper.update(department); |
| | | return update; |
| | | } |
| | | |
| | | @Override |
| | | public Integer remove(Integer id) { |
| | | Department department = new Department(); |
| | | department.setId(id); |
| | | department.setDeleted("1"); |
| | | return departmentMapper.update(department); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public DepartmentEditVO getByIdVO(Integer id) { |
| | | Department dept = departmentMapper.getById(id); |
| | | DepartmentEditVO vo = new DepartmentEditVO(); |
| | | BeanUtils.copyProperties(dept, vo); |
| | | if (Objects.nonNull(dept.getParentId())) { |
| | | // 查出完整的父级ID |
| | | List<Integer> fatherList = departmentMapper.getFather(dept.getParentId()); |
| | | Collections.reverse(fatherList); |
| | | vo.setParentIds(fatherList); |
| | | } |
| | | return vo; |
| | | } |
| | | |
| | | @Override |
| | | public List<Department> gets(List<Integer> deptId) { |
| | | return TreeUtils.build(departmentMapper.gets(deptId)); |
| | | } |
| | |
| | | */ |
| | | private List<DepartmentResponseVM> children; |
| | | |
| | | private String special; |
| | | |
| | | private Integer level; |
| | | |
| | | /** |
| | | * 管理员 |
| | | */ |
| | |
| | | <result column="name" jdbcType="VARCHAR" property="name"/> |
| | | <result column="deleted" jdbcType="VARCHAR" property="deleted"/> |
| | | <result column="parent_id" property="parentId"/> |
| | | <result column="special" property="special" typeHandler="com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler"/> |
| | | <result column="level" property="level"/> |
| | | </resultMap> |
| | | |
| | | <sql id="Base_Column_List"> |
| | | id |
| | | , name,deleted,parent_id |
| | | , name,deleted,parent_id,special,level |
| | | </sql> |
| | | |
| | | <insert id="add" parameterType="com.mindskip.xzs.domain.Department" useGeneratedKeys="true" keyProperty="id"> |
| | | insert into t_department (name, deleted, parent_id) |
| | | values (#{name,jdbcType=VARCHAR}, #{deleted,jdbcType=VARCHAR}, #{parentId}) |
| | | insert into t_department (name, deleted, parent_id,special,level) |
| | | values (#{name,jdbcType=VARCHAR}, #{deleted,jdbcType=VARCHAR}, #{parentId}, #{special.value},#{level}) |
| | | </insert> |
| | | |
| | | <update id="update" parameterType="com.mindskip.xzs.domain.Department"> |
| | |
| | | <if test="deleted != null"> |
| | | deleted = #{deleted,jdbcType=VARCHAR}, |
| | | </if> |
| | | <if test="special != null and special != ''"> |
| | | special = #{special.value}, |
| | | </if> |
| | | <if test="level != null"> |
| | | level = #{level}, |
| | | </if> |
| | | </set> |
| | | where id = #{id,jdbcType=INTEGER} |
| | | </update> |
| | |
| | | SELECT |
| | | td.id, |
| | | td.name, |
| | | td.parent_id |
| | | td.parent_id, |
| | | td.special, |
| | | td.level |
| | | FROM |
| | | t_department td |
| | | WHERE |
| | |
| | | SELECT |
| | | td.id, |
| | | td.name, |
| | | td.parent_id |
| | | td.parent_id, |
| | | td.special, |
| | | td.level |
| | | FROM |
| | | t_department td |
| | | WHERE |
| | |
| | | SELECT |
| | | td.id, |
| | | td.name, |
| | | td.parent_id |
| | | td.parent_id, |
| | | td.special, |
| | | td.level |
| | | FROM |
| | | t_department td |
| | | <where> |
| | |
| | | td.id, |
| | | td.name, |
| | | td.parent_id, |
| | | td.special, |
| | | td.level, |
| | | td.admin_id as adminId, |
| | | tu.real_name as adminName |
| | | FROM |
| | |
| | | </select> |
| | | |
| | | <select id="list" resultType="com.mindskip.xzs.domain.vo.CascaderDataVO"> |
| | | SELECT id as value, name as label, parent_id FROM t_department WHERE deleted = 0 |
| | | SELECT id as value, name as label, parent_id, special, level FROM t_department WHERE deleted = 0 |
| | | </select> |
| | | |
| | | <select id="getChilds" resultType="integer"> |
| | | WITH RECURSIVE temp_table AS ( |
| | | SELECT |
| | | id, name, parent_id FROM t_department WHERE id in <foreach collection="deptIds" open="(" item="deptId" close=")" separator=",">#{deptId}</foreach> |
| | | id, name, parent_id,special,level FROM t_department WHERE id in <foreach collection="deptIds" open="(" item="deptId" close=")" separator=",">#{deptId}</foreach> |
| | | UNION ALL |
| | | SELECT |
| | | so.id, so.name, so.parent_id FROM t_department so INNER JOIN temp_table tb ON so.parent_id = tb.id |
| | | so.id, so.name, so.parent_id, so.special, so.level FROM t_department so INNER JOIN temp_table tb ON so.parent_id = tb.id |
| | | ) |
| | | SELECT |
| | | DISTINCT id |
| | |
| | | temp_table |
| | | </select> |
| | | |
| | | <select id="getFather" resultType="integer"> |
| | | WITH RECURSIVE temp_table AS ( |
| | | SELECT |
| | | id, name, parent_id,special,level FROM t_department WHERE id = #{deptId} |
| | | UNION ALL |
| | | SELECT |
| | | so.id, so.name, so.parent_id, so.special, so.level FROM t_department so INNER JOIN temp_table tb ON so.id = tb.parent_id |
| | | ) |
| | | SELECT |
| | | DISTINCT id |
| | | FROM |
| | | temp_table |
| | | </select> |
| | | |
| | | </mapper> |