mirror of
https://github.com/ccmjga/zhilu-admin
synced 2026-03-13 21:27:19 +08:00
add bind dep and pos
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package com.zl.mjga.component;
|
||||
|
||||
import com.zl.mjga.dto.department.DepartmentBindDto;
|
||||
import com.zl.mjga.dto.position.PositionBindDto;
|
||||
import com.zl.mjga.dto.urp.PermissionUpsertDto;
|
||||
import com.zl.mjga.dto.urp.RoleUpsertDto;
|
||||
import com.zl.mjga.dto.urp.UserUpsertDto;
|
||||
import com.zl.mjga.exception.BusinessException;
|
||||
import com.zl.mjga.repository.PermissionRepository;
|
||||
import com.zl.mjga.repository.RoleRepository;
|
||||
import com.zl.mjga.repository.UserRepository;
|
||||
import com.zl.mjga.repository.*;
|
||||
import com.zl.mjga.service.IdentityAccessService;
|
||||
import dev.langchain4j.agent.tool.P;
|
||||
import dev.langchain4j.agent.tool.Tool;
|
||||
@@ -14,9 +14,7 @@ import dev.langchain4j.model.output.structured.Description;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jooq.generated.mjga.tables.pojos.Permission;
|
||||
import org.jooq.generated.mjga.tables.pojos.Role;
|
||||
import org.jooq.generated.mjga.tables.pojos.User;
|
||||
import org.jooq.generated.mjga.tables.pojos.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Description("和用户管理有关的操作工具")
|
||||
@@ -28,8 +26,10 @@ public class UserRolePermissionOperatorTool {
|
||||
private final UserRepository userRepository;
|
||||
private final RoleRepository roleRepository;
|
||||
private final PermissionRepository permissionRepository;
|
||||
private final DepartmentRepository departmentRepository;
|
||||
private final PositionRepository positionRepository;
|
||||
|
||||
@Tool(value = "创建用户或注册用户")
|
||||
@Tool(value = {"创建用户", "入职申请", "开通账号"})
|
||||
void createUser(@P(value = "用户名") String username) {
|
||||
User user = userRepository.fetchOneByUsername(username);
|
||||
if (user != null) {
|
||||
@@ -38,6 +38,13 @@ public class UserRolePermissionOperatorTool {
|
||||
identityAccessService.upsertUser(new UserUpsertDto(null, username, username, true));
|
||||
}
|
||||
|
||||
// @Tool(value = "查询用户")
|
||||
// List<User> queryUser(@P(value = "用户名",required = false) String username, @P(value =
|
||||
// "开始日期",required = false) LocalDateTime startDate, @P(value = "结束日期",required = false)
|
||||
// LocalDateTime endDate) {
|
||||
// return userRepository.fetchBy(new UserQueryDto(username, startDate, endDate));
|
||||
// }
|
||||
|
||||
@Tool(value = "删除用户")
|
||||
void deleteUser(@P(value = "用户名") String username) {
|
||||
userRepository.deleteByUsername(username);
|
||||
@@ -54,10 +61,7 @@ public class UserRolePermissionOperatorTool {
|
||||
@Tool(value = "给用户绑定/分配角色")
|
||||
void bindRoleToUser(
|
||||
@P(value = "用户名") String username, @P(value = "角色名称") List<String> roleNames) {
|
||||
User user = userRepository.fetchOneByUsername(username);
|
||||
if (user == null) {
|
||||
throw new BusinessException("指定用户不存在");
|
||||
}
|
||||
User user = checkUserExistBy(username);
|
||||
List<Long> bindRoleIds = getRoleIdsBy(roleNames);
|
||||
identityAccessService.bindRoleToUser(user.getId(), bindRoleIds);
|
||||
}
|
||||
@@ -65,12 +69,67 @@ public class UserRolePermissionOperatorTool {
|
||||
@Tool(value = "给用户解绑/撤销角色")
|
||||
void unbindRoleToUser(
|
||||
@P(value = "用户名") String username, @P(value = "角色名称") List<String> roleNames) {
|
||||
User user = checkUserExistBy(username);
|
||||
List<Long> bindRoleIds = getRoleIdsBy(roleNames);
|
||||
identityAccessService.unBindRoleToUser(user.getId(), bindRoleIds);
|
||||
}
|
||||
|
||||
@Tool(value = "给用户绑定/分配部门")
|
||||
void bindDepartmentToUser(
|
||||
@P(value = "用户名") String username, @P(value = "部门名称列表") List<String> departmentNames) {
|
||||
User user = checkUserExistBy(username);
|
||||
List<Department> departments =
|
||||
departmentRepository.fetchByName(departmentNames.toArray(String[]::new));
|
||||
if (departments.isEmpty()) {
|
||||
throw new BusinessException("指定部门不存在");
|
||||
}
|
||||
identityAccessService.bindDepartmentBy(
|
||||
new DepartmentBindDto(user.getId(), departments.stream().map(Department::getId).toList()));
|
||||
}
|
||||
|
||||
@Tool(value = "给用户绑定/分配部门")
|
||||
void unbindDepartmentToUser(
|
||||
@P(value = "用户名") String username, @P(value = "部门名称列表") List<String> departmentNames) {
|
||||
User user = checkUserExistBy(username);
|
||||
List<Department> departments =
|
||||
departmentRepository.fetchByName(departmentNames.toArray(String[]::new));
|
||||
if (departments.isEmpty()) {
|
||||
throw new BusinessException("指定部门不存在");
|
||||
}
|
||||
identityAccessService.unBindDepartmentBy(
|
||||
new DepartmentBindDto(user.getId(), departments.stream().map(Department::getId).toList()));
|
||||
}
|
||||
|
||||
private User checkUserExistBy(String username) {
|
||||
User user = userRepository.fetchOneByUsername(username);
|
||||
if (user == null) {
|
||||
throw new BusinessException("指定用户不存在");
|
||||
}
|
||||
List<Long> bindRoleIds = getRoleIdsBy(roleNames);
|
||||
identityAccessService.unBindRoleToUser(user.getId(), bindRoleIds);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Tool(value = "给用户绑定/分配职位")
|
||||
void bindPositionToUser(
|
||||
@P(value = "用户名") String username, @P(value = "职位名称列表") List<String> positionNames) {
|
||||
User user = checkUserExistBy(username);
|
||||
List<Position> positions = positionRepository.fetchByName(positionNames.toArray(String[]::new));
|
||||
if (positions.isEmpty()) {
|
||||
throw new BusinessException("指定职位不存在");
|
||||
}
|
||||
identityAccessService.bindPositionBy(
|
||||
new PositionBindDto(user.getId(), positions.stream().map(Position::getId).toList()));
|
||||
}
|
||||
|
||||
@Tool(value = "给用户解绑/撤销职位")
|
||||
void unbindPositionToUser(
|
||||
@P(value = "用户名") String username, @P(value = "职位名称列表") List<String> positionNames) {
|
||||
User user = checkUserExistBy(username);
|
||||
List<Position> positions = positionRepository.fetchByName(positionNames.toArray(String[]::new));
|
||||
if (positions.isEmpty()) {
|
||||
throw new BusinessException("指定职位不存在");
|
||||
}
|
||||
identityAccessService.unBindPositionBy(
|
||||
new PositionBindDto(user.getId(), positions.stream().map(Position::getId).toList()));
|
||||
}
|
||||
|
||||
private List<Long> getRoleIdsBy(List<String> roleNames) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.zl.mjga.dto.urp;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.*;
|
||||
|
||||
@AllArgsConstructor
|
||||
@@ -7,4 +8,6 @@ import lombok.*;
|
||||
@Data
|
||||
public class UserQueryDto {
|
||||
private String username;
|
||||
private LocalDateTime starDate;
|
||||
private LocalDateTime endDate;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.zl.mjga.dto.urp.PermissionRespDto;
|
||||
import com.zl.mjga.dto.urp.RoleDto;
|
||||
import com.zl.mjga.dto.urp.UserQueryDto;
|
||||
import com.zl.mjga.dto.urp.UserRolePermissionDto;
|
||||
import java.util.List;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jooq.*;
|
||||
import org.jooq.Record;
|
||||
@@ -55,6 +56,28 @@ public class UserRepository extends UserDao {
|
||||
.execute();
|
||||
}
|
||||
|
||||
public SelectConditionStep<Record> selectBy(UserQueryDto userQueryDto) {
|
||||
return ctx()
|
||||
.select(asterisk(), DSL.count().over().as("total_user"))
|
||||
.from(USER)
|
||||
.where(
|
||||
userQueryDto.getUsername() != null
|
||||
? USER.USERNAME.like("%" + userQueryDto.getUsername() + "%")
|
||||
: noCondition())
|
||||
.and(
|
||||
userQueryDto.getStarDate() != null
|
||||
? USER.CREATE_TIME.ge(userQueryDto.getStarDate())
|
||||
: noCondition())
|
||||
.and(
|
||||
userQueryDto.getEndDate() != null
|
||||
? USER.CREATE_TIME.le(userQueryDto.getEndDate())
|
||||
: noCondition());
|
||||
}
|
||||
|
||||
public List<User> fetchBy(UserQueryDto userQueryDto) {
|
||||
return selectBy(userQueryDto).fetchInto(User.class);
|
||||
}
|
||||
|
||||
public Result<Record> pageFetchBy(PageRequestDto pageRequestDto, UserQueryDto userQueryDto) {
|
||||
return ctx()
|
||||
.select(asterisk(), DSL.count().over().as("total_user"))
|
||||
|
||||
Reference in New Issue
Block a user