ソースを参照

人员管理逻辑代码完善

fanghuisheng 1 日 前
コミット
86c81e3013

+ 54 - 36
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/controller/web/SysPersonController.java

@@ -1,15 +1,17 @@
 package com.usky.system.controller.web;
 
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.usky.common.core.exception.BusinessException;
 import com.usky.common.core.bean.ApiResult;
-import com.usky.system.domain.SysDept;
 import com.usky.system.controller.web.page.TableDataInfo;
+import com.usky.common.security.utils.SecurityUtils;
 import com.usky.system.domain.SysPerson;
+import com.usky.system.domain.SysUser;
 import com.usky.system.domain.SysUserPerson;
-import com.usky.system.service.ISysDeptService;
+import com.usky.system.service.ISysUserService;
 import com.usky.system.service.SysPersonService;
 import com.usky.system.service.SysUserPersonService;
 import com.usky.common.core.util.StringUtils;
@@ -19,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -41,7 +44,7 @@ public class SysPersonController extends BaseController {
     @Autowired
     private SysUserPersonService sysUserPersonService;
     @Autowired
-    private ISysDeptService sysDeptService;
+    private ISysUserService sysUserService;
 
     /**
      * 人员列表(分页)
@@ -50,45 +53,60 @@ public class SysPersonController extends BaseController {
     public ApiResult<TableDataInfo> list(SysPerson person) {
         startPage();
         // 支持 fullName 模糊查询;其他字段保持按传入条件精确匹配
+        Long filterDeptId = person != null ? person.getDeptId() : null;
+
         SysPerson queryEntity = new SysPerson();
         if (person != null) {
             BeanUtils.copyProperties(person, queryEntity);
             // fullName 用 like 查询,避免 Wrappers.query(entity) 产生 fullName=xxx 的精确条件
             queryEntity.setFullName(null);
+            // 部门、岗位以关联用户为准,不作为人员表列条件;部门筛选见下方按用户 dept_id 关联
+            queryEntity.setDeptId(null);
+            queryEntity.setPostId(null);
         }
         QueryWrapper<SysPerson> queryWrapper = Wrappers.query(queryEntity);
         queryWrapper.lambda()
                 .like(person != null && StringUtils.isNotBlank(person.getFullName()),
                         SysPerson::getFullName, person.getFullName());
-        List<SysPerson> list = sysPersonService.list(queryWrapper);
 
-        // 回显部门名称
-        if (list != null && !list.isEmpty()) {
-            List<Long> deptIds = list.stream()
-                    .map(SysPerson::getDeptId)
-                    .filter(Objects::nonNull)
-                    .distinct()
-                    .collect(Collectors.toList());
-            if (!deptIds.isEmpty()) {
-                List<SysDept> depts = sysDeptService.selectDeptByIds(deptIds);
-                if (depts != null && !depts.isEmpty()) {
-                    Map<Long, String> deptNameMap = depts.stream()
-                            .collect(Collectors.toMap(
-                                    SysDept::getDeptId,
-                                    SysDept::getDeptName,
-                                    (n1, n2) -> n1
-                            ));
-                    list.forEach(p -> {
-                        String deptName = deptNameMap.get(p.getDeptId());
-                        if (deptName != null) {
-                            p.setDeptName(deptName);
-                        }
-                    });
+        // 按部门筛选:关联用户的 dept_id = filterDeptId
+        if (filterDeptId != null) {
+            LambdaQueryWrapper<SysUser> userQuery = Wrappers.<SysUser>lambdaQuery()
+                    .eq(SysUser::getDeptId, filterDeptId)
+                    .eq(SysUser::getDelFlag, "0");
+            Integer tenantId = SecurityUtils.getTenantId();
+            if (tenantId != null) {
+                userQuery.eq(SysUser::getTenantId, tenantId);
+            }
+            List<SysUser> deptUsers = sysUserService.list(userQuery);
+            if (deptUsers == null || deptUsers.isEmpty()) {
+                queryWrapper.apply("1 = 0");
+            } else {
+                List<Long> userIds = deptUsers.stream()
+                        .map(SysUser::getUserId)
+                        .filter(Objects::nonNull)
+                        .distinct()
+                        .collect(Collectors.toList());
+                List<SysUserPerson> deptRelations = sysUserPersonService.list(
+                        Wrappers.<SysUserPerson>lambdaQuery()
+                                .in(SysUserPerson::getUserId, userIds)
+                );
+                if (deptRelations == null || deptRelations.isEmpty()) {
+                    queryWrapper.apply("1 = 0");
+                } else {
+                    List<Integer> deptPersonIds = deptRelations.stream()
+                            .map(SysUserPerson::getPersonId)
+                            .filter(Objects::nonNull)
+                            .distinct()
+                            .collect(Collectors.toList());
+                    queryWrapper.lambda().in(SysPerson::getId, deptPersonIds);
                 }
             }
         }
 
-        // 回显每个人员对应的 userId
+        List<SysPerson> list = sysPersonService.list(queryWrapper);
+
+        // 回显 userId,以及通过 userId 回显部门、岗位
         if (list != null && !list.isEmpty()) {
             List<Integer> personIds = list.stream()
                     .map(SysPerson::getId)
@@ -99,8 +117,9 @@ public class SysPersonController extends BaseController {
                         Wrappers.<SysUserPerson>lambdaQuery()
                                 .in(SysUserPerson::getPersonId, personIds)
                 );
+                final Map<Integer, Long> personUserMap;
                 if (relations != null && !relations.isEmpty()) {
-                    Map<Integer, Long> personUserMap = relations.stream()
+                    personUserMap = relations.stream()
                             .collect(Collectors.toMap(
                                     SysUserPerson::getPersonId,
                                     SysUserPerson::getUserId,
@@ -112,7 +131,10 @@ public class SysPersonController extends BaseController {
                             p.setUserId(userId);
                         }
                     });
+                } else {
+                    personUserMap = new HashMap<>();
                 }
+                sysPersonService.fillDeptAndPostForList(list, personUserMap);
             }
         }
         return ApiResult.success(getDataTable(list));
@@ -125,21 +147,17 @@ public class SysPersonController extends BaseController {
     public ApiResult<SysPerson> getInfo(@PathVariable("id") Integer id) {
         SysPerson person = sysPersonService.getById(id);
         if (person != null) {
+            Long userId = null;
             SysUserPerson userPerson = sysUserPersonService.getOne(
                     Wrappers.<SysUserPerson>lambdaQuery()
                             .eq(SysUserPerson::getPersonId, id)
                             .last("limit 1")
             );
             if (userPerson != null) {
-                person.setUserId(userPerson.getUserId());
-            }
-
-            if (person.getDeptId() != null) {
-                SysDept dept = sysDeptService.selectDeptById(person.getDeptId());
-                if (dept != null) {
-                    person.setDeptName(dept.getDeptName());
-                }
+                userId = userPerson.getUserId();
+                person.setUserId(userId);
             }
+            sysPersonService.fillDeptAndPostFromUser(person, userId);
         }
         return ApiResult.success(person);
     }

+ 10 - 2
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/domain/SysPerson.java

@@ -65,13 +65,15 @@ public class SysPerson implements Serializable {
     private String linkPhone;
 
     /**
-     * 岗位ID
+     * 岗位ID(来自关联用户,不入库)
      */
+    @TableField(exist = false)
     private Long postId;
 
     /**
-     * 部门ID
+     * 部门ID(来自关联用户,不入库)
      */
+    @TableField(exist = false)
     private Long deptId;
 
     /**
@@ -151,4 +153,10 @@ public class SysPerson implements Serializable {
     @TableField(exist = false)
     private String deptName;
 
+    /**
+     * 岗位名称(来自关联用户,不入库)
+     */
+    @TableField(exist = false)
+    private String postName;
+
 }

