261022_03_ENH: 리스트 초기화 범위 세분화 및 설문별 상태이력 초기화 추가
This commit is contained in:
@@ -4,6 +4,7 @@ import com.kri.contact.server.common.enums.ListResetMode;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
@@ -14,10 +15,21 @@ import lombok.Setter;
|
||||
@Schema(description = "리스트 초기화 요청 DTO")
|
||||
public class ListResetRequestDto {
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "초기화 범위")
|
||||
@Schema(description = "기존 초기화 모드(0: 전체, 1: 전체 이력). 신규 선택 필드와 함께 사용할 수 없습니다.", deprecated = true)
|
||||
private ListResetMode resetMode;
|
||||
|
||||
@Schema(description = "리스트 초기화 여부. true이면 모든 이력과 연동 설문을 선택해야 합니다.")
|
||||
private Boolean resetList;
|
||||
|
||||
@Schema(description = "컨택이력 초기화 여부")
|
||||
private Boolean resetContactHistory;
|
||||
|
||||
@Schema(description = "면접이력 초기화 여부")
|
||||
private Boolean resetInterviewHistory;
|
||||
|
||||
@Schema(description = "상태이력을 초기화할 프로젝트 설문 고유번호 목록. 미선택은 빈 배열")
|
||||
private List<Long> resetSurveyIds;
|
||||
|
||||
@Valid
|
||||
@NotNull
|
||||
@Schema(description = "리스트 필터")
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.kri.contact.server.dto.list;
|
||||
|
||||
import com.kri.contact.server.common.enums.ListResetMode;
|
||||
import java.util.Set;
|
||||
|
||||
public record ListResetScopeDto(
|
||||
ListResetMode mode,
|
||||
boolean selective,
|
||||
boolean contactHistory,
|
||||
boolean interviewHistory,
|
||||
Set<Long> surveyIds
|
||||
) {
|
||||
public ListResetScopeDto {
|
||||
surveyIds = Set.copyOf(surveyIds);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.kri.contact.server.persistence.jdbc;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.common.enums.ResultCodeType;
|
||||
import com.kri.contact.server.dto.list.ListResetScopeDto;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Query;
|
||||
import java.util.ArrayList;
|
||||
@@ -18,6 +20,44 @@ public class ListResetJdbcSupport {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public DeleteCounts deleteSelectedHistories(Long prjctIdx, Collection<String> sids,
|
||||
ListResetScopeDto scope, int chunkSize) {
|
||||
if (sids == null || sids.isEmpty()) {
|
||||
return DeleteCounts.empty();
|
||||
}
|
||||
try {
|
||||
prepareTempSids(sids, chunkSize);
|
||||
int historyCount = deleteSelectedEvents("t_list_event_hist", prjctIdx, scope);
|
||||
int latestCount = deleteSelectedEvents("t_list_event_latest", prjctIdx, scope);
|
||||
return DeleteCounts.histories(historyCount, latestCount, 0, 0);
|
||||
} finally {
|
||||
dropTempSids();
|
||||
}
|
||||
}
|
||||
|
||||
private int deleteSelectedEvents(String tableName, Long prjctIdx, ListResetScopeDto scope) {
|
||||
List<Short> eventTypes = new ArrayList<>();
|
||||
if (scope.contactHistory()) eventTypes.add(ResultCodeType.CONTACT.getCode());
|
||||
if (scope.interviewHistory()) eventTypes.add(ResultCodeType.INTERVIEW.getCode());
|
||||
List<String> conditions = new ArrayList<>();
|
||||
if (!eventTypes.isEmpty()) conditions.add("target.event_ty in (:eventTypes)");
|
||||
if (!scope.surveyIds().isEmpty()) {
|
||||
conditions.add("(target.event_ty = :surveyType and target.prjct_survey_idx in (:surveyIds))");
|
||||
}
|
||||
if (conditions.isEmpty()) return 0;
|
||||
Query query = entityManager.createNativeQuery(
|
||||
"delete target from " + tableName + " target "
|
||||
+ "join " + TEMP_SID_TABLE + " reset_sid on reset_sid.sid = target.sid "
|
||||
+ "where target.prjct_idx = :prjctIdx and (" + String.join(" or ", conditions) + ")");
|
||||
query.setParameter("prjctIdx", prjctIdx);
|
||||
if (!eventTypes.isEmpty()) query.setParameter("eventTypes", eventTypes);
|
||||
if (!scope.surveyIds().isEmpty()) {
|
||||
query.setParameter("surveyType", ResultCodeType.SURVEY.getCode());
|
||||
query.setParameter("surveyIds", scope.surveyIds());
|
||||
}
|
||||
return query.executeUpdate();
|
||||
}
|
||||
|
||||
public DeleteCounts deleteHistories(Long prjctIdx, Collection<String> sids, int chunkSize) {
|
||||
if (sids == null || sids.isEmpty()) {
|
||||
return DeleteCounts.empty();
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.kri.contact.server.persistence.support;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.common.enums.ListResetMode;
|
||||
import com.kri.contact.server.dto.list.ListResetScopeDto;
|
||||
import com.kri.contact.server.persistence.entity.TListResetLogEntity;
|
||||
import com.kri.contact.server.persistence.jdbc.ListResetJdbcSupport;
|
||||
import com.kri.contact.server.persistence.jdbc.ListResetJdbcSupport.DeleteCounts;
|
||||
@@ -38,6 +39,16 @@ public class ListResetPersistenceSupport {
|
||||
|
||||
private int resetWithAuditContext(Long prjctIdx, Collection<String> sids, ListResetMode mode,
|
||||
String listResetLogId, String delUser, int chunkSize, boolean accumulate) {
|
||||
return resetWithAuditContext(prjctIdx, sids, mode, listResetLogId, delUser, chunkSize, accumulate, null);
|
||||
}
|
||||
|
||||
public int resetChunkWithAuditContext(Long projectId, Collection<String> sids, ListResetScopeDto scope,
|
||||
String logId, String user, int chunkSize) {
|
||||
return resetWithAuditContext(projectId, sids, scope.mode(), logId, user, chunkSize, true, scope);
|
||||
}
|
||||
|
||||
private int resetWithAuditContext(Long prjctIdx, Collection<String> sids, ListResetMode mode,
|
||||
String listResetLogId, String delUser, int chunkSize, boolean accumulate, ListResetScopeDto scope) {
|
||||
int resetCount = mode == ListResetMode.HIST ? sids.size() : 0;
|
||||
|
||||
try (var ignored = deleteSessionContext.openReset(listResetLogId, delUser)) {
|
||||
@@ -45,7 +56,13 @@ public class ListResetPersistenceSupport {
|
||||
entityManager.clear();
|
||||
|
||||
switch (mode) {
|
||||
case HIST -> listResetJdbcSupport.deleteHistories(prjctIdx, sids, chunkSize);
|
||||
case HIST -> {
|
||||
if (scope != null && scope.selective()) {
|
||||
listResetJdbcSupport.deleteSelectedHistories(prjctIdx, sids, scope, chunkSize);
|
||||
} else {
|
||||
listResetJdbcSupport.deleteHistories(prjctIdx, sids, chunkSize);
|
||||
}
|
||||
}
|
||||
case ALL -> {
|
||||
listResetJdbcSupport.deleteHistories(prjctIdx, sids, chunkSize);
|
||||
DeleteCounts counts = listResetJdbcSupport.deleteListData(prjctIdx, sids, true, true, chunkSize);
|
||||
|
||||
@@ -6,12 +6,17 @@ import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.contact.server.common.consts.ContactConstant;
|
||||
import com.kri.contact.server.common.enums.ListResetMode;
|
||||
import com.kri.contact.server.dto.list.ListFilterDto;
|
||||
import com.kri.contact.server.dto.list.ListResetRequestDto;
|
||||
import com.kri.contact.server.dto.list.ListResetScopeDto;
|
||||
import com.kri.contact.server.persistence.entity.TListResetLogEntity;
|
||||
import com.kri.contact.server.persistence.support.ListResetPersistenceSupport;
|
||||
import com.kri.contact.server.persistence.repository.ListResetLogRepository;
|
||||
import com.kri.contact.server.persistence.repository.ProjectSurveyRepository;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -23,6 +28,52 @@ public class ListResetService {
|
||||
private final ListResetLogRepository listResetLogRepository;
|
||||
private final ListSearchService listSearchService;
|
||||
private final ListResetPersistenceSupport listResetPersistenceSupport;
|
||||
private final ProjectSurveyRepository projectSurveyRepository;
|
||||
|
||||
public ListResetScopeDto resolveResetScope(Long projectId, ListResetRequestDto request) {
|
||||
if (request == null || request.getListFilter() == null) {
|
||||
throw invalid("초기화 범위와 필터가 필요합니다.");
|
||||
}
|
||||
boolean hasSelection = request.getResetList() != null || request.getResetContactHistory() != null
|
||||
|| request.getResetInterviewHistory() != null || request.getResetSurveyIds() != null;
|
||||
if (request.getResetMode() != null) {
|
||||
if (hasSelection) {
|
||||
throw invalid("resetMode와 신규 초기화 선택 필드는 함께 사용할 수 없습니다.");
|
||||
}
|
||||
if (request.getResetMode() == ListResetMode.LIST_UPLOAD) {
|
||||
throw invalid("LIST_UPLOAD는 업로드 내부 전용 모드입니다.");
|
||||
}
|
||||
return new ListResetScopeDto(request.getResetMode(), false, false, false, Set.of());
|
||||
}
|
||||
if (request.getResetList() == null || request.getResetContactHistory() == null
|
||||
|| request.getResetInterviewHistory() == null || request.getResetSurveyIds() == null) {
|
||||
throw invalid("리스트, 컨택이력, 면접이력 선택 여부와 설문 목록이 필요합니다.");
|
||||
}
|
||||
Set<Long> selected = new HashSet<>(request.getResetSurveyIds());
|
||||
if (selected.contains(null) || selected.stream().anyMatch(id -> id <= 0)
|
||||
|| selected.size() != request.getResetSurveyIds().size()) {
|
||||
throw invalid("설문 고유번호는 중복 없이 올바르게 지정해야 합니다.");
|
||||
}
|
||||
Set<Long> linked = projectSurveyRepository.findByPrjctIdxOrderBySurveyOrderAscPrjctSurveyIdxAsc(projectId)
|
||||
.stream().map(survey -> survey.getPrjctSurveyIdx()).collect(Collectors.toSet());
|
||||
if (!linked.containsAll(selected)) {
|
||||
throw invalid("프로젝트에 연동되지 않은 설문은 초기화할 수 없습니다.");
|
||||
}
|
||||
if (request.getResetList()
|
||||
&& (!request.getResetContactHistory() || !request.getResetInterviewHistory() || !selected.equals(linked))) {
|
||||
throw invalid("리스트 초기화 시 컨택이력, 면접이력과 모든 연동 설문 상태이력을 선택해야 합니다.");
|
||||
}
|
||||
if (!request.getResetList() && !request.getResetContactHistory()
|
||||
&& !request.getResetInterviewHistory() && selected.isEmpty()) {
|
||||
throw invalid("초기화 대상을 하나 이상 선택해야 합니다.");
|
||||
}
|
||||
return new ListResetScopeDto(request.getResetList() ? ListResetMode.ALL : ListResetMode.HIST,
|
||||
true, request.getResetContactHistory(), request.getResetInterviewHistory(), selected);
|
||||
}
|
||||
|
||||
private BusinessException invalid(String message) {
|
||||
return BusinessException.badRequest("INVALID_LIST_RESET", message);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public int resetByFilter(
|
||||
@@ -86,4 +137,11 @@ public class ListResetService {
|
||||
projectId, sids, mode, logId, user, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public int resetAsyncChunk(Long projectId, Set<String> sids, ListResetScopeDto scope,
|
||||
String logId, String user) {
|
||||
return listResetPersistenceSupport.resetChunkWithAuditContext(
|
||||
projectId, sids, scope, logId, user, ContactConstant.INSERT_CHUNK_SIZE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,15 +50,14 @@ public class ListAsyncMaintenanceService {
|
||||
|
||||
public ListAsyncJobAcceptedDto reset(Long projectId, ListResetRequestDto request, RequestUserContext user) {
|
||||
access.requireProject(projectId, user);
|
||||
if (request == null || request.getResetMode() == null || request.getListFilter() == null) {
|
||||
throw BusinessException.badRequest("INVALID_LIST_RESET", "초기화 범위와 필터가 필요합니다.");
|
||||
}
|
||||
var scope = resets.resolveResetScope(projectId, request);
|
||||
var saved = mapper.convertValue(request, ListResetRequestDto.class);
|
||||
requireValidFilterConditions(saved.getListFilter().getSidSet());
|
||||
Map<String, Object> snapshot = mapper.convertValue(saved, new TypeReference<>() {});
|
||||
String key = executor.submit(projectId, user.getUserId(), ListAsyncJobType.LIST_RESET, snapshot, null,
|
||||
context -> {
|
||||
access.requireProject(projectId, user);
|
||||
resets.resolveResetScope(projectId, saved);
|
||||
context.progress("FREEZING_SIDS", null, Map.of());
|
||||
List<String> frozen = List.copyOf(new LinkedHashSet<>(search.findTargetSids(projectId, saved.getListFilter())));
|
||||
context.progress("PROCESSING", (long) frozen.size(), Map.of());
|
||||
@@ -69,9 +68,9 @@ public class ListAsyncMaintenanceService {
|
||||
access.requireProject(projectId, user);
|
||||
var chunk = List.copyOf(frozen.subList(offset, Math.min(frozen.size(), offset + ContactConstant.INSERT_CHUNK_SIZE)));
|
||||
context.applyFixedChunk(projectId, ++number, "LIST_RESET", chunk, present -> {
|
||||
resets.resetAsyncChunk(projectId, new LinkedHashSet<>(present), saved.getResetMode(), logId, user.getUserId());
|
||||
var auditRequest = new ListResetRequestDto();
|
||||
auditRequest.setResetMode(saved.getResetMode());
|
||||
resets.resolveResetScope(projectId, saved);
|
||||
resets.resetAsyncChunk(projectId, new LinkedHashSet<>(present), scope, logId, user.getUserId());
|
||||
var auditRequest = mapper.convertValue(saved, ListResetRequestDto.class);
|
||||
var filter = ListFilterDto.onlyOneSid(present.get(0));
|
||||
filter.setSidSet(new LinkedHashSet<>(present));
|
||||
auditRequest.setListFilter(filter);
|
||||
@@ -79,7 +78,7 @@ public class ListAsyncMaintenanceService {
|
||||
});
|
||||
}
|
||||
context.complete(() -> access.requireProject(projectId, user));
|
||||
}, job -> job.setListResetLogId(resets.createResetLog(projectId, saved.getResetMode(), 0,
|
||||
}, job -> job.setListResetLogId(resets.createResetLog(projectId, scope.mode(), 0,
|
||||
user.getUserId(), job.getRegDt())));
|
||||
return new ListAsyncJobAcceptedDto(key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user