feat: common code group management (separate table + CRUD)
This commit is contained in:
@@ -2,8 +2,10 @@ package com.srsystem.service;
|
||||
|
||||
import com.srsystem.dto.CommonCodeRequest;
|
||||
import com.srsystem.entity.CommonCode;
|
||||
import com.srsystem.entity.CommonCodeGroup;
|
||||
import com.srsystem.exception.BusinessException;
|
||||
import com.srsystem.exception.ErrorCode;
|
||||
import com.srsystem.repository.CommonCodeGroupRepository;
|
||||
import com.srsystem.repository.CommonCodeRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -17,6 +19,7 @@ import java.util.List;
|
||||
public class CommonCodeService {
|
||||
|
||||
private final CommonCodeRepository commonCodeRepository;
|
||||
private final CommonCodeGroupRepository commonCodeGroupRepository;
|
||||
|
||||
public List<CommonCode> getCodes(String groupCode) {
|
||||
if (groupCode != null) {
|
||||
@@ -73,4 +76,51 @@ public class CommonCodeService {
|
||||
code.setIsDeleted('Y');
|
||||
commonCodeRepository.save(code);
|
||||
}
|
||||
|
||||
// ---- Group CRUD ----
|
||||
|
||||
public List<CommonCodeGroup> getGroups() {
|
||||
return commonCodeGroupRepository.findAllByOrderBySortOrderAsc();
|
||||
}
|
||||
|
||||
public CommonCodeGroup getGroup(String groupCode) {
|
||||
return commonCodeGroupRepository.findByGroupCode(groupCode)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.CODE_NOT_FOUND));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CommonCodeGroup createGroup(String groupCode, String groupName, String description, Integer sortOrder) {
|
||||
if (commonCodeGroupRepository.existsByGroupCode(groupCode)) {
|
||||
throw new BusinessException(ErrorCode.DUPLICATE_GROUP_CODE);
|
||||
}
|
||||
CommonCodeGroup group = CommonCodeGroup.builder()
|
||||
.groupCode(groupCode)
|
||||
.groupName(groupName)
|
||||
.description(description)
|
||||
.sortOrder(sortOrder != null ? sortOrder : 0)
|
||||
.build();
|
||||
return commonCodeGroupRepository.save(group);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CommonCodeGroup updateGroup(String groupCode, String groupName, String description, Integer sortOrder) {
|
||||
CommonCodeGroup group = commonCodeGroupRepository.findByGroupCode(groupCode)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.CODE_NOT_FOUND));
|
||||
if (groupName != null) group.setGroupName(groupName);
|
||||
if (description != null) group.setDescription(description);
|
||||
if (sortOrder != null) group.setSortOrder(sortOrder);
|
||||
return commonCodeGroupRepository.save(group);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteGroup(String groupCode) {
|
||||
CommonCodeGroup group = commonCodeGroupRepository.findByGroupCode(groupCode)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.CODE_NOT_FOUND));
|
||||
long codeCount = commonCodeRepository.countByGroupCode(groupCode);
|
||||
if (codeCount > 0) {
|
||||
throw new BusinessException(ErrorCode.GROUP_HAS_CODES);
|
||||
}
|
||||
group.setIsDeleted('Y');
|
||||
commonCodeGroupRepository.save(group);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user