# CompRoomApproveConfigServiceImpl.java

    package cloud.wconnect.frontoffice.service.comproom.impl;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.frontoffice.entity.comproom.CompRoomApproveConfig;
    import cloud.wconnect.frontoffice.mapper.comproom.CompRoomApproveConfigMapper;
    import cloud.wconnect.frontoffice.service.comproom.ICompRoomApproveConfigService;
    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.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import java.util.Collections;
    import java.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;

    /**
     * <p>
     * comp room审批配置表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2023-08-21
     */
    @Service
    @DS("logbook")
    public class CompRoomApproveConfigServiceImpl extends ServiceImpl<CompRoomApproveConfigMapper, CompRoomApproveConfig> implements ICompRoomApproveConfigService {

        /**
         * 根据用户id查询审批层级
         *
         * @param propertyId 资产id
         * @param userId     用户id
         * @return cloud.wconnect.frontoffice.entity.comproom.CompRoomApproveConfig
         * @author lichen
         * @date 2023/8/21
         */
        @Override
        public CompRoomApproveConfig getByUserId(Integer propertyId, int userId) {
            return getOne(new LambdaQueryWrapper<CompRoomApproveConfig>()
                    .eq(CompRoomApproveConfig::getPropertyId,propertyId)
                    .eq(CompRoomApproveConfig::getUserId,userId));
        }

        /**
         * 根据层级id查询审批配置
         *
         * @param propertyId 资产id
         * @param levelId    审批层级id
         * @return java.util.List<cloud.wconnect.frontoffice.entity.comproom.CompRoomApproveConfig>
         * @author lichen
         * @date 2023/8/21
         */
        @Override
        public List<CompRoomApproveConfig> listByLevelId(Integer propertyId, Integer levelId) {
            return list(new LambdaQueryWrapper<CompRoomApproveConfig>()
                    .eq(CompRoomApproveConfig::getPropertyId, propertyId)
                    .eq(CompRoomApproveConfig::getLevelId, levelId));
        }

        /**
         * 根据层级id查询审批配置
         *
         * @param propertyId 资产id
         * @param levelIds   审批层级id集合
         * @return java.util.List<cloud.wconnect.frontoffice.entity.comproom.CompRoomApproveConfig>
         * @author lichen
         * @date 2023/8/22
         */
        @Override
        public List<CompRoomApproveConfig> listByLevelIds(Integer propertyId, List<Integer> levelIds) {
            if(CollectionUtils.isEmpty(levelIds)) return Collections.emptyList();
            return list(new LambdaQueryWrapper<CompRoomApproveConfig>()
                    .eq(CompRoomApproveConfig::getPropertyId,propertyId)
                    .in(CompRoomApproveConfig::getLevelId,levelIds));
        }

        /**
         * 校验用户是否重复
         *
         * @param propertyId    资产id
         * @param userIds       当前配置的用户id集合
         * @param ignoreLevelId 过滤的层级id
         * @return void
         * @author lichen
         * @date 2023/8/22
         */
        @Override
        public void checkUserIsRepeat(Integer propertyId, List<Integer> userIds, Integer ignoreLevelId) {
            List<CompRoomApproveConfig> approvedConfigs = list(new LambdaQueryWrapper<CompRoomApproveConfig>()
                    .eq(CompRoomApproveConfig::getPropertyId,propertyId)
                    .ne(Objects.nonNull(ignoreLevelId), CompRoomApproveConfig::getLevelId,ignoreLevelId));
            if(CollectionUtils.isNotEmpty(approvedConfigs)){
                //配置的用户id集合
                List<Integer> configUserIds = approvedConfigs.stream().map(CompRoomApproveConfig::getUserId).collect(Collectors.toList());
                //两个集合有交集
                if(CollectionUtils.containsAny(configUserIds,userIds)){
                    throw new BadRequestException(CommonErrorI18nConstant.APPROVE_USER_ALREADY_EXIST,
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "user already exist approve setting");
                }
            }
        }
    }