# MarketCodeFileParseImpl.java

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

    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.rateshop.entity.revenue.RevenueDailySummaryTaFilterConfig;
    import cloud.wconnect.rateshop.service.revenue.IRevenueDailySummaryTaFilterConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    import java.io.InputStream;
    import java.util.List;

    /**
     * 解析MarketCode报告
     *
     * @author lichen
     * @date 2024/3/27
     */
    @Component
    @Slf4j
    @DS("rate_shop")
    public class MarketCodeFileParseImpl implements IMarketCodeFileParse{

        @Autowired
        private IRevenueDailySummaryTaFilterConfigService revenueDailySummaryTaFilterConfigService;

        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.OPERA.getDesc().equals(source);
        }

        @Override
        public Integer parse(InputStream in, PropertyDTO propertyDTO) throws ParseReportException {
            List<String> marketCodes = revenueDailySummaryTaFilterConfigService.listByPropertyId(propertyDTO.getId())
                    .stream().map(RevenueDailySummaryTaFilterConfig::getMarketCode).toList();
            if(CollectionUtils.isEmpty(marketCodes)) return null;
            try {
                int roomDay = 0;
                Document document = new SAXReader().read(in);
                List<Node> nodes = document.selectNodes("//G_GRP2");
                if (nodes.isEmpty()) {
                    return roomDay;
                }
                for (Node node : nodes) {
                    Element element = (Element) node;
                    String code = element.elementTextTrim("GRP2_CODE");
                    if (code.isEmpty()) {
                        continue;
                    }
                    if(marketCodes.contains(code)){
                        String roomsDayStr = element.elementTextTrim("ROOMS_DAY");
                        roomDay += StringUtils.isEmpty(roomsDayStr) ? 0 : Integer.parseInt(roomsDayStr);
                    }
                }
                return roomDay;
            } catch (DocumentException e) {
                log.error(e.getMessage(), e);
                throw new ParseReportException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "source=opera, parse [market code/service] report error", e);
            }
        }
    }