# M3MappingServiceImpl.java

    package cloud.wconnect.finance.service.m3.impl;

    import cloud.wconnect.finance.entity.m3.M3Mapping;
    import cloud.wconnect.finance.mapper.m3.M3MappingMapper;
    import cloud.wconnect.finance.service.m3.IM3MappingService;
    import cloud.wconnect.finance.service.pl.PlCommonService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.stream.Collectors;

    /**
     * <p>
     * iwc字段和m3系统的GL Code关系表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2025-10-14
     */
    @Service
    @DS("finance")
    public class M3MappingServiceImpl extends ServiceImpl<M3MappingMapper, M3Mapping> implements IM3MappingService {

        /**
         * 查询资产配置指标公式中使用的accountNumber集合
         *
         * @param propertyId 资产ID
         * @return java.util.List<java.lang.String>
         * @author lichen
         * @date 2025/10/17
         */
        @Override
        public List<String> listFormulaUsedAccountNumber(Integer propertyId) {
            List<M3Mapping> m3Mappings = listByPropertyId(propertyId);
            if (CollectionUtils.isEmpty(m3Mappings)) return Collections.emptyList();

            HashSet<String> set = new HashSet<>();

            for (M3Mapping m3Mapping : m3Mappings) {
                //解析公式中使用的accountNumber
                List<String> accountNumbers = parseFormulaUsedAccountNumber(m3Mapping.getFormula());
                set.addAll(accountNumbers);
            }
            return new ArrayList<>(set);
        }

        /**
         * 根据资产ID查询资产配置指标和M3系统的GL Code关系
         *
         * @param propertyId 资产ID
         * @return java.util.List<cloud.wconnect.finance.entity.m3.M3Mapping>
         * @author lichen
         * @date 2025/10/17
         */
        @Override
        public List<M3Mapping> listByPropertyId(Integer propertyId) {
            return list(new LambdaUpdateWrapper<M3Mapping>().eq(M3Mapping::getPropertyId, propertyId));
        }

        /**
         * 解析指标公式中用到的AccountNumber
         *
         * @param formula 指标公式
         * @return java.util.List<java.lang.String>
         * @author lichen
         * @date 2025/10/14
         */
        public List<String> parseFormulaUsedAccountNumber(String formula) {
            if (StringUtils.isBlank(formula)) return Collections.emptyList();
            ArrayList<String> result = Lists.newArrayList();

            //匹配花括号 {} 及其内部
            Matcher matcher = PlCommonService.CURLY_BRACES_PATTERN.matcher(formula);
            while (matcher.find()) {
                //花括号内部内容
                String dependency = matcher.group(1);
                // 正则匹配：”数字“ 格式
                if (dependency.matches("\\d+")) {
                    result.add(dependency);
                }
            }

            return result.stream().distinct().collect(Collectors.toList());
        }

        /**
         * 根据指标名称查询指标配置
         *
         * @param propertyId 资产ID
         * @param indexName  指标名称
         * @return
         */
        @Override
        public M3Mapping listByIndexName(int propertyId, String indexName) {
            return getOne(new LambdaUpdateWrapper<M3Mapping>().eq(M3Mapping::getPropertyId, propertyId)
                    .eq(M3Mapping::getFieldCode, indexName));
        }

    }