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

master
mjkhan21 2 months ago
parent 7d30471651
commit f5efb041d5

@ -2,49 +2,134 @@ package cokr.xit.interfaces.lntris;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import cokr.xit.interfaces.filejob.service.bean.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 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 sysCodes
* @param dates
*/
public List<T> read(String[] orgs, String... sysCodes) {
String interfaceID = interfaceInfoSupplier().get().interfaceID();
List<String> tails = InterfaceConfig.locals(orgs).stream()
.flatMap(org -> org.getOrganizationSystems(sysCodes).stream())
.map(orgSys -> "@" + orgSys)
.toList();
public List<T> read(String[] orgs, String[] sysCodes, String... dates) {
List<String>
heads = getHeads(dates),
tails = InterfaceConfig.locals(orgs).stream()
.flatMap(org -> org.getOrganizationSystems(sysCodes).stream())
.map(orgSys -> "@" + orgSys)
.toList();
log().debug("Reading\n{}", String.join("\n", heads.stream().map(head -> "\t" + head + "*.*").toList()));
List<Path> paths = getReceivedFilePaths(path -> {
String str = path.toString();
ArrayList<T> result = new ArrayList<>();
for (String tail: tails) {
if (str.contains(interfaceID) && str.contains(tail))
return true;
}
while (true) {
List<T> processed = processReceived(heads, tails);
if (processed.isEmpty()) break;
result.addAll(processed);
}
return result;
}
return false;
});
protected List<T> processReceived(List<String> heads, List<String> tails) {
List<Path> paths = getReceivedFilePaths(
path -> {
String str = path.toString();
return contains(str, heads)
&& contains(str, tails);
},
receiptCount()
);
if (paths.isEmpty()) return Collections.emptyList();
List<FileStatus> fileStatus = processReceived(paths);
List<FileStatus> fileStatus = paths.stream()
.map(path -> {
FileStatus status = new FileStatus().setPath(path);
try {
reader
.read(path)
.getMessages()
.forEach(info -> {
InterfaceConfig.databaseActive(() -> insertInterfaceInfo().accept(info));
List<T> msgs = (List<T>)status.get("messages");
if (msgs == null)
status.set("messages", msgs = new ArrayList<>());
msgs.add(info);
});
} catch (Exception e) {
status.setCause(e);
}
return status;
})
.toList();
Map<Boolean, List<FileStatus>> successFail = fileStatus.stream() //처리 결과를 성공 / 실패로 분류
.collect(Collectors.groupingBy(file -> file.isSuccess()));
.collect(Collectors.groupingBy(FileStatus::isSuccess));
List<FileStatus> success = successFail.get(true);
move(FileStatus.getPaths(success), successDir()); // 성공 디렉토리로 이동
@ -54,32 +139,4 @@ public abstract class InterfaceInfoReader<T extends InterfaceInfo<?, ?>> extends
Collections.emptyList() :
success.stream().flatMap(status -> ((List<T>)status.get("messages")).stream()).toList();
}
protected List<FileStatus> processReceived(List<Path> paths) {
DataFileSupport<T> reader = new DataFileSupport<T>()
.setSupplier(interfaceInfoSupplier())
.setCharset(Charset.forName("UTF-8"));
return isEmpty(paths) ?
Collections.emptyList() :
paths.stream()
.map(path -> {
FileStatus status = new FileStatus().setPath(path);
try {
reader
.read(path)
.getMessages()
.forEach(info -> {
InterfaceConfig.databaseActive(() -> insertInterfaceInfo().accept(info));
List<T> msgs = (List<T>)status.get("messages");
if (msgs == null)
status.set("messages", msgs = new ArrayList<>());
msgs.add(info);
});
} catch (Exception e) {
status.setCause(e);
}
return status;
})
.toList();
}
}
Loading…
Cancel
Save