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

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.vipUser.domain.VipUserQuery;
import com.ruoyi.bst.vipUser.mapper.VipUserMapper;
import com.ruoyi.bst.vipUser.service.VipUserValidator;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class VipUserValidatorImpl implements VipUserValidator {
    
    @Autowired
    private VipUserMapper vipUserMapper;

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

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