수신 파일 처리 시 날짜 / 건수 조건 추가

master
mjkhan21 2 months ago
parent 7d30471651
commit f5efb041d5

@ -2,66 +2,112 @@ package cokr.xit.interfaces.lntris;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Path; import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import cokr.xit.interfaces.filejob.service.bean.FileJobBean; import cokr.xit.interfaces.filejob.service.bean.FileJobBean;
public abstract class InterfaceInfoReader<T extends InterfaceInfo<?, ?>> extends FileJobBean { public abstract class InterfaceInfoReader<T extends InterfaceInfo<?, ?>> extends FileJobBean {
private DataFileSupport<T> reader = new DataFileSupport<T>()
.setSupplier(interfaceInfoSupplier())
.setCharset(Charset.forName("UTF-8"));
protected abstract Supplier<T> interfaceInfoSupplier(); protected abstract Supplier<T> interfaceInfoSupplier();
protected abstract Consumer<T> insertInterfaceInfo(); protected abstract Consumer<T> insertInterfaceInfo();
private String receiptDates() {
return config("receiptDates");
}
private int receiptCount() {
String val = config("receiptCount");
int count = Integer.parseInt(ifEmpty(val, "0"));
return Math.max(0, count);
}
private boolean contains(String str, List<String> tokens) {
for (String token: tokens)
if (str.contains(token))
return true;
return false;
}
private List<String> getHeads(String[] dates) {
String interfaceID = interfaceInfoSupplier().get().interfaceID(),
head = interfaceID + "_";
if (!isEmpty(dates))
return Stream.of(dates)
.map(date -> {
return head + date;
})
.sorted()
.toList();
try {
String rctDates = receiptDates();
if (!isEmpty(rctDates)) {
int offset = Integer.parseInt(rctDates);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
return getDates(new Date(), offset).stream()
.map(date -> head + dateFormat.format(date))
.sorted()
.toList();
} else
return List.of(head);
} catch (Exception e) {
throw runtimeException(e);
}
}
/** . /** .
* . * .
* @param orgs * @param orgs
* @param sysCodes * @param sysCodes
* @param dates
*/ */
public List<T> read(String[] orgs, String... sysCodes) { public List<T> read(String[] orgs, String[] sysCodes, String... dates) {
String interfaceID = interfaceInfoSupplier().get().interfaceID(); List<String>
List<String> tails = InterfaceConfig.locals(orgs).stream() heads = getHeads(dates),
tails = InterfaceConfig.locals(orgs).stream()
.flatMap(org -> org.getOrganizationSystems(sysCodes).stream()) .flatMap(org -> org.getOrganizationSystems(sysCodes).stream())
.map(orgSys -> "@" + orgSys) .map(orgSys -> "@" + orgSys)
.toList(); .toList();
log().debug("Reading\n{}", String.join("\n", heads.stream().map(head -> "\t" + head + "*.*").toList()));
List<Path> paths = getReceivedFilePaths(path -> { ArrayList<T> result = new ArrayList<>();
String str = path.toString();
for (String tail: tails) {
if (str.contains(interfaceID) && str.contains(tail))
return true;
}
return false;
});
if (paths.isEmpty()) return Collections.emptyList(); while (true) {
List<T> processed = processReceived(heads, tails);
if (processed.isEmpty()) break;
List<FileStatus> fileStatus = processReceived(paths); result.addAll(processed);
}
Map<Boolean, List<FileStatus>> successFail = fileStatus.stream() //처리 결과를 성공 / 실패로 분류 return result;
.collect(Collectors.groupingBy(file -> file.isSuccess())); }
List<FileStatus> success = successFail.get(true); protected List<T> processReceived(List<String> heads, List<String> tails) {
move(FileStatus.getPaths(success), successDir()); // 성공 디렉토리로 이동 List<Path> paths = getReceivedFilePaths(
move(FileStatus.getPaths(successFail.get(false)), failDir()); // 실패 디렉토리로 이동 path -> {
String str = path.toString();
return isEmpty(success) ? return contains(str, heads)
Collections.emptyList() : && contains(str, tails);
success.stream().flatMap(status -> ((List<T>)status.get("messages")).stream()).toList(); },
} receiptCount()
);
if (paths.isEmpty()) return Collections.emptyList();
protected List<FileStatus> processReceived(List<Path> paths) { List<FileStatus> fileStatus = paths.stream()
DataFileSupport<T> reader = new DataFileSupport<T>()
.setSupplier(interfaceInfoSupplier())
.setCharset(Charset.forName("UTF-8"));
return isEmpty(paths) ?
Collections.emptyList() :
paths.stream()
.map(path -> { .map(path -> {
FileStatus status = new FileStatus().setPath(path); FileStatus status = new FileStatus().setPath(path);
try { try {
@ -81,5 +127,16 @@ public abstract class InterfaceInfoReader<T extends InterfaceInfo<?, ?>> extends
return status; return status;
}) })
.toList(); .toList();
Map<Boolean, List<FileStatus>> successFail = fileStatus.stream() //처리 결과를 성공 / 실패로 분류
.collect(Collectors.groupingBy(FileStatus::isSuccess));
List<FileStatus> success = successFail.get(true);
move(FileStatus.getPaths(success), successDir()); // 성공 디렉토리로 이동
move(FileStatus.getPaths(successFail.get(false)), failDir()); // 실패 디렉토리로 이동
return isEmpty(success) ?
Collections.emptyList() :
success.stream().flatMap(status -> ((List<T>)status.get("messages")).stream()).toList();
} }
} }
Loading…
Cancel
Save