diff --git a/ddl/schema.sql b/ddl/schema.sql index e406dac..4c4c75d 100644 --- a/ddl/schema.sql +++ b/ddl/schema.sql @@ -1,4 +1,47 @@ + create table st_common_code ( + sort_order integer not null, + use_flag boolean not null, + created_at timestamp(6) not null, + created_oid bigint, + oid bigint not null, + updated_at timestamp(6) not null, + updated_oid bigint, + code varchar(50) not null unique, + group_code varchar(50) not null, + parent_code varchar(50), + character_ref1 varchar(100), + character_ref2 varchar(100), + character_ref3 varchar(100), + character_ref4 varchar(100), + character_ref5 varchar(100), + name varchar(100) not null, + description varchar(500), + created_id varchar(255), + updated_id varchar(255), + primary key (oid) + ); + + create table st_common_group_code ( + sort_order integer not null, + use_flag boolean not null, + created_at timestamp(6) not null, + created_oid bigint, + oid bigint not null, + updated_at timestamp(6) not null, + updated_oid bigint, + code varchar(50) not null unique, + character_ref1_title varchar(100), + character_ref2_title varchar(100), + character_ref3_title varchar(100), + character_ref4_title varchar(100), + character_ref5_title varchar(100), + name varchar(100) not null, + created_id varchar(255), + updated_id varchar(255), + primary key (oid) + ); + create table st_file ( use_flag boolean not null, created_at timestamp(6) not null, @@ -38,5 +81,27 @@ primary key (oid) ); + create index idx_common_code_code + on st_common_code (code); + + create index idx_common_code_group_code + on st_common_code (group_code); + + create index idx_common_code_parent_code + on st_common_code (parent_code); + + create index idx_common_group_code_code + on st_common_group_code (code); + create index idx_member_user_id on st_member (user_id); + + alter table if exists st_common_code + add constraint FKdk30t67v5e2vo1k7gl5sk1jp9 + foreign key (group_code) + references st_common_group_code (code); + + alter table if exists st_common_code + add constraint FKgcyw9ydft76p31oox94ho93wf + foreign key (parent_code) + references st_common_code (code); diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/controller/CommonCodeController.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/controller/CommonCodeController.java new file mode 100644 index 0000000..b2fa801 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/controller/CommonCodeController.java @@ -0,0 +1,208 @@ +package com.bio.bio_backend.domain.admin.CommonCode.controller; + +import com.bio.bio_backend.domain.admin.CommonCode.dto.*; +import com.bio.bio_backend.domain.admin.CommonCode.service.CommonCodeService; +import com.bio.bio_backend.domain.admin.CommonCode.mapper.CommonCodeMapper; +import com.bio.bio_backend.domain.admin.CommonCode.mapper.CommonGroupCodeMapper; +import com.bio.bio_backend.global.dto.ApiResponseDto; +import com.bio.bio_backend.global.constants.ApiResponseCode; +import com.bio.bio_backend.global.annotation.LogExecution; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import jakarta.validation.Valid; +import java.util.List; + +@Slf4j +@RestController +@RequestMapping("/admin/common-code") +@RequiredArgsConstructor +@Tag(name = "공통 코드 관리", description = "공통 코드 및 그룹 코드 관리 API") +public class CommonCodeController { + + private final CommonCodeService commonCodeService; + private final CommonCodeMapper commonCodeMapper; + private final CommonGroupCodeMapper commonGroupCodeMapper; + + // 그룹 코드 관련 API + @LogExecution("그룹 코드 생성") + @Operation(summary = "그룹 코드 생성", description = "새로운 그룹 코드를 생성합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "201", description = "그룹 코드 생성 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))), + @ApiResponse(responseCode = "409", description = "중복된 그룹 코드", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))) + }) + @PostMapping("/group") + public ResponseEntity> createGroupCode(@RequestBody @Valid CreateCommonGroupCodeRequestDto requestDto) { + CommonGroupCodeDto groupCodeDto = commonGroupCodeMapper.toCommonGroupCodeDto(requestDto); + CommonGroupCodeDto createdGroupCode = commonCodeService.createGroupCode(groupCodeDto); + CreateCommonGroupCodeResponseDto responseDto = commonGroupCodeMapper.toCreateCommonGroupCodeResponseDto(createdGroupCode); + ApiResponseDto apiResponse = ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_CREATED, responseDto); + + return ResponseEntity.status(HttpStatus.CREATED).body(apiResponse); + } + + @LogExecution("그룹 코드 수정") + @Operation(summary = "그룹 코드 수정", description = "기존 그룹 코드를 수정합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "그룹 코드 수정 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))), + @ApiResponse(responseCode = "404", description = "그룹 코드를 찾을 수 없음", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))) + }) + @PutMapping("/group/{code}") + public ResponseEntity> updateGroupCode( + @PathVariable String code, + @RequestBody @Valid UpdateCommonGroupCodeRequestDto requestDto) { + CommonGroupCodeDto groupCodeDto = commonGroupCodeMapper.toCommonGroupCodeDto(requestDto); + commonCodeService.updateGroupCode(code, groupCodeDto); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_UPDATED)); + } + + @LogExecution("그룹 코드 삭제") + @Operation(summary = "그룹 코드 삭제", description = "그룹 코드를 삭제합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "그룹 코드 삭제 성공"), + @ApiResponse(responseCode = "400", description = "하위 공통 코드가 존재하여 삭제할 수 없음", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))), + @ApiResponse(responseCode = "404", description = "그룹 코드를 찾을 수 없음", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))) + }) + @DeleteMapping("/group/{code}") + public ResponseEntity> deleteGroupCode(@PathVariable String code) { + commonCodeService.deleteGroupCode(code); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_DELETED)); + } + + @LogExecution("그룹 코드 조회") + @Operation(summary = "그룹 코드 조회", description = "특정 그룹 코드를 조회합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "그룹 코드 조회 성공"), + @ApiResponse(responseCode = "404", description = "그룹 코드를 찾을 수 없음", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))) + }) + @GetMapping("/group/{code}") + public ResponseEntity> getGroupCode(@PathVariable String code) { + CommonGroupCodeDto groupCode = commonCodeService.getGroupCode(code); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_RETRIEVED, groupCode)); + } + + @LogExecution("전체 그룹 코드 조회") + @Operation(summary = "전체 그룹 코드 조회", description = "모든 그룹 코드를 조회합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "전체 그룹 코드 조회 성공") + }) + @GetMapping("/group") + public ResponseEntity>> getAllGroupCodes() { + List groupCodes = commonCodeService.getAllGroupCodes(); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_RETRIEVED, groupCodes)); + } + + @LogExecution("활성 그룹 코드 조회") + @Operation(summary = "활성 그룹 코드 조회", description = "사용 중인 그룹 코드만 조회합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "활성 그룹 코드 조회 성공") + }) + @GetMapping("/group/active") + public ResponseEntity>> getActiveGroupCodes() { + List groupCodes = commonCodeService.getActiveGroupCodes(); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_RETRIEVED, groupCodes)); + } + + // 공통 코드 관련 API + @LogExecution("공통 코드 생성") + @Operation(summary = "공통 코드 생성", description = "새로운 공통 코드를 생성합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "201", description = "공통 코드 생성 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))), + @ApiResponse(responseCode = "409", description = "중복된 공통 코드", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))) + }) + @PostMapping + public ResponseEntity> createCode(@RequestBody @Valid CreateCommonCodeRequestDto requestDto) { + CommonCodeDto commonCodeDto = commonCodeMapper.toCommonCodeDto(requestDto); + CommonCodeDto createdCommonCode = commonCodeService.createCode(commonCodeDto); + CreateCommonCodeResponseDto responseDto = commonCodeMapper.toCreateCommonCodeResponseDto(createdCommonCode); + ApiResponseDto apiResponse = ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_CREATED, responseDto); + + return ResponseEntity.status(HttpStatus.CREATED).body(apiResponse); + } + + @LogExecution("공통 코드 수정") + @Operation(summary = "공통 코드 수정", description = "기존 공통 코드를 수정합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "공통 코드 수정 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))), + @ApiResponse(responseCode = "404", description = "공통 코드를 찾을 수 없음", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))) + }) + @PutMapping("/{code}") + public ResponseEntity> updateCode( + @PathVariable String code, + @RequestBody @Valid UpdateCommonCodeRequestDto requestDto) { + CommonCodeDto commonCodeDto = commonCodeMapper.toCommonCodeDto(requestDto); + commonCodeService.updateCode(code, commonCodeDto); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_UPDATED)); + } + + @LogExecution("공통 코드 삭제") + @Operation(summary = "공통 코드 삭제", description = "공통 코드를 삭제합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "공통 코드 삭제 성공"), + @ApiResponse(responseCode = "400", description = "하위 공통 코드가 존재하여 삭제할 수 없음", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))), + @ApiResponse(responseCode = "404", description = "공통 코드를 찾을 수 없음", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))) + }) + @DeleteMapping("/{code}") + public ResponseEntity> deleteCode(@PathVariable String code) { + commonCodeService.deleteCode(code); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_DELETED)); + } + + @LogExecution("공통 코드 조회") + @Operation(summary = "공통 코드 조회", description = "특정 공통 코드를 조회합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "공통 코드 조회 성공"), + @ApiResponse(responseCode = "404", description = "공통 코드를 찾을 수 없음", content = @Content(schema = @Schema(implementation = ApiResponseDto.class))) + }) + @GetMapping("/{code}") + public ResponseEntity> getCode(@PathVariable String code) { + CommonCodeDto commonCode = commonCodeService.getCode(code); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_RETRIEVED, commonCode)); + } + + @LogExecution("그룹별 공통 코드 조회") + @Operation(summary = "그룹별 공통 코드 조회", description = "특정 그룹에 속한 공통 코드들을 조회합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "그룹별 공통 코드 조회 성공") + }) + @GetMapping("/group/{groupCode}/codes") + public ResponseEntity>> getCodesByGroupCode(@PathVariable String groupCode) { + List commonCodes = commonCodeService.getActiveCodesByGroupCode(groupCode); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_RETRIEVED, commonCodes)); + } + + @LogExecution("상위 코드별 공통 코드 조회") + @Operation(summary = "상위 코드별 공통 코드 조회", description = "특정 상위 코드에 속한 공통 코드들을 조회합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "상위 코드별 공통 코드 조회 성공") + }) + @GetMapping("/parent/{parentCode}/codes") + public ResponseEntity>> getCodesByParentCode(@PathVariable String parentCode) { + List commonCodes = commonCodeService.getActiveCodesByParentCode(parentCode); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_RETRIEVED, commonCodes)); + } + + @LogExecution("전체 공통 코드 조회") + @Operation(summary = "전체 공통 코드 조회", description = "모든 공통 코드를 조회합니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "전체 공통 코드 조회 성공") + }) + @GetMapping + public ResponseEntity>> getAllCodes() { + List commonCodes = commonCodeService.getAllCodes(); + return ResponseEntity.ok(ApiResponseDto.success(ApiResponseCode.COMMON_SUCCESS_RETRIEVED, commonCodes)); + } +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CommonCodeDto.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CommonCodeDto.java new file mode 100644 index 0000000..4f72c12 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CommonCodeDto.java @@ -0,0 +1,29 @@ +package com.bio.bio_backend.domain.admin.CommonCode.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CommonCodeDto { + + private Long oid; + private String code; + private String name; + private String description; + private String groupCode; + private String parentCode; + private String characterRef1; + private String characterRef2; + private String characterRef3; + private String characterRef4; + private String characterRef5; + private Integer sortOrder; + private Boolean useFlag; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CommonGroupCodeDto.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CommonGroupCodeDto.java new file mode 100644 index 0000000..656bc21 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CommonGroupCodeDto.java @@ -0,0 +1,28 @@ +package com.bio.bio_backend.domain.admin.CommonCode.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CommonGroupCodeDto { + + private Long oid; + private String code; + private String name; + private String characterRef1Title; + private String characterRef2Title; + private String characterRef3Title; + private String characterRef4Title; + private String characterRef5Title; + private Integer sortOrder; + private Boolean useFlag; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonCodeRequestDto.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonCodeRequestDto.java new file mode 100644 index 0000000..e208395 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonCodeRequestDto.java @@ -0,0 +1,55 @@ +package com.bio.bio_backend.domain.admin.CommonCode.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CreateCommonCodeRequestDto { + + @NotBlank(message = "코드는 필수입니다") + @Size(max = 50, message = "코드는 50자를 초과할 수 없습니다") + private String code; + + @NotBlank(message = "이름은 필수입니다") + @Size(max = 100, message = "이름은 100자를 초과할 수 없습니다") + private String name; + + @Size(max = 500, message = "설명은 500자를 초과할 수 없습니다") + private String description; + + @NotBlank(message = "그룹 코드는 필수입니다") + @Size(max = 50, message = "그룹 코드는 50자를 초과할 수 없습니다") + private String groupCode; + + @Size(max = 50, message = "상위 코드는 50자를 초과할 수 없습니다") + private String parentCode; + + @Size(max = 100, message = "문자 참조1은 100자를 초과할 수 없습니다") + private String characterRef1; + + @Size(max = 100, message = "문자 참조2는 100자를 초과할 수 없습니다") + private String characterRef2; + + @Size(max = 100, message = "문자 참조3은 100자를 초과할 수 없습니다") + private String characterRef3; + + @Size(max = 100, message = "문자 참조4는 100자를 초과할 수 없습니다") + private String characterRef4; + + @Size(max = 100, message = "문자 참조5는 100자를 초과할 수 없습니다") + private String characterRef5; + + @Builder.Default + private Integer sortOrder = 0; + + @Builder.Default + private Boolean useFlag = true; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonCodeResponseDto.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonCodeResponseDto.java new file mode 100644 index 0000000..4b9f3ff --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonCodeResponseDto.java @@ -0,0 +1,30 @@ +package com.bio.bio_backend.domain.admin.CommonCode.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CreateCommonCodeResponseDto { + + private Long oid; + private String code; + private String name; + private String description; + private String groupCode; + private String parentCode; + private String characterRef1; + private String characterRef2; + private String characterRef3; + private String characterRef4; + private String characterRef5; + private Integer sortOrder; + private Boolean useFlag; + private LocalDateTime createdAt; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonGroupCodeRequestDto.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonGroupCodeRequestDto.java new file mode 100644 index 0000000..60df3d6 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonGroupCodeRequestDto.java @@ -0,0 +1,45 @@ +package com.bio.bio_backend.domain.admin.CommonCode.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CreateCommonGroupCodeRequestDto { + + @NotBlank(message = "코드는 필수입니다") + @Size(max = 50, message = "코드는 50자를 초과할 수 없습니다") + private String code; + + @NotBlank(message = "이름은 필수입니다") + @Size(max = 100, message = "이름은 100자를 초과할 수 없습니다") + private String name; + + @Size(max = 100, message = "차트 참조1 제목은 100자를 초과할 수 없습니다") + private String characterRef1Title; + + @Size(max = 100, message = "차트 참조2 제목은 100자를 초과할 수 없습니다") + private String characterRef2Title; + + @Size(max = 100, message = "차트 참조3 제목은 100자를 초과할 수 없습니다") + private String characterRef3Title; + + @Size(max = 100, message = "차트 참조4 제목은 100자를 초과할 수 없습니다") + private String characterRef4Title; + + @Size(max = 100, message = "차트 참조5 제목은 100자를 초과할 수 없습니다") + private String characterRef5Title; + + @Builder.Default + private Integer sortOrder = 0; + + @Builder.Default + private Boolean useFlag = true; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonGroupCodeResponseDto.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonGroupCodeResponseDto.java new file mode 100644 index 0000000..fdab153 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/CreateCommonGroupCodeResponseDto.java @@ -0,0 +1,27 @@ +package com.bio.bio_backend.domain.admin.CommonCode.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CreateCommonGroupCodeResponseDto { + + private Long oid; + private String code; + private String name; + private String characterRef1Title; + private String characterRef2Title; + private String characterRef3Title; + private String characterRef4Title; + private String characterRef5Title; + private Integer sortOrder; + private Boolean useFlag; + private LocalDateTime createdAt; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/UpdateCommonCodeRequestDto.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/UpdateCommonCodeRequestDto.java new file mode 100644 index 0000000..a4dd497 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/UpdateCommonCodeRequestDto.java @@ -0,0 +1,49 @@ +package com.bio.bio_backend.domain.admin.CommonCode.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class UpdateCommonCodeRequestDto { + + @NotBlank(message = "이름은 필수입니다") + @Size(max = 100, message = "이름은 100자를 초과할 수 없습니다") + private String name; + + @Size(max = 500, message = "설명은 500자를 초과할 수 없습니다") + private String description; + + @NotBlank(message = "그룹 코드는 필수입니다") + @Size(max = 50, message = "그룹 코드는 50자를 초과할 수 없습니다") + private String groupCode; + + @Size(max = 50, message = "상위 코드는 50자를 초과할 수 없습니다") + private String parentCode; + + @Size(max = 100, message = "문자 참조1은 100자를 초과할 수 없습니다") + private String characterRef1; + + @Size(max = 100, message = "문자 참조2는 100자를 초과할 수 없습니다") + private String characterRef2; + + @Size(max = 100, message = "문자 참조3은 100자를 초과할 수 없습니다") + private String characterRef3; + + @Size(max = 100, message = "문자 참조4는 100자를 초과할 수 없습니다") + private String characterRef4; + + @Size(max = 100, message = "문자 참조5는 100자를 초과할 수 없습니다") + private String characterRef5; + + private Integer sortOrder; + + private Boolean useFlag; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/UpdateCommonGroupCodeRequestDto.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/UpdateCommonGroupCodeRequestDto.java new file mode 100644 index 0000000..ec2d6ff --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/dto/UpdateCommonGroupCodeRequestDto.java @@ -0,0 +1,39 @@ +package com.bio.bio_backend.domain.admin.CommonCode.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class UpdateCommonGroupCodeRequestDto { + + @NotBlank(message = "이름은 필수입니다") + @Size(max = 100, message = "이름은 100자를 초과할 수 없습니다") + private String name; + + @Size(max = 100, message = "문자 참조1 제목은 100자를 초과할 수 없습니다") + private String characterRef1Title; + + @Size(max = 100, message = "문자 참조2 제목은 100자를 초과할 수 없습니다") + private String characterRef2Title; + + @Size(max = 100, message = "문자 참조3 제목은 100자를 초과할 수 없습니다") + private String characterRef3Title; + + @Size(max = 100, message = "문자 참조4 제목은 100자를 초과할 수 없습니다") + private String characterRef4Title; + + @Size(max = 100, message = "문자 참조5 제목은 100자를 초과할 수 없습니다") + private String characterRef5Title; + + private Integer sortOrder; + + private Boolean useFlag; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/entity/CommonCode.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/entity/CommonCode.java new file mode 100644 index 0000000..4616d6e --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/entity/CommonCode.java @@ -0,0 +1,73 @@ +package com.bio.bio_backend.domain.admin.CommonCode.entity; + +import com.bio.bio_backend.global.constants.AppConstants; +import com.bio.bio_backend.global.entity.BaseEntity; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@Getter @Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Table( + name = AppConstants.TABLE_PREFIX + "common_code", + indexes = { + @Index(name = "idx_common_code_code", columnList = "code"), + @Index(name = "idx_common_code_group_code", columnList = "group_code"), + @Index(name = "idx_common_code_parent_code", columnList = "parent_code") + } +) +public class CommonCode extends BaseEntity { + + @Column(name = "code", nullable = false, length = 50, unique = true) + private String code; + + @Column(name = "name", nullable = false, length = 100) + private String name; + + @Column(name = "description", length = 500) + private String description; + + @Column(name = "group_code", nullable = false, length = 50) + private String groupCode; + + @Column(name = "parent_code", length = 50) + private String parentCode; + + @Column(name = "character_ref1", length = 100) + private String characterRef1; + + @Column(name = "character_ref2", length = 100) + private String characterRef2; + + @Column(name = "character_ref3", length = 100) + private String characterRef3; + + @Column(name = "character_ref4", length = 100) + private String characterRef4; + + @Column(name = "character_ref5", length = 100) + private String characterRef5; + + @Column(name = "sort_order", nullable = false) + @Builder.Default + private Integer sortOrder = 0; + + @Column(name = "use_flag", nullable = false) + @Builder.Default + private Boolean useFlag = true; + + // 관계 설정 + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "group_code", referencedColumnName = "code", insertable = false, updatable = false) + private CommonGroupCode commonGroupCode; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "parent_code", referencedColumnName = "code", insertable = false, updatable = false) + private CommonCode parentCodeEntity; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/entity/CommonGroupCode.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/entity/CommonGroupCode.java new file mode 100644 index 0000000..22ae60c --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/entity/CommonGroupCode.java @@ -0,0 +1,53 @@ +package com.bio.bio_backend.domain.admin.CommonCode.entity; + +import com.bio.bio_backend.global.constants.AppConstants; +import com.bio.bio_backend.global.entity.BaseEntity; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@Getter @Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Table( + name = AppConstants.TABLE_PREFIX + "common_group_code", + indexes = { + @Index(name = "idx_common_group_code_code", columnList = "code") + } +) +public class CommonGroupCode extends BaseEntity { + + @Column(name = "code", nullable = false, length = 50, unique = true) + private String code; + + @Column(name = "name", nullable = false, length = 100) + private String name; + + @Column(name = "character_ref1_title", length = 100) + private String characterRef1Title; + + @Column(name = "character_ref2_title", length = 100) + private String characterRef2Title; + + @Column(name = "character_ref3_title", length = 100) + private String characterRef3Title; + + @Column(name = "character_ref4_title", length = 100) + private String characterRef4Title; + + @Column(name = "character_ref5_title", length = 100) + private String characterRef5Title; + + @Column(name = "sort_order", nullable = false) + @Builder.Default + private Integer sortOrder = 0; + + @Column(name = "use_flag", nullable = false) + @Builder.Default + private Boolean useFlag = true; +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/mapper/CommonCodeMapper.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/mapper/CommonCodeMapper.java new file mode 100644 index 0000000..c4dd553 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/mapper/CommonCodeMapper.java @@ -0,0 +1,68 @@ +package com.bio.bio_backend.domain.admin.CommonCode.mapper; + +import com.bio.bio_backend.domain.admin.CommonCode.dto.CommonCodeDto; +import com.bio.bio_backend.domain.admin.CommonCode.dto.CreateCommonCodeRequestDto; +import com.bio.bio_backend.domain.admin.CommonCode.dto.CreateCommonCodeResponseDto; +import com.bio.bio_backend.domain.admin.CommonCode.dto.UpdateCommonCodeRequestDto; +import com.bio.bio_backend.domain.admin.CommonCode.entity.CommonCode; +import com.bio.bio_backend.global.annotation.IgnoreBaseEntityMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +@Mapper(componentModel = "spring") +public interface CommonCodeMapper { + + CommonCodeMapper INSTANCE = Mappers.getMapper(CommonCodeMapper.class); + + /** + * CommonCode 엔티티를 CommonCodeDto로 변환 + */ + CommonCodeDto toCommonCodeDto(CommonCode entity); + + /** + * CommonCodeDto를 CommonCode 엔티티로 변환 + */ + @Mapping(target = "commonGroupCode", ignore = true) + @Mapping(target = "parentCodeEntity", ignore = true) + CommonCode toCommonCode(CommonCodeDto dto); + + /** + * CommonCode 엔티티 리스트를 CommonCodeDto 리스트로 변환 + */ + List toCommonCodeDtoList(List entities); + + /** + * CommonCodeDto 리스트를 CommonCode 엔티티 리스트로 변환 + */ + List toCommonCodeList(List dtos); + + /** + * CommonCodeDto를 CreateCommonCodeResponseDto로 변환 + */ + @Mapping(target = "createdAt", ignore = true) + CreateCommonCodeResponseDto toCreateCommonCodeResponseDto(CommonCodeDto dto); + + /** + * CreateCommonCodeRequestDto를 CommonCodeDto로 변환 + */ + @Mapping(target = "oid", ignore = true) + CommonCodeDto toCommonCodeDto(CreateCommonCodeRequestDto requestDto); + + /** + * UpdateCommonCodeRequestDto를 CommonCodeDto로 변환 + */ + @Mapping(target = "code", ignore = true) + @Mapping(target = "oid", ignore = true) + CommonCodeDto toCommonCodeDto(UpdateCommonCodeRequestDto requestDto); + + /** + * CommonCodeDto의 값으로 CommonCode 엔티티를 업데이트 (null 값은 무시) + */ + @IgnoreBaseEntityMapping + @Mapping(target = "commonGroupCode", ignore = true) + @Mapping(target = "parentCodeEntity", ignore = true) + void updateCommonCodeFromDto(CommonCodeDto commonCodeDto, @org.mapstruct.MappingTarget CommonCode commonCode); +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/mapper/CommonGroupCodeMapper.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/mapper/CommonGroupCodeMapper.java new file mode 100644 index 0000000..038df71 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/mapper/CommonGroupCodeMapper.java @@ -0,0 +1,67 @@ +package com.bio.bio_backend.domain.admin.CommonCode.mapper; + +import com.bio.bio_backend.domain.admin.CommonCode.dto.CommonGroupCodeDto; +import com.bio.bio_backend.domain.admin.CommonCode.dto.CreateCommonGroupCodeRequestDto; +import com.bio.bio_backend.domain.admin.CommonCode.dto.CreateCommonGroupCodeResponseDto; +import com.bio.bio_backend.domain.admin.CommonCode.dto.UpdateCommonGroupCodeRequestDto; +import com.bio.bio_backend.domain.admin.CommonCode.entity.CommonGroupCode; +import com.bio.bio_backend.global.annotation.IgnoreBaseEntityMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +@Mapper(componentModel = "spring") +public interface CommonGroupCodeMapper { + + CommonGroupCodeMapper INSTANCE = Mappers.getMapper(CommonGroupCodeMapper.class); + + /** + * CommonGroupCode 엔티티를 CommonGroupCodeDto로 변환 + */ + CommonGroupCodeDto toCommonGroupCodeDto(CommonGroupCode entity); + + /** + * CommonGroupCodeDto를 CommonGroupCode 엔티티로 변환 + */ + CommonGroupCode toCommonGroupCode(CommonGroupCodeDto dto); + + /** + * CommonGroupCode 엔티티 리스트를 CommonGroupCodeDto 리스트로 변환 + */ + List toCommonGroupCodeDtoList(List entities); + + /** + * CommonGroupCodeDto 리스트를 CommonGroupCode 엔티티 리스트로 변환 + */ + List toCommonGroupCodeList(List dtos); + + /** + * CommonGroupCodeDto를 CreateCommonGroupCodeResponseDto로 변환 + */ + CreateCommonGroupCodeResponseDto toCreateCommonGroupCodeResponseDto(CommonGroupCodeDto dto); + + /** + * CreateCommonGroupCodeRequestDto를 CommonGroupCodeDto로 변환 + */ + @Mapping(target = "oid", ignore = true) + @Mapping(target = "createdAt", ignore = true) + @Mapping(target = "updatedAt", ignore = true) + CommonGroupCodeDto toCommonGroupCodeDto(CreateCommonGroupCodeRequestDto requestDto); + + /** + * UpdateCommonGroupCodeRequestDto를 CommonGroupCodeDto로 변환 + */ + @Mapping(target = "oid", ignore = true) + @Mapping(target = "code", ignore = true) + @Mapping(target = "createdAt", ignore = true) + @Mapping(target = "updatedAt", ignore = true) + CommonGroupCodeDto toCommonGroupCodeDto(UpdateCommonGroupCodeRequestDto requestDto); + + /** + * CommonGroupCodeDto의 값으로 CommonGroupCode 엔티티를 업데이트 (null 값은 무시) + */ + @IgnoreBaseEntityMapping + void updateCommonGroupCodeFromDto(CommonGroupCodeDto groupCodeDto, @org.mapstruct.MappingTarget CommonGroupCode groupCode); +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/repository/CommonCodeRepository.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/repository/CommonCodeRepository.java new file mode 100644 index 0000000..369153d --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/repository/CommonCodeRepository.java @@ -0,0 +1,32 @@ +package com.bio.bio_backend.domain.admin.CommonCode.repository; + +import com.bio.bio_backend.domain.admin.CommonCode.entity.CommonCode; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface CommonCodeRepository extends JpaRepository { + + Optional findByCode(String code); + + List findByGroupCodeAndUseFlagOrderBySortOrderAsc(String groupCode, Boolean useFlag); + + List findByParentCodeAndUseFlagOrderBySortOrderAsc(String parentCode, Boolean useFlag); + + @Query("SELECT cc FROM CommonCode cc WHERE cc.groupCode = :groupCode AND cc.useFlag = :useFlag ORDER BY cc.sortOrder ASC") + List findActiveCodesByGroupCodeOrderBySortOrder(@Param("groupCode") String groupCode, @Param("useFlag") Boolean useFlag); + + @Query("SELECT cc FROM CommonCode cc WHERE cc.parentCode = :parentCode AND cc.useFlag = :useFlag ORDER BY cc.sortOrder ASC") + List findActiveCodesByParentCodeOrderBySortOrder(@Param("parentCode") String parentCode, @Param("useFlag") Boolean useFlag); + + boolean existsByCode(String code); + + boolean existsByGroupCode(String groupCode); + + boolean existsByParentCode(String parentCode); +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/repository/CommonGroupCodeRepository.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/repository/CommonGroupCodeRepository.java new file mode 100644 index 0000000..012c852 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/repository/CommonGroupCodeRepository.java @@ -0,0 +1,23 @@ +package com.bio.bio_backend.domain.admin.CommonCode.repository; + +import com.bio.bio_backend.domain.admin.CommonCode.entity.CommonGroupCode; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface CommonGroupCodeRepository extends JpaRepository { + + Optional findByCode(String code); + + List findByUseFlagOrderBySortOrderAsc(Boolean useFlag); + + @Query("SELECT cgc FROM CommonGroupCode cgc WHERE cgc.useFlag = :useFlag ORDER BY cgc.sortOrder ASC") + List findActiveGroupCodesOrderBySortOrder(@Param("useFlag") Boolean useFlag); + + boolean existsByCode(String code); +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/service/CommonCodeService.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/service/CommonCodeService.java new file mode 100644 index 0000000..dd9ab88 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/service/CommonCodeService.java @@ -0,0 +1,28 @@ +package com.bio.bio_backend.domain.admin.CommonCode.service; + +import com.bio.bio_backend.domain.admin.CommonCode.dto.CommonCodeDto; +import com.bio.bio_backend.domain.admin.CommonCode.dto.CommonGroupCodeDto; + +import java.util.List; + +public interface CommonCodeService { + + // 그룹 코드 관련 + CommonGroupCodeDto createGroupCode(CommonGroupCodeDto groupCodeDto); + void updateGroupCode(String code, CommonGroupCodeDto groupCodeDto); + void deleteGroupCode(String code); + CommonGroupCodeDto getGroupCode(String code); + List getAllGroupCodes(); + List getActiveGroupCodes(); + + // 공통 코드 관련 + CommonCodeDto createCode(CommonCodeDto codeDto); + void updateCode(String code, CommonCodeDto codeDto); + void deleteCode(String code); + CommonCodeDto getCode(String code); + List getCodesByGroupCode(String groupCode); + List getActiveCodesByGroupCode(String groupCode); + List getCodesByParentCode(String parentCode); + List getActiveCodesByParentCode(String parentCode); + List getAllCodes(); +} diff --git a/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/service/CommonCodeServiceImpl.java b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/service/CommonCodeServiceImpl.java new file mode 100644 index 0000000..8490ddc --- /dev/null +++ b/src/main/java/com/bio/bio_backend/domain/admin/CommonCode/service/CommonCodeServiceImpl.java @@ -0,0 +1,165 @@ +package com.bio.bio_backend.domain.admin.CommonCode.service; + +import com.bio.bio_backend.domain.admin.CommonCode.dto.CommonCodeDto; +import com.bio.bio_backend.domain.admin.CommonCode.dto.CommonGroupCodeDto; +import com.bio.bio_backend.domain.admin.CommonCode.entity.CommonCode; +import com.bio.bio_backend.domain.admin.CommonCode.entity.CommonGroupCode; +import com.bio.bio_backend.domain.admin.CommonCode.mapper.CommonCodeMapper; +import com.bio.bio_backend.domain.admin.CommonCode.mapper.CommonGroupCodeMapper; +import com.bio.bio_backend.domain.admin.CommonCode.repository.CommonCodeRepository; +import com.bio.bio_backend.domain.admin.CommonCode.repository.CommonGroupCodeRepository; +import com.bio.bio_backend.global.exception.ApiException; +import com.bio.bio_backend.global.constants.ApiResponseCode; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Slf4j +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class CommonCodeServiceImpl implements CommonCodeService { + + private final CommonGroupCodeRepository commonGroupCodeRepository; + private final CommonCodeRepository commonCodeRepository; + private final CommonCodeMapper commonCodeMapper; + private final CommonGroupCodeMapper commonGroupCodeMapper; + + // 그룹 코드 관련 메서드들 + @Override + @Transactional + public CommonGroupCodeDto createGroupCode(CommonGroupCodeDto groupCodeDto) { + if (commonGroupCodeRepository.existsByCode(groupCodeDto.getCode())) { + throw new ApiException(ApiResponseCode.USER_ID_DUPLICATE, "이미 존재하는 그룹 코드입니다: " + groupCodeDto.getCode()); + } + + CommonGroupCode groupCode = commonGroupCodeMapper.toCommonGroupCode(groupCodeDto); + CommonGroupCode savedGroupCode = commonGroupCodeRepository.save(groupCode); + return commonGroupCodeMapper.toCommonGroupCodeDto(savedGroupCode); + } + + @Override + @Transactional + public void updateGroupCode(String code, CommonGroupCodeDto groupCodeDto) { + CommonGroupCode existingGroupCode = commonGroupCodeRepository.findByCode(code) + .orElseThrow(() -> new ApiException(ApiResponseCode.COMMON_NOT_FOUND, "그룹 코드를 찾을 수 없습니다: " + code)); + + commonGroupCodeMapper.updateCommonGroupCodeFromDto(groupCodeDto, existingGroupCode); + commonGroupCodeRepository.save(existingGroupCode); + } + + @Override + @Transactional + public void deleteGroupCode(String code) { + CommonGroupCode groupCode = commonGroupCodeRepository.findByCode(code) + .orElseThrow(() -> new ApiException(ApiResponseCode.COMMON_NOT_FOUND, "그룹 코드를 찾을 수 없습니다: " + code)); + + // 하위 공통 코드가 있는지 확인 + if (commonCodeRepository.existsByGroupCode(code)) { + throw new ApiException(ApiResponseCode.COMMON_BAD_REQUEST, "하위 공통 코드가 존재하여 삭제할 수 없습니다: " + code); + } + + commonGroupCodeRepository.delete(groupCode); + } + + @Override + public CommonGroupCodeDto getGroupCode(String code) { + CommonGroupCode groupCode = commonGroupCodeRepository.findByCode(code) + .orElseThrow(() -> new ApiException(ApiResponseCode.COMMON_NOT_FOUND, "그룹 코드를 찾을 수 없습니다: " + code)); + return commonGroupCodeMapper.toCommonGroupCodeDto(groupCode); + } + + @Override + public List getAllGroupCodes() { + List groupCodes = commonGroupCodeRepository.findAll(); + return commonGroupCodeMapper.toCommonGroupCodeDtoList(groupCodes); + } + + @Override + public List getActiveGroupCodes() { + List groupCodes = commonGroupCodeRepository.findByUseFlagOrderBySortOrderAsc(true); + return commonGroupCodeMapper.toCommonGroupCodeDtoList(groupCodes); + } + + // 공통 코드 관련 메서드들 + @Override + @Transactional + public CommonCodeDto createCode(CommonCodeDto commonCodeDto) { + if (commonCodeRepository.existsByCode(commonCodeDto.getCode())) { + throw new ApiException(ApiResponseCode.USER_ID_DUPLICATE, "이미 존재하는 공통 코드입니다: " + commonCodeDto.getCode()); + } + + // 그룹 코드 존재 여부 확인 + if (!commonGroupCodeRepository.existsByCode(commonCodeDto.getGroupCode())) { + throw new ApiException(ApiResponseCode.COMMON_BAD_REQUEST, "존재하지 않는 그룹 코드입니다: " + commonCodeDto.getGroupCode()); + } + + CommonCode commonCode = commonCodeMapper.toCommonCode(commonCodeDto); + CommonCode savedCommonCode = commonCodeRepository.save(commonCode); + return commonCodeMapper.toCommonCodeDto(savedCommonCode); + } + + @Override + @Transactional + public void updateCode(String code, CommonCodeDto commonCodeDto) { + CommonCode existingCommonCode = commonCodeRepository.findByCode(code) + .orElseThrow(() -> new ApiException(ApiResponseCode.COMMON_NOT_FOUND, "공통 코드를 찾을 수 없습니다: " + code)); + + commonCodeMapper.updateCommonCodeFromDto(commonCodeDto, existingCommonCode); + commonCodeRepository.save(existingCommonCode); + } + + @Override + @Transactional + public void deleteCode(String code) { + CommonCode commonCode = commonCodeRepository.findByCode(code) + .orElseThrow(() -> new ApiException(ApiResponseCode.COMMON_NOT_FOUND, "공통 코드를 찾을 수 없습니다: " + code)); + + // 하위 공통 코드가 있는지 확인 + if (commonCodeRepository.existsByParentCode(code)) { + throw new ApiException(ApiResponseCode.COMMON_BAD_REQUEST, "하위 공통 코드가 존재하여 삭제할 수 없습니다: " + code); + } + + commonCodeRepository.delete(commonCode); + } + + @Override + public CommonCodeDto getCode(String code) { + CommonCode commonCode = commonCodeRepository.findByCode(code) + .orElseThrow(() -> new ApiException(ApiResponseCode.COMMON_NOT_FOUND, "공통 코드를 찾을 수 없습니다: " + code)); + return commonCodeMapper.toCommonCodeDto(commonCode); + } + + @Override + public List getCodesByGroupCode(String groupCode) { + List commonCodes = commonCodeRepository.findByGroupCodeAndUseFlagOrderBySortOrderAsc(groupCode, true); + return commonCodeMapper.toCommonCodeDtoList(commonCodes); + } + + @Override + public List getActiveCodesByGroupCode(String groupCode) { + List commonCodes = commonCodeRepository.findActiveCodesByGroupCodeOrderBySortOrder(groupCode, true); + return commonCodeMapper.toCommonCodeDtoList(commonCodes); + } + + @Override + public List getCodesByParentCode(String parentCode) { + List commonCodes = commonCodeRepository.findByParentCodeAndUseFlagOrderBySortOrderAsc(parentCode, true); + return commonCodeMapper.toCommonCodeDtoList(commonCodes); + } + + @Override + public List getActiveCodesByParentCode(String parentCode) { + List commonCodes = commonCodeRepository.findActiveCodesByParentCodeOrderBySortOrder(parentCode, true); + return commonCodeMapper.toCommonCodeDtoList(commonCodes); + } + + @Override + public List getAllCodes() { + List commonCodes = commonCodeRepository.findAll(); + return commonCodeMapper.toCommonCodeDtoList(commonCodes); + } +}