+ 12 - 0
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/domain/SysUser.java

@@ -13,6 +13,7 @@ import javax.validation.constraints.NotBlank;
 import javax.validation.constraints.Size;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 
 /**
@@ -24,6 +25,17 @@ public class SysUser extends BaseEntity
 {
     private static final long serialVersionUID = 1L;
 
+    /**
+     * 与 BaseEntity 同名字段:MyBatis-Plus 按子类优先收集字段,此处显式 {@code exist = false},
+     * 避免仅依赖父类 private 字段注解时仍生成 {@code search_value}/{@code params} 列。
+     * 读写仍使用继承自 BaseEntity 的 getter/setter(操作父类字段)。
+     */
+    @TableField(exist = false)
+    private String searchValue;
+
+    @TableField(exist = false)
+    private Map<String, Object> params;
+
     /** 用户ID */
     @TableId
     private Long userId;

+ 13 - 0
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/SysPersonService.java

@@ -3,6 +3,9 @@ package com.usky.system.service;
 import com.usky.system.domain.SysPerson;
 import com.usky.common.mybatis.core.CrudService;
 
+import java.util.List;
+import java.util.Map;
+
 /**
  * <p>
  * 人员信息 服务类
@@ -18,4 +21,14 @@ public interface SysPersonService extends CrudService<SysPerson> {
      * @return
      */
     SysPerson getsysPerson(Long userid);
