package com.ruoyi.bst.customerService.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.customerService.domain.CustomerServiceQuery;
import com.ruoyi.bst.customerService.mapper.CustomerServiceMapper;
import com.ruoyi.bst.customerService.service.CustomerServiceValidator;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.system.user.service.UserService;

@Service
public class CustomerServiceValidatorImpl implements CustomerServiceValidator {


    @Autowired
    private CustomerServiceMapper customerServiceMapper;

    @Autowired
    private UserService userService;

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

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


    // 是否可以操作客服
    private boolean canOperate(List<Long> customerIds) {
        return hasPermission(customerIds, AreaJoinPermission.CUSTOMER_SERVICE_EDIT.getCode());
    }

    private boolean hasPermission(List<Long> customerIds, String permission) {
        if (CollectionUtils.isEmptyElement(customerIds)) {
            return true;
        }

        // 查询客服
        CustomerServiceQuery query = new CustomerServiceQuery();
        query.setIds(customerIds);
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.CUSTOMER_SERVICE_EDIT.getCode());
        List<Long> customerServiceList = customerServiceMapper.selectIdByQuery(query);
        return new HashSet<>(customerServiceList).containsAll(customerIds);
    }
}
