# SalesKpiMonthlyConfigServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.workperformance.entity.saleskpi.SalesKpiMonthlyConfig;
    import cloud.wconnect.workperformance.mapper.saleskpi.SalesKpiMonthlyConfigMapper;
    import cloud.wconnect.workperformance.service.saleskpi.ISalesKpiMonthlyConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.google.common.collect.Sets;
    import jakarta.annotation.Resource;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.lang.Nullable;
    import org.springframework.stereotype.Service;

    import java.text.MessageFormat;
    import java.util.Collections;
    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * salses kpi monthly config 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-09-05
     */
    @Service
    @DS("work_performance")
    public class SalesKpiMonthlyConfigServiceImpl extends ServiceImpl<SalesKpiMonthlyConfigMapper, SalesKpiMonthlyConfig> implements ISalesKpiMonthlyConfigService {
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 校验 monthly config 指标是否有效
         *
         * @param propertyId 资产 id
         * @param itemIds    指标 id
         * @param type       item 类型（1：普通 item  2：员工绑定 segment）
         * @author zc
         * @date 2023/9/6
         */
        @Override
        public void checkMonthlyItemEffective(int propertyId, List<Integer> itemIds, @Nullable Integer type) {
            if (Sets.newHashSet(itemIds).size() != itemIds.size()) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_CANNOT_REPEAT,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("itemIds"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The itemIds validation error, maybe repeat; itemIds:" + itemIds);
            }
            long count = count(new QueryWrapper<SalesKpiMonthlyConfig>().lambda()
                    .eq(SalesKpiMonthlyConfig::getPropertyId, propertyId)
                    .in(SalesKpiMonthlyConfig::getId, itemIds)
                    .ne(SalesKpiMonthlyConfig::getParentId, BaseConstant.ZERO)
                    .eq(Objects.nonNull(type), SalesKpiMonthlyConfig::getType, type));
            if (itemIds.size() != count) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "There is itemIds that was not found; itemIds:" + itemIds);
            }
        }

        /**
         * 根据指标数据源获取 monthly config
         *
         * @param propertyId 资产 id
         * @param dataSource 指标数据源
         * @return SalesKpiMonthlyConfig
         * @author zc
         * @date 2023/9/6
         */
        @Override
        public SalesKpiMonthlyConfig getByDataSource(int propertyId, String dataSource) {
            return getOne(new QueryWrapper<SalesKpiMonthlyConfig>().lambda()
                    .eq(SalesKpiMonthlyConfig::getPropertyId, propertyId)
                    .eq(SalesKpiMonthlyConfig::getDataSource, dataSource));
        }

        /**
         * 获取资产下 segment config - [for update]
         *
         * @param propertyId 资产 id
         * @return SalesKpiMonthlyConfig 集合
         * @author zc
         * @date 2023/9/6
         */
        @Override
        public List<SalesKpiMonthlyConfig> getSegmentConfigForUpdate(int propertyId) {
            return list(new QueryWrapper<SalesKpiMonthlyConfig>().lambda()
                    .eq(SalesKpiMonthlyConfig::getPropertyId, propertyId)
                    .eq(SalesKpiMonthlyConfig::getType, SalesKpiMonthlyConfig.TYPE_SEGMENT)
                    .last("for update"));
        }

        /**
         * 获取 Monthly config
         *
         * @param propertyId 资产 id
         * @param type       item 类型（1：普通 item  2：员工绑定 segment）
         * @param status     true: 已删除；false：未删除
         * @return MonthlyConfigItemDTO 集合
         * @author zc
         * @date 2023/9/7
         */
        @Override
        public List<SalesKpiMonthlyConfig> getMonthlyConfigs(int propertyId, @Nullable Integer type, @Nullable Boolean status) {
            return list(new QueryWrapper<SalesKpiMonthlyConfig>().lambda()
                    .eq(SalesKpiMonthlyConfig::getPropertyId, propertyId)
                    .eq(Objects.nonNull(type), SalesKpiMonthlyConfig::getType, type)
                    .eq(Objects.nonNull(status), SalesKpiMonthlyConfig::getStatus, status));
        }

        /**
         * 校验指标数据是否存在
         *
         * @param id id
         * @return SalesKpiMonthlyConfig
         * @author zc
         * @date 2023/9/8
         */
        @Override
        public SalesKpiMonthlyConfig checkExist(int id) {
            SalesKpiMonthlyConfig salesKpiMonthlyConfig = getOne(new QueryWrapper<SalesKpiMonthlyConfig>().lambda()
                    .eq(SalesKpiMonthlyConfig::getId, id)
                    .ne(SalesKpiMonthlyConfig::getParentId, BaseConstant.ZERO));
            if (Objects.isNull(salesKpiMonthlyConfig)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The salesKpiMonthlyConfig is not found by id: " + id);
            }
            return salesKpiMonthlyConfig;
        }

        /**
         * 校验指标数据归属
         *
         * @param propertyId            资产 id
         * @param salesKpiMonthlyConfig 源数据
         * @author zc
         * @date 2023/9/8
         */
        @Override
        public void checkBelong(int propertyId, SalesKpiMonthlyConfig salesKpiMonthlyConfig) {
            if (!salesKpiMonthlyConfig.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The salesKpiMonthlyConfig does not belong to this property; propertyId:{0}; salesKpiMonthlyConfigId:{1}",
                                propertyId, salesKpiMonthlyConfig.getId()));
            }
        }

        /**
         * 根据数据源获取 monthly config
         *
         * @param propertyId     资产 id
         * @param dataSourceList 数据源集合
         * @return SalesKpiMonthlyConfig 集合
         * @author zc
         * @date 2024/6/15
         */
        @Override
        public List<SalesKpiMonthlyConfig> listByDataSources(int propertyId, List<String> dataSourceList) {
            if (CollectionUtils.isEmpty(dataSourceList)) {
                return Collections.emptyList();
            }
            return list(new LambdaQueryWrapper<SalesKpiMonthlyConfig>()
                    .eq(SalesKpiMonthlyConfig::getPropertyId, propertyId)
                    .in(SalesKpiMonthlyConfig::getDataSource, dataSourceList));
        }
    }