+
+    /**
+     * 根据 userId 从 sys_user / 部门 / 岗位 填充人员的 dept、post 展示字段;userId 为空或查无用户时置为 null。
+     */
+    void fillDeptAndPostFromUser(SysPerson person, Long userId);
+
+    /**
+     * 批量填充列表中人员的部门、岗位(personId -> userId 由调用方传入)。
+     */
+    void fillDeptAndPostForList(List<SysPerson> list, Map<Integer, Long> personIdToUserId);
 }

+ 129 - 1
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/impl/SysPersonServiceImpl.java

@@ -4,16 +4,27 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.usky.common.mybatis.core.AbstractCrudService;
-import com.usky.common.security.utils.SecurityUtils;
+import com.usky.system.domain.SysDept;
 import com.usky.system.domain.SysPerson;
+import com.usky.system.domain.SysPost;
+import com.usky.system.domain.SysUser;
 import com.usky.system.domain.SysUserPerson;
 import com.usky.system.mapper.SysPersonMapper;
+import com.usky.system.service.ISysDeptService;
+import com.usky.system.service.ISysPostService;
+import com.usky.system.service.ISysUserService;
 import com.usky.system.service.SysPersonService;
 import com.usky.system.service.SysUserPersonService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -27,6 +38,12 @@ import java.util.List;
 public class SysPersonServiceImpl extends AbstractCrudService<SysPersonMapper, SysPerson> implements SysPersonService {
     @Autowired
     private SysUserPersonService sysUserPersonService;
+    @Autowired
+    private ISysUserService sysUserService;
+    @Autowired
+    private ISysDeptService sysDeptService;
+    @Autowired
+    private ISysPostService sysPostService;
 
     @Override
     public SysPerson getsysPerson(Long userid) {
@@ -38,7 +55,118 @@ public class SysPersonServiceImpl extends AbstractCrudService<SysPersonMapper, S
             LambdaQueryWrapper<SysPerson> query = Wrappers.lambdaQuery();
             query.eq(SysPerson::getId, userPersonList.get(0).getPersonId());
             sysPerson = this.getOne(query);
+            if (sysPerson != null) {
+                fillDeptAndPostFromUser(sysPerson, userid);
+            }
         }
         return sysPerson;
     }
