This commit is contained in:
Chuck1sn
2025-05-14 10:16:48 +08:00
commit 3cd59337e7
220 changed files with 23768 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package com.zl.mjga.service;
import com.zl.mjga.config.cache.CacheConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class CacheService {
@Cacheable(value = CacheConfig.VERIFY_CODE, key = "{#identify}", unless = "#result == null")
public String getVerifyCodeBy(String identify) {
return null;
}
@CachePut(value = CacheConfig.VERIFY_CODE, key = "{#identify}")
public String upsertVerifyCodeBy(String identify, String value) {
return value;
}
@CacheEvict(value = CacheConfig.VERIFY_CODE, key = "{#identify}")
public void removeVerifyCodeBy(String identify) {}
@CacheEvict(value = CacheConfig.VERIFY_CODE, allEntries = true)
public void clearAllVerifyCode() {}
}

View File

@@ -0,0 +1,47 @@
package com.zl.mjga.service;
import static org.jooq.generated.mjga.tables.Department.DEPARTMENT;
import com.zl.mjga.dto.PageRequestDto;
import com.zl.mjga.dto.PageResponseDto;
import com.zl.mjga.dto.department.DepartmentQueryDto;
import com.zl.mjga.dto.department.DepartmentRespDto;
import com.zl.mjga.repository.DepartmentRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.stereotype.Service;
@Service
@Slf4j
@RequiredArgsConstructor
public class DepartmentService {
private final DepartmentRepository departmentRepository;
public PageResponseDto<List<DepartmentRespDto>> pageQueryDepartment(
PageRequestDto pageRequestDto, DepartmentQueryDto departmentQueryDto) {
Result<Record> records = departmentRepository.pageFetchBy(pageRequestDto, departmentQueryDto);
if (records.isEmpty()) {
return PageResponseDto.empty();
}
List<DepartmentRespDto> departments =
records.map(
record -> {
return DepartmentRespDto.builder()
.id(record.getValue(DEPARTMENT.ID))
.name(record.getValue(DEPARTMENT.NAME))
.parentId(record.getValue(DEPARTMENT.PARENT_ID))
.isBound(
record.field("is_bound") != null
? record.get("is_bound", Boolean.class)
: null)
.parentName(record.get("parent_name", String.class))
.build();
});
Long totalDepartment = records.get(0).getValue("total_department", Long.class);
return new PageResponseDto<>(totalDepartment, departments);
}
}

View File

