# GroupPickupFileParseImplByShiji.java

    package cloud.wconnect.rateshop.service.revenue.grouppickup;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.bo.ReportDataBO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.CsvFileUtils;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.cons.rateshop.BookingStatusForOperaGroupPicupEnum;
    import cloud.wconnect.rateshop.pojo.bo.revenue.DailySummaryGroupPickupBO;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.apache.commons.collections.CollectionUtils;
    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.List;

    /**
     * Shiji Group Pickup 报告解析
     *
     * @author zc
     * @date 2026/02/09
     */
    @Service
    @DS("rate_shop")
    public class GroupPickupFileParseImplByShiji implements IGroupPickupFileParse {

        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;

        /**
         * 获取支持的平台
         *
         * @param source 平台
         * @return boolean
         * @author zc
         * @date 2026/02/09
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.SHI_JI.getDesc().equals(source);
        }

        /**
         * 解析报告 group pickup 数据
         *
         * @param propertyId 资产 id
         * @param timeZone   时区
         * @param in         文件流
         * @param startDate  开始日期（时间戳）
         * @param endDate    结束日期（时间戳）
         * @return group pickup
         * @author zc
         * @date 2026/02/09
         */
        @Override
        public ReportDataBO<DailySummaryGroupPickupBO> parseReport(Integer propertyId, String timeZone, InputStream in,
                                                                   Long startDate, Long endDate) throws Exception {
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            String timezone = propertyDTO.getTimezone();

            String groupTarget = "Group Name:";
            try (InputStreamReader inputStreamReader = new InputStreamReader(in)) {
                // 读取所有记录到List
                List<CSVRecord> records = CSVFormat.EXCEL.parse(inputStreamReader).getRecords();
                int i = 1;

                List<DailySummaryGroupPickupBO> boList = Lists.newArrayList();

                while (i < records.size()) {
                    CSVRecord titleRecord_1 = records.get(i);
                    String colA = CsvFileUtils.get(titleRecord_1, "A");
                    if (!groupTarget.equals(colA)) {
                        i++;
                        continue;
                    }

                    // 跳过 "Group Name:" 行
                    i++;
                    CSVRecord titleRecord_2 = records.get(i);

                    // 获取 Booking Status
                    String bookingStatusStr = CsvFileUtils.get(titleRecord_2, "K");
                    if (StringUtils.isBlank(bookingStatusStr)) {
                        i++;
                        continue;
                    }
                    // 转换为 Booking Status Code
                    int bookingStatus = BookingStatusForOperaGroupPicupEnum.getCodeBySymbol(bookingStatusStr);
                    // 忽略无效的 Booking Status
                    if (bookingStatus == BaseConstant.ZERO) {
                        i++;
                        continue;
                    }

                    // 获取 Group Name
                    String groupNameStr = CsvFileUtils.get(titleRecord_2, "A");

                    // 跳过标题行
                    i += 5;

                    // 处理数据行，直到遇到下一个分组
                    while (i < records.size()) {
                        CSVRecord dataRecord = records.get(i);

                        // 检查是否到了下一个分组（A列是"Group Name:"）
                        String nextColA = CsvFileUtils.get(dataRecord, "A");
                        if (groupTarget.equals(nextColA)) {
                            break; // 不增加i，让外层循环处理
                        }

                        // 跳过空日期的数据行
                        if (StringUtils.isBlank(nextColA)) {
                            i++;
                            continue;
                        }

                        // 忽略掉非指定日期的数据
                        long dateTimestamp = DateTimeUtils.dateStrToEpochSecond(nextColA,
                                BaseConstant.DATE_TIME_FORMAT_MM_DD_YY, timezone);
                        if (dateTimestamp < startDate || dateTimestamp > endDate) {
                            i++;
                            continue;
                        }

                        // 添加第一个带日期的数据行
                        DailySummaryGroupPickupBO firstBo = new DailySummaryGroupPickupBO();
                        firstBo.setGroupName(groupNameStr);
                        firstBo.setBookingStatus(bookingStatus);
                        firstBo.setRoomPickup(parseInt(dataRecord, "F"));

                        boList.add(firstBo);

                        i++;

                        // 继续处理后续没有日期的数据行（属于同一天）
                        while (i < records.size()) {
                            CSVRecord nextRecord = records.get(i);

                            // 如果遇到新日期或新分组，则跳出循环
                            String nextColACheck = CsvFileUtils.get(nextRecord, "A");
                            if (groupTarget.equals(nextColACheck) || StringUtils.isNotBlank(nextColACheck)) {
                                break;
                            }

                            // 添加后续没有日期的数据行
                            DailySummaryGroupPickupBO nextBo = new DailySummaryGroupPickupBO();
                            nextBo.setGroupName(groupNameStr);
                            nextBo.setBookingStatus(bookingStatus);
                            nextBo.setRoomPickup(parseInt(nextRecord, "F"));

                            boList.add(nextBo);

                            i++;
                        }
                    }
                }
                return new ReportDataBO<>(CollectionUtils.isNotEmpty(boList), boList);
            } catch (IOException e) {
                throw new BadRequestException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Parse Shiji [Group Pickup] report error", e);
            }
        }

        /**
         * 解析数字列
         *
         * @param record CSV记录
         * @param column 列名
         * @return int
         * @author zc
         * @date 2026/2/5
         */
        private int parseInt(CSVRecord record, String column) {
            String value = CsvFileUtils.getAndFormatNumber(record, column);
            if (StringUtils.isBlank(value)) return 0;
            return Integer.parseInt(value);
        }
    }