# GmwrViewConfigServiceImpl.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.GmwrCategoryEnum;
    import cloud.wconnect.workperformance.entity.gmwr.GmwrViewConfig;
    import cloud.wconnect.workperformance.mapper.gmwr.GmwrViewConfigMapper;
    import cloud.wconnect.workperformance.pojo.dto.gmwr.view.GmwrViewItemDTO;
    import cloud.wconnect.workperformance.pojo.dto.gmwr.view.GmwrViewModuleDTO;
    import cloud.wconnect.workperformance.service.gmwr.IGmwrViewConfigService;
    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 org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Resource;
    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * Weekly GM Report 模块显示配置 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-12-31
     */
    @Service
    @DS("work_performance")
    public class GmwrViewConfigServiceImpl extends ServiceImpl<GmwrViewConfigMapper, GmwrViewConfig> implements IGmwrViewConfigService {

        @Resource
        private ObjectMapper objectMapper;

        /**
         * 获取模块显示配置
         *
         * @param propertyId 资产 id
         * @return GmwrViewModuleDTO 集合
         * @author zc
         * @date 2024/12/31
         */
        @Override
        public List<GmwrViewModuleDTO> get(int propertyId) {
            GmwrViewConfig viewConfig = getOne(new LambdaQueryWrapper<GmwrViewConfig>()
                    .eq(GmwrViewConfig::getPropertyId, propertyId));
            if (Objects.isNull(viewConfig)) {
                // 返回默认结构，Hotel KPI 相关项默认不展示
                return getDefault();
            }
            return JsonUtils.convertStringToList(objectMapper, viewConfig.getDetail(), GmwrViewModuleDTO.class);
        }

        /**
         * 获取默认结构
         *
         * @return GmwrViewModuleDTO 集合
         * @author zc
         * @date 2025/1/3
         */
        private List<GmwrViewModuleDTO> getDefault() {
            List<GmwrViewModuleDTO> resultList = Lists.newArrayList();
            resultList.add(new GmwrViewModuleDTO("Revenue", 1,
                    List.of(new GmwrViewItemDTO(GmwrCategoryEnum.MONTH_END.getCategory(), 1),
                            new GmwrViewItemDTO(GmwrCategoryEnum.Monthly_OTB_Pace.getCategory(), 2),
                            new GmwrViewItemDTO(GmwrCategoryEnum.STR_DATA.getCategory(), 3))));
            resultList.add(new GmwrViewModuleDTO(GmwrCategoryEnum.SELLOUT_EFFICIENCY.getTitle(), 2,
                    List.of(new GmwrViewItemDTO(GmwrCategoryEnum.SELLOUT_EFFICIENCY.getCategory(), 1))));
            resultList.add(new GmwrViewModuleDTO(GmwrCategoryEnum.COMMENT.getTitle(), 3,
                    List.of(new GmwrViewItemDTO(GmwrCategoryEnum.COMMENT.getCategory(), 1))));
            return resultList;
        }

        /**
         * 保存模块显示配置
         *
         * @param propertyId    资产 id
         * @param saveConfigDTO 保存配置
         * @param operatorId    操作人 id
         * @author zc
         * @date 2024/12/31
         */
        @Override
        public void save(int propertyId, List<GmwrViewModuleDTO> saveConfigDTO, int operatorId) {
            if (CollectionUtils.isEmpty(saveConfigDTO)) {
                // 空集合表示重置为默认结构
                saveConfigDTO = getDefault();
            }
            GmwrViewConfig saveDTO = new GmwrViewConfig();
            saveDTO.setPropertyId(propertyId);
            saveDTO.setDetail(JsonUtils.convertObjListToString(objectMapper, saveConfigDTO));
            saveDTO.setUpdatedBy(operatorId);
            saveDTO.setUpdatedTime(DateTimeUtils.nowTimestamp());
            baseMapper.insertOrUpdateByUnique(saveDTO);
        }

    }