+
+    @Override
+    public void fillDeptAndPostFromUser(SysPerson person, Long userId) {
+        if (person == null) {
+            return;
+        }
+        if (userId == null) {
+            clearPersonDeptPost(person);
+            return;
+        }
+        SysUser user = sysUserService.selectUserById(userId);
+        if (user == null) {
+            clearPersonDeptPost(person);
+            return;
+        }
+        person.setDeptId(user.getDeptId());
+        if (user.getDeptId() != null) {
+            SysDept dept = sysDeptService.selectDeptById(user.getDeptId());
+            person.setDeptName(dept != null ? dept.getDeptName() : null);
+        } else {
+            person.setDeptName(null);
+        }
+        List<Integer> postIds = sysPostService.selectPostListByUserId(userId);
+        if (postIds == null || postIds.isEmpty()) {
+            person.setPostId(null);
+            person.setPostName(null);
+        } else {
+            Long pid = postIds.get(0).longValue();
+            person.setPostId(pid);
+            SysPost post = sysPostService.selectPostById(pid);
+            person.setPostName(post != null ? post.getPostName() : null);
+        }
+    }
+
+    @Override
+    public void fillDeptAndPostForList(List<SysPerson> list, Map<Integer, Long> personIdToUserId) {
+        if (list == null || list.isEmpty()) {
+            return;
+        }
+        if (personIdToUserId == null || personIdToUserId.isEmpty()) {
+            list.forEach(this::clearPersonDeptPost);
+            return;
+        }
+        Set<Long> userIds = new HashSet<>(personIdToUserId.values());
+        if (userIds.isEmpty()) {
+            list.forEach(this::clearPersonDeptPost);
+            return;
+        }
+        List<SysUser> users = sysUserService.list(Wrappers.<SysUser>lambdaQuery().in(SysUser::getUserId, userIds));
+        Map<Long, SysUser> userById = users.stream()
+                .collect(Collectors.toMap(SysUser::getUserId, u -> u, (a, b) -> a));
+
+        List<Long> deptIdList = users.stream()
+                .map(SysUser::getDeptId)
+                .filter(Objects::nonNull)
+                .distinct()
+                .collect(Collectors.toList());
+        Map<Long, String> deptNameMap = new HashMap<>();
+        if (!deptIdList.isEmpty()) {
+            List<SysDept> depts = sysDeptService.selectDeptByIds(deptIdList);
+            if (depts != null) {
+                for (SysDept d : depts) {
+                    deptNameMap.put(d.getDeptId(), d.getDeptName());
+                }
+            }
+        }
+
+        Map<Long, Long> userFirstPostId = new HashMap<>();
+        for (Long uid : userIds) {
+            List<Integer> pids = sysPostService.selectPostListByUserId(uid);
+            if (pids != null && !pids.isEmpty()) {
+                userFirstPostId.put(uid, pids.get(0).longValue());
+            }
+        }
+        Set<Long> postIdSet = new HashSet<>(userFirstPostId.values());
+        Map<Long, String> postNameMap = new HashMap<>();
+        for (Long pid : postIdSet) {
+            SysPost post = sysPostService.selectPostById(pid);
+            if (post != null) {
+                postNameMap.put(pid, post.getPostName());
+            }
+        }
+
+        for (SysPerson p : list) {
+            Long uid = personIdToUserId.get(p.getId());
+            if (uid == null) {
+                clearPersonDeptPost(p);
+                continue;
+            }
+            SysUser u = userById.get(uid);
+            if (u == null) {
+                clearPersonDeptPost(p);
+                continue;
+            }
+            p.setDeptId(u.getDeptId());
+            p.setDeptName(u.getDeptId() != null ? deptNameMap.get(u.getDeptId()) : null);
+            Long pid = userFirstPostId.get(uid);
+            p.setPostId(pid);
+            p.setPostName(pid != null ? postNameMap.get(pid) : null);
+        }
+    }
+
+    private void clearPersonDeptPost(SysPerson person) {
+        person.setDeptId(null);
+        person.setDeptName(null);
+        person.setPostId(null);
+        person.setPostName(null);
+    }
 }

+ 0 - 2
base-modules/service-system/service-system-biz/src/main/resources/mapper/system/SysPersonMapper.xml

@@ -12,8 +12,6 @@
         <result column="education_degree" property="educationDegree" />
         <result column="id_number" property="idNumber" />
         <result column="link_phone" property="linkPhone" />
-        <result column="post_id" property="postId" />
-        <result column="dept_id" property="deptId" />
         <result column="entry_time" property="entryTime" />
         <result column="certificate_url1" property="certificateUrl1" />
         <result column="certificate_url2" property="certificateUrl2" />