|
|
|
@ -2,66 +2,112 @@ 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()
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
for (String tail: tails) {
|
|
|
|
|
if (str.contains(interfaceID) && str.contains(tail))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
ArrayList<T> result = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
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() //처리 결과를 성공 / 실패로 분류
|
|
|
|
|
.collect(Collectors.groupingBy(file -> file.isSuccess()));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<FileStatus> success = successFail.get(true);
|
|
|
|
|
move(FileStatus.getPaths(success), successDir()); // 성공 디렉토리로 이동
|
|
|
|
|
move(FileStatus.getPaths(successFail.get(false)), failDir()); // 실패 디렉토리로 이동
|
|
|
|
|
protected List<T> processReceived(List<String> heads, List<String> tails) {
|
|
|
|
|
List<Path> paths = getReceivedFilePaths(
|
|
|
|
|
path -> {
|
|
|
|
|
String str = path.toString();
|
|
|
|
|
|
|
|
|
|
return isEmpty(success) ?
|
|
|
|
|
Collections.emptyList() :
|
|
|
|
|
success.stream().flatMap(status -> ((List<T>)status.get("messages")).stream()).toList();
|
|
|
|
|
}
|
|
|
|
|
return contains(str, heads)
|
|
|
|
|
&& contains(str, tails);
|
|
|
|
|
},
|
|
|
|
|
receiptCount()
|
|
|
|
|
);
|
|
|
|
|
if (paths.isEmpty()) return Collections.emptyList();
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
List<FileStatus> fileStatus = paths.stream()
|
|
|
|
|
.map(path -> {
|
|
|
|
|
FileStatus status = new FileStatus().setPath(path);
|
|
|
|
|
try {
|
|
|
|
@ -81,5 +127,16 @@ public abstract class InterfaceInfoReader<T extends InterfaceInfo<?, ?>> extends
|
|
|
|
|
return status;
|
|
|
|
|
})
|
|
|
|
|
.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();
|
|
|
|
|
}
|
|
|
|
|
}
|