Forráskód Böngészése

新增用户部门关联操作

hubin 1 éve
szülő
commit
82d789c89d

+ 18 - 0
src/main/java/com/aizuda/boot/modules/system/controller/SysUserController.java

@@ -5,9 +5,11 @@
  */
 package com.aizuda.boot.modules.system.controller;
 
+import com.aizuda.boot.modules.system.entity.param.AssignDepartmentsParam;
 import com.aizuda.boot.modules.system.entity.param.AssignRolesParam;
 import com.aizuda.boot.modules.system.entity.param.ResetPasswordParam;
 import com.aizuda.boot.modules.system.entity.param.SysUserParam;
+import com.aizuda.boot.modules.system.service.ISysUserDepartmentService;
 import com.aizuda.boot.modules.system.service.ISysUserRoleService;
 import com.aizuda.boot.modules.system.service.ISysUserService;
 import com.aizuda.core.api.ApiController;
@@ -40,6 +42,7 @@ import java.util.List;
 public class SysUserController extends ApiController {
     private ISysUserService sysUserService;
     private ISysUserRoleService sysUserRoleService;
+    private ISysUserDepartmentService sysUserDepartmentService;
 
     @Operation(summary = "分页列表")
     @Permission("sys:user:page")
@@ -90,6 +93,13 @@ public class SysUserController extends ApiController {
         return sysUserRoleService.assignRoles(assignRoleParam);
     }
 
+    @Operation(summary = "分配部门")
+    @Permission("sys:user:assignDepartments")
+    @PostMapping("/assign-departments")
+    public boolean assignDepartments(@Validated @RequestBody AssignDepartmentsParam assignDepartmentsParam) {
+        return sysUserDepartmentService.assignDepartments(assignDepartmentsParam);
+    }
+
     @Operation(summary = "根据用户ID查询关联角色ID列表")
     @Permission("sys:user:roleIds")
     @PostMapping("/role-ids")
@@ -97,6 +107,14 @@ public class SysUserController extends ApiController {
         return sysUserRoleService.listRoleIdsByUserId(id);
     }
 
+
+    @Operation(summary = "根据用户ID查询关联部门ID列表")
+    @Permission("sys:user:departmentIds")
+    @PostMapping("/department-ids")
+    public List<Long> departmentIds(@RequestParam Long id) {
+        return sysUserDepartmentService.listDepartmentIdsByUserId(id);
+    }
+
     @Operation(summary = "重置密码")
     @Permission("sys:user:resetPassword")
     @PostMapping("/reset-password")

+ 37 - 0
src/main/java/com/aizuda/boot/modules/system/entity/SysUserDepartment.java

@@ -0,0 +1,37 @@
+/*
+ * 爱组搭,低代码组件化开发平台
+ * ------------------------------------------
+ * 受知识产权保护,请勿删除版权申明,开发平台不允许做非法网站,后果自负
+ */
+package com.aizuda.boot.modules.system.entity;
+
+import com.aizuda.core.bean.SuperEntity;
+import com.aizuda.core.validation.Create;
+import io.swagger.v3.oas.annotations.media.Schema;
+import jakarta.validation.constraints.NotNull;
+import jakarta.validation.constraints.PositiveOrZero;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 系统用户部门
+ *
+ * @author 青苗
+ * @since 2023-10-08
+ */
+@Getter
+@Setter
+@Schema(name = "SysUserDepartment", description = "系统用户部门")
+public class SysUserDepartment extends SuperEntity {
+
+    @Schema(description = "用户ID")
+    @NotNull(groups = Create.class)
+    @PositiveOrZero
+    private Long userId;
+
+    @Schema(description = "部门ID")
+    @NotNull(groups = Create.class)
+    @PositiveOrZero
+    private Long departmentId;
+
+}

+ 33 - 0
src/main/java/com/aizuda/boot/modules/system/entity/param/AssignDepartmentsParam.java

@@ -0,0 +1,33 @@
+/*
+ * 爱组搭,低代码组件化开发平台
+ * ------------------------------------------
+ * 受知识产权保护,请勿删除版权申明,开发平台不允许做非法网站,后果自负
+ */
+package com.aizuda.boot.modules.system.entity.param;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import jakarta.validation.constraints.NotEmpty;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.List;
+
+/**
+ * 分配部门参数,多用户分配多部门
+ *
+ * @author 青苗
+ * @since 2023-10-08
+ */
+@Getter
+@Setter
+public class AssignDepartmentsParam {
+
+    @Schema(description = "用户ID列表")
+    @NotEmpty
+    private List<Long> userIds;
+
+    @Schema(description = "部门ID列表")
+    @NotEmpty
+    private List<Long> departmentIds;
+
+}

+ 1 - 0
src/main/java/com/aizuda/boot/modules/system/entity/param/AssignRolesParam.java

@@ -21,6 +21,7 @@ import java.util.List;
 @Getter
 @Setter
 public class AssignRolesParam {
+
     @Schema(description = "用户ID列表")
     @NotEmpty
     private List<Long> userIds;

+ 3 - 0
src/main/java/com/aizuda/boot/modules/system/entity/param/SysUserParam.java

@@ -25,4 +25,7 @@ public class SysUserParam extends SysUser {
     @Schema(description = "角色ID列表")
     private List<Long> roleIds;
 
+    @Schema(description = "部门ID列表")
+    private List<Long> departmentIds;
+
 }

+ 23 - 0
src/main/java/com/aizuda/boot/modules/system/mapper/SysUserDepartmentMapper.java

@@ -0,0 +1,23 @@
+/*
+ * 爱组搭,低代码组件化开发平台
+ * ------------------------------------------
+ * 受知识产权保护,请勿删除版权申明,开发平台不允许做非法网站,后果自负
+ */
+package com.aizuda.boot.modules.system.mapper;
+
+import com.aizuda.boot.modules.system.entity.SysUserDepartment;
+import com.aizuda.service.mapper.CrudMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * <p>
+ * 系统用户部门 Mapper 接口
+ * </p>
+ *
+ * @author 青苗
+ * @since 2023-10-08
+ */
+@Mapper
+public interface SysUserDepartmentMapper extends CrudMapper<SysUserDepartment> {
+
+}

+ 46 - 0
src/main/java/com/aizuda/boot/modules/system/service/ISysUserDepartmentService.java

@@ -0,0 +1,46 @@
+/*
+ * 爱组搭,低代码组件化开发平台
+ * ------------------------------------------
+ * 受知识产权保护,请勿删除版权申明,开发平台不允许做非法网站,后果自负
+ */
+package com.aizuda.boot.modules.system.service;
+
+import com.aizuda.boot.modules.system.entity.SysUserDepartment;
+import com.aizuda.boot.modules.system.entity.SysUserDepartment;
+import com.aizuda.boot.modules.system.entity.param.AssignDepartmentsParam;
+import com.aizuda.service.service.IBaseService;
+
+import java.util.List;
+
+/**
+ * 系统用户部门 服务类
+ *
+ * @author 青苗
+ * @since 2023-10-08
+ */
+public interface ISysUserDepartmentService extends IBaseService<SysUserDepartment> {
+
+    /**
+     * 用户部门分配
+     *
+     * @param assignDepartmentsParam 分配部门参数对象
+     * @return
+     */
+    boolean assignDepartments(AssignDepartmentsParam assignDepartmentsParam);
+
+    /**
+     * 根据用户ID查询关联部门ID列表
+     *
+     * @param userId 用户ID
+     * @return
+     */
+    List<Long> listDepartmentIdsByUserId(Long userId);
+
+    /**
+     * 判断是否存在关联部门
+     *
+     * @param DepartmentId 部门ID
+     * @return
+     */
+    boolean existRelByDepartmentId(Long DepartmentId);
+}

+ 77 - 0
src/main/java/com/aizuda/boot/modules/system/service/impl/SysUserDepartmentServiceImpl.java

@@ -0,0 +1,77 @@
+/*
+ * 爱组搭,低代码组件化开发平台
+ * ------------------------------------------
+ * 受知识产权保护,请勿删除版权申明,开发平台不允许做非法网站,后果自负
+ */
+package com.aizuda.boot.modules.system.service.impl;
+
+import com.aizuda.boot.modules.system.entity.SysUserDepartment;
+import com.aizuda.boot.modules.system.entity.param.AssignDepartmentsParam;
+import com.aizuda.boot.modules.system.mapper.SysUserDepartmentMapper;
+import com.aizuda.boot.modules.system.service.ISysUserDepartmentService;
+import com.aizuda.core.api.ApiAssert;
+import com.aizuda.service.service.BaseServiceImpl;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import org.apache.commons.collections.CollectionUtils;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * 系统用户部门 服务实现类
+ *
+ * @author 青苗
+ * @since 2023-10-08
+ */
+@Service
+public class SysUserDepartmentServiceImpl extends BaseServiceImpl<SysUserDepartmentMapper, SysUserDepartment> implements ISysUserDepartmentService {
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public boolean assignDepartments(AssignDepartmentsParam assignDepartmentsParam) {
+        // 删除历史
+        List<Long> userIds = assignDepartmentsParam.getUserIds();
+        this.removeByUserIds(userIds);
+        if (CollectionUtils.isEmpty(assignDepartmentsParam.getDepartmentIds())) {
+            // 无需分配部门
+            return true;
+        }
+
+        // 批量新增
+        List<SysUserDepartment> sysUserDepartmentList = new ArrayList<>();
+        userIds.forEach(userId -> sysUserDepartmentList.addAll(assignDepartmentsParam.getDepartmentIds().stream()
+                .map(DepartmentId -> {
+                    SysUserDepartment sysUserDepartment = new SysUserDepartment();
+                    sysUserDepartment.setUserId(userId);
+                    sysUserDepartment.setDepartmentId(DepartmentId);
+                    return sysUserDepartment;
+                }).collect(Collectors.toList())));
+        return super.saveBatch(sysUserDepartmentList);
+    }
+
+    private boolean removeByUserIds(List<Long> userIds) {
+        return super.remove(Wrappers.<SysUserDepartment>lambdaQuery().in(SysUserDepartment::getUserId, userIds));
+    }
+
+    @Override
+    public boolean updateById(SysUserDepartment sysUserDepartment) {
+        ApiAssert.isEmpty(sysUserDepartment.getId(), "主键不存在无法更新");
+        return super.updateById(sysUserDepartment);
+    }
+
+    @Override
+    public List<Long> listDepartmentIdsByUserId(Long userId) {
+        List<SysUserDepartment> sysUserDepartmentList = super.list(Wrappers.<SysUserDepartment>lambdaQuery()
+                .select(SysUserDepartment::getDepartmentId).eq(SysUserDepartment::getUserId, userId));
+        return CollectionUtils.isEmpty(sysUserDepartmentList) ? null : sysUserDepartmentList.stream().map(t -> t.getDepartmentId())
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public boolean existRelByDepartmentId(Long DepartmentId) {
+        return lambdaQuery().eq(SysUserDepartment::getDepartmentId, DepartmentId).count() > 0;
+    }
+}

+ 21 - 5
src/main/java/com/aizuda/boot/modules/system/service/impl/SysUserServiceImpl.java

@@ -6,11 +6,13 @@
 package com.aizuda.boot.modules.system.service.impl;
 
 import com.aizuda.boot.modules.system.entity.SysUser;
+import com.aizuda.boot.modules.system.entity.param.AssignDepartmentsParam;
 import com.aizuda.boot.modules.system.entity.param.AssignRolesParam;
 import com.aizuda.boot.modules.system.entity.param.ResetPasswordParam;
 import com.aizuda.boot.modules.system.entity.param.SysUserParam;
 import com.aizuda.boot.modules.system.entity.vo.SysUserVO;
 import com.aizuda.boot.modules.system.mapper.SysUserMapper;
+import com.aizuda.boot.modules.system.service.ISysUserDepartmentService;
 import com.aizuda.boot.modules.system.service.ISysUserRoleService;
 import com.aizuda.boot.modules.system.service.ISysUserService;
 import com.aizuda.common.toolkit.RegexUtils;
@@ -41,6 +43,8 @@ import java.util.List;
 public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
     @Resource
     private ISysUserRoleService sysUserRoleService;
+    @Resource
+    private ISysUserDepartmentService sysUserDepartmentService;
 
     @Override
     public Page<SysUser> page(Page<SysUser> page, SysUserVO vo) {
@@ -79,11 +83,13 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
         param.setSalt(RandomUtil.getCharacterAndNumber(8));
         param.setPassword(this.encodePassword(param.getUsername(), param.getSalt(), param.getPassword()));
         ApiAssert.fail(!super.save(param), "用户信息保存失败");
-        if (CollectionUtils.isEmpty(param.getRoleIds())) {
-            // 无需分配角色
-            return true;
+        if (CollectionUtils.isNotEmpty(param.getRoleIds())) {
+            ApiAssert.fail(!this.assignRoles(param), "角色分配保存失败");
+        }
+        if (CollectionUtils.isNotEmpty(param.getDepartmentIds())) {
+            ApiAssert.fail(!this.assignDepartments(param), "部门分配保存失败");
         }
-        return this.assignRoles(param);
+        return true;
     }
 
     protected void checkPassword(String password) {
@@ -102,6 +108,14 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
         return sysUserRoleService.assignRoles(assignRolesParam);
     }
 
+    protected boolean assignDepartments(SysUserParam param) {
+        // 分配部门
+        AssignDepartmentsParam assignDepartmentsParam = new AssignDepartmentsParam();
+        assignDepartmentsParam.setDepartmentIds(param.getDepartmentIds());
+        assignDepartmentsParam.setUserIds(Arrays.asList(param.getId()));
+        return sysUserDepartmentService.assignDepartments(assignDepartmentsParam);
+    }
+
     @Override
     public boolean updateById(SysUserParam param) {
         ApiAssert.fail(!super.updateById(param), "用户信息保存失败");
@@ -109,7 +123,9 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
         param.setUsername(null);
         param.setPassword(null);
         param.setSalt(null);
-        return this.assignRoles(param);
+        ApiAssert.fail(!this.assignRoles(param), "角色分配保存失败");
+        ApiAssert.fail(!this.assignDepartments(param), "部门分配保存失败");
+        return true;
     }
 
     @Transactional(rollbackFor = Exception.class)