# BudgetDatabaseInitConfigServiceImpl.java

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

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.entity.budgetdatabase.*;
    import cloud.wconnect.rateshop.service.budgetdatabase.*;
    import com.baomidou.dynamic.datasource.annotation.DS;
    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.List;

    /**
     *
     * @author: yyt
     * 2025-11-17
     */
    @Service
    @DS("rate_shop")
    public class BudgetDatabaseInitConfigServiceImpl implements IBudgetDatabaseInitConfigService {

        @Lazy
        @Autowired
        private IBudgetDatabaseConfigRoomTabService budgetDatabaseConfigRoomTabService;
        @Autowired
        private IBudgetDatabaseConfigFbTabService budgetDatabaseConfigFbTabService;
        @Autowired
        private IBudgetDatabaseConfigOtherTabService budgetDatabaseConfigOtherTabService;
        @Lazy
        @Autowired
        private IBudgetDatabaseConfigVariableService budgetDatabaseConfigVariableService;

        /**
         * 初始化下一年的预算配置
         *
         * @param openModuleProperty 打开的模块属性
         * @author yyt
         * @date 2025/11/17
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void initNextYearConfig(List<PropertyDTO> openModuleProperty) {
            if(CollectionUtils.isEmpty(openModuleProperty)){
                return;
            }

            for (PropertyDTO propertyDTO : openModuleProperty) {
                // 处理每个资产的数据
                handleProperty(propertyDTO);
            }
        }

        /**
         * 处理资产数据
         *
         * @param propertyDTO 资产属性
         * @author yyt
         * @date 2025/11/17
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void handleProperty(PropertyDTO propertyDTO) {
            Integer propertyId = propertyDTO.getId();

            // 若下一年有配置，则不处理
            int nextYear = DateTimeUtils.dateNow(propertyDTO.getTimezone()).getYear() + 1;

            List<BudgetDatabaseConfigRoomTab> roomTabs = budgetDatabaseConfigRoomTabService.list(propertyId, nextYear);
            List<BudgetDatabaseConfigFbTab> fbTabs = budgetDatabaseConfigFbTabService.list(propertyId, nextYear);
            List<BudgetDatabaseConfigOtherTab> otherTabs = budgetDatabaseConfigOtherTabService.list(propertyId, nextYear);
            if (CollectionUtils.isNotEmpty(roomTabs) || CollectionUtils.isNotEmpty(fbTabs) || CollectionUtils.isNotEmpty(otherTabs)) {
                return;
            }

            // copy room 指标
            List<BudgetDatabaseConfigRoomTabItem> nextYearRoomTabItems = budgetDatabaseConfigRoomTabService.copy(propertyId, nextYear - 1, nextYear);
            // copy fb 指标
            List<BudgetDatabaseConfigFbTabItem> nextYearFbTabItems = budgetDatabaseConfigFbTabService.copy(propertyId, nextYear - 1, nextYear);
            // copy other 指标
            List<BudgetDatabaseConfigOtherTabItem> nextYearOtherTabItems = budgetDatabaseConfigOtherTabService.copy(propertyId, nextYear - 1, nextYear);

            // copy 变量配置
            budgetDatabaseConfigVariableService.copy(propertyId, nextYear - 1, nextYear, nextYearRoomTabItems, nextYearFbTabItems, nextYearOtherTabItems);
        }

    }