공통코드 삭제 API 커밋, 삭제일시 컬럼 추가, 에러로그에 userId 토큰으로 로직 변경

This commit is contained in:
2025-12-05 12:07:03 +09:00
parent 88cdfa7ce7
commit eeef8c8d32
8 changed files with 130 additions and 23 deletions

View File

@@ -3,17 +3,21 @@ package com.kamco.cd.kamcoback.postgres.core;
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto.Basic;
import com.kamco.cd.kamcoback.common.service.BaseCoreService;
import com.kamco.cd.kamcoback.config.api.ApiResponseDto.ApiResponseCode;
import com.kamco.cd.kamcoback.config.api.ApiResponseDto.ResponseObj;
import com.kamco.cd.kamcoback.config.api.ApiResponseDto.SuccFailCode;
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
import com.kamco.cd.kamcoback.postgres.repository.code.CommonCodeRepository;
import com.kamco.cd.kamcoback.zoo.dto.AnimalDto.SearchReq;
import jakarta.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
@RequiredArgsConstructor
public class CommonCodeCoreService
@@ -92,7 +96,9 @@ public class CommonCodeCoreService
found.getDeleted(),
req.getProps1(),
req.getProps2(),
req.getProps3());
req.getProps3(),
null
);
return commonCodeRepository.save(entity).toDto();
}
@@ -116,17 +122,30 @@ public class CommonCodeCoreService
return commonCodeRepository.getCode(parentCodeCd, childCodeCd);
}
@Override
public void remove(Long id) {
/**
* 공통코드 삭제
* @param id
* @return
*/
public ResponseObj removeCode(Long id) {
CommonCodeEntity entity =
commonCodeRepository
.findByCodeId(id)
.orElseThrow(() -> new EntityNotFoundException("code를 찾을 수 없습니다. id " + id));
// 하위 코드 deleted = false 업데이트
entity.getChildren().forEach(CommonCodeEntity::deleted);
// 하위코드가 있으면 삭제 불가
if(!entity.getChildren().isEmpty()){
return new ResponseObj(SuccFailCode.FAIL, ApiResponseCode.UNPROCESSABLE_ENTITY,"하위에 다른 공통코드를 가지고 있습니다.<br/>하위공통 코드를 이동한 후 삭제할 수 있습니다.");
}
// id 코드 deleted = false 업데이트
entity.deleted();
return new ResponseObj(SuccFailCode.SUCCESS, ApiResponseCode.OK, "삭제되었습니다.");
}
@Override
public void remove(Long aLong) {
//미사용
}
@Override

View File

@@ -15,6 +15,8 @@ import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
@@ -73,6 +75,9 @@ public class CommonCodeEntity extends CommonDateEntity {
@Column(name = "props3")
private String props3;
@Column(name = "deleted_dttm")
private ZonedDateTime deletedDttm;
public CommonCodeEntity(
String code,
String name,
@@ -102,7 +107,9 @@ public class CommonCodeEntity extends CommonDateEntity {
Boolean deleted,
String props1,
String props2,
String props3) {
String props3,
ZonedDateTime deletedDttm
) {
this.id = id;
this.code = code;
this.name = name;
@@ -113,6 +120,7 @@ public class CommonCodeEntity extends CommonDateEntity {
this.props1 = props1;
this.props2 = props2;
this.props3 = props3;
this.deletedDttm = deletedDttm;
}
public CommonCodeDto.Basic toDto() {
@@ -129,7 +137,9 @@ public class CommonCodeEntity extends CommonDateEntity {
super.getModifiedDate(),
this.props1,
this.props2,
this.props3);
this.props3,
this.deletedDttm
);
}
public void addParent(CommonCodeEntity parent) {
@@ -142,6 +152,7 @@ public class CommonCodeEntity extends CommonDateEntity {
public void deleted() {
this.deleted = true;
this.deletedDttm = ZonedDateTime.now();
}
public void updateOrder(int order) {

View File

@@ -29,7 +29,11 @@ public class CommonCodeRepositoryImpl implements CommonCodeRepositoryCustom {
.selectFrom(commonCodeEntity)
.leftJoin(commonCodeEntity.children, child)
.fetchJoin()
.where(commonCodeEntity.id.eq(id), commonCodeEntity.deleted.isFalse())
.where(
commonCodeEntity.id.eq(id),
commonCodeEntity.deleted.isFalse().or(commonCodeEntity.deleted.isNull()),
child.deleted.isFalse().or(child.deleted.isNull())
)
.orderBy(commonCodeEntity.order.asc(), child.order.asc())
.fetchOne());
}
@@ -46,7 +50,9 @@ public class CommonCodeRepositoryImpl implements CommonCodeRepositoryCustom {
commonCodeEntity.parent.isNull(),
commonCodeEntity.code.eq(code),
commonCodeEntity.used.isTrue(),
commonCodeEntity.deleted.isFalse())
commonCodeEntity.deleted.isFalse().or(commonCodeEntity.deleted.isNull()),
child.deleted.isFalse().or(child.deleted.isNull())
)
.orderBy(child.order.asc())
.fetchOne());
}
@@ -58,7 +64,11 @@ public class CommonCodeRepositoryImpl implements CommonCodeRepositoryCustom {
.selectFrom(commonCodeEntity)
.leftJoin(commonCodeEntity.children, child)
.fetchJoin()
.where(commonCodeEntity.parent.isNull(), commonCodeEntity.deleted.isFalse())
.where(
commonCodeEntity.parent.isNull(),
commonCodeEntity.deleted.isFalse().or(commonCodeEntity.deleted.isNull()),
child.deleted.isFalse().or(child.deleted.isNull())
)
.orderBy(commonCodeEntity.order.asc(), child.order.asc())
.fetch();
}
@@ -90,7 +100,10 @@ public class CommonCodeRepositoryImpl implements CommonCodeRepositoryCustom {
.select(child.name)
.from(child)
.join(child.parent, parent)
.where(parent.code.eq(parentCodeCd).and(child.code.eq(childCodeCd)))
.where(
parent.code.eq(parentCodeCd).and(child.code.eq(childCodeCd)),
child.deleted.isFalse().or(child.deleted.isNull())
)
.fetchFirst(); // 단일 결과만
return Optional.ofNullable(result);
@@ -120,7 +133,7 @@ public class CommonCodeRepositoryImpl implements CommonCodeRepositoryCustom {
private List<CommonCodeEntity> findAllByIds(Set<Long> ids) {
return queryFactory
.selectFrom(commonCodeEntity)
.where(commonCodeEntity.id.in(ids), commonCodeEntity.deleted.isFalse())
.where(commonCodeEntity.id.in(ids), commonCodeEntity.deleted.isFalse().or(commonCodeEntity.deleted.isNull()))
.fetch();
}
}