# PastReservationGuestInHouseParseByShiji.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.DateTimeUtils;
    import cloud.wconnect.commonlib.util.ShijiFileCheckUtils;
    import cloud.wconnect.frontoffice.pojo.bo.pastreservation.GuestInHouseBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.csv.CSVFormat;
    import org.apache.commons.csv.CSVRecord;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.*;
    import java.util.stream.Collectors;

    /**
     * 解析shiji平台PastReservation Guest In House Report
     */
    @Service
    @DS("logbook")
    @Slf4j
    public class PastReservationGuestInHouseParseByShiji implements IPastReservationGuestInHouseParse {

        /**
         * 判断支持的平台
         *
         * @param source 平台数据源
         * @return boolean
         * @author lichen
         * @date 2025/7/29
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.SHI_JI.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 (InputStreamReader inputStreamReader = new InputStreamReader(inputStream)) {
                //获取CSV数据迭代器
                Iterable<CSVRecord> iterable = CSVFormat.EXCEL.parse(inputStreamReader);

                //构建返回集合
                List<GuestInHouseBO> guestInHouses = Lists.newArrayList();

                for (CSVRecord csvRecord : iterable) {
                    //跳过第一行标题
                    if (csvRecord.getRecordNumber() == 1) {
                        continue;
                    }
                    //房间号
                    String roomNumber = ShijiFileCheckUtils.get(csvRecord, "A");
                    if(StringUtils.isBlank(roomNumber)){
                        continue;
                    }
                    //姓名
                    String guestName = ShijiFileCheckUtils.get(csvRecord, "B");

                    //到店日期
                    String arrivalDateStr = ShijiFileCheckUtils.get(csvRecord, "C");
                    if (StringUtils.isBlank(arrivalDateStr)) {
                        continue;
                    }
                    //截取日期
                    arrivalDateStr = arrivalDateStr.trim().substring(0,8);
                    //到店日期，格式yyyy-MM-dd
                    String arrivalDate = DateTimeUtils.localDateToStr(DateTimeUtils.strToLocalDate(arrivalDateStr, BaseConstant.DATE_TIME_FORMAT_MM_DD_YY),
                            BaseConstant.DATE_FORMAT_YYYY_MM_DD);
                    //离店日期
                    String departureDateStr = ShijiFileCheckUtils.get(csvRecord, "D");
                    if (StringUtils.isBlank(departureDateStr)) {
                        continue;
                    }
                    departureDateStr = departureDateStr.trim().substring(0,8);
                    //格式化离店日期
                    String departureDate = DateTimeUtils.localDateToStr(DateTimeUtils.strToLocalDate(departureDateStr, BaseConstant.DATE_TIME_FORMAT_MM_DD_YY),
                            BaseConstant.DATE_FORMAT_YYYY_MM_DD);

                    GuestInHouseBO guestInHouse = new GuestInHouseBO();
                    guestInHouse.setFullName(guestName);
                    //设置First Name Last Name
                    String[] fullNames = guestName.split(", ");
                    if (fullNames.length >= 2) {
                        guestInHouse.setFirstName(fullNames[1]);
                        guestInHouse.setLastName(fullNames[0]);
                    } else {
                        guestInHouse.setLastName(fullNames[0]);
                        guestInHouse.setFirstName("");
                    }
                    guestInHouse.setCheckInDate(arrivalDate);
                    guestInHouse.setCheckOutDate(departureDate);
                    guestInHouse.setRoomNumber(StringUtils.isBlank(roomNumber) ? "" : roomNumber);
                    guestInHouses.add(guestInHouse);
                }

                // 数据去重（姓名、房间号、入住日期）
                return guestInHouses.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                        new TreeSet<>(Comparator.comparing(GuestInHouseBO::getUniKey))), ArrayList::new));
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse shiji [In-House Guest Report] report error." + e, e);
            }

        }

    }