TRX 1 год назад
Родитель
Сommit
d46a7c5e7a

+ 46 - 0
src/main/java/com/zswl/dataservice/controller/user/DepartmentController.java

@@ -0,0 +1,46 @@
+package com.zswl.dataservice.controller.user;
+
+import com.zswl.dataservice.model.baseParam.IDParam;
+import com.zswl.dataservice.model.user.DepartmentParam;
+import com.zswl.dataservice.service.user.DepartmentService;
+import com.zswl.dataservice.utils.result.ResultContent;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.enums.ParameterIn;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("department")
+@Tag(name = "部门管理--通用")
+public class DepartmentController {
+
+    @Autowired
+    private DepartmentService departmentService;
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @RequestMapping(value = "addDepartment", method = {RequestMethod.POST})
+    @Operation(summary = "添加-编辑部门", description = "添加-编辑部门")
+    public ResultContent addDepartment(@RequestBody DepartmentParam param) {
+        return this.departmentService.addDepartment(param);
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "得到机构部门树-通用", description = "得到机构部门树")
+    @RequestMapping(value = "getDepartmentTree", method = {RequestMethod.GET})
+    public ResultContent getDepartmentTree() {
+        return this.departmentService.getDepartmentTree();
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "删除部门-通用", description = "删除部门")
+    @RequestMapping(value = "deleteDepartment", method = {RequestMethod.POST})
+    public ResultContent deleteDepartment(@RequestBody IDParam param) {
+        return this.departmentService.deleteDepartment(param.getId());
+    }
+
+}

+ 20 - 0
src/main/java/com/zswl/dataservice/dao/DepartmentDao.java

@@ -0,0 +1,20 @@
+package com.zswl.dataservice.dao;
+
+import com.zswl.dataservice.domain.user.Department;
+
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2024/3/21
+ */
+public interface DepartmentDao extends MongoDao<Department> {
+
+    Department findTopById(String id);
+
+    Department findTopByCode(String code);
+
+    List<Department> findByIdIn(List<String> ids);
+
+    List<Department> findByParentId(String parentId);
+}

+ 1 - 1
src/main/java/com/zswl/dataservice/dataConfig/ResultMessage.java

