# ParseHouseStatusServiceByShiji.java

    package cloud.wconnect.frontoffice.parsereport.endofshift.impl;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.util.ShijiFileCheckUtils;
    import cloud.wconnect.frontoffice.parsereport.endofshift.IParseHouseStatusService;
    import cloud.wconnect.frontoffice.pojo.dto.endofshift.report.HouseStatusServiceDTO;
    import cloud.wconnect.frontoffice.service.AmazonS3Service;
    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.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;

    /**
     * 解析Shiji系统的House Status
     */
    @Slf4j
    @Component
    public class ParseHouseStatusServiceByShiji implements IParseHouseStatusService {

        @Autowired
        private AmazonS3Service amazonS3Service;

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

        /**
         * 解析报告
         *
         * @param path 文件
         * @return cloud.wconnect.frontoffice.pojo.dto.endofshift.report.HouseStatusServiceDTO
         * @author lichen
         * @date 2025/7/26
         */
        @Override
        public HouseStatusServiceDTO parse(String path) throws ParseReportException {
            try (InputStream fileInputStream = amazonS3Service.getFile(path)) {
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                //获取CSV数据迭代器
                Iterable<CSVRecord> iterable = CSVFormat.EXCEL.parse(inputStreamReader);

                ArrayList<CSVRecord> allRows = Lists.newArrayList();
                iterable.forEach(allRows::add);

                String vdRoomsStr = "";
                String oosRoomsStr = "";
                for (int i = 0; i < allRows.size(); i++) {
                    CSVRecord csvRecord = allRows.get(i);

                    String title = ShijiFileCheckUtils.get(csvRecord, "J");
                    if (StringUtils.isNotBlank(title) && "Dirty".equals(title)) {
                        CSVRecord nextCsvRecord = allRows.get(i + 1);
                        //空房待清扫
                        vdRoomsStr = ShijiFileCheckUtils.getAndFormatNumber(nextCsvRecord, "L");
                        continue;
                    }
                    if (StringUtils.isNotBlank(title) && "Out of Service".equals(title)) {
                        //无法提供服务的房间
                        oosRoomsStr = ShijiFileCheckUtils.getAndFormatNumber(csvRecord, "L");
                    }
                }
                return new HouseStatusServiceDTO()
                        .setVdRooms(StringUtils.isBlank(vdRoomsStr) ? 0 : Integer.parseInt(vdRoomsStr))
                        .setOosRooms(StringUtils.isBlank(oosRoomsStr) ? 0 : Integer.parseInt(oosRoomsStr));
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse shiji [house status] report error", e);
            }
        }
    }