# PastReservationGuestInHouseParseByOpera.java

    package cloud.wconnect.frontoffice.service.pastreservation.report.listener.guestinhouse;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.GuestNameUtil;
    import cloud.wconnect.frontoffice.pojo.bo.pastreservation.GuestInHouseBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;
    import org.springframework.stereotype.Service;

    import java.io.InputStream;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.*;
    import java.util.stream.Collectors;

    /**
     * @author yyt
     * 2024/10/3
     */
    @Service
    @DS("logbook")
    public class PastReservationGuestInHouseParseByOpera implements IPastReservationGuestInHouseParse {

        /**
         * 判断支持的平台
         *
         * @param source 平台数据源
         * @return boolean
         * @author yyt
         * @date 2024/10/3
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.OPERA.getDesc().equals(source);
        }

        /**
         * 解析报告数据
         *
         * @param inputStream 文件流
         * @param propertyDTO 资产
         * @return guest in house
         * @author yyt
         * @date 2024/10/3
         */
        @Override
        public List<GuestInHouseBO> parsReport(InputStream inputStream, PropertyDTO propertyDTO) {
            try {
                Document document = new SAXReader().read(inputStream);
                List<Node> nodes = document.selectNodes("//G_ROOM");
                if (nodes.isEmpty()) {
                    return Collections.emptyList();
                }

                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yy");
                DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYY_MM_DD);
                List<GuestInHouseBO> guestInHouses = Lists.newArrayListWithExpectedSize(nodes.size());
                for (Node node : nodes) {
                    Element element = (Element) node;
                    GuestInHouseBO guestInHouse = new GuestInHouseBO();
                    guestInHouse.setFullName(element.elementText("FULL_NAME").trim());

                    List<String> names = GuestNameUtil.handleGuestNameForGuestInHouseReport(element.elementText("GUEST_NAME"));
                    if (CollectionUtils.isEmpty(names)) {
                        continue;
                    }
                    guestInHouse.setFirstName(names.get(0));
                    guestInHouse.setLastName(names.get(1));

                    String room = element.elementText("ROOM");
                    guestInHouse.setRoomNumber(Objects.isNull(room) ? StringUtils.EMPTY : room.trim());
                    guestInHouse.setCheckInDate(dateTimeFormatter2.format(LocalDate.parse(element.elementText("ARRIVAL"), dateTimeFormatter)));
                    guestInHouse.setCheckOutDate(dateTimeFormatter2.format(LocalDate.parse(element.elementText("DEPARTURE"), dateTimeFormatter)));
                    guestInHouses.add(guestInHouse);
                }
                if (CollectionUtils.isEmpty(guestInHouses)) {
                    return Collections.emptyList();
                }

                // 数据去重（姓名、房间号、入住日期）
                return guestInHouses.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                        new TreeSet<>(Comparator.comparing(GuestInHouseBO::getUniKey))), ArrayList::new));
            } catch (Exception e) {
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse guest in house report error." + e, e);
            }
        }

    }