@@ -0,0 +1,291 @@
package com.zl.mjga.service;
import static org.jooq.generated.mjga.tables.Permission.PERMISSION;
import static org.jooq.generated.mjga.tables.Role.ROLE;
import static org.jooq.generated.mjga.tables.User.USER;
import com.zl.mjga.dto.PageRequestDto;
import com.zl.mjga.dto.PageResponseDto;
import com.zl.mjga.dto.department.DepartmentBindDto;
import com.zl.mjga.dto.position.PositionBindDto;
import com.zl.mjga.dto.urp.*;
import com.zl.mjga.exception.BusinessException;
import com.zl.mjga.model.urp.ERole;
import com.zl.mjga.repository.*;
import java.util.*;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.generated.mjga.tables.pojos.*;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Slf4j
@RequiredArgsConstructor
public class IdentityAccessService {
private final UserRepository userRepository;
private final RoleRepository roleRepository;
private final UserRoleMapRepository userRoleMapRepository;
private final PermissionRepository permissionRepository;
private final RolePermissionMapRepository rolePermissionMapRepository;
private final UserDepartmentMapRepository userDepartmentMapRepository;
private final UserPositionMapRepository userPositionMapRepository;
private final PasswordEncoder passwordEncoder;
public void upsertUser(UserUpsertDto userUpsertDto) {
User user = new User();
BeanUtils.copyProperties(userUpsertDto, user);
if (StringUtils.isNotEmpty(userUpsertDto.getPassword())) {
user.setPassword(passwordEncoder.encode(userUpsertDto.getPassword()));
}
userRepository.mergeWithoutNullFieldBy(user);
}
public void upsertRole(RoleUpsertDto roleUpsertDto) {
Role role = new Role();
BeanUtils.copyProperties(roleUpsertDto, role);
roleRepository.merge(role);
}
public void upsertPermission(PermissionUpsertDto permissionUpsertDto) {
Permission permission = new Permission();
BeanUtils.copyProperties(permissionUpsertDto, permission);
permissionRepository.merge(permission);
}
public PageResponseDto<List<UserRolePermissionDto>> pageQueryUser(
PageRequestDto pageRequestDto, UserQueryDto userQueryDto) {
Result<Record> userRecords = userRepository.pageFetchBy(pageRequestDto, userQueryDto);
if (userRecords.isEmpty()) {
return PageResponseDto.empty();
}
List<UserRolePermissionDto> userRolePermissionDtoList =
userRecords.stream()
.map((record) -> queryUniqueUserWithRolePermission(record.getValue(USER.ID)))
.toList();
return new PageResponseDto<>(
userRecords.get(0).getValue("total_user", Integer.class), userRolePermissionDtoList);
}
public @Nullable UserRolePermissionDto queryUniqueUserWithRolePermission(Long userId) {
return userRepository.fetchUniqueUserDtoWithNestedRolePermissionBy(userId);
}
public PageResponseDto<List<RoleDto>> pageQueryRole(
PageRequestDto pageRequestDto, RoleQueryDto roleQueryDto) {
Result<Record> roleRecords = roleRepository.pageFetchBy(pageRequestDto, roleQueryDto);
if (roleRecords.isEmpty()) {
return PageResponseDto.empty();
}
List<RoleDto> roleDtoList =
roleRecords.stream()
.map(
record -> {
return RoleDto.builder()
.id(record.getValue("id", Long.class))
.code(record.getValue("code", String.class))
.name(record.getValue("name", String.class))
.isBound(
record.field("is_bound", Boolean.class) != null
? record.getValue("is_bound", Boolean.class)
: null)
.permissions(record.getValue("permissions", List.class))
.build();
})
.toList();
return new PageResponseDto<>(
roleRecords.get(0).getValue("total_role", Integer.class), roleDtoList);
}
public @Nullable RoleDto queryUniqueRoleWithPermission(Long roleId) {
Result<Record> roleWithPermissionRecords = roleRepository.fetchUniqueRoleWithPermission(roleId);
if (roleWithPermissionRecords.isEmpty()) {
return null;
}
RoleDto roleDto = createRbacDtoRolePart(roleWithPermissionRecords);
setCurrentRolePermission(roleDto, roleWithPermissionRecords);
return roleDto;
}
public PageResponseDto<List<PermissionRespDto>> pageQueryPermission(
PageRequestDto pageRequestDto, PermissionQueryDto permissionQueryDto) {
Result<Record> permissionRecords =
permissionRepository.pageFetchBy(pageRequestDto, permissionQueryDto);
if (permissionRecords.isEmpty()) {
return PageResponseDto.empty();
}
List<PermissionRespDto> permissionRespDtoList =
permissionRecords.stream()
.map(
record ->
PermissionRespDto.builder()
.id(record.getValue("id", Long.class))
.name(record.getValue("name", String.class))
.code(record.getValue("code", String.class))
.isBound(
record.field("is_bound", Boolean.class) != null
? record.getValue("is_bound", Boolean.class)
: null)
.build())
.toList();
return new PageResponseDto<>(
permissionRecords.get(0).getValue("total_permission", Integer.class),
permissionRespDtoList);
}
public void bindPermissionBy(Long roleId, List<Long> permissionIdList) {
List<RolePermissionMap> permissionMapList =
permissionIdList.stream()
.map(
(permissionId -> {
RolePermissionMap rolePermissionMap = new RolePermissionMap();
rolePermissionMap.setRoleId(roleId);
rolePermissionMap.setPermissionId(permissionId);
return rolePermissionMap;
}))
.collect(Collectors.toList());
rolePermissionMapRepository.merge(permissionMapList);
}
public void unBindPermissionBy(Long roleId, List<Long> permissionIdList) {
if (CollectionUtils.isEmpty(permissionIdList)) {
return;
}
rolePermissionMapRepository.deleteBy(roleId, permissionIdList);
}
public void unBindRoleToUser(Long userId, List<Long> roleIdList) {
if (CollectionUtils.isEmpty(roleIdList)) {
return;
}
List<Role> roles = roleRepository.selectByRoleIdIn(roleIdList);
if (CollectionUtils.isEmpty(roles)) {
throw new BusinessException("unbind role not exist");
}
userRoleMapRepository.deleteBy(userId, roleIdList);
}
public void bindRoleToUser(Long userId, List<Long> roleIdList) {
List<UserRoleMap> userRoleMapList =
roleIdList.stream()
.map(
(roleId -> {
UserRoleMap userRoleMap = new UserRoleMap();
userRoleMap.setUserId(userId);
userRoleMap.setRoleId(roleId);
return userRoleMap;
}))
.collect(Collectors.toList());
userRoleMapRepository.merge(userRoleMapList);
}
@Transactional(rollbackFor = Throwable.class)
public void bindRoleModuleToUser(Long userId, List<ERole> eRoleList) {
bindRoleToUser(
userId,
roleRepository
.selectByRoleCodeIn(eRoleList.stream().map(Enum::name).collect(Collectors.toList()))
.stream()
.map(Role::getId)
.toList());
}
private void setCurrentRolePermission(RoleDto roleDto, List<Record> roleResult) {
if (roleResult.get(0).getValue(PERMISSION.ID) != null) {
roleResult.forEach(
(record) -> {
PermissionRespDto permissionRespDto = createRbacDtoPermissionPart(record);
roleDto.getPermissions().add(permissionRespDto);
});
}
}
private PermissionRespDto createRbacDtoPermissionPart(Record record) {
PermissionRespDto permissionRespDto = new PermissionRespDto();
permissionRespDto.setId(record.getValue(PERMISSION.ID));
permissionRespDto.setCode(record.getValue(PERMISSION.CODE));
permissionRespDto.setName(record.getValue(PERMISSION.NAME));
return permissionRespDto;
}
private RoleDto createRbacDtoRolePart(List<Record> roleResult) {
RoleDto roleDto = new RoleDto();
roleDto.setId(roleResult.get(0).getValue(ROLE.ID));
roleDto.setCode(roleResult.get(0).getValue(ROLE.CODE));
roleDto.setName(roleResult.get(0).getValue(ROLE.NAME));
return roleDto;
}
public boolean isRoleDuplicate(String roleCode, String name) {
return roleRepository.fetchOneByCode(roleCode) != null
|| roleRepository.fetchOneByName(name) != null;
}
public boolean isUsernameDuplicate(String username) {
return userRepository.fetchOneByUsername(username) != null;
}
public boolean isPermissionDuplicate(String code, String name) {
return permissionRepository.fetchOneByCode(code) != null
|| permissionRepository.fetchOneByName(name) != null;
}
@Transactional(rollbackFor = Throwable.class)
public void bindDepartmentBy(DepartmentBindDto departmentBindDto) {
List<UserDepartmentMap> userDepartmentMaps =
departmentBindDto.departmentIds().stream()
.map(
(departmentId) -> {
UserDepartmentMap userDepartmentMap = new UserDepartmentMap();
userDepartmentMap.setUserId(departmentBindDto.userId());
userDepartmentMap.setDepartmentId(departmentId);
return userDepartmentMap;
})
.toList();
userDepartmentMapRepository.merge(userDepartmentMaps);
}
@Transactional(rollbackFor = Throwable.class)
public void unBindDepartmentBy(DepartmentBindDto departmentBindDto) {
for (Long departmentId : departmentBindDto.departmentIds()) {
UserDepartmentMap userDepartmentMap = new UserDepartmentMap();
userDepartmentMap.setUserId(departmentBindDto.userId());
userDepartmentMap.setDepartmentId(departmentId);
userDepartmentMapRepository.delete(userDepartmentMap);
}
}
@Transactional(rollbackFor = Throwable.class)
public void bindPositionBy(PositionBindDto positionBindDto) {
List<UserPositionMap> userPositionMaps =
positionBindDto.positionIds().stream()
.map(
(positionId) -> {
UserPositionMap userPositionMap = new UserPositionMap();
userPositionMap.setUserId(positionBindDto.userId());
userPositionMap.setPositionId(positionId);
return userPositionMap;
})
.toList();
userPositionMapRepository.merge(userPositionMaps);
}
@Transactional(rollbackFor = Throwable.class)
public void unBindPositionBy(PositionBindDto positionBindDto) {
for (Long positionId : positionBindDto.positionIds()) {
UserPositionMap userPositionMap = new UserPositionMap();
userPositionMap.setUserId(positionBindDto.userId());
userPositionMap.setPositionId(positionId);
userPositionMapRepository.delete(userPositionMap);
}
}
}

