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

import com.aliyun.core.utils.StringUtils;
import com.ruoyi.bst.booth.domain.Booth;
import com.ruoyi.bst.booth.domain.BoothQuery;
import com.ruoyi.bst.booth.domain.BoothVO;
import com.ruoyi.bst.booth.mapper.BoothMapper;
import com.ruoyi.bst.booth.service.BoothValidator;
import com.ruoyi.bst.device.domain.DeviceQuery;
import com.ruoyi.bst.device.domain.DeviceVO;
import com.ruoyi.bst.storeStaff.domain.enums.StoreStaffPermission;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
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 BoothValidatorImpl implements BoothValidator {

    private final BoothMapper boothMapper;

    public BoothValidatorImpl(BoothMapper boothMapper) {
        this.boothMapper = boothMapper;
    }

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

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

    // 是否可以操作卡座
    private boolean canOperate(List<Long> boothIds) {
        return hasPermission(boothIds);
    }

    // 是否有卡座权限
    private boolean hasPermission(List<Long> boothIds) {
        if (SecurityUtils.isSysAdmin()) {
            return true;
        }
        if (CollectionUtils.isEmptyElement(boothIds)) {
            return true;
        }
        // 查询卡座
        BoothQuery query = new BoothQuery();
        query.setIds(boothIds);
        query.setScope(true);
        query.addStorePermission(StoreStaffPermission.BOOTH_EDIT.getCode());
        List<Long> boothIdList = boothMapper.selectIdByQuery(query);
        return new HashSet<>(boothIdList).containsAll(boothIds);
    }

}
