# WMRevenueGroupPickupParseServiceByShiji.java

    package cloud.wconnect.rateshop.service.weeklymonthlyrevenue.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.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.CsvFileUtils;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.pojo.bo.weeklymonthlyrevenue.WMRevenueGroupPickupBO;
    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.Component;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.time.LocalDate;
    import java.util.List;

    /**
     * 解析 Shiji Group Pickup 报告
     *
     * @author zc
     * @date 2026/02/07
     */
    @Slf4j
    @Component
    @DS("rate_shop")
    public class WMRevenueGroupPickupParseServiceByShiji implements IWMRevenueGroupPickupParseService {

        /**
         * 支持的报告来源
         *
         * @param source 报告来源
         * @return boolean
         * @author zc
         * @date 2026/2/7
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.SHI_JI.getDesc().equals(source);
        }

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告文件流
         * @param dateRange   报告数据日期范围(yyyyMMdd)
         * @return WMRevenueGroupPickupBO 集合
         * @author zc
         * @date 2026/2/7
         */
        @Override
        public List<WMRevenueGroupPickupBO> parse(PropertyDTO propertyDTO, InputStream in, List<Long> dateRange) throws ParseReportException {
            long startDate = dateRange.get(0);
            long endDate = dateRange.get(1);

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

                // 跳过第一行空行
                int i = 1;

                List<WMRevenueGroupPickupBO> 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;
                    }

                    i += 6;

                    // 处理数据行，直到遇到下一个分组
                    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;
                        }

                        // 忽略掉非指定日期的数据
                        LocalDate localDate = DateTimeUtils.strToLocalDate(nextColA, BaseConstant.DATE_TIME_FORMAT_MM_DD_YY);
                        long theDate = Long.parseLong(DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYYMMDD));
                        // 判断数据日期是否在开始日期 和 结束日期之间
                        if (theDate < startDate || theDate > endDate) {
                            i++;
                            continue;
                        }

                        // 格式化日期为 yyyy-MM-dd
                        String formattedDateStr = DateTimeUtils.localDateToStr(localDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD);

                        // 添加第一个带日期的数据行
                        WMRevenueGroupPickupBO firstBo = new WMRevenueGroupPickupBO();
                        firstBo.setTheDay(formattedDateStr);
                        firstBo.setOrigin(parseInt(dataRecord, "D"));
                        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;
                            }

                            // 添加后续没有日期的数据行
                            WMRevenueGroupPickupBO nextBo = new WMRevenueGroupPickupBO();
                            nextBo.setTheDay(formattedDateStr);
                            nextBo.setOrigin(parseInt(nextRecord, "D"));
                            boList.add(nextBo);

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

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