View File

@@ -0,0 +1,44 @@
package com.zl.mjga.service;
import static org.jooq.generated.mjga.tables.Position.POSITION;
import com.zl.mjga.dto.PageRequestDto;
import com.zl.mjga.dto.PageResponseDto;
import com.zl.mjga.dto.position.PositionQueryDto;
import com.zl.mjga.dto.position.PositionRespDto;
import com.zl.mjga.repository.PositionRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.stereotype.Service;
@Service
@Slf4j
@RequiredArgsConstructor
public class PositionService {
private final PositionRepository positionRepository;
public PageResponseDto<List<PositionRespDto>> pageQueryPosition(
PageRequestDto pageRequestDto, PositionQueryDto positionQueryDto) {
Result<Record> records = positionRepository.pageFetchBy(pageRequestDto, positionQueryDto);
if (records.isEmpty()) {
return PageResponseDto.empty();
}
List<PositionRespDto> positions =
records.map(
record ->
PositionRespDto.builder()
.id(record.getValue(POSITION.ID))
.name(record.getValue(POSITION.NAME))
.isBound(
record.field("is_bound", Boolean.class) != null
? record.getValue("is_bound", Boolean.class)
: null)
.build());
Long totalPosition = records.get(0).getValue("total_position", Long.class);
return new PageResponseDto<>(totalPosition, positions);
}
}

