Qchen vor 1 Jahr
Ursprung
Commit
377b336bbf

+ 4 - 0
commons/pom.xml

@@ -41,5 +41,9 @@
             <groupId>org.redisson</groupId>
             <artifactId>redisson-spring-data-27</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-extension</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 19 - 0
platform/src/main/java/com/jzg/constants/ManagementSourceEnum.java

@@ -0,0 +1,19 @@
+package com.jzg.constants;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 机构来源枚举
+ */
+@Getter
+@AllArgsConstructor
+public enum ManagementSourceEnum {
+    MS_01("1", "渠道"),
+    MS_02("2", "个代"),
+    MS_03("3", "电销");
+
+    private final String code;
+
+    private final String desc;
+}

+ 65 - 0
platform/src/main/java/com/jzg/controller/DeptController.java

@@ -0,0 +1,65 @@
+package com.jzg.controller;
+
+import com.jzg.core.base.BaseController;
+import com.jzg.core.page.HttpResult;
+import com.jzg.entity.po.SysDept;
+import com.jzg.entity.vo.SysDeptVo;
+import com.jzg.service.DeptService;
+import io.swagger.v3.oas.annotations.Operation;
+import jakarta.annotation.Resource;
+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 java.util.List;
+
+@RestController
+@RequestMapping("/dept")
+public class DeptController extends BaseController {
+    @Resource
+    private DeptService deptService;
+
+
+    @Operation(summary = "新增机构接口", description = "新增机构接口")
+    @PostMapping(value = "/add")
+    public HttpResult<Boolean> addDept(@RequestBody SysDeptVo sysDeptVo) {
+        deptService.addDept(sysDeptVo);
+        return HttpResult.ok();
+    }
+
+    @PostMapping(value = "/update")
+    public HttpResult updateDept(@RequestBody SysDept sysDept) {
+        SysDept byId = deptService.getById(sysDept.getId());
+        if (sysDept.getManagementSource() != null){
+            if (!sysDept.getManagementSource().equals(byId.getManagementSource())) {
+                return HttpResult.error("机构已被设置来源无法修改");
+            }
+        }
+        deptService.updateDept(sysDept);
+        return HttpResult.ok();
+    }
+
+    @PostMapping(value = "/delete")
+    public HttpResult deleteDept(@RequestBody SysDeptVo sysDeptVo) {
+        return deptService.deleteDept(sysDeptVo);
+    }
+
+    @PostMapping(value = "/batchLeader")
+    public HttpResult batchLeader(@RequestBody SysDeptVo sysDeptVo) {
+        return deptService.batchLeader(sysDeptVo);
+    }
+
+    @PostMapping(value = "/batchDeleteDept")
+    public HttpResult batchDeleteDept(@RequestBody SysDeptVo sysDeptVo) {
+        return deptService.batchDeleteDept(sysDeptVo);
+    }
+
+    @PostMapping(value = "/getDeptTree")
+    public HttpResult<List<SysDept>> getDeptTree(){
+        String deptId = getUserName();
+        return HttpResult.ok(deptService.getDeptTree(deptId));
+    }
+
+
+}

+ 108 - 0
platform/src/main/java/com/jzg/entity/po/SysDept.java

