# BadGuestApproveConfigServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.frontoffice.entity.badguest.BadGuestApproveConfig;
    import cloud.wconnect.frontoffice.mapper.badguest.BadGuestApproveConfigMapper;
    import cloud.wconnect.frontoffice.service.badguest.IBadGuestApproveConfigService;
    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.Set;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2023-03-21
     */
    @Service
    @DS("logbook")
    public class BadGuestApproveConfigServiceImpl extends ServiceImpl<BadGuestApproveConfigMapper, BadGuestApproveConfig> implements IBadGuestApproveConfigService {

        @Override
        public List<BadGuestApproveConfig> getApproveConfig(int propertyId, long levelId) {
            LambdaQueryWrapper<BadGuestApproveConfig> approveConfigQueryWrapper = new LambdaQueryWrapper<>();
            approveConfigQueryWrapper.eq(BadGuestApproveConfig::getPropertyId, propertyId);
            approveConfigQueryWrapper.eq(BadGuestApproveConfig::getLevelId, levelId);
            return list(approveConfigQueryWrapper);
        }

        @Override
        public Set<Integer> getApproveUsers(int propertyId, long levelId) {
            List<BadGuestApproveConfig> approveConfig = getApproveConfig(propertyId, levelId);
            if (CollectionUtils.isEmpty(approveConfig)) return Collections.emptySet();
            return approveConfig.stream().map(BadGuestApproveConfig::getUserId).collect(Collectors.toSet());
        }

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

        /**
         * 检测用户是否重复
         *
         * @param propertyId    资产id
         * @param userIds       用户id集合
         * @param ignoreLevelId 忽略的审批层级id
         * @author lichen
         * @date 2025/12/29
         */
        @Override
        public void checkUserRepeat(Integer propertyId, List<Integer> userIds, Long ignoreLevelId) {
            //校验用户是否已经配置
            LambdaQueryWrapper<BadGuestApproveConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BadGuestApproveConfig::getPropertyId, propertyId);
            queryWrapper.ne(Objects.nonNull(ignoreLevelId), BadGuestApproveConfig::getLevelId, ignoreLevelId);
            List<BadGuestApproveConfig> approvedConfigs = list(queryWrapper);
            if (CollectionUtils.isNotEmpty(approvedConfigs)) {
                //配置的用户id集合
                List<Integer> configUserIds = approvedConfigs.stream().map(BadGuestApproveConfig::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");
                }
            }
        }
    }