# BudgetDatabaseConfigVariableFbServiceImpl.java

    package cloud.wconnect.rateshop.service.budgetdatabase.impl;

    import cloud.wconnect.rateshop.entity.budgetdatabase.BudgetDatabaseConfigFbTabItem;
    import cloud.wconnect.rateshop.entity.budgetdatabase.BudgetDatabaseConfigVariableFb;
    import cloud.wconnect.rateshop.mapper.budgetdatabase.BudgetDatabaseConfigVariableFbMapper;
    import cloud.wconnect.rateshop.service.budgetdatabase.IBudgetDatabaseConfigFbTabItemService;
    import cloud.wconnect.rateshop.service.budgetdatabase.IBudgetDatabaseConfigVariableFbService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Lazy;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2025-11-10
     */
    @Service
    @DS("rate_shop")
    public class BudgetDatabaseConfigVariableFbServiceImpl extends ServiceImpl<BudgetDatabaseConfigVariableFbMapper, BudgetDatabaseConfigVariableFb> implements IBudgetDatabaseConfigVariableFbService {

        @Lazy
        @Autowired
        private IBudgetDatabaseConfigFbTabItemService budgetDatabaseConfigFbTabItemService;

        /**
         * 查询变量配置下的指标
         *
         * @param variableIds 变量 id
         * @return 指标
         * @author yyt
         * @date 2025/11/10
         **/
        @Override
        public List<BudgetDatabaseConfigVariableFb> listByVariableIds(List<Integer> variableIds) {
            if (CollectionUtils.isEmpty(variableIds)) {
                return Collections.emptyList();
            }

            return list(new QueryWrapper<BudgetDatabaseConfigVariableFb>().lambda().in(BudgetDatabaseConfigVariableFb::getVariableId, variableIds));
        }

        /**
         * 检查变量配置是否已经绑定过此指标配置
         *
         * @param propertyId 资产 id
         * @param variableId 变量 id
         * @param configId   指标配置 id
         * @return true：已绑定，false：未绑定
         * @author yyt
         * @date 2025/11/10
         **/
        @Override
        public boolean checkBoundToTheConfig(Integer propertyId, Integer variableId, Integer configId) {
            return count(new QueryWrapper<BudgetDatabaseConfigVariableFb>().lambda()
                    .eq(BudgetDatabaseConfigVariableFb::getVariableId, variableId)
                    .eq(BudgetDatabaseConfigVariableFb::getItemId, configId)) > 0;
        }

        /**
         * 查询变量配置下的指标
         *
         * @param variableId 变量 id
         * @return 指标
         * @author yyt
         * @date 2025/11/10
         **/
        @Override
        public List<BudgetDatabaseConfigVariableFb> listByVariableId(Integer variableId) {
            return listByVariableIds(Collections.singletonList(variableId));
        }

        /**
         * 检查指标配置是否已经绑定过此变量配置
         *
         * @param propertyId 资产 id
         * @param configId 指标配置 id
         * @return true：已绑定，false：未绑定
         * @author yyt
         * @date 2025/11/11
         **/
        @Override
        public boolean checkConfigBoundToTheVariable(Integer propertyId, Integer configId) {
            return count(new QueryWrapper<BudgetDatabaseConfigVariableFb>().lambda()
                    .eq(BudgetDatabaseConfigVariableFb::getItemId, configId)) > 0;
        }

        /**
         * 检查 tab 是否已经绑定过变量配置
         *
         * @param tabId tab id
         * @return true：已绑定，false：未绑定
         * @author yyt
         * @date 2025/11/11
         **/
        @Override
        public boolean checkTabBoundToTheVariable(int tabId) {
            return count(new QueryWrapper<BudgetDatabaseConfigVariableFb>().lambda()
                    .eq(BudgetDatabaseConfigVariableFb::getTabId, tabId)) > 0;
        }

        /**
         * 保存变量配置下的指标
         *
         * @param propertyId 资产 id
         * @param variableId 变量 id
         * @param itemIds    指标 id
         * @author yyt
         * @date 2025/11/11
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(Integer propertyId, Integer variableId, List<Integer> itemIds) {
            // 先清除，再新增
            remove(new QueryWrapper<BudgetDatabaseConfigVariableFb>().lambda().eq(BudgetDatabaseConfigVariableFb::getVariableId, variableId));

            if (CollectionUtils.isNotEmpty(itemIds)) {
                Map<Integer, Integer> tabIdById = budgetDatabaseConfigFbTabItemService.listByIds(itemIds)
                        .stream().collect(Collectors.toMap(BudgetDatabaseConfigFbTabItem::getId, BudgetDatabaseConfigFbTabItem::getFbTabId));
                saveBatch(itemIds.stream().map(itemId -> new BudgetDatabaseConfigVariableFb().setPropertyId(propertyId)
                        .setVariableId(variableId).setTabId(tabIdById.get(itemId)).setItemId(itemId)).toList());
            }
        }

        /**
         * 批量查询指标关联的变量配置
         *
         * @param itemIds 指标 id
         * @return 批量查询指标关联的变量配置
         * @author yyt
         * @date 2025/11/12
         **/
        @Override
        public List<BudgetDatabaseConfigVariableFb> listByItemIds(List<Integer> itemIds) {
            return list(new QueryWrapper<BudgetDatabaseConfigVariableFb>().lambda().in(BudgetDatabaseConfigVariableFb::getItemId, itemIds));
        }

        /**
         * 查询资产下的变量配置
         *
         * @param propertyId 资产 id
         * @return 资产下的变量配置
         * @author yyt
         * @date 2025/11/12
         **/
        @Override
        public List<BudgetDatabaseConfigVariableFb> listByPropertyId(Integer propertyId) {
            return list(new QueryWrapper<BudgetDatabaseConfigVariableFb>().lambda().eq(BudgetDatabaseConfigVariableFb::getPropertyId, propertyId));
        }

    }