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

import com.ruoyi.bst.part.domain.PartQuery;
import com.ruoyi.bst.part.mapper.PartMapper;
import com.ruoyi.bst.part.service.PartValidator;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import org.springframework.stereotype.Service;

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

@Service
public class PartValidatorImpl implements PartValidator {

    private final PartMapper partMapper;

    public PartValidatorImpl(PartMapper partMapper) {
        this.partMapper = partMapper;
    }

    @Override
    public boolean canCheckForPart(Long partId) {
        return canOperate(Arrays.asList(partId));
    }

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

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

    // 是否可以操作运营区
    private boolean canOperate(List<Long> partIds) {
        return hasPermission(partIds);
    }

    // 是否有运营区权限
    private boolean hasPermission(List<Long> partIds) {
        if (SecurityUtils.isSysAdmin()) {
            return true;
        }
        if (CollectionUtils.isEmptyElement(partIds)) {
            return true;
        }
        // 查询运营区
        PartQuery query = new PartQuery();
        query.setIds(partIds);
        query.setScope(true);
        List<Long> partIdList = partMapper.selectIdByQuery(query);
        return new HashSet<>(partIdList).containsAll(partIds);
    }

}