@@ -0,0 +1,108 @@
+package com.jzg.entity.po;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.List;
+
+@TableName(value = "sys_dept")
+@Data
+@Schema(description = "机构管理")
+public class SysDept implements Serializable {
+    @TableId(value = "id")
+    @Schema(description = "主键")
+    private String id;
+
+    @TableField(value = "name")
+    @Schema(description = "机构名称")
+    private String name;
+
+    @TableField(value = "parent_id")
+    @Schema(description = "父级机构id")
+    private String parentId;
+
+    @TableField(value = "order_num")
+    @Schema(description = "排序")
+    private Integer orderNum;
+
+    @TableField(value = "create_by")
+    @Schema(description = "创建人")
+    private String createBy;
+
+    @TableField(value = "create_time")
+    @Schema(description = "创建时间")
+    private Date createTime;
+
+    @TableField(value = "update_by")
+    @Schema(description = "更新人")
+    private String updateBy;
+
+    @TableField(value = "update_time")
+    @Schema(description = "更新时间")
+    private Date updateTime;
+
+    @TableField(value = "del_flag")
+    @Schema(description = "是否删除  -1:已删除  0:正常")
+    private Integer delFlag;
+
+    @TableField(value = "com_type")
+    @Schema(description = "机构类型")
+    private String comType;
+
+    @TableField(value = "com_level")
+    @Schema(description = "机构级别")
+    private String comLevel;
+
+    @TableField(value = "address")
+    @Schema(description = "机构地址")
+    private String address;
+
+    @TableField(value = "leader_id")
+    @Schema(description = "负责人id")
+    private String leaderId;
+
+    @TableField(value = "com_attribute")
+    @Schema(description = "机构性质")
+    private String comAttribute;
+
+    @TableField(value = "is_stop")
+    @Schema(description = "是否停业")
+    private String isStop;
+
+    @TableField(value = "province")
+    @Schema(description = "省")
+    private String province;
+
+    @TableField(value = "city")
+    @Schema(description = "市")
+    private String city;
+
+    @TableField(value = "area")
+    @Schema(description = "区县")
+    private String area;
+
+    @TableField(value = "house")
+    @Schema(description = "门店")
+    private String house;
+
+    @TableField(value = "dept_type")
+    @Schema(description = "子公司C,门店M,部门D")
+    private String deptType;
+
+    /**
+     * @see com.jzg.constants.ManagementSourceEnum
+     */
+    @TableField(value = "management_source")
+    @Schema(description = "机构来源(渠道、个代、电销)")
+    private String managementSource;
+
+    @TableField(exist = false)
+    @Schema(description = "子机构")
+    private List<SysDept> children;
+
+}

+ 77 - 0
platform/src/main/java/com/jzg/entity/vo/SysDeptVo.java

@@ -0,0 +1,77 @@
+package com.jzg.entity.vo;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.List;
+
+@Data
+public class SysDeptVo implements Serializable {
+    @Schema(description = "机构id")
+    private String id;
+
+    @Schema(description = "机构名称")
+    private String name;
+
+    @Schema(description = "父级机构id")
+    private String parentId;
+
+    @Schema(description ="排序")
+    private Integer orderNum;
+
+    @Schema(description ="创建人")
+    private String createBy;
+
+    @Schema(description ="创建时间")
+    private Date createTime;
+
+    @Schema(description ="更新人")
+    private String lastUpdateBy;
+
+    @Schema(description ="更新时间")
+    private Date lastUpdateTime;
+
+    @Schema(description ="是否删除  -1:已删除  0:正常")
+    private Integer delFlag;
+
+    @Schema(description ="机构类型")
+    private String comType;
+
+    @Schema(description ="机构级别")
+    private String comLevel;
+
+    @Schema(description ="机构地址")
+    private String address;
+
+    @Schema(description ="负责人Id")
+    private String leaderId;
+
+    @Schema(description = "机构性质")
+    private String comAttribute;
+
+    @Schema(description = "是否停业")
+    private String isStop;
+
+    @Schema(description = "省")
+    private String province;
+
+    @Schema(description = "市")
+    private String city;
+
+    @Schema(description = "区县")
+    private String area;
+
+    @Schema(description = "门店")
+    private String house;
+
+    @Schema(description = "子公司C,门店M,部门D")
+    private String deptType;
+
+    @Schema(description = "机构来源")
+    private String managementSource;
+
+    @Schema(description = "机构ids")
+    private List<String> deptIds;
+}

+ 13 - 0
platform/src/main/java/com/jzg/mapper/SysDeptMapper.java

@@ -0,0 +1,13 @@
+package com.jzg.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.jzg.entity.po.SysDept;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+@Mapper
+public interface SysDeptMapper extends BaseMapper<SysDept> {
+
+    String getMaxId(@Param(value = "deptId") String deptId, @Param(value = "deptType")String deptType, @Param(value = "comLevel")String comLevel);
+
+}

+ 40 - 0
platform/src/main/java/com/jzg/service/DeptService.java