@@ -6,7 +6,7 @@ package com.zswl.dataservice.dataConfig;
  */
 public class ResultMessage {
 
-    public static final String DATA_NOT_EXIT = "数据不存在:%s";
+    public static final String DATA_NOT_EXIST = "数据不存在:%s";
 
     public static final String NAME_EXIT = "名称存在:%s";
 

+ 41 - 0
src/main/java/com/zswl/dataservice/domain/user/Department.java

@@ -0,0 +1,41 @@
+package com.zswl.dataservice.domain.user;
+
+import com.zswl.dataservice.domain.base.SuperEntity;
+import com.zswl.dataservice.type.DataState;
+import com.zswl.dataservice.utils.ITree;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.data.mongodb.core.index.Indexed;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+/**
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Data
+@Document
+@AllArgsConstructor
+@NoArgsConstructor
+public class Department extends SuperEntity {
+
+    @Schema(description = "部门名称")
+    private String name;
+
+    @Schema(description = "部门编码")
+    private String code;
+
+    @Schema(description = "部门状态")
+    private DataState state = DataState.Enable;
+
+    @Schema(description = "上级ID")
+    @Indexed
+    private String parentId = ITree.ROOT_ID;
+
+    public static Department build(String id) {
+        Department entity = new Department();
+        entity.setId(id);
+        return entity;
+    }
+}

+ 34 - 0
src/main/java/com/zswl/dataservice/model/user/DepartmentModel.java

@@ -0,0 +1,34 @@
+package com.zswl.dataservice.model.user;
+
+import com.zswl.dataservice.model.baseParam.SuperModel;
+import com.zswl.dataservice.type.DataState;
+import com.zswl.dataservice.utils.ITree;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Data
+public class DepartmentModel extends SuperModel implements ITree<DepartmentModel>, Serializable {
+
+    @Schema(description = "部门名称")
+    private String name;
+
+    @Schema(description = "部门编码")
+    private String code;
+
+    @Schema(description = "部门状态")
+    private DataState state = DataState.Enable;
+
+    @Schema(description = "上级ID")
+    private String parentId;
+
+    @Schema(description = "子层级")
+    private List<DepartmentModel> children = new ArrayList<>();
+}

+ 26 - 0
src/main/java/com/zswl/dataservice/model/user/DepartmentParam.java

@@ -0,0 +1,26 @@
+package com.zswl.dataservice.model.user;
+
+import com.zswl.dataservice.model.baseParam.SuperParam;
+import com.zswl.dataservice.type.DataState;
+import com.zswl.dataservice.utils.ITree;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Data
+public class DepartmentParam extends SuperParam {
+    @Schema(description = "部门名称")
+    private String name;
+
+    @Schema(description = "部门编码")
+    private String code;
+
+    @Schema(description = "部门状态")
+    private DataState state = DataState.Enable;
+
+    @Schema(description = "上级ID")
+    private String parentId = ITree.ROOT_ID;
+}

+ 23 - 0
src/main/java/com/zswl/dataservice/model/user/DepartmentSearchParam.java

@@ -0,0 +1,23 @@
+package com.zswl.dataservice.model.user;
+
+import com.zswl.dataservice.model.baseParam.SuperSearchParam;
+import com.zswl.dataservice.type.DataState;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Data
+public class DepartmentSearchParam extends SuperSearchParam {
+
+    @Schema(description = "部门名称")
+    private String name;
+
+    @Schema(description = "部门状态")
+    private DataState state = DataState.Enable;
+
+    @Schema(description = "上级ID")
+    private String parentId;
+}

+ 9 - 9
src/main/java/com/zswl/dataservice/service/iot/IotServiceImpl.java

@@ -52,7 +52,7 @@ public class IotServiceImpl extends SuperService {
         if (StringUtils.isNotEmpty(param.getId())) {
             template = iotTemplateDao.findTopById(param.getId());
             if (ObjectUtils.isEmpty(template)) {
-                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, param.getId()));
+                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
             }
             IotTemplate temp = iotTemplateDao.findTopByName(param.getName());
             if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
@@ -73,7 +73,7 @@ public class IotServiceImpl extends SuperService {
     public ResultContent deleteTemplate(String id) {
         IotTemplate template = iotTemplateDao.findTopById(id);
         if (ObjectUtils.isEmpty(template)) {
-            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, id));
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
         }
         iotTemplateDao.delete(template);
         return ResultContent.buildSuccess();
@@ -82,7 +82,7 @@ public class IotServiceImpl extends SuperService {
     public ResultContent<IotTemplateModel> getIotTemplate(String id) {
         IotTemplate template = iotTemplateDao.findTopById(id);
         if (ObjectUtils.isEmpty(template)) {
-            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, id));
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
         }
         return ResultContent.buildSuccess(toModel(template));
     }
@@ -107,7 +107,7 @@ public class IotServiceImpl extends SuperService {
         if (StringUtils.isNotEmpty(param.getId())) {
             entity = iotTopicDao.findTopById(param.getId());
             if (ObjectUtils.isEmpty(entity)) {
-                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, param.getId()));
+                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
             }
             IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate);
             if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
@@ -129,7 +129,7 @@ public class IotServiceImpl extends SuperService {
     public ResultContent deleteIotTopic(String id) {
         IotTopic entity = iotTopicDao.findTopById(id);
         if (ObjectUtils.isEmpty(entity)) {
-            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, id));
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
         }
         iotTopicDao.delete(entity);
         return ResultContent.buildSuccess();
@@ -138,7 +138,7 @@ public class IotServiceImpl extends SuperService {
     public ResultContent<IotTopicModel> getIotTopic(String id) {
         IotTopic entity = iotTopicDao.findTopById(id);
         if (ObjectUtils.isEmpty(entity)) {
-            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, id));
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
         }
         return ResultContent.buildSuccess(toModel(entity));
     }
@@ -163,7 +163,7 @@ public class IotServiceImpl extends SuperService {
         if (StringUtils.isNotEmpty(param.getId())) {
             entity = iotMainDao.findTopById(param.getId());
             if (ObjectUtils.isEmpty(entity)) {
-                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, param.getId()));
+                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
             }
             IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getName(), iotTemplate);
             if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
@@ -185,7 +185,7 @@ public class IotServiceImpl extends SuperService {
     public ResultContent deleteIotMain(String id) {
         IotMain entity = iotMainDao.findTopById(id);
         if (ObjectUtils.isEmpty(entity)) {
-            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, id));
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
         }
         iotMainDao.delete(entity);
         return ResultContent.buildSuccess();
@@ -194,7 +194,7 @@ public class IotServiceImpl extends SuperService {
     public ResultContent<IotMainModel> getIotMain(String id) {
         IotMain entity = iotMainDao.findTopById(id);
         if (ObjectUtils.isEmpty(entity)) {
-            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIT, id));
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
         }
         return ResultContent.buildSuccess(toModel(entity));
     }

+ 25 - 0
src/main/java/com/zswl/dataservice/service/user/DepartmentService.java

@@ -0,0 +1,25 @@
+package com.zswl.dataservice.service.user;
+
+import com.zswl.dataservice.model.user.DepartmentModel;
+import com.zswl.dataservice.model.user.DepartmentParam;
+import com.zswl.dataservice.utils.ITree;
+import com.zswl.dataservice.utils.result.ResultContent;
+
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2024/6/20
+ */
+public interface DepartmentService {
+
+    ResultContent addDepartment(DepartmentParam param);
+
+    ResultContent<List<ITree>> getDepartmentTree();
+
+    // 查询所有上级部门
+    List<DepartmentModel> getParents(String id);
+
+    // 删除部门
+    ResultContent deleteDepartment(String id);
+}

+ 154 - 0
src/main/java/com/zswl/dataservice/service/user/impl/DepartmentServiceImpl.java

@@ -0,0 +1,154 @@
+package com.zswl.dataservice.service.user.impl;
+
+import com.zswl.dataservice.dao.DepartmentDao;
+import com.zswl.dataservice.dataConfig.ResultMessage;
+import com.zswl.dataservice.domain.user.Department;
+import com.zswl.dataservice.model.user.DepartmentModel;
+import com.zswl.dataservice.model.user.DepartmentParam;
+import com.zswl.dataservice.service.base.SuperService;
+import com.zswl.dataservice.service.user.DepartmentService;
+import com.zswl.dataservice.type.DataState;
+import com.zswl.dataservice.utils.CommonUtil;
+import com.zswl.dataservice.utils.ITree;
+import com.zswl.dataservice.utils.TreeUtil;
+import com.zswl.dataservice.utils.bean.BeanUtils;
+import com.zswl.dataservice.utils.result.ResultContent;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Slf4j
+@Service
+public class DepartmentServiceImpl extends SuperService implements DepartmentService {
+
+    @Autowired
+    DepartmentDao departmentDao;
+
+    /**
+     * 添加部门
+     *
+     * @param param
+     * @return
+     */
+    @Override
+    public ResultContent<Department> addDepartment(DepartmentParam param) {
+        initDefaultUser(param);
+
+        if (CommonUtil.longIsEmpty(param.getSort())) {
+            param.setSort(1L);
+        }
+        if (StringUtils.isEmpty(param.getParentId())) {
+            param.setParentId(ITree.ROOT_ID);
+        }
+        if (!param.getParentId().equals(ITree.ROOT_ID)) {
+            Department temp = departmentDao.findTopById(param.getParentId());
+            if (ObjectUtils.isEmpty(temp)) {
+                return ResultContent.buildFail(String.format("上级部门ID不存在:%s", param.getParentId()));
+            }
+        }
+        if (param.getState() == null) {
+            param.setState(DataState.Enable);
+        }
+        Department department = null;
+        if (StringUtils.isNotEmpty(param.getId())) {
+            department = departmentDao.findTopById(param.getId());
+            if (ObjectUtils.isEmpty(department)) {
+                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
+            }
+            if (StringUtils.isNotEmpty(param.getCode())) {
+                department = departmentDao.findTopByCode(param.getCode());
+                if (ObjectUtils.isNotEmpty(department) && !department.getId().equals(param.getId())) {
+                    return ResultContent.buildFail(String.format("%s code已存在", param.getCode()));
+                }
+            }
+        } else {
+            if (StringUtils.isNotEmpty(param.getCode())) {
+                department = departmentDao.findTopByCode(param.getCode());
+                if (ObjectUtils.isNotEmpty(department)) {
+                    return ResultContent.buildFail(String.format("%s code已存在", param.getCode()));
+                }
+            }
+            department = new Department();
+        }
+        BeanUtils.copyProperties(param, department);
+        departmentDao.save(department);
+        return ResultContent.buildSuccess(department);
+    }
+
+    /**
+     * 删除部门
+     *
+     * @param id
+     * @return
+     */
+    @Override
+    public ResultContent deleteDepartment(String id) {
+        Department department = departmentDao.findTopById(id);
+        if (ObjectUtils.isEmpty(department)) {
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
+        }
+        List<Department> childrens = departmentDao.findByParentId(id);
+        if (ObjectUtils.isNotEmpty(childrens)) {
+            return ResultContent.buildFail(String.format("%s 有下级部门,不能删除", department.getName()));
+        }
+        departmentDao.delete(department);
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 得到结构所有部门信息 (树形结构)
+     *
+     * @param
+     * @return
+     */
+    @Override
+    public ResultContent<List<ITree>> getDepartmentTree() {
+        List<Department> list = departmentDao.findAll(Sort.by(Sort.Order.asc("sort"),
+                Sort.Order.asc("createTime")));
+        List<ITree> models = list.stream().map(this::toModel).collect(Collectors.toList());
+        List<ITree> iTrees = TreeUtil.buildTree(models, ITree.ROOT_ID);
+        return ResultContent.buildSuccess(iTrees);
+    }
+
+    /**
+     * 加载当前部门的所有上级部门(包括自己)
+     *
+     * @param id
+     * @return
+     */
+    public List<DepartmentModel> getParents(String id) {
+        List<Department> list = new ArrayList<>();
+        loopLoad(id, list);
+        List<DepartmentModel> models = list.stream().map(this::toModel).collect(Collectors.toList());
+        return models;
+    }
+
+    private void loopLoad(String id, List<Department> list) {
+        Department entity = departmentDao.findTopById(id);
+        if (ObjectUtils.isNotEmpty(entity)) {
+            list.add(entity);
+            if (StringUtils.isNotEmpty(entity.getParentId()) && !entity.getParentId().equals(ITree.ROOT_ID)) {
+                loopLoad(entity.getParentId(), list);
+            }
+        }
+    }
+
+    public DepartmentModel toModel(Department entity) {
+        DepartmentModel model = new DepartmentModel();
+        if (ObjectUtils.isNotEmpty(entity)) {
+            BeanUtils.copyProperties(entity, model);
+        }
+        return model;
+    }
+}