View File

@@ -0,0 +1,103 @@
package com.zl.mjga.service;
import static org.jooq.generated.public_.Tables.*;
import static org.quartz.TriggerBuilder.newTrigger;
import com.zl.mjga.dto.PageRequestDto;
import com.zl.mjga.dto.PageResponseDto;
import com.zl.mjga.dto.scheduler.JobTriggerDto;
import com.zl.mjga.dto.scheduler.QueryDto;
import com.zl.mjga.repository.QrtzJobRepository;
import jakarta.annotation.Resource;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jooq.Record;
import org.jooq.Result;
import org.quartz.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
@Service
@Slf4j
@RequiredArgsConstructor
public class SchedulerService {
@Resource(name = "emailJobSchedulerFactory")
private Scheduler emailJobScheduler;
@Resource(name = "dataBackupSchedulerFactory")
private Scheduler dataBackupScheduler;
private final QrtzJobRepository qrtzJobRepository;
public PageResponseDto<List<JobTriggerDto>> getJobWithTriggerBy(
PageRequestDto pageRequestDto, QueryDto queryDto) {
Result<Record> records =
qrtzJobRepository.fetchPageWithJobAndTriggerBy(pageRequestDto, queryDto);
if (records.isEmpty()) {
return PageResponseDto.empty();
}
List<JobTriggerDto> jobTriggerDtoList =
records.map(
record -> {
JobTriggerDto jobTriggerDto = new JobTriggerDto();
jobTriggerDto.setName(record.getValue(QRTZ_JOB_DETAILS.JOB_NAME));
jobTriggerDto.setGroup(record.getValue(QRTZ_JOB_DETAILS.JOB_GROUP));
jobTriggerDto.setClassName(record.getValue(QRTZ_JOB_DETAILS.JOB_CLASS_NAME));
jobTriggerDto.setTriggerName(record.getValue(QRTZ_TRIGGERS.TRIGGER_NAME));
jobTriggerDto.setTriggerGroup(record.getValue(QRTZ_TRIGGERS.TRIGGER_GROUP));
jobTriggerDto.setCronExpression(record.getValue(QRTZ_CRON_TRIGGERS.CRON_EXPRESSION));
jobTriggerDto.setStartTime(record.getValue(QRTZ_TRIGGERS.START_TIME));
jobTriggerDto.setEndTime(record.getValue(QRTZ_TRIGGERS.END_TIME));
jobTriggerDto.setNextFireTime(record.getValue(QRTZ_TRIGGERS.NEXT_FIRE_TIME));
jobTriggerDto.setPreviousFireTime(record.getValue(QRTZ_TRIGGERS.PREV_FIRE_TIME));
jobTriggerDto.setSchedulerType(record.getValue(QRTZ_TRIGGERS.TRIGGER_TYPE));
jobTriggerDto.setTriggerState(record.getValue(QRTZ_TRIGGERS.TRIGGER_STATE));
return jobTriggerDto;
});
return new PageResponseDto<>(
records.get(0).getValue("total_job", Integer.class), jobTriggerDtoList);
}
public void resumeTrigger(TriggerKey triggerKey) throws SchedulerException {
emailJobScheduler.resumeTrigger(triggerKey);
dataBackupScheduler.resumeTrigger(triggerKey);
}
public void pauseTrigger(TriggerKey triggerKey) throws SchedulerException {
emailJobScheduler.pauseTrigger(triggerKey);
dataBackupScheduler.pauseTrigger(triggerKey);
}
public void triggerJob(JobKey jobKey, Date startAt) throws SchedulerException {
JobDetail jobDetail = emailJobScheduler.getJobDetail(jobKey);
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Trigger dayLaterTrigger =
newTrigger()
.withIdentity(
String.format(
"%s-%s-%s", "trigger", authentication.getName(), Instant.now().toEpochMilli()),
"job-management")
.startAt(startAt)
.build();
emailJobScheduler.scheduleJob(jobDetail, dayLaterTrigger);
}
public void updateCronTrigger(TriggerKey triggerKey, String cron) throws SchedulerException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Trigger newTrigger =
TriggerBuilder.newTrigger()
.withIdentity(
String.format(
"%s-%s-%s",
"cronTrigger", authentication.getName(), Instant.now().toEpochMilli()),
"job-management")
.withSchedule(CronScheduleBuilder.cronSchedule(cron))
.build();
dataBackupScheduler.rescheduleJob(triggerKey, newTrigger);
}
}