@@ -0,0 +1,40 @@
+package com.jzg.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.jzg.core.page.HttpResult;
+import com.jzg.entity.po.SysDept;
+import com.jzg.entity.vo.SysDeptVo;
+
+import java.util.List;
+
+public interface DeptService extends IService<SysDept> {
+    /**
+     * 新增机构
+     */
+    SysDeptVo addDept(SysDeptVo sysDeptVo);
+
+    /**
+     * 更新机构
+     */
+    HttpResult updateDept(SysDept sysDept);
+
+    /**
+     * 删除机构
+     */
+    HttpResult deleteDept(SysDeptVo sysDeptVo);
+
+    /**
+     * 批量更新机构负责人
+     */
+    HttpResult batchLeader(SysDeptVo sysDeptVo);
+
+    /**
+     *  批量删除机构
+     */
+    HttpResult batchDeleteDept(SysDeptVo sysDeptVo);
+
+    /**
+     * 机构树
+     */
+    List<SysDept> getDeptTree(String deptId);
+}

+ 256 - 0
platform/src/main/java/com/jzg/service/impl/DeptServiceImpl.java

@@ -0,0 +1,256 @@
+package com.jzg.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.jzg.core.page.HttpResult;
+import com.jzg.entity.po.SysDept;
+import com.jzg.entity.vo.SysDeptVo;
+import com.jzg.exception.SystemException;
+import com.jzg.mapper.SysDeptMapper;
+import com.jzg.service.DeptService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.ObjectUtils;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Service
+@Slf4j
+public class DeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements DeptService {
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public SysDeptVo addDept(SysDeptVo sysDeptVo) {
+        SysDept sysDept = new SysDept();
+        BeanUtils.copyProperties(sysDeptVo, sysDept);
+        String comlevel = ObjectUtils.isEmpty(sysDept.getComLevel()) ? "" : sysDept.getComLevel();
+        String parentId = sysDept.getParentId();
+
+        String number;
+        if (comlevel.equals("2")) {
+            AssertionUtils.isStringEmpty(sysDept.getProvince(), "二级机构省份必填");
+            number = sysDept.getProvince().substring(0, 2);
+        } else if (comlevel.equals("3")) {
+            AssertionUtils.isStringEmpty(sysDept.getCity(), "三级机构市必填");
+            number = sysDept.getCity().substring(0, 4);
+            String maxId = baseMapper.getMaxId(parentId, sysDeptVo.getDeptType(), comlevel);
+            if (!maxId.equals("0")) {
+                String substring = maxId.substring(sysDept.getParentId().length() + 1);
+                int i = Integer.parseInt("1" + substring) + 1;
+                number = String.valueOf(i);
+            } else {
+                number = "101";
+            }
+        } else if (comlevel.equals("4")) {
+            AssertionUtils.isStringEmpty(sysDept.getArea(), "四级机构区必填");
+            number = sysDept.getArea().substring(0, 6);
+            String maxId = baseMapper.getMaxId(parentId, sysDeptVo.getDeptType(), comlevel);
+            if (!maxId.equals("0")) {
+                String substring = maxId.substring(sysDept.getParentId().length() + 1);
+                int i = Integer.parseInt("1" + substring) + 1;
+                number = String.valueOf(i);
+            } else {
+                number = "101";
+            }
+        } else {
+            String deptType = sysDeptVo.getDeptType();
+
+            if ("C".equals(sysDept.getDeptType()) && comlevel.equals("5")) {
+                sysDeptVo.setDeptType("M");
+            } else if ("D".equals(sysDept.getDeptType())) {
+                sysDeptVo.setDeptType("D");
+                deptType = "M";
+            }
+
+            String maxId = baseMapper.getMaxId(parentId, deptType, comlevel);
+            if (!maxId.equals("0")) {
+                String substring = maxId.substring(sysDept.getParentId().length() + 1);
+                int i = Integer.parseInt("1" + substring) + 1;
+                number = String.valueOf(i);
+            } else {
+                number = "101";
+            }
+        }
+
+        String substring = number.substring(number.length() - 2);
+        String serialNumber;
+        if ("M,D".contains(sysDeptVo.getDeptType())) {
+            serialNumber = parentId + sysDeptVo.getDeptType() + substring;
+        } else {
+            serialNumber = parentId + substring;
+        }
+
+        sysDept.setId(serialNumber);
+
+        SysDept dept = getById(sysDept.getId());
+        if (!ObjectUtils.isEmpty(dept)) {
+            throw new SystemException("机构编号为:" + dept.getId() + " " + dept.getName() + "已被占用");
+        }
+        SysDept parentDept = baseMapper.selectById(sysDept.getParentId());
+
+        if (parentDept.getComLevel().equals("5")) {
+            sysDept.setManagementSource(parentDept.getManagementSource());
+        }
+
+        // 查询当前部门序号是否存在
+        LambdaQueryWrapper<SysDept> qw = new LambdaQueryWrapper<>();
+        qw.eq(SysDept::getDelFlag, 0);  // 删除标志为未删除
+        qw.eq(SysDept::getParentId, sysDept.getParentId()); // 父级部门ID相同
+        qw.eq(SysDept::getOrderNum, sysDept.getOrderNum());  // 排序号相同
+        boolean exists = baseMapper.exists(qw);
+
+        if (exists) {
+            // 如果存在,查询该父级部门下排序号大于当前部门的部门列表
+            LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
+            lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
+            lqw.eq(SysDept::getParentId, sysDept.getParentId()); // 父级部门ID相同
+            lqw.ge(SysDept::getOrderNum, sysDept.getOrderNum());  // 排序号大于等于当前部门
+            List<SysDept> sysDeptList = list(lqw);
+            // 更新这些部门的排序号
+            sysDeptList.forEach(i -> {
+                i.setOrderNum(i.getOrderNum() + 1);
+                updateById(i);
+            });
+        }
+        boolean b = save(sysDept);
+        log.info("------------------添加部门成功-----------------------");
+        AssertionUtils.isSucceed(b, "删除失败");
+        sysDeptVo.setId(sysDept.getId());
+        return sysDeptVo;
+    }
+
+    @Override
+    @Transactional
+    public HttpResult updateDept(SysDept sysDeptVo) {
+        // 查询修改的序号是否存在
+        LambdaQueryWrapper<SysDept> qw = new LambdaQueryWrapper<>();
+        qw.eq(SysDept::getDelFlag, 0);  // 删除标志为未删除
+        qw.eq(SysDept::getParentId, sysDeptVo.getParentId()); // 父级部门ID相同
+        qw.eq(SysDept::getOrderNum, sysDeptVo.getOrderNum());  // 排序号相同
+        boolean exists = baseMapper.exists(qw);
+        //不存在则直接修改
+        if (!exists) {
+            baseMapper.updateById(sysDeptVo);
+            return HttpResult.ok();
+        }
+
+        //查询机构
+        SysDept sysDept = getById(sysDeptVo.getId());
+        //原本的序号
+        Integer orderNum = sysDept.getOrderNum();
+        //本次修改的序号
+        Integer newOrderNum = sysDeptVo.getOrderNum();
+        // 向上换位
+        if (newOrderNum < orderNum) {
+            // 查询该父级部门下排序号大于当前部门的部门列表
+            LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
+            lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
+            lqw.eq(SysDept::getParentId, sysDeptVo.getParentId()); // 父级部门ID相同
+            lqw.ge(SysDept::getOrderNum, sysDeptVo.getOrderNum());  // 排序号大于等于当前部门
+            List<SysDept> sysDeptList = list(lqw);
+            sysDeptList.sort(Comparator.comparing(SysDept::getOrderNum));
+            for (SysDept dept : sysDeptList) {
+                dept.setOrderNum(dept.getOrderNum() + 1);
+                updateById(dept);
+                if (dept.getOrderNum().equals(orderNum)) {
+                    break;
+                }
+            }
+            baseMapper.updateById(sysDeptVo);
+        }
+        //向下换位
+        if (newOrderNum > orderNum) {
+            // 查询该父级部门下排序号小于当前部门的部门列表
+            LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
+            lqw.eq(SysDept::getDelFlag, 0); // 删除标志为未删除
+            lqw.eq(SysDept::getParentId, sysDeptVo.getParentId()); // 父级部门ID相同
+            lqw.le(SysDept::getOrderNum, sysDeptVo.getOrderNum());  // 排序号大于等于当前部门
+            List<SysDept> sysDeptList = list(lqw);
+            sysDeptList.sort(Comparator.comparing(SysDept::getOrderNum).reversed());
+            for (SysDept dept : sysDeptList) {
+                dept.setOrderNum(dept.getOrderNum() - 1);
+                updateById(dept);
+                if (dept.getOrderNum().equals(orderNum)) {
+                    break;
+                }
+            }
+            baseMapper.updateById(sysDeptVo);
+        }
+        if (newOrderNum.equals(orderNum)) {
+            baseMapper.updateById(sysDeptVo);
+        }
+        return HttpResult.ok();
+    }
+
+    @Override
+    @Transactional
+    public HttpResult batchLeader(SysDeptVo sysDeptVo) {
+        // 更新每个部门的负责人
+        for (String id : sysDeptVo.getDeptIds()) {
+            SysDept sysDept = getById(id);
+            if (sysDept == null) {
+                throw new SystemException("机构不存在");
+            }
+            sysDept.setLeaderId(sysDeptVo.getLeaderId());
+            baseMapper.updateById(sysDept);
+        }
+        return HttpResult.ok("批量设置负责人成功。");
+    }
+
+    @Override
+    @Transactional
+    public HttpResult deleteDept(SysDeptVo sysDeptVo) {
+        SysDept sysDept = getById(sysDeptVo.getId());
+        if (sysDept == null) {
+            throw new SystemException("机构不存在");
+        }
+        List<SysDept> deptList = lambdaQuery().eq(SysDept::getParentId, sysDept.getId()).list();
+//        List<SysUser> userList = userService.lambdaQuery().eq(SysUser::getDeptId, sysDept.getId()).list();
+        if (!deptList.isEmpty()) {
+            throw new SystemException(sysDept.getName() + "机构下还有其他隶属机构请将这些子机构删除后,再进行删除");
+        }
+//        if (!userList.isEmpty()) {
+//            throw new SystemException(sysDept.getName() + "机构下还有其他用户请将这些用户删除后,再进行删除");
+//        }
+        sysDept.setDelFlag(1);
+        baseMapper.updateById(sysDept);
+        return HttpResult.ok("删除机构成功");
+    }
+
+    @Override
+    @Transactional
+    public HttpResult batchDeleteDept(SysDeptVo sysDeptVo) {
+        // 更新每个部门的负责人
+        for (String id : sysDeptVo.getDeptIds()) {
+            SysDept sysDept = getById(id);
+            if (sysDept == null) {
+                throw new SystemException("机构不存在");
+            }
+            List<SysDept> deptList = lambdaQuery().eq(SysDept::getParentId, sysDept.getId()).list();
+//            List<SysUser> userList = userService.lambdaQuery().eq(SysUser::getDeptId, sysDept.getId()).list();
+            if (!deptList.isEmpty()) {
+                throw new SystemException(sysDept.getName() + "机构下还有其他隶属机构请将这些子机构删除后,再进行删除");
+            }
+//            if (!userList.isEmpty()) {
+//                throw new SystemException(sysDept.getName() + "机构下还有其他用户请将这些用户删除后,再进行删除");
+//            }
+            sysDept.setDelFlag(1);
+            baseMapper.updateById(sysDept);
+        }
+        return HttpResult.ok("批量删除机构成功");
+    }
+
+    @Override
+    public List<SysDept> getDeptTree(String deptId) {
+        SysDept sysDept = getById(deptId);
+        LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
+        lqw.eq(SysDept::getDelFlag, 0);
+        lqw.likeRight(SysDept::getId, deptId);
+        List<SysDept> sysDeptList = list(lqw);
+        Map<String, List<SysDept>> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
+        sysDeptList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>())));
+        return map.get(sysDept.getParentId());
+    }
+}

+ 12 - 0
platform/src/main/resources/mapper/SysDeptMapper.xml

@@ -0,0 +1,12 @@
+<?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.jzg.mapper.SysDeptMapper">
+    <select id="getMaxId" resultType="java.lang.String">
+        SELECT IFNULL(MAX(id), 0) maxid
+        FROM `sys_dept`
+        WHERE parent_id = #{deptId}
+        <if test='comLevel != null and comLevel != ""'>
+            and comlevel = #{comlevel}
+        </if>
+    </select>
+</mapper>