package com.ruoyi.bst.vip.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.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.vip.domain.VipQuery;
import com.ruoyi.bst.vip.mapper.VipMapper;
import com.ruoyi.bst.vip.service.VipValidator;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class VipValidatorImpl implements VipValidator {
    
    @Autowired
    private VipMapper vipMapper;

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

    @Override
    public boolean canEdit(List<Long> ids) {
        return hasPermission(ids, AreaJoinPermission.VIP_EDIT.getCode());
    }

    private boolean hasPermission(List<Long> ids, String permission) {
        
        if (SecurityUtils.isSysAdmin()) {
            return true;
        }
        if (CollectionUtils.isEmptyElement(ids)) {
            return true;
        }
        VipQuery query = new VipQuery();    
        query.setIds(ids);
        query.addAreaPermission(permission);
        query.setScope(true);
        List<Long> vipIds = vipMapper.selectIdByQuery(query);
        return new HashSet<>(vipIds).containsAll(ids);
    }

}
