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

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

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

import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.credit.domain.CreditQuery;
import com.ruoyi.bst.credit.mapper.CreditMapper;
import com.ruoyi.bst.credit.service.CreditValidator;
import com.ruoyi.common.utils.SecurityUtils;

@Service
public class CreditValidatorImpl implements CreditValidator {

    @Autowired
    private CreditMapper creditMapper;

    @Override
    public boolean hasPermission(List<Long> ids, AreaJoinPermission permission) {
        if (SecurityUtils.isSysAdmin()) {
            return true;
        }
        if (CollectionUtils.isEmpty(ids)) {
            return true;
        }
        CreditQuery query = new CreditQuery();
        query.setIds(ids);
        query.setScope(true);
        query.addAreaPermission(permission.getCode());
        List<Long> creditIds = creditMapper.selectIdList(query);
        return new HashSet<>(creditIds).containsAll(ids);
    }

}
