# M3ForecastsNameServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.pojo.dto.m3Config.PropertyM3ConfigDTO;
    import cloud.wconnect.commonlib.pojo.dto.m3Config.TenantM3ConfigDTO;
    import cloud.wconnect.commonlib.util.YearMonthUtils;
    import cloud.wconnect.finance.cons.pl.M3DataTypeEnum;
    import cloud.wconnect.finance.entity.pl.M3ForecastName;
    import cloud.wconnect.finance.mapper.pl.M3ForecastsNameMapper;
    import cloud.wconnect.finance.pojo.dto.pl.m3api.ForecastNameDTO;
    import cloud.wconnect.finance.service.pl.IM3ForecastsNameService;
    import cloud.wconnect.finance.service.pl.M3ApiService;
    import cloud.wconnect.finance.service.pl.PlCommonService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.List;

    /**
     * <p>
     * 按年存储M3系统forecast name数据 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2025-09-25
     */
    @Service
    @DS("finance")
    public class M3ForecastsNameServiceImpl extends ServiceImpl<M3ForecastsNameMapper, M3ForecastName> implements IM3ForecastsNameService {

        @Autowired
        private M3ApiService m3ApiService;
        @Autowired
        private PlCommonService plCommonService;
        @Autowired
        private BeanMapper beanMapper;
        /**
         * 数据类型
         */
        public static final String DATA_TYPE = M3DataTypeEnum.FORECAST_NAME.getDataType();

        /**
         * 处理forecastName数据
         *
         * @param tenantM3ConfigDTO   租户M3配置
         * @param propertyM3ConfigDTO 资产M3配置
         * @param yearMonth           年月,yyyyMM
         * @param forcedRefresh       是否强制刷新m3数据
         * @author lichen
         * @date 2025/10/20
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void handleForecastName(TenantM3ConfigDTO tenantM3ConfigDTO, PropertyM3ConfigDTO propertyM3ConfigDTO, Integer yearMonth, Boolean forcedRefresh) {
            Integer propertyId = propertyM3ConfigDTO.getPropertyId();
            boolean m3DataHandleStatus = plCommonService.getM3DataHandleStatus(DATA_TYPE, yearMonth.toString(), propertyId);
            if (forcedRefresh || !m3DataHandleStatus) {
                int year = YearMonthUtils.getYearMonth(yearMonth.toString()).getYear();
                //删除旧数据
                remove(new LambdaQueryWrapper<M3ForecastName>().eq(M3ForecastName::getPropertyId, propertyM3ConfigDTO.getPropertyId())
                        .eq(M3ForecastName::getYear, year));
                //查询m3 forecast name数据
                List<ForecastNameDTO> forecastNameDTOS = m3ApiService.listForecastsName(tenantM3ConfigDTO, propertyM3ConfigDTO, String.valueOf(year));
                List<M3ForecastName> m3ForecastNames = beanMapper.mapList(forecastNameDTOS, M3ForecastName.class, (dto, entity) -> {
                    entity.setTenantId(tenantM3ConfigDTO.getTenantId());
                    entity.setPropertyId(propertyM3ConfigDTO.getPropertyId());
                    entity.setYear(year);
                    entity.setName(dto.getForecastName());
                    entity.setM3ForecastId(dto.getForecastId());
                    entity.setEffectiveDate(dto.getEffectiveDate().substring(0, 10).replace("-", ""));
                });
                //保存数据
                saveBatch(m3ForecastNames);

                plCommonService.setM3DataAlreadyHandled(DATA_TYPE, yearMonth.toString(), propertyId);
            }
        }

        /**
         * 按年查询M3系统forecast name数据
         *
         * @param propertyId 资产id
         * @param year       年
         * @return java.util.List<cloud.wconnect.finance.entity.pl.M3ForecastsName>
         * @author lichen
         * @date 2025/9/26
         */
        @Override
        public List<M3ForecastName> listByYear(Integer propertyId, int year) {
            return list(new LambdaQueryWrapper<M3ForecastName>().eq(M3ForecastName::getPropertyId, propertyId)
                    .eq(M3ForecastName::getYear, year));
        }
    }