Browse Source

组织架构增删改查

zzf 1 year ago
parent
commit
9d77cf513d

+ 1 - 1
src/main/java/com/rf/psychological/structure/dao/model/StructureEntity.java

@@ -26,7 +26,7 @@ import javax.persistence.Table;
 @EqualsAndHashCode(callSuper=true)
 @org.hibernate.annotations.Table(appliesTo = "t_structure_info", comment = "组织架构信息表")
 public class StructureEntity extends BaseEntity {
-    @Column(name = "structure_no",columnDefinition = "varchar(50) not null comment '组织架构编号'")
+    @Column(name = "structure_no",columnDefinition = "varchar(50) unique not null comment '组织架构编号'")
     private String structureNo;
     @Column(name = "structure_name",columnDefinition = "varchar(50) not null comment '组织架构名称'")
     private String structureName;

+ 2 - 1
src/main/java/com/rf/psychological/structure/dao/repository/StructureRepository.java

@@ -11,5 +11,6 @@ public interface StructureRepository extends BaseRepository<StructureEntity,Stri
     @Query(value = "SELECT t3.id, t3.parent_structure_no, t3.structure_name, t3.structure_no FROM ( SELECT t1.*, IF ( FIND_IN_SET(parent_structure_no, @pids) > 0, @pids \\:= concat(@pids, ',', structure_no), '' ) AS ischild FROM ( SELECT * FROM t_structure_info ) t1,( SELECT @pids \\:= :structureNo ) t2 ) t3 WHERE t3.ischild LIKE :structureNo2 ",nativeQuery = true)
     List<StructureEntity> findByIdRecursion(@Param("structureNo") String structureNo,@Param("structureNo2")String structureNo2);
 
-
+    @Query(value = "SELECT t3.structure_no FROM ( SELECT t1.*, IF ( FIND_IN_SET(parent_structure_no, @pids) > 0, @pids \\:= concat(@pids, ',', structure_no), '' ) AS ischild FROM ( SELECT * FROM t_structure_info ) t1,( SELECT @pids \\:= :structureNo ) t2 ) t3 WHERE t3.ischild LIKE :structureNo2 ",nativeQuery = true)
+    List<String> findStructureNoByIdRecursion(String structureNo, String s);
 }

+ 20 - 4
src/main/java/com/rf/psychological/structure/rest/StructureController.java

@@ -6,6 +6,8 @@ import com.rf.psychological.security.AesEncryptUtils;
 import com.rf.psychological.security.SafetyProcess;
 import com.rf.psychological.structure.dao.model.StructureEntity;
 import com.rf.psychological.structure.service.StructureService;
+import com.rf.psychological.user.dao.model.UserEntity;
+import com.rf.psychological.user.service.UserService;
 import com.rf.psychological.utils.Result;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -30,6 +32,9 @@ public class StructureController extends BaseController {
 
     @Autowired
     private StructureService service;
+
+    @Autowired
+    private UserService userService;
     @ApiOperation(value = "新增/编辑组织架构",notes = "structureNo 组织架构编号;structureName:组织架构名称;parentStructureNo:上级组织架构编号")
     @PostMapping("/save")
     @SafetyProcess
@@ -40,7 +45,7 @@ public class StructureController extends BaseController {
             return fail("组织架构编号为空");
         }
         if(StringUtils.isBlank(entity.getStructureName())){
-            return fail("组织架构编号为空");
+            return fail("组织架构名称为空");
         }
         if(StringUtils.isBlank(entity.getParentStructureNo())){
             entity.setParentStructureNo("0");
@@ -54,9 +59,20 @@ public class StructureController extends BaseController {
     @DeleteMapping("delete/{id}")
     public Result delete(@PathVariable String id ){
         //TODO 检查组织架构下是否存在用户
-
-         this.service.deleteById(id);
-         return success();
+        StructureEntity entity = this.service.findById(id);
+        if(entity == null){
+            return fail("组织架构不存在");
+        }
+        List<String> structureNos = this.service.findStructureNoByIdRecursion(entity.getStructureNo());
+        if(structureNos != null && structureNos.size()>0){
+            return fail(entity.getStructureName()+"下存在子级组织架构,不能删除");
+        }
+        List<UserEntity> userEntityList = this.userService.findByStructureNo(entity.getStructureNo());
+        if(userEntityList != null && userEntityList.size()>0){
+            return fail(entity.getStructureName()+"下存在用户,不能删除");
+        }
+        this.service.deleteById(id);
+        return success();
     }
 
     @ApiOperation(value = "查询详情")

+ 2 - 0
src/main/java/com/rf/psychological/structure/service/StructureService.java

@@ -12,4 +12,6 @@ public interface StructureService {
     StructureEntity findById(String id);
 
     List<StructureEntity> findByIdRecursion(String structureNo);
+
+    List<String> findStructureNoByIdRecursion(String structureNo);
 }

+ 5 - 0
src/main/java/com/rf/psychological/structure/service/impl/StructureServiceImpl.java

@@ -37,4 +37,9 @@ public class StructureServiceImpl implements StructureService {
     public List<StructureEntity> findByIdRecursion(String structureNo) {
         return this.repository.findByIdRecursion( structureNo,"%"+structureNo+"%");
     }
+
+    @Override
+    public List<String> findStructureNoByIdRecursion(String structureNo) {
+        return  this.repository.findStructureNoByIdRecursion( structureNo,"%"+structureNo+"%");
+    }
 }

+ 2 - 0
src/main/java/com/rf/psychological/user/dao/model/UserEntity.java

@@ -80,4 +80,6 @@ public class UserEntity extends BaseEntity {
     @Column(name = "extend", columnDefinition = "varchar(500) comment '附加信息'")
     private String extend;
 
+    @Column(name = "structure_no",columnDefinition = "varchar(50) not null comment '组织架构编号'")
+    private String structureNo;
 }

+ 2 - 0
src/main/java/com/rf/psychological/user/dao/repository/UserRepository.java

@@ -196,4 +196,6 @@ public interface UserRepository extends BaseRepository<UserEntity, String> {
     void updateUserStatusByPhone(@Param("phone") String phone, @Param("userStatus") String userStatus, @Param("institutionNo") String institutionNo);
 
     UserEntity findByPhoneAndPasswordAndRoleType(String phone, String password, String roleType);
+
+    List<UserEntity> findByStructureNo(String structureNo);
 }

+ 2 - 0
src/main/java/com/rf/psychological/user/service/UserService.java

@@ -222,4 +222,6 @@ public interface UserService {
      * @return
      */
     UserEntity findByPhoneAndPassword(String phone,String password,String roleType);
+
+    List<UserEntity> findByStructureNo(String structureNo);
 }

+ 6 - 0
src/main/java/com/rf/psychological/user/service/impl/UserServiceImpl.java

@@ -332,6 +332,12 @@ public class UserServiceImpl implements UserService {
         return userRepository.findByPhoneAndPasswordAndRoleType(phone,password,roleType);
     }
 
+    @Override
+    public List<UserEntity> findByStructureNo(String structureNo) {
+        return this.userRepository.findByStructureNo(structureNo);
+    }
+
+
     /**
      * 改变用户状态
      * @param phone 账号