package com.ruoyi.bst.areaSub.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.areaSub.domain.AreaSubQuery;
import com.ruoyi.bst.areaSub.mapper.AreaSubMapper;
import com.ruoyi.bst.areaSub.service.AreaSubValidator;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class AreaSubValidatorImpl implements AreaSubValidator {

    @Autowired
    private AreaSubMapper areaSubMapper;

    @Override
    public boolean canEdit(Long id) {
        return canOperate(Arrays.asList(id));
    }

    @Override
    public boolean canDeleteAll(List<Long> ids) {
        return canOperate(ids);
    }

    private boolean canOperate(List<Long> areaSubIds) {
        if (CollectionUtils.isEmptyElement(areaSubIds)) {
            return false;
        }

        AreaSubQuery query = new AreaSubQuery();
        query.setIds(areaSubIds);
        query.setScope(true);
        List<Long> ids = areaSubMapper.selectIdByQuery(query);
        return new HashSet<>(ids).containsAll(areaSubIds);
    }
}
