# PastReservationCreditLimitParseByShiji.java

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

    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.pojo.dto.internationalization.InternationalizationNumberDTO;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.ShijiFileCheckUtils;
    import cloud.wconnect.frontoffice.pojo.bo.pastreservation.CreditLimitInfoBO;
    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.math.BigDecimal;
    import java.math.RoundingMode;
    import java.util.*;

    /**
     * 解析shiji平台Credit Limit Report报告数据
     */
    @Service
    @DS("logbook")
    @Slf4j
    public class PastReservationCreditLimitParseByShiji implements IPastReservationCreditLImitParse {

        /**
         * 判断支持的平台
         *
         * @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 java.util.List<cloud.wconnect.frontoffice.pojo.bo.pastreservation.CreditLimitInfoBO>
         * @author lichen
         * @date 2025/7/29
         */
        @Override
        public List<CreditLimitInfoBO> parsReport(InputStream inputStream, PropertyDTO propertyDTO) {
            InternationalizationNumberDTO number = propertyDTO.getInternationalization().getNumber();
            String calculationRule = number.getCalculationRule();
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(calculationRule);

            List<CreditLimitInfoBO> creditLimitInfoBOS = Lists.newArrayList();
            try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream)) {
                //获取CSV数据迭代器
                Iterable<CSVRecord> iterable = CSVFormat.EXCEL.parse(inputStreamReader);
                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, "C").trim();
                    //总消费金额
                    String totalBalance = ShijiFileCheckUtils.getAndFormatNumber(csvRecord, "L");
                    //核准总金额
                    String totalApprovalAmount = ShijiFileCheckUtils.getAndFormatNumber(csvRecord, "J");

                    CreditLimitInfoBO creditLimitInfo = new CreditLimitInfoBO();
                    creditLimitInfo.setRoomNumber(roomNumber);
                    creditLimitInfo.setFullName(guestName);
                    creditLimitInfo.setTotalBalance(StringUtils.isEmpty(totalBalance) ? BigDecimal.ZERO :
                            new BigDecimal(totalBalance).setScale(BaseConstant.DATA_PRECISION, roundingMode));
                    creditLimitInfo.setTotalApprovalAmount(StringUtils.isEmpty(totalApprovalAmount) ? BigDecimal.ZERO :
                            new BigDecimal(totalApprovalAmount).setScale(BaseConstant.DATA_PRECISION, roundingMode));
                    creditLimitInfoBOS.add(creditLimitInfo);
                }
                return creditLimitInfoBOS;
            } catch (IOException e) {
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(), "parse shiji [Credit Limit Report] report error." + e, e);
            }
        }

    }