diff --git a/src/main/java/cokr/xit/interfaces/lntris/InterfaceInfoReader.java b/src/main/java/cokr/xit/interfaces/lntris/InterfaceInfoReader.java index fdc6143..f1b1706 100644 --- a/src/main/java/cokr/xit/interfaces/lntris/InterfaceInfoReader.java +++ b/src/main/java/cokr/xit/interfaces/lntris/InterfaceInfoReader.java @@ -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> extends FileJobBean { + private DataFileSupport reader = new DataFileSupport() + .setSupplier(interfaceInfoSupplier()) + .setCharset(Charset.forName("UTF-8")); + protected abstract Supplier interfaceInfoSupplier(); protected abstract Consumer 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 tokens) { + for (String token: tokens) + if (str.contains(token)) + return true; + return false; + } + + private List 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 read(String[] orgs, String... sysCodes) { - String interfaceID = interfaceInfoSupplier().get().interfaceID(); - List tails = InterfaceConfig.locals(orgs).stream() - .flatMap(org -> org.getOrganizationSystems(sysCodes).stream()) - .map(orgSys -> "@" + orgSys) - .toList(); + public List read(String[] orgs, String[] sysCodes, String... dates) { + List + 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 paths = getReceivedFilePaths(path -> { - String str = path.toString(); + ArrayList result = new ArrayList<>(); - for (String tail: tails) { - if (str.contains(interfaceID) && str.contains(tail)) - return true; - } + while (true) { + List processed = processReceived(heads, tails); + if (processed.isEmpty()) break; + + result.addAll(processed); + } + + return result; + } - return false; - }); + protected List processReceived(List heads, List tails) { + List paths = getReceivedFilePaths( + path -> { + String str = path.toString(); + return contains(str, heads) + && contains(str, tails); + }, + receiptCount() + ); if (paths.isEmpty()) return Collections.emptyList(); - List fileStatus = processReceived(paths); + List fileStatus = paths.stream() + .map(path -> { + FileStatus status = new FileStatus().setPath(path); + try { + reader + .read(path) + .getMessages() + .forEach(info -> { + InterfaceConfig.databaseActive(() -> insertInterfaceInfo().accept(info)); + List msgs = (List)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> successFail = fileStatus.stream() //처리 결과를 성공 / 실패로 분류 - .collect(Collectors.groupingBy(file -> file.isSuccess())); + .collect(Collectors.groupingBy(FileStatus::isSuccess)); List success = successFail.get(true); move(FileStatus.getPaths(success), successDir()); // 성공 디렉토리로 이동 @@ -54,32 +139,4 @@ public abstract class InterfaceInfoReader> extends Collections.emptyList() : success.stream().flatMap(status -> ((List)status.get("messages")).stream()).toList(); } - - protected List processReceived(List paths) { - DataFileSupport reader = new DataFileSupport() - .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 msgs = (List)status.get("messages"); - if (msgs == null) - status.set("messages", msgs = new ArrayList<>()); - msgs.add(info); - }); - } catch (Exception e) { - status.setCause(e); - } - return status; - }) - .toList(); - } } \ No newline at end of file