View File

@@ -0,0 +1,51 @@
package com.zl.mjga.service;
import com.zl.mjga.dto.sign.SignInDto;
import com.zl.mjga.dto.sign.SignUpDto;
import com.zl.mjga.exception.BusinessException;
import com.zl.mjga.model.urp.ERole;
import com.zl.mjga.repository.UserRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jooq.generated.mjga.tables.pojos.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Slf4j
@RequiredArgsConstructor
public class SignService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final IdentityAccessService identityAccessService;
public Long signIn(SignInDto signInDto) {
User user = userRepository.fetchOneByUsername(signInDto.getUsername());
if (user == null) {
throw new BusinessException(String.format("%s user not found", signInDto.getUsername()));
}
if (!passwordEncoder.matches(signInDto.getPassword(), user.getPassword())) {
throw new BusinessException("password invalid");
}
return user.getId();
}
@Transactional(rollbackFor = Throwable.class)
public void signUp(SignUpDto signUpDto) {
if (identityAccessService.isUsernameDuplicate(signUpDto.getUsername())) {
throw new BusinessException(
String.format("username %s already exist", signUpDto.getUsername()));
}
User user = new User();
user.setUsername(signUpDto.getUsername());
user.setPassword(passwordEncoder.encode(signUpDto.getPassword()));
userRepository.insert(user);
User insertUser = userRepository.fetchOneByUsername(signUpDto.getUsername());
identityAccessService.bindRoleModuleToUser(insertUser.getId(), List.of(ERole.GENERAL));
}
}