# GmwrMonthEndForecastConfigServiceImpl.java

    package cloud.wconnect.workperformance.service.gmwr.impl;

    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.workperformance.cons.gmwr.monthend.GmwrMonthEndEnum;
    import cloud.wconnect.workperformance.entity.gmwr.GmwrMonthEndForecastConfig;
    import cloud.wconnect.workperformance.mapper.gmwr.GmwrMonthEndForecastConfigMapper;
    import cloud.wconnect.workperformance.pojo.dto.gmwr.monthend.GmwrMonthEndForecastConfigDTO;
    import cloud.wconnect.workperformance.pojo.dto.gmwr.monthend.GmwrMonthEndForecastConfigBasicDTO;
    import cloud.wconnect.workperformance.service.gmwr.IGmwrMonthEndForecastConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.function.Function;
    import java.util.stream.Collectors;

    /**
     * <p>
     * GM Weekly Report Month End Forecast Config 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-10-15
     */
    @Service
    @DS("work_performance")
    public class GmwrMonthEndForecastConfigServiceImpl extends ServiceImpl<GmwrMonthEndForecastConfigMapper, GmwrMonthEndForecastConfig> implements IGmwrMonthEndForecastConfigService {
        @Resource
        private ObjectMapper objectMapper;

        /**
         * 获取 Month End Forecast Config
         *
         * @param propertyId 资产 id
         * @return GmwrMonthEndForecastConfigDTO 集合
         * @author zc
         * @date 2024/10/16
         */
        @Override
        public List<GmwrMonthEndForecastConfigDTO> get(int propertyId) {
            List<GmwrMonthEndForecastConfigDTO> configDTOList = generateInitConfigDTOList();
            GmwrMonthEndForecastConfig config = getConfig(propertyId);
            if (Objects.nonNull(config)) {
                Map<Integer, GmwrMonthEndForecastConfigBasicDTO> category2BasicDTO = JsonUtils.convertStringToList(objectMapper,
                                config.getDetail(), GmwrMonthEndForecastConfigBasicDTO.class).stream()
                        .collect(Collectors.toMap(GmwrMonthEndForecastConfigBasicDTO::getCategory, Function.identity()));

                configDTOList.forEach(item -> {
                    GmwrMonthEndForecastConfigBasicDTO basicDTO = category2BasicDTO.get(item.getCategory());
                    if (Objects.nonNull(basicDTO)) {
                        item.setMappingName(basicDTO.getMappingName());
                    }
                });
            }
            return configDTOList;
        }

        /**
         * 生成初始配置
         *
         * @return GmwrMonthEndForecastConfigDTO 集合
         * @author zc
         * @date 2025/3/4
         */
        private List<GmwrMonthEndForecastConfigDTO> generateInitConfigDTOList() {
            return Lists.newArrayList(GmwrMonthEndEnum.OCC_PERCENT, GmwrMonthEndEnum.REV_PAR,
                    GmwrMonthEndEnum.ADR, GmwrMonthEndEnum.ROOM_REVENUE, GmwrMonthEndEnum.FB_REVENUE,
                    GmwrMonthEndEnum.OTHERS_REVENUE).stream().map(item -> {
                GmwrMonthEndForecastConfigDTO configDTO = new GmwrMonthEndForecastConfigDTO();
                configDTO.setCategory(item.getCategory());
                configDTO.setCategoryName(item.getCategoryName());
                return configDTO;
            }).toList();
        }

        /**
         * 获取配置
         *
         * @param propertyId 资产 id
         * @return GmwrMonthEndForecastConfig
         * @author zc
         * @date 2025/3/4
         */
        private GmwrMonthEndForecastConfig getConfig(int propertyId) {
            return getOne(new LambdaQueryWrapper<GmwrMonthEndForecastConfig>()
                    .eq(GmwrMonthEndForecastConfig::getPropertyId, propertyId));
        }

        /**
         * 保存 Month End Forecast Config
         *
         * @param propertyId    资产 id
         * @param configDTOList 配置集合
         * @param operatorId    操作人 id
         * @author zc
         * @date 2024/10/16
         */
        @Override
        public void save(int propertyId, List<GmwrMonthEndForecastConfigBasicDTO> configDTOList, int operatorId) {
            GmwrMonthEndForecastConfig config = new GmwrMonthEndForecastConfig();
            config.setPropertyId(propertyId);
            config.setDetail(JsonUtils.convertObjListToString(objectMapper, configDTOList));
            config.setUpdatedBy(operatorId);
            config.setUpdatedTime(DateTimeUtils.nowTimestamp());
            baseMapper.insertOrUpdateByUnique(config);
        }
    }