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

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

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

import com.ruoyi.bst.device.domain.DeviceQuery;
import com.ruoyi.bst.device.mapper.DeviceMapper;
import com.ruoyi.bst.device.service.DeviceValidator;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class DeviceValidatorImpl implements DeviceValidator {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public boolean canEdit(Long id) {
        return canOperate(Collections.singletonList(id));
    }

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

    private boolean canOperate(List<Long> ids) {
        return canView(ids);
    }

    private boolean canView(List<Long> ids) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return false;
        }
        DeviceQuery query = new DeviceQuery();
        query.setIds(ids);
        List<Long> idList = deviceMapper.selectIdList(query);
        return new HashSet<>(idList).containsAll(ids);
    }

}
