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

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

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

import com.ruoyi.bst.area.domain.AreaQuery;
import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.area.mapper.AreaMapper;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.area.service.AreaValidator;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class AreaValidatorImpl implements AreaValidator {

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private AreaService areaService;


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

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

    @Override
    public boolean canAddSubArea(Long areaId) {
        return canOperate(Arrays.asList(areaId));
    }

    // 是否可以操作运营区
    private boolean canOperate(List<Long> areaIds) {
        
        if (SecurityUtils.isSysAdmin()) {
            return true;
        }
        if (CollectionUtils.isEmptyElement(areaIds)) {
            return true;
        }
        // 查询运营区
        AreaQuery query = new AreaQuery();
        query.setIds(areaIds);
        query.setScope(true);
        List<AreaVO> areaList = areaMapper.selectAreaList(query);
        Long userId = SecurityUtils.getUserId();

        // 判断是否可以操作运营区
        for (Long areaId : areaIds) {
            AreaVO area = areaList.stream()
                .filter(item -> item.getId().equals(areaId))
                .findFirst().orElse(null);

            if (area == null) {
                return false;
            }
            boolean canOperate = isAgent(area, userId) || isMch(area, userId);
            if (!canOperate) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean canView(List<Long> areaIds) {
        if (CollectionUtils.isEmptyElement(areaIds)) {
            return true;
        }
        // 查询运营区ID
        AreaQuery query = new AreaQuery();
        query.setIds(areaIds);
        query.setScope(true);
        List<Long> ids = areaMapper.selectIdByQuery(query);
        // 判断运营区ID是否一致
        return new HashSet<>(ids).containsAll(areaIds);
    }

    @Override
    public boolean canCheckForJoin(Long areaId) {
        return canOperate(Arrays.asList(areaId));
    }

    @Override
    public boolean canAddCustomerService(Long areaId) {
        return canOperate(Collections.singletonList(areaId));
    }

    private boolean isAgent(AreaVO area, Long userId) {
        return area != null && area.getAgentId() != null && area.getAgentId().equals(userId);
    }

    private boolean isMch(AreaVO area, Long userId) {
        return area != null && area.getUserId() != null && area.getUserId().equals(userId);
    }



}
